From cd6dba8a3154cdabf537c08065c6ecbd47e74cca Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Mon, 23 Dec 2019 13:27:15 +0100 Subject: [PATCH] injecting insulin... plugin --- .../info/nightscout/androidaps/MainApp.java | 8 +- .../activities/MyPreferenceFragment.java | 157 ++++++++++++++++++ .../activities/PreferencesActivity.java | 142 ++-------------- .../dependencyInjection/ActivitiesModule.kt | 5 + .../dependencyInjection/AppComponent.kt | 1 + .../dependencyInjection/AppModule.kt | 5 +- .../dependencyInjection/FragmentsModule.kt | 11 ++ .../dependency-injection-todo.txt | 16 ++ .../insulin/InsulinOrefFreePeakPlugin.java | 49 ------ .../insulin/InsulinOrefFreePeakPlugin.kt | 45 +++++ .../androidaps/utils/ResourceHelper.kt | 22 +++ .../androidaps/utils/sharedPreferences/SP.kt | 144 ++++------------ .../utils/sharedPreferences/SPImpl.kt | 122 ++++++++++++++ 13 files changed, 433 insertions(+), 294 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/activities/MyPreferenceFragment.java create mode 100644 app/src/main/java/info/nightscout/androidaps/dependencyInjection/FragmentsModule.kt create mode 100644 app/src/main/java/info/nightscout/androidaps/dependencyInjection/dependency-injection-todo.txt delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/insulin/InsulinOrefFreePeakPlugin.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/insulin/InsulinOrefFreePeakPlugin.kt create mode 100644 app/src/main/java/info/nightscout/androidaps/utils/ResourceHelper.kt create mode 100644 app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SPImpl.kt diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index b50ced81e0..c4bfe32cbe 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -20,6 +20,8 @@ import org.slf4j.LoggerFactory; import java.io.File; import java.util.ArrayList; +import javax.inject.Inject; + import dagger.android.AndroidInjector; import dagger.android.DaggerApplication; import info.nightscout.androidaps.data.ConstraintChecker; @@ -92,6 +94,7 @@ import info.nightscout.androidaps.services.Intents; import info.nightscout.androidaps.utils.ActivityMonitor; import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.LocaleHelper; +import info.nightscout.androidaps.utils.sharedPreferences.SPImpl; import io.fabric.sdk.android.Fabric; import static info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerUtilsKt.triggerCheckVersion; @@ -122,6 +125,9 @@ public class MainApp extends DaggerApplication { public static boolean devBranch; public static boolean engineeringMode; + @Inject + InsulinOrefFreePeakPlugin insulinOrefFreePeakPlugin; + @Override public void onCreate() { super.onCreate(); @@ -181,7 +187,7 @@ public class MainApp extends DaggerApplication { if (!Config.NSCLIENT) pluginsList.add(ActionsPlugin.INSTANCE); pluginsList.add(InsulinOrefRapidActingPlugin.getPlugin()); pluginsList.add(InsulinOrefUltraRapidActingPlugin.getPlugin()); - pluginsList.add(InsulinOrefFreePeakPlugin.getPlugin()); + pluginsList.add(insulinOrefFreePeakPlugin); pluginsList.add(SensitivityOref0Plugin.getPlugin()); pluginsList.add(SensitivityAAPSPlugin.getPlugin()); pluginsList.add(SensitivityWeightedAveragePlugin.getPlugin()); diff --git a/app/src/main/java/info/nightscout/androidaps/activities/MyPreferenceFragment.java b/app/src/main/java/info/nightscout/androidaps/activities/MyPreferenceFragment.java new file mode 100644 index 0000000000..533eaa0416 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/activities/MyPreferenceFragment.java @@ -0,0 +1,157 @@ +package info.nightscout.androidaps.activities; + +import android.os.Bundle; +import android.preference.PreferenceFragment; + +import javax.inject.Inject; + +import dagger.android.AndroidInjection; +import dagger.android.AndroidInjector; +import dagger.android.DispatchingAndroidInjector; +import dagger.android.HasAndroidInjector; +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.interfaces.PluginType; +import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; +import info.nightscout.androidaps.plugins.aps.openAPSAMA.OpenAPSAMAPlugin; +import info.nightscout.androidaps.plugins.aps.openAPSMA.OpenAPSMAPlugin; +import info.nightscout.androidaps.plugins.aps.openAPSSMB.OpenAPSSMBPlugin; +import info.nightscout.androidaps.plugins.constraints.safety.SafetyPlugin; +import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin; +import info.nightscout.androidaps.plugins.general.careportal.CareportalPlugin; +import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin; +import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin; +import info.nightscout.androidaps.plugins.general.tidepool.TidepoolPlugin; +import info.nightscout.androidaps.plugins.general.wear.WearPlugin; +import info.nightscout.androidaps.plugins.general.xdripStatusline.StatuslinePlugin; +import info.nightscout.androidaps.plugins.insulin.InsulinOrefFreePeakPlugin; +import info.nightscout.androidaps.plugins.pump.combo.ComboPlugin; +import info.nightscout.androidaps.plugins.pump.danaR.DanaRPlugin; +import info.nightscout.androidaps.plugins.pump.danaRKorean.DanaRKoreanPlugin; +import info.nightscout.androidaps.plugins.pump.danaRS.DanaRSPlugin; +import info.nightscout.androidaps.plugins.pump.danaRv2.DanaRv2Plugin; +import info.nightscout.androidaps.plugins.pump.insight.LocalInsightPlugin; +import info.nightscout.androidaps.plugins.pump.medtronic.MedtronicPumpPlugin; +import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin; +import info.nightscout.androidaps.plugins.sensitivity.SensitivityAAPSPlugin; +import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref0Plugin; +import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref1Plugin; +import info.nightscout.androidaps.plugins.sensitivity.SensitivityWeightedAveragePlugin; +import info.nightscout.androidaps.plugins.source.SourceDexcomPlugin; + +/** + * Created by adrian on 2019-12-23. + */ +public class MyPreferenceFragment extends PreferenceFragment implements HasAndroidInjector { + private Integer id; + + @Inject + DispatchingAndroidInjector androidInjector; + + @Inject + InsulinOrefFreePeakPlugin insulinOrefFreePeakPlugin; + + @Override + public void setArguments(Bundle args) { + super.setArguments(args); + id = args.getInt("id"); + } + + void addPreferencesFromResourceIfEnabled(PluginBase p, PluginType type) { + if (p.isEnabled(type) && p.getPreferencesId() != -1) + addPreferencesFromResource(p.getPreferencesId()); + } + + @Override + public AndroidInjector androidInjector() { + return androidInjector; + } + + @Override + public void onCreate(final Bundle savedInstanceState) { + AndroidInjection.inject(this); + super.onCreate(savedInstanceState); + + if (savedInstanceState != null && savedInstanceState.containsKey("id")) { + id = savedInstanceState.getInt("id"); + } + + AndroidInjection.inject(this); + + if (id != -1) { + addPreferencesFromResource(id); + } else { + + if (!Config.NSCLIENT) { + addPreferencesFromResource(R.xml.pref_password); + } + addPreferencesFromResource(R.xml.pref_general); + addPreferencesFromResource(R.xml.pref_age); + + addPreferencesFromResource(R.xml.pref_overview); + + addPreferencesFromResourceIfEnabled(SourceDexcomPlugin.INSTANCE, PluginType.BGSOURCE); + addPreferencesFromResourceIfEnabled(CareportalPlugin.getPlugin(), PluginType.GENERAL); + addPreferencesFromResourceIfEnabled(SafetyPlugin.getPlugin(), PluginType.CONSTRAINTS); + if (Config.APS) { + addPreferencesFromResourceIfEnabled(LoopPlugin.getPlugin(), PluginType.LOOP); + addPreferencesFromResourceIfEnabled(OpenAPSMAPlugin.getPlugin(), PluginType.APS); + addPreferencesFromResourceIfEnabled(OpenAPSAMAPlugin.getPlugin(), PluginType.APS); + addPreferencesFromResourceIfEnabled(OpenAPSSMBPlugin.getPlugin(), PluginType.APS); + } + + addPreferencesFromResourceIfEnabled(SensitivityAAPSPlugin.getPlugin(), PluginType.SENSITIVITY); + addPreferencesFromResourceIfEnabled(SensitivityWeightedAveragePlugin.getPlugin(), PluginType.SENSITIVITY); + addPreferencesFromResourceIfEnabled(SensitivityOref0Plugin.getPlugin(), PluginType.SENSITIVITY); + addPreferencesFromResourceIfEnabled(SensitivityOref1Plugin.getPlugin(), PluginType.SENSITIVITY); + + if (Config.PUMPDRIVERS) { + addPreferencesFromResourceIfEnabled(DanaRPlugin.getPlugin(), PluginType.PUMP); + addPreferencesFromResourceIfEnabled(DanaRKoreanPlugin.getPlugin(), PluginType.PUMP); + addPreferencesFromResourceIfEnabled(DanaRv2Plugin.getPlugin(), PluginType.PUMP); + addPreferencesFromResourceIfEnabled(DanaRSPlugin.getPlugin(), PluginType.PUMP); + addPreferencesFromResourceIfEnabled(LocalInsightPlugin.getPlugin(), PluginType.PUMP); + addPreferencesFromResourceIfEnabled(ComboPlugin.getPlugin(), PluginType.PUMP); + addPreferencesFromResourceIfEnabled(MedtronicPumpPlugin.getPlugin(), PluginType.PUMP); + + if (DanaRPlugin.getPlugin().isEnabled(PluginType.PROFILE) + || DanaRKoreanPlugin.getPlugin().isEnabled(PluginType.PROFILE) + || DanaRv2Plugin.getPlugin().isEnabled(PluginType.PROFILE) + || DanaRSPlugin.getPlugin().isEnabled(PluginType.PROFILE)) { + addPreferencesFromResource(R.xml.pref_danarprofile); + } + } + + if (!Config.NSCLIENT) { + addPreferencesFromResourceIfEnabled(VirtualPumpPlugin.getPlugin(), PluginType.PUMP); + } + + addPreferencesFromResourceIfEnabled(insulinOrefFreePeakPlugin, PluginType.INSULIN); + + addPreferencesFromResourceIfEnabled(NSClientPlugin.getPlugin(), PluginType.GENERAL); + addPreferencesFromResourceIfEnabled(TidepoolPlugin.INSTANCE, PluginType.GENERAL); + addPreferencesFromResourceIfEnabled(SmsCommunicatorPlugin.INSTANCE, PluginType.GENERAL); + addPreferencesFromResourceIfEnabled(AutomationPlugin.INSTANCE, PluginType.GENERAL); + + addPreferencesFromResource(R.xml.pref_others); + addPreferencesFromResource(R.xml.pref_datachoices); + + addPreferencesFromResourceIfEnabled(WearPlugin.getPlugin(), PluginType.GENERAL); + addPreferencesFromResourceIfEnabled(StatuslinePlugin.getPlugin(), PluginType.GENERAL); + } + + PreferencesActivity.initSummary(getPreferenceScreen()); + + for (PluginBase plugin : MainApp.getPluginsList()) { + plugin.preprocessPreferences(this); + } + } + + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putInt("id", id); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java b/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java index c0dbf43527..4d859279d2 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java @@ -7,57 +7,39 @@ import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceActivity; -import android.preference.PreferenceFragment; import android.preference.PreferenceGroup; import android.preference.PreferenceManager; import java.util.Arrays; -import info.nightscout.androidaps.Config; +import javax.inject.Inject; + +import dagger.android.AndroidInjection; +import dagger.android.AndroidInjector; +import dagger.android.DispatchingAndroidInjector; +import dagger.android.HasAndroidInjector; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventRebuildTabs; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.PluginType; -import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; -import info.nightscout.androidaps.plugins.aps.openAPSAMA.OpenAPSAMAPlugin; -import info.nightscout.androidaps.plugins.aps.openAPSMA.OpenAPSMAPlugin; -import info.nightscout.androidaps.plugins.aps.openAPSSMB.OpenAPSSMBPlugin; import info.nightscout.androidaps.plugins.bus.RxBus; -import info.nightscout.androidaps.plugins.constraints.safety.SafetyPlugin; -import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin; -import info.nightscout.androidaps.plugins.general.careportal.CareportalPlugin; -import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin; -import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin; -import info.nightscout.androidaps.plugins.general.tidepool.TidepoolPlugin; -import info.nightscout.androidaps.plugins.general.wear.WearPlugin; -import info.nightscout.androidaps.plugins.general.xdripStatusline.StatuslinePlugin; import info.nightscout.androidaps.plugins.insulin.InsulinOrefFreePeakPlugin; -import info.nightscout.androidaps.plugins.pump.combo.ComboPlugin; -import info.nightscout.androidaps.plugins.pump.danaR.DanaRPlugin; -import info.nightscout.androidaps.plugins.pump.danaRKorean.DanaRKoreanPlugin; -import info.nightscout.androidaps.plugins.pump.danaRS.DanaRSPlugin; -import info.nightscout.androidaps.plugins.pump.danaRv2.DanaRv2Plugin; -import info.nightscout.androidaps.plugins.pump.insight.LocalInsightPlugin; -import info.nightscout.androidaps.plugins.pump.medtronic.MedtronicPumpPlugin; -import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin; -import info.nightscout.androidaps.plugins.sensitivity.SensitivityAAPSPlugin; -import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref0Plugin; -import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref1Plugin; -import info.nightscout.androidaps.plugins.sensitivity.SensitivityWeightedAveragePlugin; -import info.nightscout.androidaps.plugins.source.SourceDexcomPlugin; import info.nightscout.androidaps.utils.LocaleHelper; import info.nightscout.androidaps.utils.OKDialog; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.SafeParse; -public class PreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener { +public class PreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener, HasAndroidInjector { MyPreferenceFragment myPreferenceFragment; + @Inject + DispatchingAndroidInjector androidInjector; + @Override protected void onCreate(Bundle savedInstanceState) { + AndroidInjection.inject(this); setTheme(R.style.AppTheme_NoActionBar); super.onCreate(savedInstanceState); myPreferenceFragment = new MyPreferenceFragment(); @@ -68,6 +50,11 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this); } + @Override + public AndroidInjector androidInjector() { + return androidInjector; + } + @Override public void attachBaseContext(Context newBase) { super.attachBaseContext(LocaleHelper.INSTANCE.wrap(newBase)); @@ -144,101 +131,4 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre } } - public static class MyPreferenceFragment extends PreferenceFragment { - private Integer id; - - @Override - public void setArguments(Bundle args) { - super.setArguments(args); - id = args.getInt("id"); - } - - void addPreferencesFromResourceIfEnabled(PluginBase p, PluginType type) { - if (p.isEnabled(type) && p.getPreferencesId() != -1) - addPreferencesFromResource(p.getPreferencesId()); - } - - @Override - public void onCreate(final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - if (savedInstanceState != null && savedInstanceState.containsKey("id")) { - id = savedInstanceState.getInt("id"); - } - - if (id != -1) { - addPreferencesFromResource(id); - } else { - - if (!Config.NSCLIENT) { - addPreferencesFromResource(R.xml.pref_password); - } - addPreferencesFromResource(R.xml.pref_general); - addPreferencesFromResource(R.xml.pref_age); - - addPreferencesFromResource(R.xml.pref_overview); - - addPreferencesFromResourceIfEnabled(SourceDexcomPlugin.INSTANCE, PluginType.BGSOURCE); - addPreferencesFromResourceIfEnabled(CareportalPlugin.getPlugin(), PluginType.GENERAL); - addPreferencesFromResourceIfEnabled(SafetyPlugin.getPlugin(), PluginType.CONSTRAINTS); - if (Config.APS) { - addPreferencesFromResourceIfEnabled(LoopPlugin.getPlugin(), PluginType.LOOP); - addPreferencesFromResourceIfEnabled(OpenAPSMAPlugin.getPlugin(), PluginType.APS); - addPreferencesFromResourceIfEnabled(OpenAPSAMAPlugin.getPlugin(), PluginType.APS); - addPreferencesFromResourceIfEnabled(OpenAPSSMBPlugin.getPlugin(), PluginType.APS); - } - - addPreferencesFromResourceIfEnabled(SensitivityAAPSPlugin.getPlugin(), PluginType.SENSITIVITY); - addPreferencesFromResourceIfEnabled(SensitivityWeightedAveragePlugin.getPlugin(), PluginType.SENSITIVITY); - addPreferencesFromResourceIfEnabled(SensitivityOref0Plugin.getPlugin(), PluginType.SENSITIVITY); - addPreferencesFromResourceIfEnabled(SensitivityOref1Plugin.getPlugin(), PluginType.SENSITIVITY); - - if (Config.PUMPDRIVERS) { - addPreferencesFromResourceIfEnabled(DanaRPlugin.getPlugin(), PluginType.PUMP); - addPreferencesFromResourceIfEnabled(DanaRKoreanPlugin.getPlugin(), PluginType.PUMP); - addPreferencesFromResourceIfEnabled(DanaRv2Plugin.getPlugin(), PluginType.PUMP); - addPreferencesFromResourceIfEnabled(DanaRSPlugin.getPlugin(), PluginType.PUMP); - addPreferencesFromResourceIfEnabled(LocalInsightPlugin.getPlugin(), PluginType.PUMP); - addPreferencesFromResourceIfEnabled(ComboPlugin.getPlugin(), PluginType.PUMP); - addPreferencesFromResourceIfEnabled(MedtronicPumpPlugin.getPlugin(), PluginType.PUMP); - - if (DanaRPlugin.getPlugin().isEnabled(PluginType.PROFILE) - || DanaRKoreanPlugin.getPlugin().isEnabled(PluginType.PROFILE) - || DanaRv2Plugin.getPlugin().isEnabled(PluginType.PROFILE) - || DanaRSPlugin.getPlugin().isEnabled(PluginType.PROFILE)) { - addPreferencesFromResource(R.xml.pref_danarprofile); - } - } - - if (!Config.NSCLIENT) { - addPreferencesFromResourceIfEnabled(VirtualPumpPlugin.getPlugin(), PluginType.PUMP); - } - - addPreferencesFromResourceIfEnabled(InsulinOrefFreePeakPlugin.getPlugin(), PluginType.INSULIN); - - addPreferencesFromResourceIfEnabled(NSClientPlugin.getPlugin(), PluginType.GENERAL); - addPreferencesFromResourceIfEnabled(TidepoolPlugin.INSTANCE, PluginType.GENERAL); - addPreferencesFromResourceIfEnabled(SmsCommunicatorPlugin.INSTANCE, PluginType.GENERAL); - addPreferencesFromResourceIfEnabled(AutomationPlugin.INSTANCE, PluginType.GENERAL); - - addPreferencesFromResource(R.xml.pref_others); - addPreferencesFromResource(R.xml.pref_datachoices); - - addPreferencesFromResourceIfEnabled(WearPlugin.getPlugin(), PluginType.GENERAL); - addPreferencesFromResourceIfEnabled(StatuslinePlugin.getPlugin(), PluginType.GENERAL); - } - - initSummary(getPreferenceScreen()); - - for (PluginBase plugin : MainApp.getPluginsList()) { - plugin.preprocessPreferences(this); - } - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - outState.putInt("id", id); - } - } } diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ActivitiesModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ActivitiesModule.kt index ee98184025..cb3d910e5e 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ActivitiesModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ActivitiesModule.kt @@ -3,9 +3,14 @@ package info.nightscout.androidaps.dependencyInjection import dagger.Module import dagger.android.ContributesAndroidInjector import info.nightscout.androidaps.MainActivity +import info.nightscout.androidaps.activities.PreferencesActivity @Module abstract class ActivitiesModule { + @ContributesAndroidInjector abstract fun contributeMainActivity(): MainActivity + + @ContributesAndroidInjector + abstract fun contributesPreferencesActivity(): PreferencesActivity } \ No newline at end of file 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 2168f9f8d0..cb6fc27cb9 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppComponent.kt @@ -13,6 +13,7 @@ import javax.inject.Singleton modules = [ AndroidInjectionModule::class, ActivitiesModule::class, + FragmentsModule::class, AppModule::class ] ) 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 4f2ea5cd6b..48ab314606 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt @@ -7,15 +7,16 @@ import dagger.Binds import dagger.Module import dagger.Provides import info.nightscout.androidaps.utils.sharedPreferences.SP +import info.nightscout.androidaps.utils.sharedPreferences.SPImpl import javax.inject.Singleton @Module(includes = [AppModule.AppBindings::class]) -class AppModule() { +class AppModule { @Provides @Singleton fun provideSharedPreferences(context: Context): SP { - return SP(PreferenceManager.getDefaultSharedPreferences(context)) + return SPImpl(PreferenceManager.getDefaultSharedPreferences(context)) } @Module diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/FragmentsModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/FragmentsModule.kt new file mode 100644 index 0000000000..db9a555388 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/FragmentsModule.kt @@ -0,0 +1,11 @@ +package info.nightscout.androidaps.dependencyInjection + +import dagger.Module +import dagger.android.ContributesAndroidInjector +import info.nightscout.androidaps.activities.MyPreferenceFragment + +@Module +abstract class FragmentsModule { + @ContributesAndroidInjector + abstract fun contributesPreferencesFragment(): MyPreferenceFragment +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/dependency-injection-todo.txt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/dependency-injection-todo.txt new file mode 100644 index 0000000000..6ee0c902dc --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/dependency-injection-todo.txt @@ -0,0 +1,16 @@ +PreferenceActivity is an Example how to make an Activity injectable. +We might undo that as we don't actually have us inject things. + +MyPreferencesFragment is an injectable Fragment. Here we actually have Dagger2 provide us an InsulinOrefFreePeakPlugin instance. + +If we use multiple Fragments, we should generate a base-class. Then we only have to add a "provides***Fragment" to the FragmentsModule and derive from that class. + +InsulinOrefFreePeakPlugin is an example how to get things injected via the constructor. +You could call "new InsulinOrefFreePeakPlugin(new SPImpl(SPImpl(PreferenceManager.getDefaultSharedPreferences(context))), new ResourceHelper())"... but Dagger will resolve that for you in MainApp. :) + +SPImpl is an example how to bind an implementation to an Interface via Dagger. + +ResourceHelper is an example how to generate simple classes that Dagger can auto-resolve how to inject them. No need for a provider here. +ResourceHelper might have a weak reference at some point and not a static dependency on MainApp... but at least we reduced the static dependencies within a plugin. + +In order to Test: Any dependency of InsulinOrefFreePeakPlugin is passed via the constructor. Instead of static mocking MainApp you now can Mock ResourceHelper with a very small interface. \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/insulin/InsulinOrefFreePeakPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/insulin/InsulinOrefFreePeakPlugin.java deleted file mode 100644 index 3d32d2317b..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/insulin/InsulinOrefFreePeakPlugin.java +++ /dev/null @@ -1,49 +0,0 @@ -package info.nightscout.androidaps.plugins.insulin; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.utils.SP; - -/** - * Created by adrian on 14/08/17. - */ - -public class InsulinOrefFreePeakPlugin extends InsulinOrefBasePlugin { - - private static InsulinOrefFreePeakPlugin plugin = null; - - public static InsulinOrefFreePeakPlugin getPlugin() { - if (plugin == null) - plugin = new InsulinOrefFreePeakPlugin(); - return plugin; - } - - private static final int DEFAULT_PEAK = 75; - - private InsulinOrefFreePeakPlugin() { - super(); - pluginDescription - .pluginName(R.string.free_peak_oref) - .preferencesId(R.xml.pref_insulinoreffreepeak) - .description(R.string.description_insulin_free_peak); - } - - @Override - public int getId() { - return OREF_FREE_PEAK; - } - - public String getFriendlyName() { - return MainApp.gs(R.string.free_peak_oref); - } - - @Override - public String commentStandardText() { - return MainApp.gs(R.string.insulin_peak_time) + ": " + getPeak(); - } - - @Override - int getPeak() { - return SP.getInt(R.string.key_insulin_oref_peak, DEFAULT_PEAK); - } -} 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 new file mode 100644 index 0000000000..6707fd7873 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/insulin/InsulinOrefFreePeakPlugin.kt @@ -0,0 +1,45 @@ +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.sharedPreferences.SP +import javax.inject.Inject +import javax.inject.Singleton + +/** + * Created by adrian on 14/08/17. + */ +@Singleton +class InsulinOrefFreePeakPlugin @Inject constructor( + private val sp: SP, + private val resourceHelper: ResourceHelper +) : InsulinOrefBasePlugin() { + + override fun getId(): Int { + return InsulinInterface.OREF_FREE_PEAK + } + + override fun getFriendlyName(): String { + return resourceHelper.gs(R.string.free_peak_oref) + } + + public override fun commentStandardText(): String { + return resourceHelper.gs(R.string.insulin_peak_time) + ": " + peak + } + + public override fun getPeak(): Int { + return sp.getInt(R.string.key_insulin_oref_peak, DEFAULT_PEAK) + } + + companion object { + private const val DEFAULT_PEAK = 75 + } + + init { + pluginDescription + .pluginName(R.string.free_peak_oref) + .preferencesId(R.xml.pref_insulinoreffreepeak) + .description(R.string.description_insulin_free_peak) + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/utils/ResourceHelper.kt b/app/src/main/java/info/nightscout/androidaps/utils/ResourceHelper.kt new file mode 100644 index 0000000000..655c319170 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/utils/ResourceHelper.kt @@ -0,0 +1,22 @@ +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. + */ +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/sharedPreferences/SP.kt b/app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SP.kt index a56f4736b1..e1bb9e9373 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SP.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SP.kt @@ -1,122 +1,34 @@ package info.nightscout.androidaps.utils.sharedPreferences -import android.content.SharedPreferences -import info.nightscout.androidaps.MainApp -import info.nightscout.androidaps.utils.SafeParse -import javax.inject.Inject -import javax.inject.Singleton - /** - * Created by mike on 17.02.2017. + * Created by adrian on 2019-12-23. */ -@Singleton -class SP @Inject constructor(private val sharedPreferences: SharedPreferences) { - - fun getAll(): Map = sharedPreferences.all - - fun clear() = sharedPreferences.edit().clear().apply() - - fun contains(key: String): Boolean = sharedPreferences.contains(key) - - fun contains(resourceId: Int): Boolean = sharedPreferences.contains(MainApp.gs(resourceId)) - - fun remove(resourceID: Int) = - sharedPreferences.edit().remove(MainApp.gs(resourceID)).apply() - - fun remove(key: String) = - sharedPreferences.edit().remove(key).apply() - - fun getString(resourceID: Int, defaultValue: String): String = - sharedPreferences.getString(MainApp.gs(resourceID), defaultValue) - - fun getString(key: String, defaultValue: String): String = - sharedPreferences.getString(key, defaultValue) - - fun getBoolean(resourceID: Int, defaultValue: Boolean): Boolean { - return try { - sharedPreferences.getBoolean(MainApp.gs(resourceID), defaultValue) - } catch (e: Exception) { - defaultValue - } - } - - fun getBoolean(key: String, defaultValue: Boolean): Boolean { - return try { - sharedPreferences.getBoolean(key, defaultValue) - } catch (e: Exception) { - defaultValue - } - } - - fun getDouble(resourceID: Int, defaultValue: Double): Double = - SafeParse.stringToDouble(sharedPreferences.getString(MainApp.gs(resourceID), defaultValue.toString())) - - fun getDouble(key: String, defaultValue: Double): Double = - SafeParse.stringToDouble(sharedPreferences.getString(key, defaultValue.toString())) - - fun getInt(resourceID: Int, defaultValue: Int): Int { - return try { - sharedPreferences.getInt(MainApp.gs(resourceID), defaultValue) - } catch (e: Exception) { - SafeParse.stringToInt(sharedPreferences.getString(MainApp.gs(resourceID), defaultValue.toString())) - } - } - - fun getInt(key: String, defaultValue: Int): Int { - return try { - sharedPreferences.getInt(key, defaultValue) - } catch (e: Exception) { - SafeParse.stringToInt(sharedPreferences.getString(key, defaultValue.toString())) - } - } - - fun getLong(resourceID: Int, defaultValue: Long): Long { - return try { - sharedPreferences.getLong(MainApp.gs(resourceID), defaultValue) - } catch (e: Exception) { - SafeParse.stringToLong(sharedPreferences.getString(MainApp.gs(resourceID), defaultValue.toString())) - } - } - - fun getLong(key: String, defaultValue: Long): Long { - return try { - sharedPreferences.getLong(key, defaultValue) - } catch (e: Exception) { - SafeParse.stringToLong(sharedPreferences.getString(key, defaultValue.toString())) - } - } - - fun putBoolean(key: String, value: Boolean) = sharedPreferences.edit().putBoolean(key, value).apply() - - fun putBoolean(resourceID: Int, value: Boolean) = - sharedPreferences.edit().putBoolean(MainApp.gs(resourceID), value).apply() - - - fun putDouble(key: String, value: Double) = - sharedPreferences.edit().putString(key, java.lang.Double.toString(value)).apply() - - - fun putLong(key: String, value: Long) = - sharedPreferences.edit().putLong(key, value).apply() - - fun putLong(resourceID: Int, value: Long) = - sharedPreferences.edit().putLong(MainApp.gs(resourceID), value).apply() - - fun putInt(key: String, value: Int) = - sharedPreferences.edit().putInt(key, value).apply() - - fun putInt(resourceID: Int, value: Int) = - sharedPreferences.edit().putInt(MainApp.gs(resourceID), value).apply() - - fun incInt(resourceID: Int) { - val value = getInt(resourceID, 0) + 1 - sharedPreferences.edit().putInt(MainApp.gs(resourceID), value).apply() - } - - fun putString(resourceID: Int, value: String) = - sharedPreferences.edit().putString(MainApp.gs(resourceID), value).apply() - - fun putString(key: String, value: String) = - sharedPreferences.edit().putString(key, value).apply() +interface SP { + fun getAll(): Map + fun clear() + fun contains(key: String): Boolean + fun contains(resourceId: Int): Boolean + fun remove(resourceID: Int) + fun remove(key: String) + fun getString(resourceID: Int, defaultValue: String): String + fun getString(key: String, defaultValue: String): String + fun getBoolean(resourceID: Int, defaultValue: Boolean): Boolean + fun getBoolean(key: String, defaultValue: Boolean): Boolean + fun getDouble(resourceID: Int, defaultValue: Double): Double + fun getDouble(key: String, defaultValue: Double): Double + fun getInt(resourceID: Int, defaultValue: Int): Int + fun getInt(key: String, defaultValue: Int): Int + fun getLong(resourceID: Int, defaultValue: Long): Long + fun getLong(key: String, defaultValue: Long): Long + fun putBoolean(key: String, value: Boolean) + fun putBoolean(resourceID: Int, value: Boolean) + fun putDouble(key: String, value: Double) + fun putLong(key: String, value: Long) + fun putLong(resourceID: Int, value: Long) + fun putInt(key: String, value: Int) + fun putInt(resourceID: Int, value: Int) + fun incInt(resourceID: Int) + fun putString(resourceID: Int, value: String) + fun putString(key: String, value: String) } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SPImpl.kt b/app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SPImpl.kt new file mode 100644 index 0000000000..5c73a56967 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SPImpl.kt @@ -0,0 +1,122 @@ +package info.nightscout.androidaps.utils.sharedPreferences + +import android.content.SharedPreferences +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.utils.SafeParse +import javax.inject.Inject +import javax.inject.Singleton + +/** + * Created by mike on 17.02.2017. + */ +@Singleton +class SPImpl @Inject constructor(private val sharedPreferences: SharedPreferences) : SP { + + override fun getAll(): Map = sharedPreferences.all + + override fun clear() = sharedPreferences.edit().clear().apply() + + override fun contains(key: String): Boolean = sharedPreferences.contains(key) + + override fun contains(resourceId: Int): Boolean = sharedPreferences.contains(MainApp.gs(resourceId)) + + override fun remove(resourceID: Int) = + sharedPreferences.edit().remove(MainApp.gs(resourceID)).apply() + + override fun remove(key: String) = + sharedPreferences.edit().remove(key).apply() + + override fun getString(resourceID: Int, defaultValue: String): String = + sharedPreferences.getString(MainApp.gs(resourceID), defaultValue) + + override fun getString(key: String, defaultValue: String): String = + sharedPreferences.getString(key, defaultValue) + + override fun getBoolean(resourceID: Int, defaultValue: Boolean): Boolean { + return try { + sharedPreferences.getBoolean(MainApp.gs(resourceID), defaultValue) + } catch (e: Exception) { + defaultValue + } + } + + override fun getBoolean(key: String, defaultValue: Boolean): Boolean { + return try { + sharedPreferences.getBoolean(key, defaultValue) + } catch (e: Exception) { + defaultValue + } + } + + override fun getDouble(resourceID: Int, defaultValue: Double): Double = + SafeParse.stringToDouble(sharedPreferences.getString(MainApp.gs(resourceID), defaultValue.toString())) + + override fun getDouble(key: String, defaultValue: Double): Double = + SafeParse.stringToDouble(sharedPreferences.getString(key, defaultValue.toString())) + + override fun getInt(resourceID: Int, defaultValue: Int): Int { + return try { + sharedPreferences.getInt(MainApp.gs(resourceID), defaultValue) + } catch (e: Exception) { + SafeParse.stringToInt(sharedPreferences.getString(MainApp.gs(resourceID), defaultValue.toString())) + } + } + + override fun getInt(key: String, defaultValue: Int): Int { + return try { + sharedPreferences.getInt(key, defaultValue) + } catch (e: Exception) { + SafeParse.stringToInt(sharedPreferences.getString(key, defaultValue.toString())) + } + } + + override fun getLong(resourceID: Int, defaultValue: Long): Long { + return try { + sharedPreferences.getLong(MainApp.gs(resourceID), defaultValue) + } catch (e: Exception) { + SafeParse.stringToLong(sharedPreferences.getString(MainApp.gs(resourceID), defaultValue.toString())) + } + } + + override fun getLong(key: String, defaultValue: Long): Long { + return try { + sharedPreferences.getLong(key, defaultValue) + } catch (e: Exception) { + SafeParse.stringToLong(sharedPreferences.getString(key, defaultValue.toString())) + } + } + + override fun putBoolean(key: String, value: Boolean) = sharedPreferences.edit().putBoolean(key, value).apply() + + override fun putBoolean(resourceID: Int, value: Boolean) = + sharedPreferences.edit().putBoolean(MainApp.gs(resourceID), value).apply() + + + override fun putDouble(key: String, value: Double) = + sharedPreferences.edit().putString(key, java.lang.Double.toString(value)).apply() + + + override fun putLong(key: String, value: Long) = + sharedPreferences.edit().putLong(key, value).apply() + + override fun putLong(resourceID: Int, value: Long) = + sharedPreferences.edit().putLong(MainApp.gs(resourceID), value).apply() + + override fun putInt(key: String, value: Int) = + sharedPreferences.edit().putInt(key, value).apply() + + override fun putInt(resourceID: Int, value: Int) = + sharedPreferences.edit().putInt(MainApp.gs(resourceID), value).apply() + + override fun incInt(resourceID: Int) { + val value = getInt(resourceID, 0) + 1 + sharedPreferences.edit().putInt(MainApp.gs(resourceID), value).apply() + } + + override fun putString(resourceID: Int, value: String) = + sharedPreferences.edit().putString(MainApp.gs(resourceID), value).apply() + + override fun putString(key: String, value: String) = + sharedPreferences.edit().putString(key, value).apply() + +} \ No newline at end of file