Merge pull request #2798 from Philoul/wear/new_custom_watchface

Wear CWF Code improvement
This commit is contained in:
Milos Kozak 2023-09-19 14:47:38 +02:00 committed by GitHub
commit 12b0ae9633
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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,35 +183,11 @@ 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()
}
} }
} }
@ -497,6 +471,45 @@ class CustomWatchface : BaseWatchFace() {
else -> drawableDataMap[cd.fileName]?.toDrawable(resources) else -> drawableDataMap[cd.fileName]?.toDrawable(resources)
} }
} }
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()
}
} }
} }
@ -522,6 +535,7 @@ private enum class TrendArrowMap(val symbol: String, @DrawableRes val icon: Int,
} }
} }
@SuppressLint("RtlHardcoded")
private enum class GravityMap(val key: String, val gravity: Int) { 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),
@ -599,5 +613,6 @@ private enum class PrefMap(val key: String, @StringRes val prefKey: Int) {
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)
} }
}