diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.kt b/app/src/main/java/info/nightscout/androidaps/MainApp.kt index 40b6b64764..f91f208f97 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.kt +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.kt @@ -29,6 +29,7 @@ import info.nightscout.androidaps.plugins.configBuilder.PluginStore import info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerUtils import info.nightscout.androidaps.plugins.general.overview.notifications.Notification import info.nightscout.androidaps.plugins.general.overview.notifications.NotificationStore +import info.nightscout.androidaps.plugins.general.themes.ThemeSwitcherPlugin import info.nightscout.androidaps.receivers.BTReceiver import info.nightscout.androidaps.receivers.ChargingStateReceiver import info.nightscout.androidaps.receivers.KeepAliveReceiver.KeepAliveManager @@ -74,6 +75,7 @@ class MainApp : DaggerApplication() { @Inject lateinit var alarmSoundServiceHelper: AlarmSoundServiceHelper @Inject lateinit var notificationStore: NotificationStore @Inject lateinit var processLifecycleListener: ProcessLifecycleListener + @Inject lateinit var profileSwitchPlugin: ThemeSwitcherPlugin override fun onCreate() { super.onCreate() @@ -104,7 +106,7 @@ class MainApp : DaggerApplication() { disposable += compatDBHelper.dbChangeDisposable() registerActivityLifecycleCallbacks(activityMonitor) JodaTimeAndroid.init(this) - selectThemeMode() + profileSwitchPlugin.setThemeMode() aapsLogger.debug("Version: " + BuildConfig.VERSION_NAME) aapsLogger.debug("BuildVersion: " + BuildConfig.BUILDVERSION) aapsLogger.debug("Remote: " + BuildConfig.REMOTE) @@ -125,16 +127,6 @@ class MainApp : DaggerApplication() { passwordCheck.passwordResetCheck(this) } - private fun selectThemeMode() { - when(sp.getString(R.string.key_use_dark_mode, "dark")) { - sp.getString(R.string.value_dark_theme, "dark") -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); - sp.getString(R.string.value_light_theme, "light") -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); - else -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); - } - // temp while switch is not public - setTheme(R.style.AppTheme) - } - private fun setRxErrorHandler() { RxJavaPlugins.setErrorHandler { t: Throwable -> var e = t diff --git a/app/src/main/java/info/nightscout/androidaps/di/PluginsModule.kt b/app/src/main/java/info/nightscout/androidaps/di/PluginsModule.kt index 393eb4ba04..127643b855 100644 --- a/app/src/main/java/info/nightscout/androidaps/di/PluginsModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/di/PluginsModule.kt @@ -32,6 +32,7 @@ import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin import info.nightscout.androidaps.plugins.general.overview.OverviewPlugin import info.nightscout.androidaps.plugins.general.persistentNotification.PersistentNotificationPlugin import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin +import info.nightscout.androidaps.plugins.general.themes.ThemeSwitcherPlugin import info.nightscout.androidaps.plugins.general.wear.WearPlugin import info.nightscout.androidaps.plugins.general.xdripStatusline.StatusLinePlugin import info.nightscout.androidaps.plugins.insulin.InsulinLyumjevPlugin @@ -387,6 +388,12 @@ abstract class PluginsModule { @IntKey(490) abstract fun bindConfigBuilderPlugin(plugin: ConfigBuilderPlugin): PluginBase + @Binds + @AllConfigs + @IntoMap + @IntKey(500) + abstract fun bindThemeSwitcherPlugin(plugin: ThemeSwitcherPlugin): PluginBase + @Qualifier annotation class AllConfigs diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventThemeSwitch.kt b/app/src/main/java/info/nightscout/androidaps/events/EventThemeSwitch.kt new file mode 100644 index 0000000000..37cbef5ae3 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventThemeSwitch.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventThemeSwitch : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/themes/ThemeSwitcherPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/themes/ThemeSwitcherPlugin.kt new file mode 100644 index 0000000000..0523fde640 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/themes/ThemeSwitcherPlugin.kt @@ -0,0 +1,61 @@ +package info.nightscout.androidaps.plugins.general.themes + +import androidx.appcompat.app.AppCompatDelegate +import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM +import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES +import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO +import dagger.android.HasAndroidInjector +import info.nightscout.androidaps.R +import info.nightscout.androidaps.events.EventPreferenceChange +import info.nightscout.androidaps.events.EventThemeSwitch +import info.nightscout.androidaps.interfaces.PluginBase +import info.nightscout.androidaps.interfaces.PluginDescription +import info.nightscout.androidaps.interfaces.PluginType +import info.nightscout.shared.logging.AAPSLogger +import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.utils.resources.ResourceHelper +import info.nightscout.shared.sharedPreferences.SP +import io.reactivex.rxjava3.disposables.CompositeDisposable +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class ThemeSwitcherPlugin @Inject constructor( + injector: HasAndroidInjector, + aapsLogger: AAPSLogger, + rh: ResourceHelper, + private val sp: SP, + private val rxBus: RxBus, +) : PluginBase(PluginDescription() + .mainType(PluginType.GENERAL) + .neverVisible(true) + .alwaysEnabled(true) + .showInList(false) + .pluginName(R.string.dst_plugin_name), + aapsLogger, rh, injector +) { + + private val compositeDisposable = CompositeDisposable() + + override fun onStart() { + compositeDisposable.add(rxBus.toObservable(EventPreferenceChange::class.java).subscribe { + if (it.isChanged(rh, id = R.string.key_use_dark_mode)) { + setThemeMode() + rxBus.send(EventThemeSwitch()) + } + }) + } + + fun setThemeMode() { + val mode = when (sp.getString(R.string.key_use_dark_mode, "dark")) { + sp.getString(R.string.value_dark_theme, "dark") -> MODE_NIGHT_YES + sp.getString(R.string.value_light_theme, "light") -> MODE_NIGHT_NO + else -> MODE_NIGHT_FOLLOW_SYSTEM + } + AppCompatDelegate.setDefaultNightMode(mode) + } + + override fun onStop() { + compositeDisposable.dispose() + } +} diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 0e006e2bbf..771a2e7a73 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -147,4 +147,16 @@ 4 + + @string/dark_theme + @string/light_theme + @string/follow_system_theme + + + + @string/value_dark_theme + @string/value_light_theme + @string/value_system_theme + + diff --git a/app/src/main/res/xml/pref_general.xml b/app/src/main/res/xml/pref_general.xml index 55bc0a30fa..abede4e252 100644 --- a/app/src/main/res/xml/pref_general.xml +++ b/app/src/main/res/xml/pref_general.xml @@ -101,6 +101,15 @@ android:key="@string/key_skin" android:title="@string/skin" /> + +