diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt index cb6fc27cb9..edf202b5ce 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt @@ -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 { interface Builder { @BindsInstance - fun application(application: Application): Builder + fun application(mainApp: MainApp): Builder fun build(): AppComponent } diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt index f55ee48ae9..367f228ed3 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt @@ -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 } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/insulin/InsulinOrefFreePeakPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/insulin/InsulinOrefFreePeakPlugin.kt index 6707fd7873..ba6773d324 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/insulin/InsulinOrefFreePeakPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/insulin/InsulinOrefFreePeakPlugin.kt @@ -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 { diff --git a/app/src/main/java/info/nightscout/androidaps/utils/ResourceHelper.kt b/app/src/main/java/info/nightscout/androidaps/utils/ResourceHelper.kt deleted file mode 100644 index a718a0803e..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/utils/ResourceHelper.kt +++ /dev/null @@ -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) -} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/utils/resources/ResourceHelper.kt b/app/src/main/java/info/nightscout/androidaps/utils/resources/ResourceHelper.kt new file mode 100644 index 0000000000..2eb62f6ee6 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/utils/resources/ResourceHelper.kt @@ -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? +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/utils/resources/ResourceHelperImplementation.kt b/app/src/main/java/info/nightscout/androidaps/utils/resources/ResourceHelperImplementation.kt new file mode 100644 index 0000000000..08660d3745 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/utils/resources/ResourceHelperImplementation.kt @@ -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) +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/insulin/InsulinOrefFreePeakPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/insulin/InsulinOrefFreePeakPluginTest.kt index 806c8e0379..a0e5b7954c 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/insulin/InsulinOrefFreePeakPluginTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/insulin/InsulinOrefFreePeakPluginTest.kt @@ -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() {