Skip to content

Commit

Permalink
Cycle through through all checkmark states
Browse files Browse the repository at this point in the history
  • Loading branch information
iSoron committed Aug 22, 2021
1 parent 79f5b8b commit 57f5f6e
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright (C) 2016-2021 Álinson Santos Xavier <[email protected]>
*
* 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 <http://www.gnu.org/licenses/>.
*/

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<Int, Int>,
isSkipEnabled: Boolean,
areQuestionMarksEnabled: Boolean
) {
for ((value, expected) in map) {
assertEquals(
nextToggleValue(
value = value,
isSkipEnabled = isSkipEnabled,
areQuestionMarksEnabled = areQuestionMarksEnabled,
),
expected,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 57f5f6e

Please sign in to comment.