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

View file

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

View file

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

View file

@ -58,16 +58,17 @@ data class Action(
@StringRes val nameRes: Int,
val activityClass: String,
@DrawableRes val iconRes: Int,
val background: Boolean?,
val actionString: String?,
val background: Boolean = false,
val actionString: String? = null,
)
abstract open class TileBase : TileService() {
abstract class TileBase : TileService() {
open val preferencePrefix = "tile_action_"
open val resourceVersion = "1"
open val idIconActionPrefix = "ic_action_"
open val source: TileSource = ActionSource
abstract val preferencePrefix: String
abstract val source: TileSource
private val serviceJob = Job()
private val serviceScope = CoroutineScope(Dispatchers.IO + serviceJob)
@ -114,18 +115,6 @@ abstract open class TileBase : TileService() {
.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 {
if (!enabled) {
return Text.Builder()
@ -154,18 +143,31 @@ abstract open class TileBase : TileService() {
.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 {
val ab = ActionBuilders.AndroidActivity.Builder()
val inBackground = ActionBuilders.AndroidBooleanExtra.Builder().setValue(action.background).build()
val builder = ActionBuilders.AndroidActivity.Builder()
.setClassName(action.activityClass)
.setPackageName(this.packageName)
.addKeyToExtraMapping("inBackground", inBackground)
if (action.actionString != null) {
ab.addKeyToExtraMapping("actionString", ActionBuilders.AndroidStringExtra.Builder().setValue(action.actionString).build())
}
if (action.background != null) {
ab.addKeyToExtraMapping("inBackground", ActionBuilders.AndroidBooleanExtra.Builder().setValue(action.background).build())
val actionString = ActionBuilders.AndroidStringExtra.Builder().setValue(action.actionString).build()
builder.addKeyToExtraMapping("actionString", actionString)
}
return ActionBuilders.LaunchAction.Builder()
.setAndroidActivity(ab.build())
.setAndroidActivity(builder.build())
.build()
}
@ -245,18 +247,18 @@ abstract open class TileBase : TileService() {
}
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 }
}
fun setDefaultSettings(sharedPrefs: SharedPreferences) {
private fun setDefaultSettings(sharedPrefs: SharedPreferences) {
val defaults = source.getDefaultConfig()
val firstKey = defaults.firstNotNullOf { settings -> settings.key }
if (!sharedPrefs.contains(firstKey)) {
Log.i(TAG, "setDefaultSettings: set defaults")
val editor = sharedPrefs.edit()
for ((key, value) in defaults) {
println("$key = $value")
// println("$key = $value")
editor.putString(key, value)
}
editor.apply()

View file

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

View file

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