Merge pull request #2798 from Philoul/wear/new_custom_watchface
Wear CWF Code improvement
This commit is contained in:
commit
12b0ae9633
1 changed files with 142 additions and 127 deletions
|
@ -53,7 +53,7 @@ import org.joda.time.TimeOfDay
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
@SuppressLint("UseCompatLoadingForDrawables")
|
||||||
class CustomWatchface : BaseWatchFace() {
|
class CustomWatchface : BaseWatchFace() {
|
||||||
|
|
||||||
@Inject lateinit var context: Context
|
@Inject lateinit var context: Context
|
||||||
|
@ -88,7 +88,6 @@ class CustomWatchface : BaseWatchFace() {
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("UseCompatLoadingForDrawables")
|
|
||||||
override fun setDataFields() {
|
override fun setDataFields() {
|
||||||
super.setDataFields()
|
super.setDataFields()
|
||||||
binding.direction2.setImageDrawable(TrendArrowMap.drawable(singleBg.slopeArrow, resources, resDataMap))
|
binding.direction2.setImageDrawable(TrendArrowMap.drawable(singleBg.slopeArrow, resources, resDataMap))
|
||||||
|
@ -141,7 +140,6 @@ class CustomWatchface : BaseWatchFace() {
|
||||||
binding.secondHand.visibility = (binding.secondHand.visibility == View.VISIBLE && showSecond).toVisibility()
|
binding.secondHand.visibility = (binding.secondHand.visibility == View.VISIBLE && showSecond).toVisibility()
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("UseCompatLoadingForDrawables")
|
|
||||||
private fun setWatchfaceStyle() {
|
private fun setWatchfaceStyle() {
|
||||||
val customWatchface = persistence.readCustomWatchface() ?: persistence.readCustomWatchface(true)
|
val customWatchface = persistence.readCustomWatchface() ?: persistence.readCustomWatchface(true)
|
||||||
customWatchface?.let {
|
customWatchface?.let {
|
||||||
|
@ -185,39 +183,15 @@ class CustomWatchface : BaseWatchFace() {
|
||||||
view.visibility = setVisibility(viewJson.optString(VISIBILITY.key, JsonKeyValues.GONE.key), id.visibility(sp))
|
view.visibility = setVisibility(viewJson.optString(VISIBILITY.key, JsonKeyValues.GONE.key), id.visibility(sp))
|
||||||
when (view) {
|
when (view) {
|
||||||
is TextView -> {
|
is TextView -> {
|
||||||
view.rotation = viewJson.optInt(ROTATION.key).toFloat()
|
id.customizeTextView(view, viewJson, resDataMap, zoomFactor, resources, ::getColor)
|
||||||
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, (viewJson.optInt(TEXTSIZE.key, 22) * zoomFactor).toFloat())
|
|
||||||
view.gravity = GravityMap.gravity(viewJson.optString(GRAVITY.key, GravityMap.CENTER.key))
|
|
||||||
view.setTypeface(
|
|
||||||
FontMap.font(viewJson.optString(FONT.key, FontMap.DEFAULT.key)),
|
|
||||||
StyleMap.style(viewJson.optString(FONTSTYLE.key, StyleMap.NORMAL.key))
|
|
||||||
)
|
|
||||||
view.setTextColor(getColor(viewJson.optString(FONTCOLOR.key)))
|
|
||||||
view.isAllCaps = viewJson.optBoolean(ALLCAPS.key)
|
|
||||||
if (viewJson.has(TEXTVALUE.key))
|
|
||||||
view.text = viewJson.optString(TEXTVALUE.key)
|
|
||||||
view.background = resDataMap[viewJson.optString(BACKGROUND.key)]?.toDrawable(resources, width, height)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
is ImageView -> {
|
is ImageView -> {
|
||||||
view.clearColorFilter()
|
id.customizeImageView(view, viewJson, resDataMap, singleBg, resources, ::getColor, ::changeDrawableColor)
|
||||||
id.drawable(resources, resDataMap, singleBg.sgvLevel)?.let {
|
|
||||||
if (viewJson.has(COLOR.key)) // Note only works on bitmap (png or jpg) or xml included into res, not for svg files
|
|
||||||
it.colorFilter = changeDrawableColor(getColor(viewJson.optString(COLOR.key)))
|
|
||||||
else
|
|
||||||
it.clearColorFilter()
|
|
||||||
view.setImageDrawable(it)
|
|
||||||
} ?: apply {
|
|
||||||
view.setImageDrawable(id.defaultDrawable?.let {resources.getDrawable(it)})
|
|
||||||
if (viewJson.has(COLOR.key))
|
|
||||||
view.setColorFilter(getColor(viewJson.optString(COLOR.key)))
|
|
||||||
else
|
|
||||||
view.clearColorFilter()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
} ?:apply {
|
} ?: apply {
|
||||||
view.visibility = View.GONE
|
view.visibility = View.GONE
|
||||||
if (view is TextView) {
|
if (view is TextView) {
|
||||||
view.text = ""
|
view.text = ""
|
||||||
|
@ -394,7 +368,7 @@ class CustomWatchface : BaseWatchFace() {
|
||||||
@StringRes val pref: Int?,
|
@StringRes val pref: Int?,
|
||||||
@IdRes val defaultDrawable: Int?,
|
@IdRes val defaultDrawable: Int?,
|
||||||
val customDrawable: ResFileMap?,
|
val customDrawable: ResFileMap?,
|
||||||
val customHigh:ResFileMap?,
|
val customHigh: ResFileMap?,
|
||||||
val customLow: ResFileMap?
|
val customLow: ResFileMap?
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
@ -491,16 +465,55 @@ class CustomWatchface : BaseWatchFace() {
|
||||||
|
|
||||||
fun drawable(resources: Resources, drawableDataMap: CwfResDataMap, sgvLevel: Long): Drawable? = customDrawable?.let { cd ->
|
fun drawable(resources: Resources, drawableDataMap: CwfResDataMap, sgvLevel: Long): Drawable? = customDrawable?.let { cd ->
|
||||||
when (sgvLevel) {
|
when (sgvLevel) {
|
||||||
1L -> { customHigh?.let {drawableDataMap[customHigh.fileName]}?.toDrawable(resources) ?: drawableDataMap[cd.fileName]?.toDrawable(resources) }
|
1L -> { customHigh?.let { drawableDataMap[customHigh.fileName] }?.toDrawable(resources) ?: drawableDataMap[cd.fileName]?.toDrawable(resources) }
|
||||||
0L -> { drawableDataMap[cd.fileName]?.toDrawable(resources) }
|
0L -> { drawableDataMap[cd.fileName]?.toDrawable(resources) }
|
||||||
-1L -> { customLow?.let {drawableDataMap[customLow.fileName]}?.toDrawable(resources) ?: drawableDataMap[cd.fileName]?.toDrawable(resources) }
|
-1L -> { customLow?.let {drawableDataMap[customLow.fileName] }?.toDrawable(resources) ?: drawableDataMap[cd.fileName]?.toDrawable(resources) }
|
||||||
else -> drawableDataMap[cd.fileName]?.toDrawable(resources)
|
else -> drawableDataMap[cd.fileName]?.toDrawable(resources)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private enum class TrendArrowMap(val symbol: String, @DrawableRes val icon: Int,val customDrawable: ResFileMap?) {
|
fun customizeTextView(view: TextView, viewJson: JSONObject, resDataMap: CwfResDataMap, zoomFactor: Double, resources: Resources, getColor: (String) -> Int) {
|
||||||
|
view.rotation = viewJson.optInt(ROTATION.key).toFloat()
|
||||||
|
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, (viewJson.optInt(TEXTSIZE.key, 22) * zoomFactor).toFloat())
|
||||||
|
view.gravity = GravityMap.gravity(viewJson.optString(GRAVITY.key, GravityMap.CENTER.key))
|
||||||
|
view.setTypeface(
|
||||||
|
FontMap.font(viewJson.optString(FONT.key, FontMap.DEFAULT.key)),
|
||||||
|
StyleMap.style(viewJson.optString(FONTSTYLE.key, StyleMap.NORMAL.key))
|
||||||
|
)
|
||||||
|
view.setTextColor(getColor(viewJson.optString(FONTCOLOR.key)))
|
||||||
|
view.isAllCaps = viewJson.optBoolean(ALLCAPS.key)
|
||||||
|
if (viewJson.has(TEXTVALUE.key))
|
||||||
|
view.text = viewJson.optString(TEXTVALUE.key)
|
||||||
|
view.background = resDataMap[viewJson.optString(BACKGROUND.key)]?.toDrawable(resources, view.width, view.height)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun customizeImageView(
|
||||||
|
view: ImageView,
|
||||||
|
viewJson: JSONObject,
|
||||||
|
resDataMap: CwfResDataMap,
|
||||||
|
singleBg: EventData.SingleBg,
|
||||||
|
resources: Resources,
|
||||||
|
getColor: (String) -> Int,
|
||||||
|
changeDrawableColor: (Int) -> ColorFilter
|
||||||
|
) {
|
||||||
|
view.clearColorFilter()
|
||||||
|
drawable(resources, resDataMap, singleBg.sgvLevel)?.let {
|
||||||
|
if (viewJson.has(COLOR.key)) // Note only works on bitmap (png or jpg) or xml included into res, not for svg files
|
||||||
|
it.colorFilter = changeDrawableColor(getColor(viewJson.optString(COLOR.key)))
|
||||||
|
else
|
||||||
|
it.clearColorFilter()
|
||||||
|
view.setImageDrawable(it)
|
||||||
|
} ?: apply {
|
||||||
|
view.setImageDrawable(defaultDrawable?.let { resources.getDrawable(it) })
|
||||||
|
if (viewJson.has(COLOR.key))
|
||||||
|
view.setColorFilter(getColor(viewJson.optString(COLOR.key)))
|
||||||
|
else
|
||||||
|
view.clearColorFilter()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum class TrendArrowMap(val symbol: String, @DrawableRes val icon: Int, val customDrawable: ResFileMap?) {
|
||||||
NONE("??", R.drawable.ic_invalid, ResFileMap.ARROW_NONE),
|
NONE("??", R.drawable.ic_invalid, ResFileMap.ARROW_NONE),
|
||||||
TRIPLE_UP("X", R.drawable.ic_doubleup, ResFileMap.ARROW_DOUBLE_UP),
|
TRIPLE_UP("X", R.drawable.ic_doubleup, ResFileMap.ARROW_DOUBLE_UP),
|
||||||
DOUBLE_UP("\u21c8", R.drawable.ic_doubleup, ResFileMap.ARROW_DOUBLE_UP),
|
DOUBLE_UP("\u21c8", R.drawable.ic_doubleup, ResFileMap.ARROW_DOUBLE_UP),
|
||||||
|
@ -515,14 +528,15 @@ private enum class TrendArrowMap(val symbol: String, @DrawableRes val icon: Int,
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
fun drawable(direction: String?, resources: Resources, drawableDataMap: CwfResDataMap): Drawable {
|
fun drawable(direction: String?, resources: Resources, drawableDataMap: CwfResDataMap): Drawable {
|
||||||
val arrow = values().firstOrNull { it.symbol == direction } ?:NONE
|
val arrow = values().firstOrNull { it.symbol == direction } ?: NONE
|
||||||
return arrow.customDrawable?. let {drawableDataMap[arrow.customDrawable.fileName]}?.toDrawable(resources) ?:resources.getDrawable(arrow.icon)
|
return arrow.customDrawable?.let { drawableDataMap[arrow.customDrawable.fileName] }?.toDrawable(resources) ?: resources.getDrawable(arrow.icon)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum class GravityMap(val key: String, val gravity: Int) {
|
@SuppressLint("RtlHardcoded")
|
||||||
|
private enum class GravityMap(val key: String, val gravity: Int) {
|
||||||
CENTER(JsonKeyValues.CENTER.key, Gravity.CENTER),
|
CENTER(JsonKeyValues.CENTER.key, Gravity.CENTER),
|
||||||
LEFT(JsonKeyValues.LEFT.key, Gravity.LEFT),
|
LEFT(JsonKeyValues.LEFT.key, Gravity.LEFT),
|
||||||
RIGHT(JsonKeyValues.RIGHT.key, Gravity.RIGHT);
|
RIGHT(JsonKeyValues.RIGHT.key, Gravity.RIGHT);
|
||||||
|
@ -532,9 +546,9 @@ private enum class GravityMap(val key: String, val gravity: Int) {
|
||||||
fun gravity(key: String?) = values().firstOrNull { it.key == key }?.gravity ?: CENTER.gravity
|
fun gravity(key: String?) = values().firstOrNull { it.key == key }?.gravity ?: CENTER.gravity
|
||||||
fun key(gravity: Int) = values().firstOrNull { it.gravity == gravity }?.key ?: CENTER.key
|
fun key(gravity: Int) = values().firstOrNull { it.gravity == gravity }?.key ?: CENTER.key
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum class FontMap(val key: String, var font: Typeface, @FontRes val fontRessources: Int?) {
|
private enum class FontMap(val key: String, var font: Typeface, @FontRes val fontRessources: Int?) {
|
||||||
SANS_SERIF(JsonKeyValues.SANS_SERIF.key, Typeface.SANS_SERIF, null),
|
SANS_SERIF(JsonKeyValues.SANS_SERIF.key, Typeface.SANS_SERIF, null),
|
||||||
DEFAULT(JsonKeyValues.DEFAULT.key, Typeface.DEFAULT, null),
|
DEFAULT(JsonKeyValues.DEFAULT.key, Typeface.DEFAULT, null),
|
||||||
DEFAULT_BOLD(JsonKeyValues.DEFAULT_BOLD.key, Typeface.DEFAULT_BOLD, null),
|
DEFAULT_BOLD(JsonKeyValues.DEFAULT_BOLD.key, Typeface.DEFAULT_BOLD, null),
|
||||||
|
@ -558,16 +572,16 @@ private enum class FontMap(val key: String, var font: Typeface, @FontRes val fon
|
||||||
resDataMap.filter { (_, resData) ->
|
resDataMap.filter { (_, resData) ->
|
||||||
resData.format == ResFormat.TTF || resData.format == ResFormat.OTF
|
resData.format == ResFormat.TTF || resData.format == ResFormat.OTF
|
||||||
}.forEach { (key, resData) ->
|
}.forEach { (key, resData) ->
|
||||||
customFonts[key.lowercase()] = resData.toTypeface() ?:Typeface.DEFAULT
|
customFonts[key.lowercase()] = resData.toTypeface() ?: Typeface.DEFAULT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun font(key: String) = customFonts[key.lowercase()] ?: DEFAULT.font
|
fun font(key: String) = customFonts[key.lowercase()] ?: DEFAULT.font
|
||||||
fun key() = DEFAULT.key
|
fun key() = DEFAULT.key
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum class StyleMap(val key: String, val style: Int) {
|
private enum class StyleMap(val key: String, val style: Int) {
|
||||||
NORMAL(JsonKeyValues.NORMAL.key, Typeface.NORMAL),
|
NORMAL(JsonKeyValues.NORMAL.key, Typeface.NORMAL),
|
||||||
BOLD(JsonKeyValues.BOLD.key, Typeface.BOLD),
|
BOLD(JsonKeyValues.BOLD.key, Typeface.BOLD),
|
||||||
BOLD_ITALIC(JsonKeyValues.BOLD_ITALIC.key, Typeface.BOLD_ITALIC),
|
BOLD_ITALIC(JsonKeyValues.BOLD_ITALIC.key, Typeface.BOLD_ITALIC),
|
||||||
|
@ -578,10 +592,10 @@ private enum class StyleMap(val key: String, val style: Int) {
|
||||||
fun style(key: String?) = values().firstOrNull { it.key == key }?.style ?: NORMAL.style
|
fun style(key: String?) = values().firstOrNull { it.key == key }?.style ?: NORMAL.style
|
||||||
fun key(style: Int) = values().firstOrNull { it.style == style }?.key ?: NORMAL.key
|
fun key(style: Int) = values().firstOrNull { it.style == style }?.key ?: NORMAL.key
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This class containt mapping between keys used within json of Custom Watchface and preferences
|
// This class containt mapping between keys used within json of Custom Watchface and preferences
|
||||||
private enum class PrefMap(val key: String, @StringRes val prefKey: Int) {
|
private enum class PrefMap(val key: String, @StringRes val prefKey: Int) {
|
||||||
|
|
||||||
SHOW_IOB(CwfMetadataKey.CWF_PREF_WATCH_SHOW_IOB.key, R.string.key_show_iob),
|
SHOW_IOB(CwfMetadataKey.CWF_PREF_WATCH_SHOW_IOB.key, R.string.key_show_iob),
|
||||||
SHOW_DETAILED_IOB(CwfMetadataKey.CWF_PREF_WATCH_SHOW_DETAILED_IOB.key, R.string.key_show_detailed_iob),
|
SHOW_DETAILED_IOB(CwfMetadataKey.CWF_PREF_WATCH_SHOW_DETAILED_IOB.key, R.string.key_show_detailed_iob),
|
||||||
|
@ -598,6 +612,7 @@ private enum class PrefMap(val key: String, @StringRes val prefKey: Int) {
|
||||||
SHOW_BGI(CwfMetadataKey.CWF_PREF_WATCH_SHOW_BGI.key, R.string.key_show_bgi),
|
SHOW_BGI(CwfMetadataKey.CWF_PREF_WATCH_SHOW_BGI.key, R.string.key_show_bgi),
|
||||||
SHOW_LOOP_STATUS(CwfMetadataKey.CWF_PREF_WATCH_SHOW_LOOP_STATUS.key, R.string.key_show_external_status),
|
SHOW_LOOP_STATUS(CwfMetadataKey.CWF_PREF_WATCH_SHOW_LOOP_STATUS.key, R.string.key_show_external_status),
|
||||||
SHOW_WEEK_NUMBER(CwfMetadataKey.CWF_PREF_WATCH_SHOW_WEEK_NUMBER.key, R.string.key_show_week_number)
|
SHOW_WEEK_NUMBER(CwfMetadataKey.CWF_PREF_WATCH_SHOW_WEEK_NUMBER.key, R.string.key_show_week_number)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue