Last active
May 10, 2022 16:34
-
-
Save jemshit/285d750c399bc267dbdad545185d5f7a to your computer and use it in GitHub Desktop.
Android Resource Provider for Presenter in MVP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.support.annotation.* | |
import android.support.v4.content.ContextCompat | |
import interface_adapters.ResourceProvider | |
import javax.inject.Inject | |
import javax.inject.Named | |
import javax.inject.Singleton | |
@Singleton | |
class AndroidResourceProvider | |
@Inject constructor(@Named("AppContext") private val context: Context) | |
: ResourceProvider { | |
override fun getString(@StringRes resourceIdentifier: Int, vararg arguments: Any): String { | |
return if (arguments.isNotEmpty()) | |
context.resources.getString(resourceIdentifier, *arguments) | |
else | |
context.resources.getString(resourceIdentifier) | |
} | |
override fun getStringArray(@ArrayRes resourceIdentifier: Int): Array<String> = | |
context.resources.getStringArray(resourceIdentifier) | |
override fun getInteger(@IntegerRes resourceIdentifier: Int): Int = | |
context.resources.getInteger(resourceIdentifier) | |
override fun getIntegerArray(@ArrayRes resourceIdentifier: Int): Array<Int> = | |
context.resources.getIntArray(resourceIdentifier).toTypedArray() | |
override fun getBoolean(@BoolRes resourceIdentifier: Int): Boolean = | |
context.resources.getBoolean(resourceIdentifier) | |
override fun getColor(@ColorRes resourceIdentifier: Int): Int = | |
ContextCompat.getColor(context, resourceIdentifier) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
abstract class ApplicationBindsModule { | |
@Binds | |
abstract fun provideAndroidResouce(androidResourceProvider: AndroidResourceProvider): ResourceProvider | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ResourceProvider { | |
fun getString(resourceIdentifier: Int, vararg arguments: Any = arrayOf()): String | |
fun getStringArray(resourceIdentifier: Int): Array<String> | |
fun getInteger(resourceIdentifier: Int): Int | |
fun getIntegerArray(resourceIdentifier: Int): Array<Int> | |
fun getBoolean(resourceIdentifier: Int): Boolean | |
fun getColor(resourceIdentifier: Int): Int | |
} |
Author
jemshit
commented
Mar 19, 2022
via email
If it is just 'copy file' logic, not persist to file, you can write Helper class in domain layer. I think appContext (provide using DI) will be enough instead of Activity context. If you have more File related logic, then you can have one class for FileHelper, then another for your copy logic/other logics which use FileHelper
hi. i think i there are some problems with getColor
function because some Activities can override Theme of Application(in manifest) as a result you receive the wrong color(
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment