From eb9e4a45f975bc7a4f3ca78a891090c323b38488 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 23 Apr 2022 12:35:09 +0200 Subject: [PATCH] Wear: Tile injection --- .../androidaps/di/WearServicesModule.kt | 6 +-- .../androidaps/tile/ActionSource.kt | 8 +++- .../androidaps/tile/ActionsTileService.kt | 14 +++++- .../androidaps/tile/QuickWizardSource.kt | 9 ++-- .../androidaps/tile/QuickWizardTileService.kt | 13 +++++- .../androidaps/tile/StaticTileSource.kt | 45 +++++++++---------- .../androidaps/tile/TempTargetSource.kt | 8 +++- .../androidaps/tile/TempTargetTileService.kt | 13 +++++- .../nightscout/androidaps/tile/TileBase.kt | 9 ++-- 9 files changed, 83 insertions(+), 42 deletions(-) diff --git a/wear/src/main/java/info/nightscout/androidaps/di/WearServicesModule.kt b/wear/src/main/java/info/nightscout/androidaps/di/WearServicesModule.kt index 4894e25027..babfb10af4 100644 --- a/wear/src/main/java/info/nightscout/androidaps/di/WearServicesModule.kt +++ b/wear/src/main/java/info/nightscout/androidaps/di/WearServicesModule.kt @@ -4,10 +4,7 @@ import dagger.Module import dagger.android.ContributesAndroidInjector import info.nightscout.androidaps.comm.DataLayerListenerServiceWear import info.nightscout.androidaps.complications.* -import info.nightscout.androidaps.tile.ActionsTileService -import info.nightscout.androidaps.tile.QuickWizardTileService -import info.nightscout.androidaps.tile.TempTargetTileService -import info.nightscout.androidaps.tile.TileBase +import info.nightscout.androidaps.tile.* import info.nightscout.androidaps.watchfaces.* @Module @@ -46,4 +43,5 @@ abstract class WearServicesModule { @ContributesAndroidInjector abstract fun contributesQuickWizardTileService(): QuickWizardTileService @ContributesAndroidInjector abstract fun contributesTempTargetTileService(): TempTargetTileService @ContributesAndroidInjector abstract fun contributesActionsTileService(): ActionsTileService + } \ No newline at end of file diff --git a/wear/src/main/java/info/nightscout/androidaps/tile/ActionSource.kt b/wear/src/main/java/info/nightscout/androidaps/tile/ActionSource.kt index 9223060e68..492c10d9ec 100644 --- a/wear/src/main/java/info/nightscout/androidaps/tile/ActionSource.kt +++ b/wear/src/main/java/info/nightscout/androidaps/tile/ActionSource.kt @@ -1,10 +1,16 @@ package info.nightscout.androidaps.tile +import android.content.Context import android.content.res.Resources import info.nightscout.androidaps.R import info.nightscout.androidaps.interaction.actions.* +import info.nightscout.shared.logging.AAPSLogger +import info.nightscout.shared.sharedPreferences.SP +import javax.inject.Inject +import javax.inject.Singleton -object ActionSource : StaticTileSource() { +@Singleton +class ActionSource @Inject constructor(context: Context, sp : SP, aapsLogger: AAPSLogger) : StaticTileSource(context, sp, aapsLogger) { override val preferencePrefix = "tile_action_" diff --git a/wear/src/main/java/info/nightscout/androidaps/tile/ActionsTileService.kt b/wear/src/main/java/info/nightscout/androidaps/tile/ActionsTileService.kt index b30ec2a87a..959948443a 100644 --- a/wear/src/main/java/info/nightscout/androidaps/tile/ActionsTileService.kt +++ b/wear/src/main/java/info/nightscout/androidaps/tile/ActionsTileService.kt @@ -1,6 +1,18 @@ package info.nightscout.androidaps.tile +import dagger.android.AndroidInjection +import javax.inject.Inject + class ActionsTileService : TileBase() { + + @Inject lateinit var actionSource: ActionSource + + // Not derived from DaggerService, do injection here + override fun onCreate() { + AndroidInjection.inject(this) + super.onCreate() + } + override val resourceVersion = "ActionsTileService" - override val source = ActionSource + override val source get() = actionSource } diff --git a/wear/src/main/java/info/nightscout/androidaps/tile/QuickWizardSource.kt b/wear/src/main/java/info/nightscout/androidaps/tile/QuickWizardSource.kt index b03123076f..00993b09bf 100644 --- a/wear/src/main/java/info/nightscout/androidaps/tile/QuickWizardSource.kt +++ b/wear/src/main/java/info/nightscout/androidaps/tile/QuickWizardSource.kt @@ -9,10 +9,13 @@ import info.nightscout.shared.logging.LTag import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.weardata.EventData import java.util.* +import javax.inject.Inject +import javax.inject.Singleton -object QuickWizardSource : TileSource { +@Singleton +class QuickWizardSource @Inject constructor(private val context: Context, private val sp: SP, private val aapsLogger: AAPSLogger): TileSource { - override fun getSelectedActions(context: Context, sp: SP, aapsLogger: AAPSLogger): List { + override fun getSelectedActions(): List { val quickList = mutableListOf() val quickMap = getQuickWizardData(sp) val sfm = secondsFromMidnight() @@ -38,7 +41,7 @@ object QuickWizardSource : TileSource { return quickList } - override fun getValidFor(sp: SP): Long? { + override fun getValidFor(): Long? { val quickMap = getQuickWizardData(sp) if (quickMap.entries.size == 0) return null diff --git a/wear/src/main/java/info/nightscout/androidaps/tile/QuickWizardTileService.kt b/wear/src/main/java/info/nightscout/androidaps/tile/QuickWizardTileService.kt index d6afa427f9..7dfc32a540 100644 --- a/wear/src/main/java/info/nightscout/androidaps/tile/QuickWizardTileService.kt +++ b/wear/src/main/java/info/nightscout/androidaps/tile/QuickWizardTileService.kt @@ -1,7 +1,18 @@ package info.nightscout.androidaps.tile +import dagger.android.AndroidInjection +import javax.inject.Inject + class QuickWizardTileService : TileBase() { + @Inject lateinit var quickWizardSource: QuickWizardSource + + // Not derived from DaggerService, do injection here + override fun onCreate() { + AndroidInjection.inject(this) + super.onCreate() + } + override val resourceVersion = "QuickWizardTileService" - override val source = QuickWizardSource + override val source get() = quickWizardSource } diff --git a/wear/src/main/java/info/nightscout/androidaps/tile/StaticTileSource.kt b/wear/src/main/java/info/nightscout/androidaps/tile/StaticTileSource.kt index 5f27d2e367..93d29d6927 100644 --- a/wear/src/main/java/info/nightscout/androidaps/tile/StaticTileSource.kt +++ b/wear/src/main/java/info/nightscout/androidaps/tile/StaticTileSource.kt @@ -4,35 +4,33 @@ import android.content.Context import android.content.SharedPreferences import android.content.res.Resources import androidx.annotation.DrawableRes -import androidx.preference.PreferenceManager import info.nightscout.shared.logging.AAPSLogger import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.weardata.EventData -class StaticAction( - val settingName: String, - buttonText: String, - buttonTextSub: String? = null, - activityClass: String, - @DrawableRes iconRes: Int, - action: EventData? = null, - message: String? = null, -) : Action(buttonText, buttonTextSub, activityClass, iconRes, action, message) +abstract class StaticTileSource(val context: Context, val sp: SP, val aapsLogger: AAPSLogger) : TileSource { -abstract class StaticTileSource : TileSource { + class StaticAction( + val settingName: String, + buttonText: String, + buttonTextSub: String? = null, + activityClass: String, + @DrawableRes iconRes: Int, + action: EventData? = null, + message: String? = null, + ) : Action(buttonText, buttonTextSub, activityClass, iconRes, action, message) abstract fun getActions(resources: Resources): List abstract val preferencePrefix: String abstract fun getDefaultConfig(): Map - override fun getSelectedActions(context: Context, sp: SP, aapsLogger: AAPSLogger): List { - val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context) - setDefaultSettings(sharedPrefs) + override fun getSelectedActions(): List { + setDefaultSettings() val actionList: MutableList = mutableListOf() for (i in 1..4) { - val action = getActionFromPreference(context.resources, sharedPrefs, i) + val action = getActionFromPreference(i) if (action != null) { actionList.add(action) } @@ -43,23 +41,20 @@ abstract class StaticTileSource : TileSource { return actionList } - override fun getValidFor(sp: SP): Long? = null + override fun getValidFor(): Long? = null - private fun getActionFromPreference(resources: Resources, sharedPrefs: SharedPreferences, index: Int): Action? { - val actionPref = sharedPrefs.getString(preferencePrefix + index, "none") - return getActions(resources).find { action -> action.settingName == actionPref } + private fun getActionFromPreference(index: Int): Action? { + val actionPref = sp.getString(preferencePrefix + index, "none") + return getActions(context.resources).find { action -> action.settingName == actionPref } } - private fun setDefaultSettings(sharedPrefs: SharedPreferences) { + private fun setDefaultSettings() { val defaults = getDefaultConfig() val firstKey = defaults.firstNotNullOf { settings -> settings.key } - if (!sharedPrefs.contains(firstKey)) { - val editor = sharedPrefs.edit() + if (!sp.contains(firstKey)) { for ((key, value) in defaults) { - editor.putString(key, value) + sp.putString(key, value) } - editor.apply() } } - } diff --git a/wear/src/main/java/info/nightscout/androidaps/tile/TempTargetSource.kt b/wear/src/main/java/info/nightscout/androidaps/tile/TempTargetSource.kt index 48c39de5de..3ebbfdc7e2 100644 --- a/wear/src/main/java/info/nightscout/androidaps/tile/TempTargetSource.kt +++ b/wear/src/main/java/info/nightscout/androidaps/tile/TempTargetSource.kt @@ -1,12 +1,18 @@ package info.nightscout.androidaps.tile +import android.content.Context import android.content.res.Resources import info.nightscout.androidaps.R import info.nightscout.androidaps.interaction.actions.BackgroundActionActivity import info.nightscout.androidaps.interaction.actions.TempTargetActivity +import info.nightscout.shared.logging.AAPSLogger +import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.weardata.EventData +import javax.inject.Inject +import javax.inject.Singleton -object TempTargetSource : StaticTileSource() { +@Singleton +class TempTargetSource @Inject constructor(context: Context, sp: SP, aapsLogger: AAPSLogger) : StaticTileSource(context, sp, aapsLogger) { override val preferencePrefix = "tile_tempt_" diff --git a/wear/src/main/java/info/nightscout/androidaps/tile/TempTargetTileService.kt b/wear/src/main/java/info/nightscout/androidaps/tile/TempTargetTileService.kt index 1f315fb2b8..8b17893ac1 100644 --- a/wear/src/main/java/info/nightscout/androidaps/tile/TempTargetTileService.kt +++ b/wear/src/main/java/info/nightscout/androidaps/tile/TempTargetTileService.kt @@ -1,7 +1,18 @@ package info.nightscout.androidaps.tile +import dagger.android.AndroidInjection +import javax.inject.Inject + class TempTargetTileService : TileBase() { + @Inject lateinit var tempTargetSource: TempTargetSource + + // Not derived from DaggerService, do injection here + override fun onCreate() { + AndroidInjection.inject(this) + super.onCreate() + } + override val resourceVersion = "TempTargetTileService" - override val source = TempTargetSource + override val source get() = tempTargetSource } diff --git a/wear/src/main/java/info/nightscout/androidaps/tile/TileBase.kt b/wear/src/main/java/info/nightscout/androidaps/tile/TileBase.kt index 069883ee03..4bf01205fe 100644 --- a/wear/src/main/java/info/nightscout/androidaps/tile/TileBase.kt +++ b/wear/src/main/java/info/nightscout/androidaps/tile/TileBase.kt @@ -1,6 +1,5 @@ package info.nightscout.androidaps.tile -import android.content.Context import android.os.Build import androidx.annotation.DrawableRes import androidx.annotation.RequiresApi @@ -49,8 +48,8 @@ private const val LARGE_SCREEN_WIDTH_DP = 210 interface TileSource { fun getResourceReferences(resources: android.content.res.Resources): List - fun getSelectedActions(context: Context, sp: SP, aapsLogger: AAPSLogger): List - fun getValidFor(sp: SP): Long? + fun getSelectedActions(): List + fun getValidFor(): Long? } open class Action( @@ -107,11 +106,11 @@ abstract class TileBase : TileService() { private fun getSelectedActions(): List { // TODO check why thi scan not be don in scope of the coroutine - return source.getSelectedActions(this, sp, aapsLogger) + return source.getSelectedActions() } private fun validFor(): Long? { - return source.getValidFor(sp) + return source.getValidFor() } @RequiresApi(Build.VERSION_CODES.N)