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 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
}

View file

@ -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_"

View file

@ -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
}

View file

@ -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<Action> {
override fun getSelectedActions(): List<Action> {
val quickList = mutableListOf<Action>()
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

View file

@ -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
}

View file

@ -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<StaticAction>
abstract val preferencePrefix: String
abstract fun getDefaultConfig(): Map<String, String>
override fun getSelectedActions(context: Context, sp: SP, aapsLogger: AAPSLogger): List<Action> {
val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context)
setDefaultSettings(sharedPrefs)
override fun getSelectedActions(): List<Action> {
setDefaultSettings()
val actionList: MutableList<Action> = 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()
}
}
}

View file

@ -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_"

View file

@ -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
}

View file

@ -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<Int>
fun getSelectedActions(context: Context, sp: SP, aapsLogger: AAPSLogger): List<Action>
fun getValidFor(sp: SP): Long?
fun getSelectedActions(): List<Action>
fun getValidFor(): Long?
}
open class Action(
@ -107,11 +106,11 @@ abstract class TileBase : TileService() {
private fun getSelectedActions(): List<Action> {
// 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)