mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-08 01:58:52 -06:00
Added a notes field and implemented dialog for numeric habits
This commit is contained in:
@@ -47,6 +47,7 @@ class NumberPickerFactory
|
||||
fun create(
|
||||
value: Double,
|
||||
unit: String,
|
||||
notes: String,
|
||||
callback: ListHabitsBehavior.NumberPickerCallback
|
||||
): AlertDialog {
|
||||
val inflater = LayoutInflater.from(context)
|
||||
@@ -54,6 +55,7 @@ class NumberPickerFactory
|
||||
|
||||
val picker = view.findViewById<NumberPicker>(R.id.picker)
|
||||
val picker2 = view.findViewById<NumberPicker>(R.id.picker2)
|
||||
val etNotes = view.findViewById<EditText>(R.id.etNotes)
|
||||
|
||||
val watcherFilter: InputFilter = SeparatorWatcherInputFilter(picker2)
|
||||
val numberPickerInputText = getNumberPickerInputText(picker)
|
||||
@@ -77,13 +79,18 @@ class NumberPickerFactory
|
||||
picker2.setFormatter { v -> String.format("%02d", v) }
|
||||
picker2.value = intValue % 100
|
||||
|
||||
etNotes.setText(notes)
|
||||
val dialog = AlertDialog.Builder(context)
|
||||
.setView(view)
|
||||
.setTitle(R.string.change_value)
|
||||
.setPositiveButton(android.R.string.ok) { _, _ ->
|
||||
.setPositiveButton(R.string.save) { _, _ ->
|
||||
picker.clearFocus()
|
||||
val v = picker.value + 0.01 * picker2.value
|
||||
callback.onNumberPicked(v)
|
||||
val note = etNotes.text.toString()
|
||||
callback.onNumberPicked(v, note)
|
||||
}
|
||||
.setNegativeButton(R.string.cancel) { _, _ ->
|
||||
callback.onNumberPickerDismissed()
|
||||
}
|
||||
.setOnDismissListener {
|
||||
callback.onNumberPickerDismissed()
|
||||
|
||||
@@ -225,9 +225,10 @@ class ListHabitsScreen
|
||||
override fun showNumberPicker(
|
||||
value: Double,
|
||||
unit: String,
|
||||
notes: String,
|
||||
callback: ListHabitsBehavior.NumberPickerCallback
|
||||
) {
|
||||
numberPickerFactory.create(value, unit, callback).show()
|
||||
numberPickerFactory.create(value, unit, notes, callback).show()
|
||||
}
|
||||
|
||||
private fun getExecuteString(command: Command): String? {
|
||||
|
||||
@@ -71,6 +71,12 @@ class CheckmarkButtonView(
|
||||
invalidate()
|
||||
}
|
||||
|
||||
var hasNotes = false
|
||||
set(value) {
|
||||
field = value
|
||||
invalidate()
|
||||
}
|
||||
|
||||
var onToggle: (Int) -> Unit = {}
|
||||
private var drawer = Drawer()
|
||||
|
||||
|
||||
@@ -54,6 +54,12 @@ class CheckmarkPanelView(
|
||||
setupButtons()
|
||||
}
|
||||
|
||||
var notes = BooleanArray(0)
|
||||
set(values) {
|
||||
field = values
|
||||
setupButtons()
|
||||
}
|
||||
|
||||
var onToggle: (Timestamp, Int) -> Unit = { _, _ -> }
|
||||
set(value) {
|
||||
field = value
|
||||
@@ -72,6 +78,10 @@ class CheckmarkPanelView(
|
||||
index + dataOffset < values.size -> values[index + dataOffset]
|
||||
else -> UNKNOWN
|
||||
}
|
||||
button.hasNotes = when {
|
||||
index + dataOffset < notes.size -> notes[index + dataOffset]
|
||||
else -> false
|
||||
}
|
||||
button.color = color
|
||||
button.onToggle = { value -> onToggle(timestamp, value) }
|
||||
}
|
||||
|
||||
@@ -124,8 +124,9 @@ class HabitCardListAdapter @Inject constructor(
|
||||
val habit = cache.getHabitByPosition(position)
|
||||
val score = cache.getScore(habit!!.id!!)
|
||||
val checkmarks = cache.getCheckmarks(habit.id!!)
|
||||
val notesIndicators = cache.getNoteIndicators(habit.id!!)
|
||||
val selected = selected.contains(habit)
|
||||
listView!!.bindCardView(holder, habit, score, checkmarks, selected)
|
||||
listView!!.bindCardView(holder, habit, score, checkmarks, notesIndicators, selected)
|
||||
}
|
||||
|
||||
override fun onViewAttachedToWindow(holder: HabitCardViewHolder) {
|
||||
|
||||
@@ -87,6 +87,7 @@ class HabitCardListView(
|
||||
habit: Habit,
|
||||
score: Double,
|
||||
checkmarks: IntArray,
|
||||
notesIndicators: BooleanArray,
|
||||
selected: Boolean
|
||||
): View {
|
||||
val cardView = holder.itemView as HabitCardView
|
||||
@@ -98,6 +99,7 @@ class HabitCardListView(
|
||||
cardView.score = score
|
||||
cardView.unit = habit.unit
|
||||
cardView.threshold = habit.targetValue / habit.frequency.denominator
|
||||
cardView.notes = notesIndicators
|
||||
|
||||
val detector = GestureDetector(context, CardViewGestureDetector(holder))
|
||||
cardView.setOnTouchListener { _, ev ->
|
||||
|
||||
@@ -115,6 +115,13 @@ class HabitCardView(
|
||||
numberPanel.threshold = value
|
||||
}
|
||||
|
||||
var notes
|
||||
get() = numberPanel.notes
|
||||
set(values) {
|
||||
checkmarkPanel.notes = values
|
||||
numberPanel.notes = values
|
||||
}
|
||||
|
||||
var checkmarkPanel: CheckmarkPanelView
|
||||
private var numberPanel: NumberPanelView
|
||||
private var innerFrame: LinearLayout
|
||||
@@ -143,7 +150,7 @@ class HabitCardView(
|
||||
checkmarkPanel = checkmarkPanelFactory.create().apply {
|
||||
onToggle = { timestamp, value ->
|
||||
triggerRipple(timestamp)
|
||||
habit?.let { behavior.onToggle(it, timestamp, value) }
|
||||
habit?.let { behavior.onToggle(it, timestamp, value, "") }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -101,6 +101,11 @@ class NumberButtonView(
|
||||
field = value
|
||||
invalidate()
|
||||
}
|
||||
var hasNotes = false
|
||||
set(value) {
|
||||
field = value
|
||||
invalidate()
|
||||
}
|
||||
|
||||
var onEdit: () -> Unit = {}
|
||||
private var drawer: Drawer = Drawer(context)
|
||||
@@ -111,8 +116,7 @@ class NumberButtonView(
|
||||
}
|
||||
|
||||
override fun onClick(v: View) {
|
||||
if (preferences.isShortToggleEnabled) onEdit()
|
||||
else showMessage(resources.getString(R.string.long_press_to_edit))
|
||||
onEdit()
|
||||
}
|
||||
|
||||
override fun onLongClick(v: View): Boolean {
|
||||
@@ -153,6 +157,8 @@ class NumberButtonView(
|
||||
textAlign = Paint.Align.CENTER
|
||||
}
|
||||
|
||||
private val pNotesIndicator: Paint = Paint()
|
||||
|
||||
init {
|
||||
em = pNumber.measureText("m")
|
||||
lowContrast = sres.getColor(R.attr.contrast40)
|
||||
@@ -200,6 +206,7 @@ class NumberButtonView(
|
||||
pNumber.color = activeColor
|
||||
pNumber.typeface = typeface
|
||||
pUnit.color = activeColor
|
||||
pNotesIndicator.color = activeColor
|
||||
|
||||
if (units.isBlank()) {
|
||||
rect.set(0f, 0f, width.toFloat(), height.toFloat())
|
||||
@@ -211,6 +218,11 @@ class NumberButtonView(
|
||||
rect.offset(0f, 1.3f * em)
|
||||
canvas.drawText(units, rect.centerX(), rect.centerY(), pUnit)
|
||||
}
|
||||
|
||||
if (hasNotes) {
|
||||
val cy = 0.8f * em
|
||||
canvas.drawCircle(width.toFloat() - cy, cy, 8f, pNotesIndicator)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,6 +72,12 @@ class NumberPanelView(
|
||||
setupButtons()
|
||||
}
|
||||
|
||||
var notes = BooleanArray(0)
|
||||
set(values) {
|
||||
field = values
|
||||
setupButtons()
|
||||
}
|
||||
|
||||
var onEdit: (Timestamp) -> Unit = {}
|
||||
set(value) {
|
||||
field = value
|
||||
@@ -90,6 +96,10 @@ class NumberPanelView(
|
||||
index + dataOffset < values.size -> values[index + dataOffset]
|
||||
else -> 0.0
|
||||
}
|
||||
button.hasNotes = when {
|
||||
index + dataOffset < notes.size -> notes[index + dataOffset]
|
||||
else -> false
|
||||
}
|
||||
button.color = color
|
||||
button.targetType = targetType
|
||||
button.threshold = threshold
|
||||
|
||||
@@ -164,9 +164,10 @@ class ShowHabitActivity : AppCompatActivity(), CommandRunner.Listener {
|
||||
override fun showNumberPicker(
|
||||
value: Double,
|
||||
unit: String,
|
||||
notes: String,
|
||||
callback: ListHabitsBehavior.NumberPickerCallback,
|
||||
) {
|
||||
NumberPickerFactory(this@ShowHabitActivity).create(value, unit, callback).show()
|
||||
NumberPickerFactory(this@ShowHabitActivity).create(value, unit, notes, callback).show()
|
||||
}
|
||||
|
||||
override fun showEditHabitScreen(habit: Habit) {
|
||||
|
||||
@@ -60,8 +60,8 @@ class NumericalCheckmarkWidgetActivity : Activity(), ListHabitsBehavior.NumberPi
|
||||
SystemUtils.unlockScreen(this)
|
||||
}
|
||||
|
||||
override fun onNumberPicked(newValue: Double) {
|
||||
behavior.setValue(data.habit, data.timestamp, (newValue * 1000).toInt())
|
||||
override fun onNumberPicked(newValue: Double, notes: String) {
|
||||
behavior.setValue(data.habit, data.timestamp, (newValue * 1000).toInt(), notes)
|
||||
widgetUpdater.updateWidgets()
|
||||
finish()
|
||||
}
|
||||
@@ -79,6 +79,7 @@ class NumericalCheckmarkWidgetActivity : Activity(), ListHabitsBehavior.NumberPi
|
||||
numberPickerFactory.create(
|
||||
entry.value / 1000.0,
|
||||
data.habit.unit,
|
||||
entry.notes,
|
||||
this
|
||||
).show()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user