Wear: tile code cleanup

This commit is contained in:
Andries Smit 2022-01-12 12:22:53 +01:00
parent 1a514f1ca2
commit 22474e5928
6 changed files with 44 additions and 53 deletions

View file

@ -13,55 +13,45 @@ object ActionSource : TileSource {
return listOf( return listOf(
Action( Action(
id = 0, id = 0,
settingName = "wizzard", settingName = "wizard",
nameRes = R.string.menu_wizard_short, nameRes = R.string.menu_wizard_short,
iconRes = R.drawable.ic_calculator_green, iconRes = R.drawable.ic_calculator_green,
activityClass = WizardActivity::class.java.getName(), activityClass = WizardActivity::class.java.name,
background = false,
actionString = "",
), ),
Action( Action(
id = 1, id = 1,
settingName = "treatment", settingName = "treatment",
nameRes = R.string.menu_treatment_short, nameRes = R.string.menu_treatment_short,
iconRes = R.drawable.ic_bolus_carbs, iconRes = R.drawable.ic_bolus_carbs,
activityClass = TreatmentActivity::class.java.getName(), activityClass = TreatmentActivity::class.java.name,
background = false,
actionString = "",
), ),
Action( Action(
id = 2, id = 2,
settingName = "bolus", settingName = "bolus",
nameRes = R.string.action_insulin, nameRes = R.string.action_insulin,
iconRes = R.drawable.ic_bolus, iconRes = R.drawable.ic_bolus,
activityClass = BolusActivity::class.java.getName(), activityClass = BolusActivity::class.java.name,
background = false,
actionString = "",
), ),
Action( Action(
id = 3, id = 3,
settingName = "carbs", settingName = "carbs",
nameRes = R.string.action_carbs, nameRes = R.string.action_carbs,
iconRes = R.drawable.ic_carbs_orange, iconRes = R.drawable.ic_carbs_orange,
activityClass = ECarbActivity::class.java.getName(), activityClass = ECarbActivity::class.java.name,
background = false,
actionString = "",
), ),
Action( Action(
id = 4, id = 4,
settingName = "temp_target", settingName = "temp_target",
nameRes = R.string.menu_tempt, nameRes = R.string.menu_tempt,
iconRes = R.drawable.ic_temptarget_flat, iconRes = R.drawable.ic_temptarget_flat,
activityClass = TempTargetActivity::class.java.getName(), activityClass = TempTargetActivity::class.java.name,
background = false,
actionString = "",
) )
) )
} }
override fun getDefaultConfig(): Map<String, String> { override fun getDefaultConfig(): Map<String, String> {
return mapOf( return mapOf(
"tile_action_1" to "wizzard", "tile_action_1" to "wizard",
"tile_action_2" to "treatment", "tile_action_2" to "treatment",
"tile_action_3" to "carbs", "tile_action_3" to "carbs",
"tile_action_4" to "temp_target" "tile_action_4" to "temp_target"

View file

@ -6,4 +6,5 @@ class ActionsTileService : TileBase() {
override val resourceVersion = "1" override val resourceVersion = "1"
override val idIconActionPrefix = "ic_action_" override val idIconActionPrefix = "ic_action_"
override val source = ActionSource override val source = ActionSource
} }

View file

@ -12,7 +12,7 @@ object TempTargetSource : TileSource {
settingName = "activity", settingName = "activity",
nameRes = R.string.temp_target_activity, nameRes = R.string.temp_target_activity,
iconRes = R.drawable.ic_target_activity, iconRes = R.drawable.ic_target_activity,
activityClass = TempTargetActivity::class.java.getName(), activityClass = TempTargetActivity::class.java.name,
background = true, background = true,
// actionString = "temptarget false 90 8.0 8.0", // actionString = "temptarget false 90 8.0 8.0",
actionString = "temptarget preset activity", actionString = "temptarget preset activity",
@ -22,7 +22,7 @@ object TempTargetSource : TileSource {
settingName = "eating_soon", settingName = "eating_soon",
nameRes = R.string.temp_target_eating_soon, nameRes = R.string.temp_target_eating_soon,
iconRes = R.drawable.ic_target_eatingsoon, iconRes = R.drawable.ic_target_eatingsoon,
activityClass = TempTargetActivity::class.java.getName(), activityClass = TempTargetActivity::class.java.name,
background = true, background = true,
// actionString = "temptarget false 45 4.5 4.5", // actionString = "temptarget false 45 4.5 4.5",
actionString = "temptarget preset eating", actionString = "temptarget preset eating",
@ -32,7 +32,7 @@ object TempTargetSource : TileSource {
settingName = "hypo", settingName = "hypo",
nameRes = R.string.temp_target_hypo, nameRes = R.string.temp_target_hypo,
iconRes = R.drawable.ic_target_hypo, iconRes = R.drawable.ic_target_hypo,
activityClass = TempTargetActivity::class.java.getName(), activityClass = TempTargetActivity::class.java.name,
background = true, background = true,
// actionString = "temptarget false 45 7.0 7.0", // actionString = "temptarget false 45 7.0 7.0",
actionString = "temptarget preset hypo", actionString = "temptarget preset hypo",
@ -42,18 +42,16 @@ object TempTargetSource : TileSource {
settingName = "manual", settingName = "manual",
nameRes = R.string.temp_target_manual, nameRes = R.string.temp_target_manual,
iconRes = R.drawable.ic_target_manual, iconRes = R.drawable.ic_target_manual,
activityClass = TempTargetActivity::class.java.getName(), activityClass = TempTargetActivity::class.java.name,
actionString = "",
background = false,
), ),
Action( Action(
id = 4, id = 4,
settingName = "cancel", settingName = "cancel",
nameRes = R.string.generic_cancel, nameRes = R.string.generic_cancel,
iconRes = R.drawable.ic_target_cancel, iconRes = R.drawable.ic_target_cancel,
activityClass = TempTargetActivity::class.java.getName(), activityClass = TempTargetActivity::class.java.name,
actionString = "temptarget cancel", actionString = "temptarget cancel",
background = false, background = true,
) )
) )
} }

View file

@ -58,16 +58,17 @@ data class Action(
@StringRes val nameRes: Int, @StringRes val nameRes: Int,
val activityClass: String, val activityClass: String,
@DrawableRes val iconRes: Int, @DrawableRes val iconRes: Int,
val background: Boolean?, val background: Boolean = false,
val actionString: String?, val actionString: String? = null,
) )
abstract open class TileBase : TileService() { abstract class TileBase : TileService() {
open val preferencePrefix = "tile_action_"
open val resourceVersion = "1" open val resourceVersion = "1"
open val idIconActionPrefix = "ic_action_" open val idIconActionPrefix = "ic_action_"
open val source: TileSource = ActionSource
abstract val preferencePrefix: String
abstract val source: TileSource
private val serviceJob = Job() private val serviceJob = Job()
private val serviceScope = CoroutineScope(Dispatchers.IO + serviceJob) private val serviceScope = CoroutineScope(Dispatchers.IO + serviceJob)
@ -114,18 +115,6 @@ abstract open class TileBase : TileService() {
.build() .build()
} }
private fun addRowSingle(action1: Action, deviceParameters: DeviceParameters): LayoutElement =
Row.Builder()
.addContent(action(action1, deviceParameters))
.build()
private fun addRowDouble(action1: Action, action2: Action, deviceParameters: DeviceParameters): LayoutElement =
Row.Builder()
.addContent(action(action1, deviceParameters))
.addContent(Spacer.Builder().setWidth(SPACING_ACTIONS).build())
.addContent(action(action2, deviceParameters))
.build()
private fun layout(enabled: Boolean, actions: List<Action>, deviceParameters: DeviceParameters): LayoutElement { private fun layout(enabled: Boolean, actions: List<Action>, deviceParameters: DeviceParameters): LayoutElement {
if (!enabled) { if (!enabled) {
return Text.Builder() return Text.Builder()
@ -154,18 +143,31 @@ abstract open class TileBase : TileService() {
.build() .build()
} }
private fun addRowSingle(action1: Action, deviceParameters: DeviceParameters): LayoutElement =
Row.Builder()
.addContent(action(action1, deviceParameters))
.build()
private fun addRowDouble(action1: Action, action2: Action, deviceParameters: DeviceParameters): LayoutElement =
Row.Builder()
.addContent(action(action1, deviceParameters))
.addContent(Spacer.Builder().setWidth(SPACING_ACTIONS).build())
.addContent(action(action2, deviceParameters))
.build()
private fun doAction(action: Action): ActionBuilders.Action { private fun doAction(action: Action): ActionBuilders.Action {
val ab = ActionBuilders.AndroidActivity.Builder() val inBackground = ActionBuilders.AndroidBooleanExtra.Builder().setValue(action.background).build()
val builder = ActionBuilders.AndroidActivity.Builder()
.setClassName(action.activityClass) .setClassName(action.activityClass)
.setPackageName(this.packageName) .setPackageName(this.packageName)
.addKeyToExtraMapping("inBackground", inBackground)
if (action.actionString != null) { if (action.actionString != null) {
ab.addKeyToExtraMapping("actionString", ActionBuilders.AndroidStringExtra.Builder().setValue(action.actionString).build()) val actionString = ActionBuilders.AndroidStringExtra.Builder().setValue(action.actionString).build()
} builder.addKeyToExtraMapping("actionString", actionString)
if (action.background != null) {
ab.addKeyToExtraMapping("inBackground", ActionBuilders.AndroidBooleanExtra.Builder().setValue(action.background).build())
} }
return ActionBuilders.LaunchAction.Builder() return ActionBuilders.LaunchAction.Builder()
.setAndroidActivity(ab.build()) .setAndroidActivity(builder.build())
.build() .build()
} }
@ -245,18 +247,18 @@ abstract open class TileBase : TileService() {
} }
private fun getActionFromPreference(sharedPrefs: SharedPreferences, index: Int): Action? { private fun getActionFromPreference(sharedPrefs: SharedPreferences, index: Int): Action? {
val actionPref = sharedPrefs?.getString(preferencePrefix + index, "none") val actionPref = sharedPrefs.getString(preferencePrefix + index, "none")
return source.getActions().find { action -> action.settingName == actionPref } return source.getActions().find { action -> action.settingName == actionPref }
} }
fun setDefaultSettings(sharedPrefs: SharedPreferences) { private fun setDefaultSettings(sharedPrefs: SharedPreferences) {
val defaults = source.getDefaultConfig() val defaults = source.getDefaultConfig()
val firstKey = defaults.firstNotNullOf { settings -> settings.key } val firstKey = defaults.firstNotNullOf { settings -> settings.key }
if (!sharedPrefs.contains(firstKey)) { if (!sharedPrefs.contains(firstKey)) {
Log.i(TAG, "setDefaultSettings: set defaults") Log.i(TAG, "setDefaultSettings: set defaults")
val editor = sharedPrefs.edit() val editor = sharedPrefs.edit()
for ((key, value) in defaults) { for ((key, value) in defaults) {
println("$key = $value") // println("$key = $value")
editor.putString(key, value) editor.putString(key, value)
} }
editor.apply() editor.apply()

View file

@ -89,7 +89,7 @@
</string-array> </string-array>
<string-array name="tile_action_values"> <string-array name="tile_action_values">
<item>wizzard</item> <item>wizard</item>
<item>treatment</item> <item>treatment</item>
<item>bolus</item> <item>bolus</item>
<item>carbs</item> <item>carbs</item>

View file

@ -5,7 +5,7 @@
<ListPreference <ListPreference
android:key="tile_action_1" android:key="tile_action_1"
android:title="Action 1" android:title="Action 1"
android:defaultValue="wizzard" android:defaultValue="wizard"
android:entries="@array/tile_action_names" android:entries="@array/tile_action_names"
android:entryValues="@array/tile_action_values"/> android:entryValues="@array/tile_action_values"/>