From fb9991ad4b7769c5c0927e02b5a0be4a6a5a19fb Mon Sep 17 00:00:00 2001 From: Philoul Date: Tue, 22 Aug 2023 00:29:00 +0200 Subject: [PATCH] Wear cwf put all hardcoded keys within enum --- .../rx/weardata/CustomWatchfaceFormat.kt | 128 +++++++- .../androidaps/watchfaces/CustomWatchface.kt | 274 +++++++++--------- 2 files changed, 248 insertions(+), 154 deletions(-) diff --git a/app-wear-shared/shared/src/main/java/info/nightscout/rx/weardata/CustomWatchfaceFormat.kt b/app-wear-shared/shared/src/main/java/info/nightscout/rx/weardata/CustomWatchfaceFormat.kt index 39070179e7..bd37791053 100644 --- a/app-wear-shared/shared/src/main/java/info/nightscout/rx/weardata/CustomWatchfaceFormat.kt +++ b/app-wear-shared/shared/src/main/java/info/nightscout/rx/weardata/CustomWatchfaceFormat.kt @@ -18,33 +18,38 @@ import java.util.zip.ZipInputStream import java.util.zip.ZipOutputStream val CUSTOM_VERSION = "0.10" + enum class CustomWatchfaceDrawableDataKey(val key: String, @DrawableRes val icon: Int?, val fileName: String) { UNKNOWN("unknown", null, "Unknown"), CUSTOM_WATCHFACE("customWatchface", R.drawable.watchface_custom, "CustomWatchface"), - BACKGROUND("background", R.drawable.background, "Background"), - BACKGROUND_HIGH("background", R.drawable.background, "BackgroundHigh"), - BACKGROUND_LOW("background", R.drawable.background, "BackgroundLow"), - COVERCHART("cover_chart", null, "CoverChart"), - COVERPLATE("cover_plate", R.drawable.simplified_dial, "CoverPlate"), - HOURHAND("hour_hand", R.drawable.hour_hand, "HourHand"), - MINUTEHAND("minute_hand", R.drawable.minute_hand, "MinuteHand"), - SECONDHAND("second_hand", R.drawable.second_hand, "SecondHand"); + BACKGROUND(ViewKeys.BACKGROUND.key, R.drawable.background, "Background"), + BACKGROUND_HIGH(ViewKeys.BACKGROUND.key, R.drawable.background, "BackgroundHigh"), + BACKGROUND_LOW(ViewKeys.BACKGROUND.key, R.drawable.background, "BackgroundLow"), + COVER_CHART(ViewKeys.COVER_CHART.key, null, "CoverChart"), + COVER_PLATE(ViewKeys.COVER_PLATE.key, R.drawable.simplified_dial, "CoverPlate"), + HOUR_HAND(ViewKeys.HOUR_HAND.key, R.drawable.hour_hand, "HourHand"), + MINUTE_HAND(ViewKeys.MINUTE_HAND.key, R.drawable.minute_hand, "MinuteHand"), + SECOND_HAND(ViewKeys.SECOND_HAND.key, R.drawable.second_hand, "SecondHand"); companion object { + fun fromKey(key: String): CustomWatchfaceDrawableDataKey = values().firstOrNull { it.key == key } ?: UNKNOWN + fun fromFileName(file: String): CustomWatchfaceDrawableDataKey = values().firstOrNull { it.fileName == file.substringBeforeLast(".") } ?: UNKNOWN } } enum class DrawableFormat(val extension: String) { UNKNOWN(""), + //XML("xml"), //SVG("svg"), JPG("jpg"), PNG("png"); companion object { + fun fromFileName(fileName: String): DrawableFormat = values().firstOrNull { it.extension == fileName.substringAfterLast(".") } ?: UNKNOWN @@ -112,12 +117,109 @@ enum class CustomWatchfaceMetadataKey(val key: String, @StringRes val label: Int CWF_PREF_WATCH_SHOW_AGO("key_show_ago", R.string.metadata_label_watchface_pref, true), CWF_PREF_WATCH_SHOW_BG("key_show_bg", R.string.metadata_label_watchface_pref, true), CWF_PREF_WATCH_SHOW_LOOP_STATUS("key_show_loop_status", R.string.metadata_label_watchface_pref, true); + companion object { + fun fromKey(key: String): CustomWatchfaceMetadataKey? = values().firstOrNull { it.key == key } } } +enum class ViewKeys(val key: String, @StringRes val comment: Int?) { + + BACKGROUND("background", null), + CHART("chart", null), + COVER_CHART("cover_chart", null), + FREETEXT1("freetext1", null), + FREETEXT2("freetext2", null), + FREETEXT3("freetext3", null), + FREETEXT4("freetext4", null), + IOB1("iob1", null), + IOB2("iob2", null), + COB1("cob1", null), + COB2("cob2", null), + DELTA("delta", null), + AVG_DELTA("avg_delta", null), + UPLOADER_BATTERY("uploader_battery", null), + RIG_BATTERY("rig_battery", null), + BASALRATE("basalRate", null), + BGI("bgi", null), + TIME("time", null), + HOUR("hour", null), + MINUTE("minute", null), + SECOND("second", null), + TIMEPERIOD("timePeriod", null), + DAY_NAME("day_name", null), + DAY("day", null), + MONTH("month", null), + LOOP("loop", null), + DIRECTION("direction", null), + TIMESTAMP("timestamp", null), + SGV("sgv", null), + COVER_PLATE("cover_plate", null), + HOUR_HAND("hour_hand", null), + MINUTE_HAND("minute_hand", null), + SECOND_HAND("second_hand", null) +} + +enum class JsonKeys(val key: String, val viewType: ViewType, @StringRes val comment: Int?) { + METADATA("metadata", ViewType.NONE, null), + ENABLESECOND("enableSecond", ViewType.NONE, null), + HIGHCOLOR("highColor", ViewType.NONE, null), + MIDCOLOR("midColor", ViewType.NONE, null), + LOWCOLOR("lowColor", ViewType.NONE, null), + LOWBATCOLOR("lowBatColor", ViewType.NONE, null), + CARBCOLOR("carbColor", ViewType.NONE, null), + BASALBACKGROUNDCOLOR("basalBackgroundColor", ViewType.NONE, null), + BASALCENTERCOLOR("basalCenterColor", ViewType.NONE, null), + GRIDCOLOR("gridColor", ViewType.NONE, null), + POINTSIZE("pointSize", ViewType.NONE, null), + WIDTH("width", ViewType.ALLVIEWS, null), + HEIGHT("height", ViewType.ALLVIEWS, null), + TOPMARGIN("topmargin", ViewType.ALLVIEWS, null), + LEFTMARGIN("leftmargin", ViewType.ALLVIEWS, null), + ROTATION("rotation", ViewType.TEXTVIEW, null), + VISIBILITY("visibility", ViewType.ALLVIEWS, null), + TEXTSIZE("textsize", ViewType.TEXTVIEW, null), + TEXTVALUE("textvalue", ViewType.TEXTVIEW, null), + GRAVITY("gravity", ViewType.TEXTVIEW, null), + FONT("font", ViewType.TEXTVIEW, null), + FONTSTYLE("fontStyle", ViewType.TEXTVIEW, null), + FONTCOLOR("fontColor", ViewType.TEXTVIEW, null), + COLOR("color", ViewType.IMAGEVIEW, null) +} + +enum class JsonKeyValues(val key: String, val jsonKey: JsonKeys) { + GONE("gone", JsonKeys.VISIBILITY), + VISIBLE("visible", JsonKeys.VISIBILITY), + INVISIBLE("invisible", JsonKeys.VISIBILITY), + CENTER("center", JsonKeys.GRAVITY), + LEFT("left", JsonKeys.GRAVITY), + RIGHT("right", JsonKeys.GRAVITY), + SANS_SERIF("sans_serif", JsonKeys.FONT), + DEFAULT("default", JsonKeys.FONT), + DEFAULT_BOLD("default_bold", JsonKeys.FONT), + MONOSPACE("monospace", JsonKeys.FONT), + SERIF("serif", JsonKeys.FONT), + ROBOTO_CONDENSED_BOLD("roboto_condensed_bold", JsonKeys.FONT), + ROBOTO_CONDENSED_LIGHT("roboto_condensed_light", JsonKeys.FONT), + ROBOTO_CONDENSED_REGULAR("roboto_condensed_regular", JsonKeys.FONT), + ROBOTO_SLAB_LIGHT("roboto_slab_light", JsonKeys.FONT), + NORMAL("normal", JsonKeys.FONTSTYLE), + BOLD("bold", JsonKeys.FONTSTYLE), + BOLD_ITALIC("bold_italic", JsonKeys.FONTSTYLE), + ITALIC("italic", JsonKeys.FONTSTYLE), + BGCOLOR("bgColor", JsonKeys.COLOR), + BGCOLOR1("bgColor", JsonKeys.FONTCOLOR) +} + +enum class ViewType(@StringRes val comment: Int?) { + NONE(null), + TEXTVIEW(null), + IMAGEVIEW(null), + ALLVIEWS(null) +} + class ZipWatchfaceFormat { companion object { @@ -151,10 +253,10 @@ class ZipWatchfaceFormat { metadata[CustomWatchfaceMetadataKey.CWF_FILENAME] = cwfFile.name metadata[CustomWatchfaceMetadataKey.CWF_AUTHORIZATION] = authorization.toString() } else { - val customWatchfaceDrawableDataKey = CustomWatchfaceDrawableDataKey.fromFileName(entryName) + val customWatchfaceDrawableData = CustomWatchfaceDrawableDataKey.fromFileName(entryName) val drawableFormat = DrawableFormat.fromFileName(entryName) - if (customWatchfaceDrawableDataKey != CustomWatchfaceDrawableDataKey.UNKNOWN && drawableFormat != DrawableFormat.UNKNOWN) { - drawableDatas[customWatchfaceDrawableDataKey] = DrawableData(byteArrayOutputStream.toByteArray(), drawableFormat) + if (customWatchfaceDrawableData != CustomWatchfaceDrawableDataKey.UNKNOWN && drawableFormat != DrawableFormat.UNKNOWN) { + drawableDatas[customWatchfaceDrawableData] = DrawableData(byteArrayOutputStream.toByteArray(), drawableFormat) } } zipEntry = zipInputStream.nextEntry @@ -200,8 +302,8 @@ class ZipWatchfaceFormat { fun loadMetadata(contents: JSONObject): CustomWatchfaceMetadataMap { val metadata: CustomWatchfaceMetadataMap = mutableMapOf() - if (contents.has("metadata")) { - val meta = contents.getJSONObject("metadata") + if (contents.has(JsonKeys.METADATA.key)) { + val meta = contents.getJSONObject(JsonKeys.METADATA.key) for (key in meta.keys()) { val metaKey = CustomWatchfaceMetadataKey.fromKey(key) if (metaKey != null) { diff --git a/wear/src/main/java/info/nightscout/androidaps/watchfaces/CustomWatchface.kt b/wear/src/main/java/info/nightscout/androidaps/watchfaces/CustomWatchface.kt index 132fa00d49..e61b3b3194 100644 --- a/wear/src/main/java/info/nightscout/androidaps/watchfaces/CustomWatchface.kt +++ b/wear/src/main/java/info/nightscout/androidaps/watchfaces/CustomWatchface.kt @@ -41,6 +41,9 @@ import info.nightscout.rx.weardata.CustomWatchfaceMetadataMap import info.nightscout.rx.weardata.DrawableData import info.nightscout.rx.weardata.DrawableFormat import info.nightscout.rx.weardata.EventData +import info.nightscout.rx.weardata.JsonKeyValues +import info.nightscout.rx.weardata.JsonKeys.* +import info.nightscout.rx.weardata.ViewKeys import info.nightscout.rx.weardata.ZipWatchfaceFormat import info.nightscout.shared.extensions.toVisibility import info.nightscout.shared.extensions.toVisibilityKeepSpace @@ -86,7 +89,7 @@ class CustomWatchface : BaseWatchFace() { @SuppressLint("UseCompatLoadingForDrawables") override fun setDataFields() { super.setDataFields() - binding.direction2.setImageDrawable(this.resources.getDrawable(TrendArrow.icon(singleBg.slopeArrow))) + binding.direction2.setImageDrawable(this.resources.getDrawable(TrendArrowMap.icon(singleBg.slopeArrow))) // rotate the second hand. binding.secondHand.rotation = TimeOfDay().secondOfMinute * 6f // rotate the minute hand. @@ -145,17 +148,17 @@ class CustomWatchface : BaseWatchFace() { try { val json = JSONObject(it.customWatchfaceData.json) val drawableDataMap = it.customWatchfaceData.drawableDatas - enableSecond = (if (json.has("enableSecond")) json.getBoolean("enableSecond") else false) && sp.getBoolean(R.string.key_show_seconds, true) + enableSecond = (if (json.has(ENABLESECOND.key)) json.getBoolean(ENABLESECOND.key) else false) && sp.getBoolean(R.string.key_show_seconds, true) - highColor = if (json.has("highColor")) Color.parseColor(json.getString("highColor")) else ContextCompat.getColor(this, R.color.dark_highColor) - midColor = if (json.has("midColor")) Color.parseColor(json.getString("midColor")) else ContextCompat.getColor(this, R.color.inrange) - lowColor = if (json.has("lowColor")) Color.parseColor(json.getString("lowColor")) else ContextCompat.getColor(this, R.color.low) - lowBatColor = if (json.has("lowBatColor")) Color.parseColor(json.getString("lowBatColor")) else ContextCompat.getColor(this, R.color.dark_uploaderBatteryEmpty) - carbColor = if (json.has("carbColor")) Color.parseColor(json.getString("carbColor")) else ContextCompat.getColor(this, R.color.carbs) - basalBackgroundColor = if (json.has("basalBackgroundColor")) Color.parseColor(json.getString("basalBackgroundColor")) else ContextCompat.getColor(this, R.color.basal_dark) - basalCenterColor = if (json.has("basalCenterColor")) Color.parseColor(json.getString("basalCenterColor")) else ContextCompat.getColor(this, R.color.basal_light) - gridColor = if (json.has("gridColor")) Color.parseColor(json.getString("gridColor")) else Color.WHITE - pointSize = if (json.has("pointSize")) json.getInt("pointSize") else 2 + highColor = if (json.has(HIGHCOLOR.key)) Color.parseColor(json.getString(HIGHCOLOR.key)) else ContextCompat.getColor(this, R.color.dark_highColor) + midColor = if (json.has(MIDCOLOR.key)) Color.parseColor(json.getString(MIDCOLOR.key)) else ContextCompat.getColor(this, R.color.inrange) + lowColor = if (json.has(LOWCOLOR.key)) Color.parseColor(json.getString(LOWCOLOR.key)) else ContextCompat.getColor(this, R.color.low) + lowBatColor = if (json.has(LOWBATCOLOR.key)) Color.parseColor(json.getString(LOWBATCOLOR.key)) else ContextCompat.getColor(this, R.color.dark_uploaderBatteryEmpty) + carbColor = if (json.has(CARBCOLOR.key)) Color.parseColor(json.getString(CARBCOLOR.key)) else ContextCompat.getColor(this, R.color.carbs) + basalBackgroundColor = if (json.has(BASALBACKGROUNDCOLOR.key)) Color.parseColor(json.getString(BASALBACKGROUNDCOLOR.key)) else ContextCompat.getColor(this, R.color.basal_dark) + basalCenterColor = if (json.has(BASALCENTERCOLOR.key)) Color.parseColor(json.getString(BASALCENTERCOLOR.key)) else ContextCompat.getColor(this, R.color.basal_light) + gridColor = if (json.has(GRIDCOLOR.key)) Color.parseColor(json.getString(GRIDCOLOR.key)) else Color.WHITE + pointSize = if (json.has(POINTSIZE.key)) json.getInt(POINTSIZE.key) else 2 bgColor = when (singleBg.sgvLevel) { 1L -> highColor 0L -> midColor @@ -170,30 +173,30 @@ class CustomWatchface : BaseWatchFace() { } binding.mainLayout.forEach { view -> - CustomViews.fromId(view.id)?.let { id -> + ViewMap.fromId(view.id)?.let { id -> if (json.has(id.key)) { val viewJson = json.getJSONObject(id.key) val wrapContent = LayoutParams.WRAP_CONTENT - val width = if (viewJson.has("width")) (viewJson.getInt("width") * zoomFactor).toInt() else wrapContent - val height = if (viewJson.has("height")) (viewJson.getInt("height") * zoomFactor).toInt() else wrapContent + val width = if (viewJson.has(WIDTH.key)) (viewJson.getInt(WIDTH.key) * zoomFactor).toInt() else wrapContent + val height = if (viewJson.has(HEIGHT.key)) (viewJson.getInt(HEIGHT.key) * zoomFactor).toInt() else wrapContent val params = FrameLayout.LayoutParams(width, height) - params.topMargin = if (viewJson.has("topmargin")) (viewJson.getInt("topmargin") * zoomFactor).toInt() else 0 - params.leftMargin = if (viewJson.has("leftmargin")) (viewJson.getInt("leftmargin") * zoomFactor).toInt() else 0 + params.topMargin = if (viewJson.has(TOPMARGIN.key)) (viewJson.getInt(TOPMARGIN.key) * zoomFactor).toInt() else 0 + params.leftMargin = if (viewJson.has(LEFTMARGIN.key)) (viewJson.getInt(LEFTMARGIN.key) * zoomFactor).toInt() else 0 view.layoutParams = params - view.visibility = if (viewJson.has("visibility")) setVisibility(viewJson.getString("visibility"), id.visibility(sp)) else View.GONE + view.visibility = if (viewJson.has(VISIBILITY.key)) setVisibility(viewJson.getString(VISIBILITY.key), id.visibility(sp)) else View.GONE if (view is TextView) { - view.rotation = if (viewJson.has("rotation")) viewJson.getInt("rotation").toFloat() else 0F - view.setTextSize(TypedValue.COMPLEX_UNIT_PX, ((if (viewJson.has("textsize")) viewJson.getInt("textsize") else 22) * zoomFactor).toFloat()) - view.gravity = GravityMap.gravity(if (viewJson.has("gravity")) viewJson.getString("gravity") else GravityMap.CENTER.key) + view.rotation = if (viewJson.has(ROTATION.key)) viewJson.getInt(ROTATION.key).toFloat() else 0F + view.setTextSize(TypedValue.COMPLEX_UNIT_PX, ((if (viewJson.has(TEXTSIZE.key)) viewJson.getInt(TEXTSIZE.key) else 22) * zoomFactor).toFloat()) + view.gravity = GravityMap.gravity(if (viewJson.has(GRAVITY.key)) viewJson.getString(GRAVITY.key) else GravityMap.CENTER.key) view.setTypeface( - FontMap.font(if (viewJson.has("font")) viewJson.getString("font") else FontMap.DEFAULT.key), - StyleMap.style(if (viewJson.has("fontStyle")) viewJson.getString("fontStyle") else StyleMap.NORMAL.key) + FontMap.font(if (viewJson.has(FONT.key)) viewJson.getString(FONT.key) else FontMap.DEFAULT.key), + StyleMap.style(if (viewJson.has(FONTSTYLE.key)) viewJson.getString(FONTSTYLE.key) else StyleMap.NORMAL.key) ) - if (viewJson.has("fontColor")) - view.setTextColor(getColor(viewJson.getString("fontColor"))) + if (viewJson.has(FONTCOLOR.key)) + view.setTextColor(getColor(viewJson.getString(FONTCOLOR.key))) - if (viewJson.has("textvalue")) - view.text = viewJson.getString("textvalue") + if (viewJson.has(TEXTVALUE.key)) + view.text = viewJson.getString(TEXTVALUE.key) } if (view is ImageView) { @@ -203,15 +206,15 @@ class CustomWatchface : BaseWatchFace() { else drawableDataMap[CustomWatchfaceDrawableDataKey.fromKey(id.key)]?.toDrawable(resources) drawable?.let { - if (viewJson.has("color")) - it.colorFilter = changeDrawableColor(getColor(viewJson.getString("color"))) + if (viewJson.has(COLOR.key)) + it.colorFilter = changeDrawableColor(getColor(viewJson.getString(COLOR.key))) else it.clearColorFilter() view.setImageDrawable(it) } ?: apply { view.setImageDrawable(CustomWatchfaceDrawableDataKey.fromKey(id.key).icon?.let { context.getDrawable(it) }) - if (viewJson.has("color")) - view.setColorFilter(getColor(viewJson.getString("color"))) + if (viewJson.has(COLOR.key)) + view.setColorFilter(getColor(viewJson.getString(COLOR.key))) else view.clearColorFilter() } @@ -226,6 +229,7 @@ class CustomWatchface : BaseWatchFace() { } binding.background.visibility = View.VISIBLE updateSecondVisibility() + setSecond() // Update second visibility for time view } catch (e: Exception) { aapsLogger.debug(LTag.WEAR, "Crash during Custom watch load") persistence.store(defaultWatchface(), false) // relaod correct values to avoid crash of watchface @@ -254,58 +258,47 @@ class CustomWatchface : BaseWatchFace() { .put(CustomWatchfaceMetadataKey.CWF_VERSION.key, CUSTOM_VERSION) .put(CustomWatchfaceMetadataKey.CWF_COMMENT.key, getString(info.nightscout.shared.R.string.default_custom_watchface_comment)) val json = JSONObject() - .put("metadata", metadata) - .put("highColor", String.format("#%06X", 0xFFFFFF and highColor)) - .put("midColor", String.format("#%06X", 0xFFFFFF and midColor)) - .put("lowColor", String.format("#%06X", 0xFFFFFF and lowColor)) - .put("lowBatColor", String.format("#%06X", 0xFFFFFF and lowBatColor)) - .put("carbColor", String.format("#%06X", 0xFFFFFF and carbColor)) - .put("basalBackgroundColor", String.format("#%06X", 0xFFFFFF and basalBackgroundColor)) - .put("basalCenterColor", String.format("#%06X", 0xFFFFFF and basalCenterColor)) - .put("gridColor", String.format("#%06X", 0xFFFFFF and Color.WHITE)) - .put("pointSize", 2) - .put("enableSecond", true) + .put(METADATA.key, metadata) + .put(HIGHCOLOR.key, String.format("#%06X", 0xFFFFFF and highColor)) + .put(MIDCOLOR.key, String.format("#%06X", 0xFFFFFF and midColor)) + .put(LOWCOLOR.key, String.format("#%06X", 0xFFFFFF and lowColor)) + .put(LOWBATCOLOR.key, String.format("#%06X", 0xFFFFFF and lowBatColor)) + .put(CARBCOLOR.key, String.format("#%06X", 0xFFFFFF and carbColor)) + .put(BASALBACKGROUNDCOLOR.key, String.format("#%06X", 0xFFFFFF and basalBackgroundColor)) + .put(BASALCENTERCOLOR.key, String.format("#%06X", 0xFFFFFF and basalCenterColor)) + .put(GRIDCOLOR.key, String.format("#%06X", 0xFFFFFF and Color.WHITE)) + .put(POINTSIZE.key, 2) + .put(ENABLESECOND.key, true) binding.mainLayout.forEach { view -> val params = view.layoutParams as FrameLayout.LayoutParams - CustomViews.fromId(view.id)?.let { + ViewMap.fromId(view.id)?.let { if (view is TextView) { json.put( it.key, JSONObject() - .put("width", (params.width / zoomFactor).toInt()) - .put("height", (params.height / zoomFactor).toInt()) - .put("topmargin", (params.topMargin / zoomFactor).toInt()) - .put("leftmargin", (params.leftMargin / zoomFactor).toInt()) - .put("rotation", view.rotation.toInt()) - .put("visibility", getVisibility(view.visibility)) - .put("textsize", view.textSize.toInt()) - .put("gravity", GravityMap.key(view.gravity)) - .put("font", FontMap.key()) - .put("fontStyle", StyleMap.key(view.typeface.style)) - .put("fontColor", String.format("#%06X", 0xFFFFFF and view.currentTextColor)) + .put(WIDTH.key, (params.width / zoomFactor).toInt()) + .put(HEIGHT.key, (params.height / zoomFactor).toInt()) + .put(TOPMARGIN.key, (params.topMargin / zoomFactor).toInt()) + .put(LEFTMARGIN.key, (params.leftMargin / zoomFactor).toInt()) + .put(ROTATION.key, view.rotation.toInt()) + .put(VISIBILITY.key, getVisibility(view.visibility)) + .put(TEXTSIZE.key, view.textSize.toInt()) + .put(GRAVITY.key, GravityMap.key(view.gravity)) + .put(FONT.key, FontMap.key()) + .put(FONTSTYLE.key, StyleMap.key(view.typeface.style)) + .put(FONTCOLOR.key, String.format("#%06X", 0xFFFFFF and view.currentTextColor)) ) } - if (view is ImageView) { + if (view is ImageView || view is lecho.lib.hellocharts.view.LineChartView) { json.put( it.key, JSONObject() - .put("width", (params.width / zoomFactor).toInt()) - .put("height", (params.height / zoomFactor).toInt()) - .put("topmargin", (params.topMargin / zoomFactor).toInt()) - .put("leftmargin", (params.leftMargin / zoomFactor).toInt()) - .put("visibility", getVisibility(view.visibility)) - ) - } - if (view is lecho.lib.hellocharts.view.LineChartView) { - json.put( - it.key, - JSONObject() - .put("width", (params.width / zoomFactor).toInt()) - .put("height", (params.height / zoomFactor).toInt()) - .put("topmargin", (params.topMargin / zoomFactor).toInt()) - .put("leftmargin", (params.leftMargin / zoomFactor).toInt()) - .put("visibility", getVisibility(view.visibility)) + .put(WIDTH.key, (params.width / zoomFactor).toInt()) + .put(HEIGHT.key, (params.height / zoomFactor).toInt()) + .put(TOPMARGIN.key, (params.topMargin / zoomFactor).toInt()) + .put(LEFTMARGIN.key, (params.leftMargin / zoomFactor).toInt()) + .put(VISIBILITY.key, getVisibility(view.visibility)) ) } } @@ -331,17 +324,17 @@ class CustomWatchface : BaseWatchFace() { } private fun setVisibility(visibility: String, pref: Boolean = true): Int = when (visibility) { - "visible" -> pref.toVisibility() - "invisible" -> pref.toVisibilityKeepSpace() - "gone" -> View.GONE + JsonKeyValues.VISIBLE.key -> pref.toVisibility() + JsonKeyValues.INVISIBLE.key -> pref.toVisibilityKeepSpace() + JsonKeyValues.GONE.key -> View.GONE else -> View.GONE } private fun getVisibility(visibility: Int): String = when (visibility) { - View.VISIBLE -> "visible" - View.INVISIBLE -> "invisible" - View.GONE -> "gone" - else -> "gone" + View.VISIBLE -> JsonKeyValues.VISIBLE.key + View.INVISIBLE -> JsonKeyValues.INVISIBLE.key + View.GONE -> JsonKeyValues.GONE.key + else -> JsonKeyValues.GONE.key } private fun getResourceByteArray(resourceId: Int): ByteArray? { @@ -377,68 +370,67 @@ class CustomWatchface : BaseWatchFace() { } private fun getColor(color: String): Int = - if (color == "bgColor") + if (color == JsonKeyValues.BGCOLOR.key) bgColor else try { Color.parseColor(color) } catch (e: Exception) { Color.GRAY } - private enum class CustomViews(val key: String, @IdRes val id: Int, @StringRes val pref: Int?) { + private enum class ViewMap(val key: String, @IdRes val id: Int, @StringRes val pref: Int?) { - BACKGROUND(CustomWatchfaceDrawableDataKey.BACKGROUND.key, R.id.background, null), - CHART("chart", R.id.chart, null), - COVER_CHART(CustomWatchfaceDrawableDataKey.COVERCHART.key, R.id.cover_chart, null), - FREETEXT1("freetext1", R.id.freetext1, null), - FREETEXT2("freetext2", R.id.freetext2, null), - FREETEXT3("freetext3", R.id.freetext3, null), - FREETEXT4("freetext4", R.id.freetext4, null), - IOB1("iob1", R.id.iob1, R.string.key_show_iob), - IOB2("iob2", R.id.iob2, R.string.key_show_iob), - COB1("cob1", R.id.cob1, R.string.key_show_cob), - COB2("cob2", R.id.cob2, R.string.key_show_cob), - DELTA("delta", R.id.delta, R.string.key_show_delta), - AVG_DELTA("avg_delta", R.id.avg_delta, R.string.key_show_avg_delta), - UPLOADER_BATTERY("uploader_battery", R.id.uploader_battery, R.string.key_show_uploader_battery), - RIG_BATTERY("rig_battery", R.id.rig_battery, R.string.key_show_rig_battery), - BASALRATE("basalRate", R.id.basalRate, R.string.key_show_temp_basal), - BGI("bgi", R.id.bgi, R.string.key_show_bgi), - TIME("time", R.id.time, null), - HOUR("hour", R.id.hour, null), - MINUTE("minute", R.id.minute, null), - SECOND("second", R.id.second, R.string.key_show_seconds), - TIMEPERIOD("timePeriod", R.id.timePeriod, null), - DAY_NAME("day_name", R.id.day_name, null), - DAY("day", R.id.day, null), - MONTH("month", R.id.month, null), - LOOP("loop", R.id.loop, R.string.key_show_external_status), - DIRECTION("direction", R.id.direction2, R.string.key_show_direction), - TIMESTAMP("timestamp", R.id.timestamp, R.string.key_show_ago), - SGV("sgv", R.id.sgv, R.string.key_show_bg), - COVER_PLATE(CustomWatchfaceDrawableDataKey.COVERPLATE.key, R.id.cover_plate, null), - HOUR_HABD(CustomWatchfaceDrawableDataKey.HOURHAND.key, R.id.hour_hand, null), - MINUTE_HAND(CustomWatchfaceDrawableDataKey.MINUTEHAND.key, R.id.minute_hand, null), - SECOND_HAND(CustomWatchfaceDrawableDataKey.SECONDHAND.key, R.id.second_hand, R.string.key_show_seconds); + BACKGROUND(ViewKeys.BACKGROUND.key, R.id.background, null), + CHART(ViewKeys.CHART.key, R.id.chart, null), + COVER_CHART(ViewKeys.COVER_CHART.key, R.id.cover_chart, null), + FREETEXT1(ViewKeys.FREETEXT1.key, R.id.freetext1, null), + FREETEXT2(ViewKeys.FREETEXT2.key, R.id.freetext2, null), + FREETEXT3(ViewKeys.FREETEXT3.key, R.id.freetext3, null), + FREETEXT4(ViewKeys.FREETEXT4.key, R.id.freetext4, null), + IOB1(ViewKeys.IOB1.key, R.id.iob1, R.string.key_show_iob), + IOB2(ViewKeys.IOB2.key, R.id.iob2, R.string.key_show_iob), + COB1(ViewKeys.COB1.key, R.id.cob1, R.string.key_show_cob), + COB2(ViewKeys.COB2.key, R.id.cob2, R.string.key_show_cob), + DELTA(ViewKeys.DELTA.key, R.id.delta, R.string.key_show_delta), + AVG_DELTA(ViewKeys.AVG_DELTA.key, R.id.avg_delta, R.string.key_show_avg_delta), + UPLOADER_BATTERY(ViewKeys.UPLOADER_BATTERY.key, R.id.uploader_battery, R.string.key_show_uploader_battery), + RIG_BATTERY(ViewKeys.RIG_BATTERY.key, R.id.rig_battery, R.string.key_show_rig_battery), + BASALRATE(ViewKeys.BASALRATE.key, R.id.basalRate, R.string.key_show_temp_basal), + BGI(ViewKeys.BGI.key, R.id.bgi, R.string.key_show_bgi), + TIME(ViewKeys.TIME.key, R.id.time, null), + HOUR(ViewKeys.HOUR.key, R.id.hour, null), + MINUTE(ViewKeys.MINUTE.key, R.id.minute, null), + SECOND(ViewKeys.SECOND.key, R.id.second, R.string.key_show_seconds), + TIMEPERIOD(ViewKeys.TIMEPERIOD.key, R.id.timePeriod, null), + DAY_NAME(ViewKeys.DAY_NAME.key, R.id.day_name, null), + DAY(ViewKeys.DAY.key, R.id.day, null), + MONTH(ViewKeys.MONTH.key, R.id.month, null), + LOOP(ViewKeys.LOOP.key, R.id.loop, R.string.key_show_external_status), + DIRECTION(ViewKeys.DIRECTION.key, R.id.direction2, R.string.key_show_direction), + TIMESTAMP(ViewKeys.TIMESTAMP.key, R.id.timestamp, R.string.key_show_ago), + SGV(ViewKeys.SGV.key, R.id.sgv, R.string.key_show_bg), + COVER_PLATE(ViewKeys.COVER_PLATE.key, R.id.cover_plate, null), + HOUR_HAND(ViewKeys.HOUR_HAND.key, R.id.hour_hand, null), + MINUTE_HAND(ViewKeys.MINUTE_HAND.key, R.id.minute_hand, null), + SECOND_HAND(ViewKeys.SECOND_HAND.key, R.id.second_hand, R.string.key_show_seconds); companion object { - fun fromId(id: Int): CustomViews? = values().firstOrNull { it.id == id } + fun fromId(id: Int): ViewMap? = values().firstOrNull { it.id == id } } fun visibility(sp: SP): Boolean = this.pref?.let { sp.getBoolean(it, true) } ?: true } - private enum class TrendArrow(val text: String, val symbol: String, @DrawableRes val icon: Int) { - NONE("NONE", "??", R.drawable.ic_invalid), - TRIPLE_UP("TripleUp", "X", R.drawable.ic_doubleup), - DOUBLE_UP("DoubleUp", "\u21c8", R.drawable.ic_doubleup), - SINGLE_UP("SingleUp", "\u2191", R.drawable.ic_singleup), - FORTY_FIVE_UP("FortyFiveUp", "\u2197", R.drawable.ic_fortyfiveup), - FLAT("Flat", "\u2192", R.drawable.ic_flat), - FORTY_FIVE_DOWN("FortyFiveDown", "\u2198", R.drawable.ic_fortyfivedown), - SINGLE_DOWN("SingleDown", "\u2193", R.drawable.ic_singledown), - DOUBLE_DOWN("DoubleDown", "\u21ca", R.drawable.ic_doubledown), - TRIPLE_DOWN("TripleDown", "X", R.drawable.ic_doubledown) - ; + private enum class TrendArrowMap(val symbol: String, @DrawableRes val icon: Int) { + NONE("??", R.drawable.ic_invalid), + TRIPLE_UP("X", R.drawable.ic_doubleup), + DOUBLE_UP("\u21c8", R.drawable.ic_doubleup), + SINGLE_UP("\u2191", R.drawable.ic_singleup), + FORTY_FIVE_UP("\u2197", R.drawable.ic_fortyfiveup), + FLAT("\u2192", R.drawable.ic_flat), + FORTY_FIVE_DOWN("\u2198", R.drawable.ic_fortyfivedown), + SINGLE_DOWN("\u2193", R.drawable.ic_singledown), + DOUBLE_DOWN("\u21ca", R.drawable.ic_doubledown), + TRIPLE_DOWN("X", R.drawable.ic_doubledown); companion object { @@ -447,9 +439,9 @@ class CustomWatchface : BaseWatchFace() { } private enum class GravityMap(val key: String, val gravity: Int) { - CENTER("center", Gravity.CENTER), - LEFT("left", Gravity.LEFT), - RIGHT("right", Gravity.RIGHT); + CENTER(JsonKeyValues.CENTER.key, Gravity.CENTER), + LEFT(JsonKeyValues.LEFT.key, Gravity.LEFT), + RIGHT(JsonKeyValues.RIGHT.key, Gravity.RIGHT); companion object { @@ -459,15 +451,15 @@ class CustomWatchface : BaseWatchFace() { } private enum class FontMap(val key: String, var font: Typeface, @FontRes val fontRessources: Int?) { - SANS_SERIF("sans_serif", Typeface.SANS_SERIF, null), - DEFAULT("default", Typeface.DEFAULT, null), - DEFAULT_BOLD("default_bold", Typeface.DEFAULT_BOLD, null), - MONOSPACE("monospace", Typeface.MONOSPACE, null), - SERIF("serif", Typeface.SERIF, null), - ROBOTO_CONDENSED_BOLD("roboto_condensed_bold", Typeface.DEFAULT, R.font.roboto_condensed_bold), - ROBOTO_CONDENSED_LIGHT("roboto_condensed_light", Typeface.DEFAULT, R.font.roboto_condensed_light), - ROBOTO_CONDENSED_REGULAR("roboto_condensed_regular", Typeface.DEFAULT, R.font.roboto_condensed_regular), - ROBOTO_SLAB_LIGHT("roboto_slab_light", Typeface.DEFAULT, R.font.roboto_slab_light); + SANS_SERIF(JsonKeyValues.SANS_SERIF.key, Typeface.SANS_SERIF, null), + DEFAULT(JsonKeyValues.DEFAULT.key, Typeface.DEFAULT, null), + DEFAULT_BOLD(JsonKeyValues.DEFAULT_BOLD.key, Typeface.DEFAULT_BOLD, null), + MONOSPACE(JsonKeyValues.MONOSPACE.key, Typeface.MONOSPACE, null), + SERIF(JsonKeyValues.SERIF.key, Typeface.SERIF, null), + ROBOTO_CONDENSED_BOLD(JsonKeyValues.ROBOTO_CONDENSED_BOLD.key, Typeface.DEFAULT, R.font.roboto_condensed_bold), + ROBOTO_CONDENSED_LIGHT(JsonKeyValues.ROBOTO_CONDENSED_LIGHT.key, Typeface.DEFAULT, R.font.roboto_condensed_light), + ROBOTO_CONDENSED_REGULAR(JsonKeyValues.ROBOTO_CONDENSED_REGULAR.key, Typeface.DEFAULT, R.font.roboto_condensed_regular), + ROBOTO_SLAB_LIGHT(JsonKeyValues.ROBOTO_SLAB_LIGHT.key, Typeface.DEFAULT, R.font.roboto_slab_light); companion object { fun init(context: Context) = values().forEach { it.font = it.fontRessources?.let { font -> ResourcesCompat.getFont(context, font) } ?: it.font } @@ -477,10 +469,10 @@ class CustomWatchface : BaseWatchFace() { } private enum class StyleMap(val key: String, val style: Int) { - NORMAL("normal", Typeface.NORMAL), - BOLD("bold", Typeface.BOLD), - BOLD_ITALIC("bold_italic", Typeface.BOLD_ITALIC), - ITALIC("italic", Typeface.ITALIC); + NORMAL(JsonKeyValues.NORMAL.key, Typeface.NORMAL), + BOLD(JsonKeyValues.BOLD.key, Typeface.BOLD), + BOLD_ITALIC(JsonKeyValues.BOLD_ITALIC.key, Typeface.BOLD_ITALIC), + ITALIC(JsonKeyValues.ITALIC.key, Typeface.ITALIC); companion object {