Implement numerical habits with AT_MOST target type

This commit is contained in:
KristianTashkov
2021-09-11 23:23:52 +03:00
parent fc1478645b
commit 804edfa64e
11 changed files with 155 additions and 34 deletions

View File

@@ -117,6 +117,10 @@ class EditHabitActivity : AppCompatActivity() {
binding.notesInput.setText(habit.description)
binding.unitInput.setText(habit.unit)
binding.targetInput.setText(habit.targetValue.toString())
if (habit.targetType == NumericalHabitType.AT_MOST) {
binding.targetTypeAtMost.isChecked = true
binding.targetTypeAtLeast.isChecked = false
}
} else {
habitType = HabitType.fromInt(intent.getIntExtra("habitType", HabitType.YES_NO.value))
}
@@ -138,6 +142,7 @@ class EditHabitActivity : AppCompatActivity() {
HabitType.YES_NO -> {
binding.unitOuterBox.visibility = View.GONE
binding.targetOuterBox.visibility = View.GONE
binding.targetTypeOuterBox.visibility = View.GONE
}
HabitType.NUMERICAL -> {
binding.nameInput.hint = getString(R.string.measurable_short_example)
@@ -262,7 +267,10 @@ class EditHabitActivity : AppCompatActivity() {
habit.frequency = Frequency(freqNum, freqDen)
if (habitType == HabitType.NUMERICAL) {
habit.targetValue = targetInput.text.toString().toDouble()
habit.targetType = NumericalHabitType.AT_LEAST
if (binding.targetTypeAtLeast.isChecked)
habit.targetType = NumericalHabitType.AT_LEAST
else
habit.targetType = NumericalHabitType.AT_MOST
habit.unit = unitInput.text.trim().toString()
}
habit.type = habitType

View File

@@ -36,6 +36,7 @@ import dagger.Lazy
import org.isoron.uhabits.R
import org.isoron.uhabits.activities.common.views.BundleSavedState
import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.models.NumericalHabitType
import org.isoron.uhabits.inject.ActivityContext
import javax.inject.Inject
@@ -97,7 +98,13 @@ class HabitCardListView(
cardView.dataOffset = dataOffset
cardView.score = score
cardView.unit = habit.unit
cardView.threshold = habit.targetValue / habit.frequency.denominator
if (habit.targetType == NumericalHabitType.AT_LEAST) {
cardView.higherThreshold = habit.targetValue / habit.frequency.denominator
cardView.lowerThreshold = 0.0
} else {
cardView.higherThreshold = (habit.targetValue * 2) / habit.frequency.denominator
cardView.lowerThreshold = habit.targetValue / habit.frequency.denominator
}
val detector = GestureDetector(context, CardViewGestureDetector(holder))
cardView.setOnTouchListener { _, ev ->

View File

@@ -109,10 +109,16 @@ class HabitCardView(
numberPanel.values = values.map { it / 1000.0 }.toDoubleArray()
}
var threshold: Double
get() = numberPanel.threshold
var lowerThreshold: Double
get() = numberPanel.lowerThreshold
set(value) {
numberPanel.threshold = value
numberPanel.lowerThreshold = value
}
var higherThreshold: Double
get() = numberPanel.higherThreshold
set(value) {
numberPanel.higherThreshold = value
}
var checkmarkPanel: CheckmarkPanelView
@@ -236,7 +242,9 @@ class HabitCardView(
numberPanel.apply {
color = c
units = h.unit
threshold = h.targetValue
targetType = h.targetType
lowerThreshold = 0.0
higherThreshold = h.targetValue
visibility = when (h.isNumerical) {
true -> View.VISIBLE
false -> View.GONE

View File

@@ -29,13 +29,14 @@ import android.view.View
import android.view.View.OnClickListener
import android.view.View.OnLongClickListener
import org.isoron.uhabits.R
import org.isoron.uhabits.core.models.NumericalHabitType
import org.isoron.uhabits.core.preferences.Preferences
import org.isoron.uhabits.inject.ActivityContext
import org.isoron.uhabits.utils.InterfaceUtils.getDimension
import org.isoron.uhabits.utils.StyledResources
import org.isoron.uhabits.utils.dim
import org.isoron.uhabits.utils.getFontAwesome
import org.isoron.uhabits.utils.showMessage
import org.isoron.uhabits.utils.sres
import java.text.DecimalFormat
import javax.inject.Inject
@@ -82,7 +83,19 @@ class NumberButtonView(
invalidate()
}
var threshold = 0.0
var lowerThreshold = 0.0
set(value) {
field = value
invalidate()
}
var higherThreshold = 0.0
set(value) {
field = value
invalidate()
}
var targetType = NumericalHabitType.AT_LEAST
set(value) {
field = value
invalidate()
@@ -127,7 +140,6 @@ class NumberButtonView(
private val em: Float
private val rect: RectF = RectF()
private val sr = StyledResources(context)
private val lowContrast: Int
private val mediumContrast: Int
@@ -148,15 +160,23 @@ class NumberButtonView(
init {
em = pNumber.measureText("m")
lowContrast = sr.getColor(R.attr.contrast40)
mediumContrast = sr.getColor(R.attr.contrast60)
lowContrast = sres.getColor(R.attr.contrast40)
mediumContrast = sres.getColor(R.attr.contrast60)
}
fun draw(canvas: Canvas) {
val activeColor = when {
value <= 0.0 -> lowContrast
value < threshold -> mediumContrast
else -> color
var activeColor = if (targetType == NumericalHabitType.AT_LEAST) {
when {
value <= lowerThreshold -> lowContrast
value < higherThreshold -> mediumContrast
else -> color
}
} else {
when {
value >= higherThreshold || value < 0 -> lowContrast
value > lowerThreshold -> mediumContrast
else -> color
}
}
val label: String
@@ -175,7 +195,7 @@ class NumberButtonView(
textSize = dim(R.dimen.smallerTextSize)
}
else -> {
label = "0"
label = if (targetType == NumericalHabitType.AT_LEAST) "0" else "inf"
typeface = BOLD_TYPEFACE
textSize = dim(R.dimen.smallTextSize)
}

View File

@@ -20,6 +20,7 @@
package org.isoron.uhabits.activities.habits.list.views
import android.content.Context
import org.isoron.uhabits.core.models.NumericalHabitType
import org.isoron.uhabits.core.models.Timestamp
import org.isoron.uhabits.core.preferences.Preferences
import org.isoron.uhabits.core.utils.DateUtils
@@ -47,7 +48,19 @@ class NumberPanelView(
setupButtons()
}
var threshold = 0.0
var targetType = NumericalHabitType.AT_LEAST
set(value) {
field = value
setupButtons()
}
var lowerThreshold = 0.0
set(value) {
field = value
setupButtons()
}
var higherThreshold = 0.0
set(value) {
field = value
setupButtons()
@@ -84,7 +97,9 @@ class NumberPanelView(
else -> 0.0
}
button.color = color
button.threshold = threshold
button.targetType = targetType
button.lowerThreshold = lowerThreshold
button.higherThreshold = higherThreshold
button.units = units
button.onEdit = { onEdit(timestamp) }
}

View File

@@ -167,6 +167,29 @@
android:hint="@string/measurable_units_example"/>
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="@+id/targetTypeOuterBox"
style="@style/FormOuterBox">
<LinearLayout style="@style/FormInnerBox">
<TextView
style="@style/FormLabel"
android:text="@string/target_type" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton android:id="@+id/targetTypeAtLeast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/target_type_at_least"
android:checked="true"/>
<RadioButton android:id="@+id/targetTypeAtMost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/target_type_at_most"/>
</RadioGroup>
</LinearLayout>
</FrameLayout>
<LinearLayout
android:id="@+id/targetOuterBox"
android:layout_width="match_parent"

View File

@@ -184,6 +184,9 @@
<string name="change_value">Change value</string>
<string name="calendar">Calendar</string>
<string name="unit">Unit</string>
<string name="target_type">Target Type</string>
<string name="target_type_at_least">At Least</string>
<string name="target_type_at_most">At Most</string>
<string name="example_question_boolean">e.g. Did you exercise today?</string>
<string name="question">Question</string>
<string name="target">Target</string>