Minor refactoring

This commit is contained in:
Tim Kluge 2024-12-08 16:07:26 +01:00
parent 462134f20d
commit 6b260a9f71
3 changed files with 54 additions and 62 deletions

View File

@ -1,7 +1,6 @@
package de.timklge.karoopowerbar package de.timklge.karoopowerbar
import android.content.Context import android.content.Context
import android.content.res.Configuration
import android.graphics.BlurMaskFilter import android.graphics.BlurMaskFilter
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Paint import android.graphics.Paint
@ -14,17 +13,9 @@ import androidx.core.graphics.ColorUtils
class CustomProgressBar @JvmOverloads constructor( class CustomProgressBar @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null context: Context, attrs: AttributeSet? = null
) : View(context, attrs) { ) : View(context, attrs) {
private val mPaddedRect = RectF()
private val mBlurRadius = 4
var progress: Double = 0.5 var progress: Double = 0.5
@ColorInt var progressColor: Int = 0xFF2b86e6.toInt() @ColorInt var progressColor: Int = 0xFF2b86e6.toInt()
fun isDarkMode(context: Context): Boolean {
val flags = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
return flags == Configuration.UI_MODE_NIGHT_YES
}
override fun onDrawForeground(canvas: Canvas) { override fun onDrawForeground(canvas: Canvas) {
super.onDrawForeground(canvas) super.onDrawForeground(canvas)
@ -57,18 +48,18 @@ class CustomProgressBar @JvmOverloads constructor(
strokeWidth = 2f strokeWidth = 2f
} }
val halfStrokeWidth = 1f val rect = RectF(
mPaddedRect.left = halfStrokeWidth 1f,
mPaddedRect.top = halfStrokeWidth + mBlurRadius 1f + 4f,
mPaddedRect.right = ((canvas.width.toDouble() - 1f) * progress.coerceIn(0.0, 1.0)).toFloat(),
((canvas.width.toDouble() - halfStrokeWidth) * progress.coerceIn(0.0, 1.0)).toFloat() canvas.height.toFloat() - 1f
mPaddedRect.bottom = canvas.height.toFloat() - halfStrokeWidth )
val corners = 2f val corners = 2f
canvas.drawRoundRect(0f, 2f + mBlurRadius, canvas.width.toFloat(), canvas.height.toFloat(), 2f, 2f, background) canvas.drawRoundRect(0f, 2f + 4f, canvas.width.toFloat(), canvas.height.toFloat(), 2f, 2f, background)
canvas.drawRoundRect(mPaddedRect, corners, corners, blurPaint) canvas.drawRoundRect(rect, corners, corners, blurPaint)
canvas.drawRoundRect(mPaddedRect, corners, corners, linePaint) canvas.drawRoundRect(rect, corners, corners, linePaint)
canvas.drawRoundRect(mPaddedRect.right-4, mPaddedRect.top, mPaddedRect.right+4, mPaddedRect.bottom, 2f, 2f, blurPaintHighlight) canvas.drawRoundRect(rect.right-4, rect.top, rect.right+4, rect.bottom, 2f, 2f, blurPaintHighlight)
} }
} }

View File

@ -27,45 +27,6 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
enum class PowerZone(val colorResource: Int) {
ACTIVE_RECOVERY(R.color.zoneActiveRecovery),
ENDURANCE(R.color.zoneEndurance),
TEMPO(R.color.zoneTempo),
THRESHOLD(R.color.zoneThreshold),
VO2_MAX(R.color.zoneVO2Max),
AEROBIC_CAPACITY(R.color.zoneAerobic),
ANAEROBIC_CAPACITY(R.color.zoneAnaerobic),
}
enum class HrZone(val colorResource: Int) {
ACTIVE_RECOVERY(R.color.zoneActiveRecovery),
ENDURANCE(R.color.zoneEndurance),
TEMPO(R.color.zoneTempo),
THRESHOLD(R.color.zoneThreshold),
VO2_MAX(R.color.zoneAerobic),
}
fun UserProfile.getUserPowerZone(power: Int): PowerZone? {
powerZones.forEachIndexed { index, zone ->
if (power in zone.min..zone.max) {
return PowerZone.entries[index]
}
}
return null
}
fun UserProfile.getUserHrZone(hr: Int): HrZone? {
heartRateZones.forEachIndexed { index, zone ->
if (hr in zone.min..zone.max) {
return HrZone.entries[index]
}
}
return null
}
fun remap(value: Double, fromMin: Double, fromMax: Double, toMin: Double, toMax: Double): Double { fun remap(value: Double, fromMin: Double, fromMax: Double, toMin: Double, toMax: Double): Double {
return (value - fromMin) * (toMax - toMin) / (fromMax - fromMin) + toMin return (value - fromMin) * (toMax - toMin) / (fromMax - fromMin) + toMin
} }
@ -92,7 +53,7 @@ class Window(
layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
mView = layoutInflater.inflate(R.layout.popup_window, null) mView = layoutInflater.inflate(R.layout.popup_window, null)
mProgressBar = mView.findViewById(R.id.progressBar) mProgressBar = mView.findViewById(R.id.progressBar)
mProgressBar.progress = 0.5 mProgressBar.progress = 0.0
mWindowManager = context.getSystemService(WINDOW_SERVICE) as WindowManager mWindowManager = context.getSystemService(WINDOW_SERVICE) as WindowManager
val displayMetrics = DisplayMetrics() val displayMetrics = DisplayMetrics()
@ -105,7 +66,7 @@ class Window(
displayMetrics.heightPixels = bounds.height() - insets.top - insets.bottom displayMetrics.heightPixels = bounds.height() - insets.top - insets.bottom
} else { } else {
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
mWindowManager.getDefaultDisplay().getMetrics(displayMetrics) mWindowManager.defaultDisplay.getMetrics(displayMetrics)
} }
// Define the position of the // Define the position of the
@ -151,7 +112,7 @@ class Window(
mWindowManager.addView(mView, mParams) mWindowManager.addView(mView, mParams)
} }
} catch (e: Exception) { } catch (e: Exception) {
Log.d("Error1", e.toString()) Log.d("karoo-powerbar", e.toString())
} }
} }
@ -187,7 +148,6 @@ class Window(
RAW(DataType.Type.POWER), RAW(DataType.Type.POWER),
SMOOTHED_3S(DataType.Type.SMOOTHED_3S_AVERAGE_POWER), SMOOTHED_3S(DataType.Type.SMOOTHED_3S_AVERAGE_POWER),
SMOOTHED_10S(DataType.Type.SMOOTHED_10S_AVERAGE_POWER), SMOOTHED_10S(DataType.Type.SMOOTHED_10S_AVERAGE_POWER),
SMOOTHED_30S(DataType.Type.SMOOTHED_30S_AVERAGE_POWER),
} }
private suspend fun streamPower(smoothed: PowerStreamSmoothing) { private suspend fun streamPower(smoothed: PowerStreamSmoothing) {

View File

@ -0,0 +1,41 @@
package de.timklge.karoopowerbar
import io.hammerhead.karooext.models.UserProfile
enum class PowerZone(val colorResource: Int) {
ACTIVE_RECOVERY(R.color.zoneActiveRecovery),
ENDURANCE(R.color.zoneEndurance),
TEMPO(R.color.zoneTempo),
THRESHOLD(R.color.zoneThreshold),
VO2_MAX(R.color.zoneVO2Max),
AEROBIC_CAPACITY(R.color.zoneAerobic),
ANAEROBIC_CAPACITY(R.color.zoneAnaerobic),
}
enum class HrZone(val colorResource: Int) {
ACTIVE_RECOVERY(R.color.zoneActiveRecovery),
ENDURANCE(R.color.zoneEndurance),
TEMPO(R.color.zoneTempo),
THRESHOLD(R.color.zoneThreshold),
VO2_MAX(R.color.zoneAerobic),
}
fun UserProfile.getUserPowerZone(power: Int): PowerZone? {
powerZones.forEachIndexed { index, zone ->
if (power in zone.min..zone.max) {
return PowerZone.entries[index]
}
}
return null
}
fun UserProfile.getUserHrZone(hr: Int): HrZone? {
heartRateZones.forEachIndexed { index, zone ->
if (hr in zone.min..zone.max) {
return HrZone.entries[index]
}
}
return null
}