ResourceHelper

This commit is contained in:
Milos Kozak 2019-12-26 17:50:16 +01:00
parent 895b32d8f3
commit 3654b0b66f
7 changed files with 50 additions and 30 deletions

View file

@ -1,6 +1,5 @@
package info.nightscout.androidaps.dependencyInjection
import android.app.Application
import dagger.BindsInstance
import dagger.Component
import dagger.android.AndroidInjectionModule
@ -23,7 +22,7 @@ interface AppComponent : AndroidInjector<MainApp> {
interface Builder {
@BindsInstance
fun application(application: Application): Builder
fun application(mainApp: MainApp): Builder
fun build(): AppComponent
}

View file

@ -1,11 +1,13 @@
package info.nightscout.androidaps.dependencyInjection
import android.app.Application
import android.content.Context
import android.preference.PreferenceManager
import dagger.Binds
import dagger.Module
import dagger.Provides
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.resources.ResourceHelperImplementation
import info.nightscout.androidaps.utils.sharedPreferences.SP
import info.nightscout.androidaps.utils.sharedPreferences.SPImplementation
import javax.inject.Singleton
@ -19,10 +21,16 @@ class AppModule {
return SPImplementation(PreferenceManager.getDefaultSharedPreferences(context))
}
@Provides
@Singleton
fun provideResources(mainApp: MainApp): ResourceHelper {
return ResourceHelperImplementation(mainApp)
}
@Module
interface AppBindings {
@Binds
fun bindContext(application: Application): Context
fun bindContext(mainApp: MainApp): Context
}
}

View file

@ -2,7 +2,7 @@ package info.nightscout.androidaps.plugins.insulin
import info.nightscout.androidaps.R
import info.nightscout.androidaps.interfaces.InsulinInterface
import info.nightscout.androidaps.utils.ResourceHelper
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.sharedPreferences.SP
import javax.inject.Inject
import javax.inject.Singleton
@ -21,7 +21,7 @@ class InsulinOrefFreePeakPlugin @Inject constructor(
}
override fun getFriendlyName(): String {
return resourceHelper.gs(R.string.free_peak_oref)
return resourceHelper.gs(R.string.free_peak_oref) ?: ""
}
public override fun commentStandardText(): String {

View file

@ -1,22 +0,0 @@
package info.nightscout.androidaps.utils
import androidx.annotation.ColorRes
import androidx.annotation.PluralsRes
import androidx.annotation.StringRes
import info.nightscout.androidaps.MainApp
import javax.inject.Inject
/**
* Created by adrian on 2019-12-23.
*/
open class ResourceHelper @Inject constructor() {
fun gs(@StringRes id: Int): String = MainApp.sResources.getString(id)
fun gs(@StringRes id: Int, vararg args: Any?) = MainApp.sResources.getString(id, *args)
fun gq(@PluralsRes id: Int, quantity: Int, vararg args: Any?): String =
MainApp.sResources.getQuantityString(id, quantity, *args)
fun gc(@ColorRes id: Int): Int = MainApp.sResources.getColor(id)
}

View file

@ -0,0 +1,12 @@
package info.nightscout.androidaps.utils.resources
import androidx.annotation.ColorRes
import androidx.annotation.PluralsRes
import androidx.annotation.StringRes
interface ResourceHelper {
fun gs(@StringRes id: Int): String?
fun gs(@StringRes id: Int, vararg args: Any?): String?
fun gq(@PluralsRes id: Int, quantity: Int, vararg args: Any?): String?
fun gc(@ColorRes id: Int): Int?
}

View file

@ -0,0 +1,23 @@
package info.nightscout.androidaps.utils.resources
import androidx.annotation.ColorRes
import androidx.annotation.PluralsRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import info.nightscout.androidaps.MainApp
import javax.inject.Inject
/**
* Created by adrian on 2019-12-23.
*/
class ResourceHelperImplementation @Inject constructor(private val mainApp: MainApp) : ResourceHelper {
override fun gs(@StringRes id: Int): String? = mainApp.getString(id)
override fun gs(@StringRes id: Int, vararg args: Any?): String? = mainApp.getString(id, *args)
override fun gq(@PluralsRes id: Int, quantity: Int, vararg args: Any?): String? =
mainApp.resources.getQuantityString(id, quantity, *args)
override fun gc(@ColorRes id: Int): Int = ContextCompat.getColor(mainApp, id)
}

View file

@ -1,7 +1,7 @@
package info.nightscout.androidaps.plugins.insulin
import info.nightscout.androidaps.R
import info.nightscout.androidaps.utils.ResourceHelper
import info.nightscout.androidaps.utils.resources.ResourceHelperImplementation
import info.nightscout.androidaps.utils.sharedPreferences.SP
import junit.framework.Assert.assertEquals
import org.junit.Before
@ -34,7 +34,7 @@ class InsulinOrefFreePeakPluginTest {
lateinit var sp: SP
@Mock
lateinit var resourceHelper: ResourceHelper
lateinit var resourceHelper: ResourceHelperImplementation
@Before
fun setup() {