Wear: Tile injection

This commit is contained in:
Milos Kozak 2022-04-23 12:35:09 +02:00
parent 7050cd507c
commit eb9e4a45f9
9 changed files with 83 additions and 42 deletions

View file

@ -4,10 +4,7 @@ import dagger.Module
import dagger.android.ContributesAndroidInjector import dagger.android.ContributesAndroidInjector
import info.nightscout.androidaps.comm.DataLayerListenerServiceWear import info.nightscout.androidaps.comm.DataLayerListenerServiceWear
import info.nightscout.androidaps.complications.* import info.nightscout.androidaps.complications.*
import info.nightscout.androidaps.tile.ActionsTileService import info.nightscout.androidaps.tile.*
import info.nightscout.androidaps.tile.QuickWizardTileService
import info.nightscout.androidaps.tile.TempTargetTileService
import info.nightscout.androidaps.tile.TileBase
import info.nightscout.androidaps.watchfaces.* import info.nightscout.androidaps.watchfaces.*
@Module @Module
@ -46,4 +43,5 @@ abstract class WearServicesModule {
@ContributesAndroidInjector abstract fun contributesQuickWizardTileService(): QuickWizardTileService @ContributesAndroidInjector abstract fun contributesQuickWizardTileService(): QuickWizardTileService
@ContributesAndroidInjector abstract fun contributesTempTargetTileService(): TempTargetTileService @ContributesAndroidInjector abstract fun contributesTempTargetTileService(): TempTargetTileService
@ContributesAndroidInjector abstract fun contributesActionsTileService(): ActionsTileService @ContributesAndroidInjector abstract fun contributesActionsTileService(): ActionsTileService
} }

View file

@ -1,10 +1,16 @@
package info.nightscout.androidaps.tile package info.nightscout.androidaps.tile
import android.content.Context
import android.content.res.Resources import android.content.res.Resources
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.interaction.actions.* 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_" override val preferencePrefix = "tile_action_"

View file

@ -1,6 +1,18 @@
package info.nightscout.androidaps.tile package info.nightscout.androidaps.tile
import dagger.android.AndroidInjection
import javax.inject.Inject
class ActionsTileService : TileBase() { 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 resourceVersion = "ActionsTileService"
override val source = ActionSource override val source get() = actionSource
} }

View file

@ -9,10 +9,13 @@ import info.nightscout.shared.logging.LTag
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.weardata.EventData import info.nightscout.shared.weardata.EventData
import java.util.* 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<Action> { override fun getSelectedActions(): List<Action> {
val quickList = mutableListOf<Action>() val quickList = mutableListOf<Action>()
val quickMap = getQuickWizardData(sp) val quickMap = getQuickWizardData(sp)
val sfm = secondsFromMidnight() val sfm = secondsFromMidnight()
@ -38,7 +41,7 @@ object QuickWizardSource : TileSource {
return quickList return quickList
} }
override fun getValidFor(sp: SP): Long? { override fun getValidFor(): Long? {
val quickMap = getQuickWizardData(sp) val quickMap = getQuickWizardData(sp)
if (quickMap.entries.size == 0) return null if (quickMap.entries.size == 0) return null

View file

@ -1,7 +1,18 @@
package info.nightscout.androidaps.tile package info.nightscout.androidaps.tile
import dagger.android.AndroidInjection
import javax.inject.Inject
class QuickWizardTileService : TileBase() { 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 resourceVersion = "QuickWizardTileService"
override val source = QuickWizardSource override val source get() = quickWizardSource
} }

View file

@ -4,12 +4,13 @@ import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.content.res.Resources import android.content.res.Resources
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import androidx.preference.PreferenceManager
import info.nightscout.shared.logging.AAPSLogger import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.weardata.EventData import info.nightscout.shared.weardata.EventData
class StaticAction( abstract class StaticTileSource(val context: Context, val sp: SP, val aapsLogger: AAPSLogger) : TileSource {
class StaticAction(
val settingName: String, val settingName: String,
buttonText: String, buttonText: String,
buttonTextSub: String? = null, buttonTextSub: String? = null,
@ -17,22 +18,19 @@ class StaticAction(
@DrawableRes iconRes: Int, @DrawableRes iconRes: Int,
action: EventData? = null, action: EventData? = null,
message: String? = null, message: String? = null,
) : Action(buttonText, buttonTextSub, activityClass, iconRes, action, message) ) : Action(buttonText, buttonTextSub, activityClass, iconRes, action, message)
abstract class StaticTileSource : TileSource {
abstract fun getActions(resources: Resources): List<StaticAction> abstract fun getActions(resources: Resources): List<StaticAction>
abstract val preferencePrefix: String abstract val preferencePrefix: String
abstract fun getDefaultConfig(): Map<String, String> abstract fun getDefaultConfig(): Map<String, String>
override fun getSelectedActions(context: Context, sp: SP, aapsLogger: AAPSLogger): List<Action> { override fun getSelectedActions(): List<Action> {
val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context) setDefaultSettings()
setDefaultSettings(sharedPrefs)
val actionList: MutableList<Action> = mutableListOf() val actionList: MutableList<Action> = mutableListOf()
for (i in 1..4) { for (i in 1..4) {
val action = getActionFromPreference(context.resources, sharedPrefs, i) val action = getActionFromPreference(i)
if (action != null) { if (action != null) {
actionList.add(action) actionList.add(action)
} }
@ -43,23 +41,20 @@ abstract class StaticTileSource : TileSource {
return actionList return actionList
} }
override fun getValidFor(sp: SP): Long? = null override fun getValidFor(): Long? = null
private fun getActionFromPreference(resources: Resources, sharedPrefs: SharedPreferences, index: Int): Action? { private fun getActionFromPreference(index: Int): Action? {
val actionPref = sharedPrefs.getString(preferencePrefix + index, "none") val actionPref = sp.getString(preferencePrefix + index, "none")
return getActions(resources).find { action -> action.settingName == actionPref } return getActions(context.resources).find { action -> action.settingName == actionPref }
} }
private fun setDefaultSettings(sharedPrefs: SharedPreferences) { private fun setDefaultSettings() {
val defaults = getDefaultConfig() val defaults = getDefaultConfig()
val firstKey = defaults.firstNotNullOf { settings -> settings.key } val firstKey = defaults.firstNotNullOf { settings -> settings.key }
if (!sharedPrefs.contains(firstKey)) { if (!sp.contains(firstKey)) {
val editor = sharedPrefs.edit()
for ((key, value) in defaults) { for ((key, value) in defaults) {
editor.putString(key, value) sp.putString(key, value)
} }
editor.apply()
} }
} }
} }

View file

@ -1,12 +1,18 @@
package info.nightscout.androidaps.tile package info.nightscout.androidaps.tile
import android.content.Context
import android.content.res.Resources import android.content.res.Resources
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.interaction.actions.BackgroundActionActivity import info.nightscout.androidaps.interaction.actions.BackgroundActionActivity
import info.nightscout.androidaps.interaction.actions.TempTargetActivity 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 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_" override val preferencePrefix = "tile_tempt_"

View file

@ -1,7 +1,18 @@
package info.nightscout.androidaps.tile package info.nightscout.androidaps.tile
import dagger.android.AndroidInjection
import javax.inject.Inject
class TempTargetTileService : TileBase() { 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 resourceVersion = "TempTargetTileService"
override val source = TempTargetSource override val source get() = tempTargetSource
} }

View file

@ -1,6 +1,5 @@
package info.nightscout.androidaps.tile package info.nightscout.androidaps.tile
import android.content.Context
import android.os.Build import android.os.Build
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
@ -49,8 +48,8 @@ private const val LARGE_SCREEN_WIDTH_DP = 210
interface TileSource { interface TileSource {
fun getResourceReferences(resources: android.content.res.Resources): List<Int> fun getResourceReferences(resources: android.content.res.Resources): List<Int>
fun getSelectedActions(context: Context, sp: SP, aapsLogger: AAPSLogger): List<Action> fun getSelectedActions(): List<Action>
fun getValidFor(sp: SP): Long? fun getValidFor(): Long?
} }
open class Action( open class Action(
@ -107,11 +106,11 @@ abstract class TileBase : TileService() {
private fun getSelectedActions(): List<Action> { private fun getSelectedActions(): List<Action> {
// TODO check why thi scan not be don in scope of the coroutine // 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? { private fun validFor(): Long? {
return source.getValidFor(sp) return source.getValidFor()
} }
@RequiresApi(Build.VERSION_CODES.N) @RequiresApi(Build.VERSION_CODES.N)