From 57f5f6ed5bda3d3d6d23b7cd597516cde3ebcf5c Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Sun, 22 Aug 2021 06:23:06 -0500 Subject: [PATCH] Cycle through through all checkmark states --- .../habits/list/views/CheckmarkButtonView.kt | 10 +- .../org/isoron/uhabits/core/models/Entry.kt | 21 ++-- .../screens/habits/show/views/HistoryCard.kt | 10 +- .../uhabits/core/ui/widgets/WidgetBehavior.kt | 15 ++- .../isoron/uhabits/core/models/EntryTest.kt | 96 +++++++++++++++++++ .../core/ui/widgets/WidgetBehaviorTest.kt | 12 ++- 6 files changed, 129 insertions(+), 35 deletions(-) create mode 100644 uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/EntryTest.kt diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/views/CheckmarkButtonView.kt b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/views/CheckmarkButtonView.kt index e11d23868..339cc46b4 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/views/CheckmarkButtonView.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/views/CheckmarkButtonView.kt @@ -81,11 +81,11 @@ class CheckmarkButtonView( } fun performToggle() { - value = if (preferences.isSkipEnabled) { - Entry.nextToggleValueWithSkip(value) - } else { - Entry.nextToggleValueWithoutSkip(value) - } + value = Entry.nextToggleValue( + value = value, + isSkipEnabled = preferences.isSkipEnabled, + areQuestionMarksEnabled = preferences.areQuestionMarksEnabled + ) onToggle(value) performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) invalidate() diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/Entry.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/Entry.kt index a0fbc4b3b..e7aefb8a6 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/Entry.kt +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/Entry.kt @@ -50,19 +50,18 @@ data class Entry( */ const val UNKNOWN = -1 - fun nextToggleValueWithSkip(value: Int): Int { + fun nextToggleValue( + value: Int, + isSkipEnabled: Boolean, + areQuestionMarksEnabled: Boolean + ): Int { return when (value) { - NO, UNKNOWN, YES_AUTO -> YES_MANUAL - YES_MANUAL -> SKIP + YES_AUTO -> YES_MANUAL + YES_MANUAL -> if (isSkipEnabled) SKIP else NO SKIP -> NO - else -> NO - } - } - - fun nextToggleValueWithoutSkip(value: Int): Int { - return when (value) { - NO, UNKNOWN, YES_AUTO -> YES_MANUAL - else -> NO + NO -> if (areQuestionMarksEnabled) UNKNOWN else YES_MANUAL + UNKNOWN -> YES_MANUAL + else -> YES_MANUAL } } } diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/screens/habits/show/views/HistoryCard.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/screens/habits/show/views/HistoryCard.kt index 5ef58520e..bd201e0c5 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/screens/habits/show/views/HistoryCard.kt +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/screens/habits/show/views/HistoryCard.kt @@ -75,11 +75,11 @@ class HistoryCardPresenter( } } else { val currentValue = habit.computedEntries.get(timestamp).value - val nextValue = if (preferences.isSkipEnabled) { - Entry.nextToggleValueWithSkip(currentValue) - } else { - Entry.nextToggleValueWithoutSkip(currentValue) - } + val nextValue = Entry.nextToggleValue( + value = currentValue, + isSkipEnabled = preferences.isSkipEnabled, + areQuestionMarksEnabled = preferences.areQuestionMarksEnabled + ) commandRunner.run( CreateRepetitionCommand( habitList, diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/widgets/WidgetBehavior.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/widgets/WidgetBehavior.kt index 2350bde85..164ca04f3 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/widgets/WidgetBehavior.kt +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/ui/widgets/WidgetBehavior.kt @@ -21,8 +21,7 @@ package org.isoron.uhabits.core.ui.widgets import org.isoron.uhabits.core.commands.CommandRunner import org.isoron.uhabits.core.commands.CreateRepetitionCommand import org.isoron.uhabits.core.models.Entry -import org.isoron.uhabits.core.models.Entry.Companion.nextToggleValueWithSkip -import org.isoron.uhabits.core.models.Entry.Companion.nextToggleValueWithoutSkip +import org.isoron.uhabits.core.models.Entry.Companion.nextToggleValue import org.isoron.uhabits.core.models.Habit import org.isoron.uhabits.core.models.HabitList import org.isoron.uhabits.core.models.Timestamp @@ -48,13 +47,11 @@ class WidgetBehavior @Inject constructor( fun onToggleRepetition(habit: Habit, timestamp: Timestamp) { val currentValue = habit.originalEntries.get(timestamp).value - val newValue: Int - newValue = - if (preferences.isSkipEnabled) nextToggleValueWithSkip( - currentValue - ) else nextToggleValueWithoutSkip( - currentValue - ) + val newValue = nextToggleValue( + value = currentValue, + isSkipEnabled = preferences.isSkipEnabled, + areQuestionMarksEnabled = preferences.areQuestionMarksEnabled + ) setValue(habit, timestamp, newValue) notificationTray.cancel(habit) } diff --git a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/EntryTest.kt b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/EntryTest.kt new file mode 100644 index 000000000..0f79969a6 --- /dev/null +++ b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/models/EntryTest.kt @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2016-2021 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.core.models + +import org.isoron.uhabits.core.models.Entry.Companion.NO +import org.isoron.uhabits.core.models.Entry.Companion.SKIP +import org.isoron.uhabits.core.models.Entry.Companion.UNKNOWN +import org.isoron.uhabits.core.models.Entry.Companion.YES_AUTO +import org.isoron.uhabits.core.models.Entry.Companion.YES_MANUAL +import org.isoron.uhabits.core.models.Entry.Companion.nextToggleValue +import org.junit.Test +import kotlin.test.assertEquals + +class EntryTest { + @Test + fun testNextValue() { + check( + mapOf( + YES_AUTO to YES_MANUAL, + YES_MANUAL to SKIP, + SKIP to NO, + NO to UNKNOWN, + UNKNOWN to YES_MANUAL, + ), + isSkipEnabled = true, + areQuestionMarksEnabled = true, + ) + check( + mapOf( + YES_AUTO to YES_MANUAL, + YES_MANUAL to NO, + SKIP to NO, + NO to UNKNOWN, + UNKNOWN to YES_MANUAL, + ), + isSkipEnabled = false, + areQuestionMarksEnabled = true, + ) + check( + mapOf( + YES_AUTO to YES_MANUAL, + YES_MANUAL to SKIP, + SKIP to NO, + NO to YES_MANUAL, + UNKNOWN to YES_MANUAL, + ), + isSkipEnabled = true, + areQuestionMarksEnabled = false, + ) + check( + mapOf( + YES_AUTO to YES_MANUAL, + YES_MANUAL to NO, + SKIP to NO, + NO to YES_MANUAL, + UNKNOWN to YES_MANUAL, + ), + isSkipEnabled = false, + areQuestionMarksEnabled = false, + ) + } + + private fun check( + map: Map, + isSkipEnabled: Boolean, + areQuestionMarksEnabled: Boolean + ) { + for ((value, expected) in map) { + assertEquals( + nextToggleValue( + value = value, + isSkipEnabled = isSkipEnabled, + areQuestionMarksEnabled = areQuestionMarksEnabled, + ), + expected, + ) + } + } +} diff --git a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/ui/widgets/WidgetBehaviorTest.kt b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/ui/widgets/WidgetBehaviorTest.kt index e02e5e043..18a06c8da 100644 --- a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/ui/widgets/WidgetBehaviorTest.kt +++ b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/ui/widgets/WidgetBehaviorTest.kt @@ -26,8 +26,7 @@ import com.nhaarman.mockitokotlin2.whenever import org.isoron.uhabits.core.BaseUnitTest import org.isoron.uhabits.core.commands.CreateRepetitionCommand import org.isoron.uhabits.core.models.Entry -import org.isoron.uhabits.core.models.Entry.Companion.nextToggleValueWithSkip -import org.isoron.uhabits.core.models.Entry.Companion.nextToggleValueWithoutSkip +import org.isoron.uhabits.core.models.Entry.Companion.nextToggleValue import org.isoron.uhabits.core.models.Habit import org.isoron.uhabits.core.models.Timestamp import org.isoron.uhabits.core.preferences.Preferences @@ -42,6 +41,7 @@ class WidgetBehaviorTest : BaseUnitTest() { private lateinit var behavior: WidgetBehavior private lateinit var habit: Habit private lateinit var today: Timestamp + @Before @Throws(Exception::class) override fun setUp() { @@ -81,12 +81,14 @@ class WidgetBehaviorTest : BaseUnitTest() { Entry.NO, Entry.YES_MANUAL, Entry.YES_AUTO, - Entry.SKIP + Entry.SKIP, ) ) { whenever(preferences.isSkipEnabled).thenReturn(skipEnabled) - val nextValue: Int = if (skipEnabled) nextToggleValueWithSkip(currentValue) else nextToggleValueWithoutSkip( - currentValue + val nextValue: Int = nextToggleValue( + currentValue, + isSkipEnabled = skipEnabled, + areQuestionMarksEnabled = false ) habit.originalEntries.add(Entry(today, currentValue)) behavior.onToggleRepetition(habit, today)