Skip to content

Commit

Permalink
feat: alarms working
Browse files Browse the repository at this point in the history
  • Loading branch information
agronick committed Feb 1, 2024
1 parent 9bea046 commit 0a7030e
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 94 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ dependencies {

testImplementation 'junit:junit:4.13.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'com.ezylang:EvalEx:3.0.5'
implementation 'com.ezylang:EvalEx:3.1.0'

implementation 'androidx.annotation:annotation:1.7.1'
implementation "androidx.datastore:datastore:1.0.0"
implementation 'com.google.protobuf:protobuf-java-util:3.25.1'
implementation 'rongi.rotate-layout:rotate-layout:3.0.0'
implementation 'androidx.databinding:databinding-runtime:8.2.1'
implementation 'androidx.databinding:databinding-runtime:8.2.2'
// implementation 'com.google.dagger:hilt-android:2.48'
// kapt 'com.google.dagger:hilt-compiler:2.48'
implementation "ch.acra:acra-mail:$acraVersion"
Expand Down
8 changes: 8 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,12 @@
<fields>;
}

-keepclassmembers class * extends com.google.protobuf.DescriptorProtos {
<fields>;
}

-keep class * extends com.google.protobuf.DescriptorProtos$**{
*;
}

-keep,allowoptimization class com.aatorque.** { *; }
12 changes: 2 additions & 10 deletions app/src/main/java/com/aatorque/prefs/AlarmFragment.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.aatorque.prefs

import com.aatorque.datastore.Operation
import android.os.Bundle
import android.os.Parcelable
import android.view.LayoutInflater
Expand Down Expand Up @@ -45,26 +44,24 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import com.aatorque.datastore.Coloring
import com.aatorque.datastore.Display
import com.aatorque.datastore.Operation
import com.aatorque.datastore.Screen
import com.aatorque.datastore.UserPreferenceOrBuilder
import com.aatorque.stats.R
import com.aatorque.ui.AppTheme
import com.rarepebble.colorpicker.ColorPickerView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.parcelize.Parcelize
Expand Down Expand Up @@ -414,7 +411,7 @@ fun VerticalReorderList(model: StateModel = viewModel()) {
model.uiState.forEach { current ->
ListItem(
headlineContent = {
Text(current.operation.op)
Text("value ${current.operation.op} ${current.value}")
},
leadingContent = {
TextButton(
Expand All @@ -434,11 +431,6 @@ fun VerticalReorderList(model: StateModel = viewModel()) {

}
},
supportingContent = {
Text(
current.value.toString(),
)
},

trailingContent = {
Button(onClick = {
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/com/aatorque/prefs/SettingsPIDFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ import com.aatorque.stats.TorqueServiceWrapper
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.launch
import timber.log.Timber
import kotlin.math.ceil
Expand Down Expand Up @@ -195,7 +192,9 @@ class SettingsPIDFragment: PreferenceFragmentCompat() {
maxMarksActivePref.value = display.maxMarksActive.number.toString()
highVisActivePref.isChecked = display.highVisActive
colorPref.colorValue = display.chartColor.let {
if (it == 0) {
if (!isClock) {
Color.WHITE
} else if (it == 0) {
resources.obtainTypedArray(R.array.chartColors).run {
val color = getColor(index, Color.WHITE)
recycle()
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/com/aatorque/stats/TorqueData.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.aatorque.stats

import android.database.Observable
import android.icu.math.BigDecimal
import android.icu.text.NumberFormat
import androidx.lifecycle.MutableLiveData
Expand Down Expand Up @@ -63,7 +62,10 @@ class TorqueData(var display: Display) {
}

private fun updateAlarm(field: Double) {
currentAlarm.value = display.alarmsList.firstOrNull {
if (!hasReceivedNonZero) {
return
}
val newValue = display.alarmsList.lastOrNull {
when (it.operation) {
Operation.GT -> field > it.value
Operation.GTE -> field >= it.value
Expand All @@ -73,6 +75,9 @@ class TorqueData(var display: Display) {
else -> false
}
}
if ((currentAlarm.value != null && newValue == null) || newValue?.equals(currentAlarm) == false) {
currentAlarm.postValue(newValue)
}
}


Expand Down
18 changes: 10 additions & 8 deletions app/src/main/java/com/aatorque/stats/TorqueDisplay.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModelProvider
import com.aatorque.datastore.Coloring
import com.aatorque.prefs.SettingsViewModel
import com.aatorque.stats.databinding.FragmentDisplayBinding
import com.aatorque.utils.AwareObserver
import timber.log.Timber


class TorqueDisplay : Fragment() {
lateinit var rootView: View
private var unit = ""
Expand Down Expand Up @@ -82,14 +85,7 @@ class TorqueDisplay : Fragment() {
binding.value = "-"
}


/*
if (icon == "empty") {
label.setBackgroundResource(0)
val params = label.layoutParams as ConstraintLayout.LayoutParams
params.width = 40
label.layoutParams = params
} */
alarmObserver.bind(viewLifecycleOwner, data.currentAlarm)
}

fun setupTypeface(typeface: Typeface) {
Expand All @@ -100,4 +96,10 @@ class TorqueDisplay : Fragment() {
fun onUpdate(data: TorqueData) {
binding.value = data.lastDataStr + unit
}

val alarmObserver = object : AwareObserver<Coloring>() {
override fun onChanged(value: Coloring?) {
binding.alarm = value?.color
}
}
}
5 changes: 2 additions & 3 deletions app/src/main/java/com/aatorque/stats/TorqueGauge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import com.aatorque.datastore.MaxControl
import com.aatorque.prefs.SettingsViewModel
import com.aatorque.stats.databinding.FragmentGaugeBinding
import com.aatorque.utils.AwareObserver
import com.github.anastr.speedviewlib.ImageSpeedometer
import com.github.anastr.speedviewlib.RaySpeedometer
import com.github.anastr.speedviewlib.Speedometer
import com.github.anastr.speedviewlib.components.Section
Expand All @@ -32,7 +31,7 @@ val MIN_MAX_DEFAULT = Pair(0f, 100f)
class TorqueGauge : Fragment() {

private var rootView: View? = null
private val mClock: ImageSpeedometer
private val mClock: TorqueSpeedometer
get() {
return binding.dial
}
Expand Down Expand Up @@ -294,7 +293,7 @@ class TorqueGauge : Fragment() {

val alarmObserver = object: AwareObserver<Coloring>() {
override fun onChanged(value: Coloring?) {
binding.alarm = value
mClock.setAlarm(value)
}
}
}
36 changes: 13 additions & 23 deletions app/src/main/java/com/aatorque/stats/TorqueSpeedometer.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package com.aatorque.stats

import android.animation.Animator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.ColorSpace
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.drawable.Drawable
import android.support.v4.media.session.PlaybackStateCompat.RepeatMode
import android.util.AttributeSet
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.core.graphics.toRect
import com.aatorque.datastore.Coloring
import com.aatorque.utils.OpenCloseAnimator
import com.github.anastr.speedviewlib.ImageSpeedometer

class TorqueSpeedometer @JvmOverloads constructor(
Expand All @@ -25,25 +21,19 @@ class TorqueSpeedometer @JvmOverloads constructor(

var icon: Drawable? = null
var alarmPaint: Paint? = null
val alarmBottomOffset = dpTOpx(5f)
val alarmAnimator = ValueAnimator.ofFloat(0f, 1f).setDuration(
300
).apply {
repeatCount = 0
val alarmBottomOffset = dpTOpx(3f)
val alarmAnimator = OpenCloseAnimator.ofFloat(0f, 1f).apply {
addUpdateListener {
invalidate()
}
duration = 300
}
var hasAlarmValue = false

fun setAlarm(coloring: Coloring?) {
val value = coloring?.color
if (!hasAlarmValue && value != null) {
alarmAnimator.repeatMode = ValueAnimator.RESTART
alarmAnimator.pause()
alarmAnimator.start()
alarmAnimator.setState(coloring != null)
coloring?.let {
alarmPaint = Paint().apply {
color = value ?: Color(0f, 0f, 0f, 0f).toArgb()
color = it.color
style = Paint.Style.STROKE
strokeWidth = alarmBottomOffset
setShadowLayer(
Expand All @@ -53,12 +43,7 @@ class TorqueSpeedometer @JvmOverloads constructor(
Color(0f, 0f, 0f, 0.3f).toArgb()
)
}
} else if (hasAlarmValue && value == null) {
alarmAnimator.repeatMode = ValueAnimator.REVERSE
alarmAnimator.pause()
alarmAnimator.start()
}
hasAlarmValue = value != null
}

override fun onDraw(canvas: Canvas) {
Expand All @@ -71,7 +56,12 @@ class TorqueSpeedometer @JvmOverloads constructor(
start += start - (start * percent)
distance *= percent
canvas.drawArc(
RectF(0f, 0f, size.toFloat(), size.toFloat() - alarmBottomOffset),
RectF(
0f + alarmBottomOffset,
0f + alarmBottomOffset,
size.toFloat() - alarmBottomOffset,
size.toFloat() - alarmBottomOffset
),
start,
distance,
false,
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/aatorque/stats/ViewAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.Guideline
import androidx.core.view.children
import androidx.databinding.BindingAdapter
import com.github.anastr.speedviewlib.Gauge
import com.github.anastr.speedviewlib.ImageSpeedometer
Expand Down Expand Up @@ -110,4 +112,16 @@ fun bitmapOrResource(view: ImageView, bitmap: Bitmap?, resource: Int?) {
} else {
view.setImageResource(0)
}
}

@BindingAdapter("reversed")
fun reversed(view: LinearLayout, isReversed: Boolean) {
if ((view.getTag(R.id.container) ?: false) != isReversed) {
val children = view.children.toList()
view.removeAllViews()
children.reversed().forEachIndexed { index, child ->
view.addView(child, index)
}
}
view.setTag(R.id.container, isReversed)
}
74 changes: 74 additions & 0 deletions app/src/main/java/com/aatorque/utils/AnimatedLine.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.aatorque.utils

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb


class AnimatedLine(context: Context, attributeSet: AttributeSet) : View(context, attributeSet) {

var lastRect: RectF? = null
val animator = OpenCloseAnimator.ofFloat(0f, 1f).apply {
duration = 300
}

var paint: Paint? = null
var color: Int? = null
set(value) {
field = value
animator.setState(field != null)
field?.let {
paint = Paint().apply {
color = it
style = Paint.Style.FILL
strokeWidth = 3f
setShadowLayer(
3f,
0f,
0f,
Color(0f, 0f, 0f, 0.5f).toArgb()
)
}
}
}

override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
lastRect?.let { lr ->
paint?.let {
canvas.drawRect(
lr, it
)
}
}
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()
measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED)
handler.post {
val midpoint = measuredWidth * 0.5f
animator.addUpdateListener {
lastRect = if (it.animatedValue == 0f) {
null
} else {
val halfWidthDiff = midpoint * it.animatedValue as Float
RectF(
midpoint - halfWidthDiff,
0f,
midpoint + halfWidthDiff,
height.toFloat()
)
}
invalidate()
}
}
}


}
Loading

0 comments on commit 0a7030e

Please sign in to comment.