mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Add notes to DelayedToggle, make delay skipable
This commit is contained in:
@@ -43,7 +43,7 @@ class EntryButtonViewTest : BaseViewTest() {
|
|||||||
view = component.getEntryButtonViewFactory().create().apply {
|
view = component.getEntryButtonViewFactory().create().apply {
|
||||||
value = Entry.NO
|
value = Entry.NO
|
||||||
color = PaletteUtils.getAndroidTestColor(5)
|
color = PaletteUtils.getAndroidTestColor(5)
|
||||||
onToggle = { toggled = true }
|
onToggle = { _, _, _ -> toggled = true }
|
||||||
}
|
}
|
||||||
measureView(view, dpToPixels(48), dpToPixels(48))
|
measureView(view, dpToPixels(48), dpToPixels(48))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ class EntryPanelViewTest : BaseViewTest() {
|
|||||||
@Test
|
@Test
|
||||||
fun testToggle() {
|
fun testToggle() {
|
||||||
val timestamps = mutableListOf<Timestamp>()
|
val timestamps = mutableListOf<Timestamp>()
|
||||||
view.onToggle = { t, _ -> timestamps.add(t) }
|
view.onToggle = { t, _, _, _ -> timestamps.add(t) }
|
||||||
view.buttons[0].performLongClick()
|
view.buttons[0].performLongClick()
|
||||||
view.buttons[2].performLongClick()
|
view.buttons[2].performLongClick()
|
||||||
view.buttons[3].performLongClick()
|
view.buttons[3].performLongClick()
|
||||||
@@ -88,7 +88,7 @@ class EntryPanelViewTest : BaseViewTest() {
|
|||||||
fun testToggle_withOffset() {
|
fun testToggle_withOffset() {
|
||||||
val timestamps = mutableListOf<Timestamp>()
|
val timestamps = mutableListOf<Timestamp>()
|
||||||
view.dataOffset = 3
|
view.dataOffset = 3
|
||||||
view.onToggle = { t, _ -> timestamps += t }
|
view.onToggle = { t, _, _, _ -> timestamps += t }
|
||||||
view.buttons[0].performLongClick()
|
view.buttons[0].performLongClick()
|
||||||
view.buttons[2].performLongClick()
|
view.buttons[2].performLongClick()
|
||||||
view.buttons[3].performLongClick()
|
view.buttons[3].performLongClick()
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ import org.isoron.uhabits.utils.sres
|
|||||||
import org.isoron.uhabits.utils.toMeasureSpec
|
import org.isoron.uhabits.utils.toMeasureSpec
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
const val TOGGLE_DELAY_MILLIS = 2000L
|
||||||
|
|
||||||
class CheckmarkButtonViewFactory
|
class CheckmarkButtonViewFactory
|
||||||
@Inject constructor(
|
@Inject constructor(
|
||||||
@ActivityContext val context: Context,
|
@ActivityContext val context: Context,
|
||||||
@@ -77,7 +79,7 @@ class CheckmarkButtonView(
|
|||||||
invalidate()
|
invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
var onToggle: (Int) -> Unit = {}
|
var onToggle: (Int, String, Long) -> Unit = { _, _, _ -> }
|
||||||
|
|
||||||
var onEdit: () -> Unit = {}
|
var onEdit: () -> Unit = {}
|
||||||
private var drawer = Drawer()
|
private var drawer = Drawer()
|
||||||
@@ -87,25 +89,25 @@ class CheckmarkButtonView(
|
|||||||
setOnLongClickListener(this)
|
setOnLongClickListener(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun performToggle() {
|
fun performToggle(delay: Long) {
|
||||||
value = Entry.nextToggleValue(
|
value = Entry.nextToggleValue(
|
||||||
value = value,
|
value = value,
|
||||||
isSkipEnabled = preferences.isSkipEnabled,
|
isSkipEnabled = preferences.isSkipEnabled,
|
||||||
areQuestionMarksEnabled = preferences.areQuestionMarksEnabled
|
areQuestionMarksEnabled = preferences.areQuestionMarksEnabled
|
||||||
)
|
)
|
||||||
onToggle(value)
|
onToggle(value, notes, delay)
|
||||||
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
|
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
|
||||||
invalidate()
|
invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onClick(v: View) {
|
override fun onClick(v: View) {
|
||||||
if (preferences.isShortToggleEnabled) performToggle()
|
if (preferences.isShortToggleEnabled) performToggle(TOGGLE_DELAY_MILLIS)
|
||||||
else onEdit()
|
else onEdit()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onLongClick(v: View): Boolean {
|
override fun onLongClick(v: View): Boolean {
|
||||||
if (preferences.isShortToggleEnabled) onEdit()
|
if (preferences.isShortToggleEnabled) onEdit()
|
||||||
else performToggle()
|
else performToggle(TOGGLE_DELAY_MILLIS)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ class CheckmarkPanelView(
|
|||||||
setupButtons()
|
setupButtons()
|
||||||
}
|
}
|
||||||
|
|
||||||
var onToggle: (Timestamp, Int) -> Unit = { _, _ -> }
|
var onToggle: (Timestamp, Int, String, Long) -> Unit = { _, _, _, _ -> }
|
||||||
set(value) {
|
set(value) {
|
||||||
field = value
|
field = value
|
||||||
setupButtons()
|
setupButtons()
|
||||||
@@ -89,7 +89,7 @@ class CheckmarkPanelView(
|
|||||||
else -> ""
|
else -> ""
|
||||||
}
|
}
|
||||||
button.color = color
|
button.color = color
|
||||||
button.onToggle = { value -> onToggle(timestamp, value) }
|
button.onToggle = { value, notes, delay -> onToggle(timestamp, value, notes, delay) }
|
||||||
button.onEdit = { onEdit(timestamp) }
|
button.onEdit = { onEdit(timestamp) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,7 +60,8 @@ class HabitCardViewFactory
|
|||||||
data class DelayedToggle(
|
data class DelayedToggle(
|
||||||
var habit: Habit,
|
var habit: Habit,
|
||||||
var timestamp: Timestamp,
|
var timestamp: Timestamp,
|
||||||
var value: Int
|
var value: Int,
|
||||||
|
var notes: String
|
||||||
)
|
)
|
||||||
|
|
||||||
class HabitCardView(
|
class HabitCardView(
|
||||||
@@ -159,11 +160,11 @@ class HabitCardView(
|
|||||||
}
|
}
|
||||||
|
|
||||||
checkmarkPanel = checkmarkPanelFactory.create().apply {
|
checkmarkPanel = checkmarkPanelFactory.create().apply {
|
||||||
onToggle = { timestamp, value ->
|
onToggle = { timestamp, value, notes, delay ->
|
||||||
triggerRipple(timestamp)
|
if (delay > 0) triggerRipple(timestamp)
|
||||||
habit?.let {
|
habit?.let {
|
||||||
val taskId = queueToggle(it, timestamp, value);
|
val taskId = queueToggle(it, timestamp, value, notes);
|
||||||
{ runPendingToggles(taskId) }.delay(TOGGLE_DELAY_MILLIS)
|
{ runPendingToggles(taskId) }.delay(delay)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
onEdit = { timestamp ->
|
onEdit = { timestamp ->
|
||||||
@@ -207,7 +208,7 @@ class HabitCardView(
|
|||||||
@Synchronized
|
@Synchronized
|
||||||
private fun runPendingToggles(id: Int) {
|
private fun runPendingToggles(id: Int) {
|
||||||
if (currentToggleTaskId != id) return
|
if (currentToggleTaskId != id) return
|
||||||
for ((h, t, v) in queuedToggles) behavior.onToggle(h, t, v)
|
for ((h, t, v, n) in queuedToggles) behavior.onToggle(h, t, v, n)
|
||||||
queuedToggles.clear()
|
queuedToggles.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,10 +216,11 @@ class HabitCardView(
|
|||||||
private fun queueToggle(
|
private fun queueToggle(
|
||||||
it: Habit,
|
it: Habit,
|
||||||
timestamp: Timestamp,
|
timestamp: Timestamp,
|
||||||
value: Int
|
value: Int,
|
||||||
|
notes: String,
|
||||||
): Int {
|
): Int {
|
||||||
currentToggleTaskId += 1
|
currentToggleTaskId += 1
|
||||||
queuedToggles.add(DelayedToggle(it, timestamp, value))
|
queuedToggles.add(DelayedToggle(it, timestamp, value, notes))
|
||||||
return currentToggleTaskId
|
return currentToggleTaskId
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -308,8 +310,6 @@ class HabitCardView(
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val TOGGLE_DELAY_MILLIS = 1000L
|
|
||||||
|
|
||||||
fun (() -> Unit).delay(delayInMillis: Long) {
|
fun (() -> Unit).delay(delayInMillis: Long) {
|
||||||
Handler(Looper.getMainLooper()).postDelayed(this, delayInMillis)
|
Handler(Looper.getMainLooper()).postDelayed(this, delayInMillis)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -123,8 +123,7 @@ open class ListHabitsBehavior @Inject constructor(
|
|||||||
if (prefs.isFirstRun) onFirstRun()
|
if (prefs.isFirstRun) onFirstRun()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onToggle(habit: Habit, timestamp: Timestamp?, value: Int) {
|
fun onToggle(habit: Habit, timestamp: Timestamp, value: Int, notes: String) {
|
||||||
val notes = habit.computedEntries.get(timestamp!!).notes
|
|
||||||
commandRunner.run(
|
commandRunner.run(
|
||||||
CreateRepetitionCommand(habitList, habit, timestamp, value, notes)
|
CreateRepetitionCommand(habitList, habit, timestamp, value, notes)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -168,7 +168,12 @@ class ListHabitsBehaviorTest : BaseUnitTest() {
|
|||||||
@Test
|
@Test
|
||||||
fun testOnToggle() {
|
fun testOnToggle() {
|
||||||
assertTrue(habit1.isCompletedToday())
|
assertTrue(habit1.isCompletedToday())
|
||||||
behavior.onToggle(habit1, getToday(), Entry.NO)
|
behavior.onToggle(
|
||||||
|
habit = habit1,
|
||||||
|
timestamp = getToday(),
|
||||||
|
value = Entry.NO,
|
||||||
|
notes = ""
|
||||||
|
)
|
||||||
assertFalse(habit1.isCompletedToday())
|
assertFalse(habit1.isCompletedToday())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user