Minor code improvement

This commit is contained in:
Philoul 2023-08-17 11:13:54 +02:00
parent 2c383bbee0
commit 1379639229
2 changed files with 23 additions and 26 deletions

View file

@ -2,6 +2,7 @@
package info.nightscout.androidaps.watchfaces package info.nightscout.androidaps.watchfaces
import android.annotation.SuppressLint
import android.app.ActionBar.LayoutParams import android.app.ActionBar.LayoutParams
import android.content.Context import android.content.Context
import android.graphics.Color import android.graphics.Color
@ -81,9 +82,10 @@ class CustomWatchface : BaseWatchFace() {
.build() .build()
} }
@SuppressLint("UseCompatLoadingForDrawables")
override fun setDataFields() { override fun setDataFields() {
super.setDataFields() super.setDataFields()
binding.direction2.setImageDrawable(resources.getDrawable(TrendArrow.icon(singleBg.slopeArrow))) binding.direction2.setImageDrawable(this.resources.getDrawable(TrendArrow.icon(singleBg.slopeArrow)))
// rotate the second hand. // rotate the second hand.
binding.secondHand.rotation = TimeOfDay().secondOfMinute * 6f binding.secondHand.rotation = TimeOfDay().secondOfMinute * 6f
// rotate the minute hand. // rotate the minute hand.
@ -119,11 +121,14 @@ class CustomWatchface : BaseWatchFace() {
} }
override fun setSecond() { override fun setSecond() {
binding.time.text = "${dateUtil.hourString()}:${dateUtil.minuteString()}" + if (showSecond) ":${dateUtil.secondString()}" else "" binding.time.text = if (showSecond)
getString(R.string.hour_minute_second, dateUtil.hourString(), dateUtil.minuteString(), dateUtil.secondString())
else
getString(R.string.hour_minute, dateUtil.hourString(), dateUtil.minuteString())
//binding.time.text = "${dateUtil.hourString()}:${dateUtil.minuteString()}" + if (showSecond) ":${dateUtil.secondString()}" else ""
binding.second.text = dateUtil.secondString() binding.second.text = dateUtil.secondString()
// rotate the second hand. // rotate the second hand.
binding.secondHand.rotation = TimeOfDay().secondOfMinute * 6f binding.secondHand.rotation = TimeOfDay().secondOfMinute * 6f
//aapsLogger.debug("XXXXX SetSecond $watchModeString")
} }
override fun updateSecondVisibility() { override fun updateSecondVisibility() {
@ -131,6 +136,7 @@ class CustomWatchface : BaseWatchFace() {
binding.secondHand.visibility = showSecond.toVisibility() binding.secondHand.visibility = 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 {
@ -315,11 +321,10 @@ class CustomWatchface : BaseWatchFace() {
else -> "gone" else -> "gone"
} }
fun getResourceByteArray(resourceId: Int): ByteArray? { private fun getResourceByteArray(resourceId: Int): ByteArray? {
val inputStream = resources.openRawResource(resourceId) val inputStream = resources.openRawResource(resourceId)
val byteArrayOutputStream = ByteArrayOutputStream() val byteArrayOutputStream = ByteArrayOutputStream()
try {
val buffer = ByteArray(1024) val buffer = ByteArray(1024)
var count: Int var count: Int
while (inputStream.read(buffer).also { count = it } != -1) { while (inputStream.read(buffer).also { count = it } != -1) {
@ -329,9 +334,6 @@ class CustomWatchface : BaseWatchFace() {
inputStream.close() inputStream.close()
return byteArrayOutputStream.toByteArray() return byteArrayOutputStream.toByteArray()
} catch (e: Exception) {
}
return null
} }
private fun changeDrawableColor(color: Int): ColorFilter { private fun changeDrawableColor(color: Int): ColorFilter {
@ -351,16 +353,11 @@ class CustomWatchface : BaseWatchFace() {
return ColorMatrixColorFilter(colorMatrix) return ColorMatrixColorFilter(colorMatrix)
} }
private fun getColor(color: String): Int { private fun getColor(color: String): Int =
if (color == "bgColor") if (color == "bgColor")
return bgColor bgColor
else else
return try { try { Color.parseColor(color) } catch (e: Exception) { Color.GRAY }
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 CustomViews(val key: String, @IdRes val id: Int, @StringRes val pref: Int?) {
@ -398,7 +395,6 @@ class CustomWatchface : BaseWatchFace() {
companion object { companion object {
fun fromKey(key: String): CustomViews? = values().firstOrNull { it.key == key }
fun fromId(id: Int): CustomViews? = values().firstOrNull { it.id == id } fun fromId(id: Int): CustomViews? = values().firstOrNull { it.id == id }
} }
@ -449,7 +445,6 @@ class CustomWatchface : BaseWatchFace() {
ROBOTO_SLAB_LIGHT("roboto-slab-light", Typeface.DEFAULT, R.font.roboto_slab_light); ROBOTO_SLAB_LIGHT("roboto-slab-light", Typeface.DEFAULT, R.font.roboto_slab_light);
companion object { companion object {
fun init(context: Context) = values().forEach { it.font = it.fontRessources?.let { font -> ResourcesCompat.getFont(context, font) } ?: it.font } 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 font(key: String) = values().firstOrNull { it.key == key }?.font ?: DEFAULT.font
fun key() = DEFAULT.key fun key() = DEFAULT.key

View file

@ -240,6 +240,8 @@
<string name="week_short">w</string> <string name="week_short">w</string>
<string name="day_short">d</string> <string name="day_short">d</string>
<string name="hour_short">h</string> <string name="hour_short">h</string>
<string name="hour_minute">%1$s:%2$s</string>
<string name="hour_minute_second">%1$s:%2$s:%3$s</string>
<string name="old">old</string> <string name="old">old</string>
<string name="old_warning">!old!</string> <string name="old_warning">!old!</string>
<string name="error">!err!</string> <string name="error">!err!</string>