Skip to content

Instantly share code, notes, and snippets.

@jemshit
Last active May 10, 2022 16:34
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jemshit/285d750c399bc267dbdad545185d5f7a to your computer and use it in GitHub Desktop.
Save jemshit/285d750c399bc267dbdad545185d5f7a to your computer and use it in GitHub Desktop.
Android Resource Provider for Presenter in MVP
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)
}
@Module
abstract class ApplicationBindsModule {
@Binds
abstract fun provideAndroidResouce(androidResourceProvider: AndroidResourceProvider): ResourceProvider
}
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
}
@jemshit
Copy link
Author

jemshit commented Mar 19, 2022 via email

@jemshit
Copy link
Author

jemshit commented Mar 19, 2022

@elvisfromsouth
Copy link

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