CustomWF add enum for font, style and gravity management
This commit is contained in:
parent
5bf787bdc0
commit
2c383bbee0
1 changed files with 69 additions and 76 deletions
|
@ -20,6 +20,7 @@ import android.widget.FrameLayout
|
|||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.annotation.FontRes
|
||||
import androidx.annotation.IdRes
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.core.content.ContextCompat
|
||||
|
@ -57,6 +58,10 @@ class CustomWatchface : BaseWatchFace() {
|
|||
private var lowBatColor = Color.RED
|
||||
private var bgColor = Color.WHITE
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
FontMap.init(context)
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
override fun inflateLayout(inflater: LayoutInflater): ViewBinding {
|
||||
|
@ -78,7 +83,7 @@ class CustomWatchface : BaseWatchFace() {
|
|||
|
||||
override fun setDataFields() {
|
||||
super.setDataFields()
|
||||
binding.direction2.setImageDrawable(resources.getDrawable(TrendArrow.fromSymbol(singleBg.slopeArrow).icon))
|
||||
binding.direction2.setImageDrawable(resources.getDrawable(TrendArrow.icon(singleBg.slopeArrow)))
|
||||
// rotate the second hand.
|
||||
binding.secondHand.rotation = TimeOfDay().secondOfMinute * 6f
|
||||
// rotate the minute hand.
|
||||
|
@ -86,6 +91,7 @@ class CustomWatchface : BaseWatchFace() {
|
|||
// rotate the hour hand.
|
||||
binding.hourHand.rotation = TimeOfDay().hourOfDay * 30f + TimeOfDay().minuteOfHour * 0.5f
|
||||
}
|
||||
|
||||
override fun setColorDark() {
|
||||
setWatchfaceStyle()
|
||||
binding.sgv.setTextColor(bgColor)
|
||||
|
@ -164,10 +170,10 @@ class CustomWatchface : BaseWatchFace() {
|
|||
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 = setGravity(if (viewjson.has("gravity")) viewjson.getString("gravity") else "center")
|
||||
view.gravity = GravityMap.gravity(if (viewjson.has("gravity")) viewjson.getString("gravity") else GravityMap.CENTER.key)
|
||||
view.setTypeface(
|
||||
setFont(if (viewjson.has("font")) viewjson.getString("font") else "sans-serif"),
|
||||
setStyle(if (viewjson.has("fontStyle")) viewjson.getString("fontStyle") else "normal")
|
||||
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)
|
||||
)
|
||||
if (viewjson.has("fontColor"))
|
||||
view.setTextColor(getColor(viewjson.getString("fontColor")))
|
||||
|
@ -245,9 +251,9 @@ class CustomWatchface : BaseWatchFace() {
|
|||
.put("rotation", view.rotation.toInt())
|
||||
.put("visibility", getVisibility(view.visibility))
|
||||
.put("textsize", view.textSize.toInt())
|
||||
.put("gravity", getGravity(view.gravity))
|
||||
.put("font", getFont(view.typeface))
|
||||
.put("fontStyle", getStyle(view.typeface.style))
|
||||
.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))
|
||||
)
|
||||
}
|
||||
|
@ -309,62 +315,6 @@ class CustomWatchface : BaseWatchFace() {
|
|||
else -> "gone"
|
||||
}
|
||||
|
||||
private fun setGravity(gravity: String): Int = when (gravity) {
|
||||
"center" -> Gravity.CENTER
|
||||
"left" -> Gravity.LEFT
|
||||
"right" -> Gravity.RIGHT
|
||||
else -> Gravity.CENTER
|
||||
}
|
||||
|
||||
private fun getGravity(gravity: Int): String = when (gravity) {
|
||||
Gravity.CENTER -> "center"
|
||||
Gravity.LEFT -> "left"
|
||||
Gravity.RIGHT -> "right"
|
||||
else -> "center"
|
||||
}
|
||||
|
||||
private fun setFont(font: String): Typeface = when (font) {
|
||||
"sans-serif" -> Typeface.SANS_SERIF
|
||||
"default" -> Typeface.DEFAULT
|
||||
"default-bold" -> Typeface.DEFAULT_BOLD
|
||||
"monospace" -> Typeface.MONOSPACE
|
||||
"serif" -> Typeface.SERIF
|
||||
"roboto-condensed-bold" -> ResourcesCompat.getFont(context, R.font.roboto_condensed_bold)!!
|
||||
"roboto-condensed-light" -> ResourcesCompat.getFont(context, R.font.roboto_condensed_light)!!
|
||||
"roboto-condensed-regular" -> ResourcesCompat.getFont(context, R.font.roboto_condensed_regular)!!
|
||||
"roboto-slab-light" -> ResourcesCompat.getFont(context, R.font.roboto_slab_light)!!
|
||||
else -> Typeface.DEFAULT
|
||||
}
|
||||
|
||||
private fun getFont(font: Typeface): String = when (font) {
|
||||
Typeface.SANS_SERIF -> "sans-serif"
|
||||
Typeface.DEFAULT -> "default"
|
||||
Typeface.DEFAULT_BOLD -> "default-bold"
|
||||
Typeface.MONOSPACE -> "monospace"
|
||||
Typeface.SERIF -> "serif"
|
||||
ResourcesCompat.getFont(context, R.font.roboto_condensed_bold)!! -> "roboto-condensed-bold"
|
||||
ResourcesCompat.getFont(context, R.font.roboto_condensed_light)!! -> "roboto-condensed-light"
|
||||
ResourcesCompat.getFont(context, R.font.roboto_condensed_regular)!! -> "roboto-condensed-regular"
|
||||
ResourcesCompat.getFont(context, R.font.roboto_slab_light)!! -> "roboto-slab-light"
|
||||
else -> "default"
|
||||
}
|
||||
|
||||
private fun setStyle(style: String): Int = when (style) {
|
||||
"normal" -> Typeface.NORMAL
|
||||
"bold" -> Typeface.BOLD
|
||||
"bold-italic" -> Typeface.BOLD_ITALIC
|
||||
"italic" -> Typeface.ITALIC
|
||||
else -> Typeface.NORMAL
|
||||
}
|
||||
|
||||
private fun getStyle(style: Int): String = when (style) {
|
||||
Typeface.NORMAL -> "normal"
|
||||
Typeface.BOLD -> "bold"
|
||||
Typeface.BOLD_ITALIC -> "bold-italic"
|
||||
Typeface.ITALIC -> "italic"
|
||||
else -> "normal"
|
||||
}
|
||||
|
||||
fun getResourceByteArray(resourceId: Int): ByteArray? {
|
||||
val inputStream = resources.openRawResource(resourceId)
|
||||
val byteArrayOutputStream = ByteArrayOutputStream()
|
||||
|
@ -412,7 +362,7 @@ class CustomWatchface : BaseWatchFace() {
|
|||
}
|
||||
}
|
||||
|
||||
enum class CustomViews(val key: String, @IdRes val id: Int, @StringRes val pref: Int?) {
|
||||
private enum class CustomViews(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),
|
||||
|
@ -447,17 +397,16 @@ class CustomWatchface : BaseWatchFace() {
|
|||
SECOND_HAND(CustomWatchfaceDrawableDataKey.SECONDHAND.key, R.id.second_hand, R.string.key_show_seconds);
|
||||
|
||||
companion object {
|
||||
|
||||
fun fromKey(key: String): CustomViews? = values().firstOrNull { it.key == key }
|
||||
fun fromId(id: Int): CustomViews? = values().firstOrNull { it.id == id }
|
||||
}
|
||||
|
||||
|
||||
fun visibility(sp: SP): Boolean = this.pref?.let { sp.getBoolean(it, true) }
|
||||
?: true
|
||||
}
|
||||
|
||||
|
||||
enum class TrendArrow(val text: String, val symbol: String,@DrawableRes val icon: Int) {
|
||||
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),
|
||||
|
@ -471,8 +420,52 @@ class CustomWatchface : BaseWatchFace() {
|
|||
;
|
||||
|
||||
companion object {
|
||||
fun fromSymbol(direction: String?) =
|
||||
values().firstOrNull { it.symbol == direction } ?: NONE
|
||||
|
||||
fun icon(direction: String?) = values().firstOrNull { it.symbol == direction }?.icon ?: NONE.icon
|
||||
}
|
||||
}
|
||||
|
||||
private enum class GravityMap(val key: String, val gravity: Int) {
|
||||
CENTER("center", Gravity.CENTER),
|
||||
LEFT("left", Gravity.LEFT),
|
||||
RIGHT("right", Gravity.RIGHT);
|
||||
|
||||
companion object {
|
||||
|
||||
fun gravity(key: String?) = values().firstOrNull { it.key == key }?.gravity ?: CENTER.gravity
|
||||
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?) {
|
||||
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);
|
||||
|
||||
companion object {
|
||||
|
||||
fun init(context: Context) = values().forEach { it.font = it.fontRessources?.let { font -> ResourcesCompat.getFont(context, font) } ?: it.font }
|
||||
fun font(key: String) = values().firstOrNull { it.key == key }?.font ?: DEFAULT.font
|
||||
fun key() = DEFAULT.key
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
companion object {
|
||||
|
||||
fun style(key: String?) = values().firstOrNull { it.key == key }?.style ?: NORMAL.style
|
||||
fun key(style: Int) = values().firstOrNull { it.style == style }?.key ?: NORMAL.key
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue