Minor refactoring
This commit is contained in:
parent
462134f20d
commit
6b260a9f71
@ -1,7 +1,6 @@
|
||||
package de.timklge.karoopowerbar
|
||||
|
||||
import android.content.Context
|
||||
import android.content.res.Configuration
|
||||
import android.graphics.BlurMaskFilter
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Paint
|
||||
@ -14,17 +13,9 @@ import androidx.core.graphics.ColorUtils
|
||||
class CustomProgressBar @JvmOverloads constructor(
|
||||
context: Context, attrs: AttributeSet? = null
|
||||
) : View(context, attrs) {
|
||||
private val mPaddedRect = RectF()
|
||||
private val mBlurRadius = 4
|
||||
|
||||
var progress: Double = 0.5
|
||||
@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) {
|
||||
super.onDrawForeground(canvas)
|
||||
|
||||
@ -57,18 +48,18 @@ class CustomProgressBar @JvmOverloads constructor(
|
||||
strokeWidth = 2f
|
||||
}
|
||||
|
||||
val halfStrokeWidth = 1f
|
||||
mPaddedRect.left = halfStrokeWidth
|
||||
mPaddedRect.top = halfStrokeWidth + mBlurRadius
|
||||
mPaddedRect.right =
|
||||
((canvas.width.toDouble() - halfStrokeWidth) * progress.coerceIn(0.0, 1.0)).toFloat()
|
||||
mPaddedRect.bottom = canvas.height.toFloat() - halfStrokeWidth
|
||||
val rect = RectF(
|
||||
1f,
|
||||
1f + 4f,
|
||||
((canvas.width.toDouble() - 1f) * progress.coerceIn(0.0, 1.0)).toFloat(),
|
||||
canvas.height.toFloat() - 1f
|
||||
)
|
||||
|
||||
val corners = 2f
|
||||
canvas.drawRoundRect(0f, 2f + mBlurRadius, canvas.width.toFloat(), canvas.height.toFloat(), 2f, 2f, background)
|
||||
canvas.drawRoundRect(mPaddedRect, corners, corners, blurPaint)
|
||||
canvas.drawRoundRect(mPaddedRect, corners, corners, linePaint)
|
||||
canvas.drawRoundRect(0f, 2f + 4f, canvas.width.toFloat(), canvas.height.toFloat(), 2f, 2f, background)
|
||||
canvas.drawRoundRect(rect, corners, corners, blurPaint)
|
||||
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)
|
||||
}
|
||||
}
|
||||
@ -27,45 +27,6 @@ import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.mapNotNull
|
||||
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 {
|
||||
return (value - fromMin) * (toMax - toMin) / (fromMax - fromMin) + toMin
|
||||
}
|
||||
@ -92,7 +53,7 @@ class Window(
|
||||
layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
|
||||
mView = layoutInflater.inflate(R.layout.popup_window, null)
|
||||
mProgressBar = mView.findViewById(R.id.progressBar)
|
||||
mProgressBar.progress = 0.5
|
||||
mProgressBar.progress = 0.0
|
||||
|
||||
mWindowManager = context.getSystemService(WINDOW_SERVICE) as WindowManager
|
||||
val displayMetrics = DisplayMetrics()
|
||||
@ -105,7 +66,7 @@ class Window(
|
||||
displayMetrics.heightPixels = bounds.height() - insets.top - insets.bottom
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
mWindowManager.getDefaultDisplay().getMetrics(displayMetrics)
|
||||
mWindowManager.defaultDisplay.getMetrics(displayMetrics)
|
||||
}
|
||||
|
||||
// Define the position of the
|
||||
@ -151,7 +112,7 @@ class Window(
|
||||
mWindowManager.addView(mView, mParams)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.d("Error1", e.toString())
|
||||
Log.d("karoo-powerbar", e.toString())
|
||||
}
|
||||
}
|
||||
|
||||
@ -187,7 +148,6 @@ class Window(
|
||||
RAW(DataType.Type.POWER),
|
||||
SMOOTHED_3S(DataType.Type.SMOOTHED_3S_AVERAGE_POWER),
|
||||
SMOOTHED_10S(DataType.Type.SMOOTHED_10S_AVERAGE_POWER),
|
||||
SMOOTHED_30S(DataType.Type.SMOOTHED_30S_AVERAGE_POWER),
|
||||
}
|
||||
|
||||
private suspend fun streamPower(smoothed: PowerStreamSmoothing) {
|
||||
|
||||
41
app/src/main/kotlin/de/timklge/karoopowerbar/Zones.kt
Normal file
41
app/src/main/kotlin/de/timklge/karoopowerbar/Zones.kt
Normal 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
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user