mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Convert Repetition to Kotlin
This commit is contained in:
@@ -158,9 +158,9 @@ public class HistoryChart extends ScrollableChart
|
||||
if (offset < checkmarks.length)
|
||||
{
|
||||
if(skipsEnabled)
|
||||
newValue = Repetition.nextToggleValueWithSkip(checkmarks[offset]);
|
||||
newValue = Checkmark.Companion.nextToggleValueWithSkip(checkmarks[offset]);
|
||||
else
|
||||
newValue = Repetition.nextToggleValueWithoutSkip(checkmarks[offset]);
|
||||
newValue = Checkmark.Companion.nextToggleValueWithoutSkip(checkmarks[offset]);
|
||||
}
|
||||
|
||||
onToggleCheckmarkListener.onToggleCheckmark(timestamp, newValue);
|
||||
|
||||
@@ -66,9 +66,9 @@ class CheckmarkButtonView(
|
||||
|
||||
fun performToggle() {
|
||||
value = if(preferences.isSkipEnabled) {
|
||||
Repetition.nextToggleValueWithSkip(value)
|
||||
Checkmark.nextToggleValueWithSkip(value)
|
||||
} else {
|
||||
Repetition.nextToggleValueWithoutSkip(value)
|
||||
Checkmark.nextToggleValueWithoutSkip(value)
|
||||
}
|
||||
onToggle(value)
|
||||
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
|
||||
|
||||
@@ -59,5 +59,22 @@ data class Checkmark(
|
||||
* Checkmark value indicating that no data is available for the given timestamp.
|
||||
*/
|
||||
const val UNKNOWN = -1
|
||||
|
||||
fun nextToggleValueWithSkip(value: Int): Int {
|
||||
return when (value) {
|
||||
NO, UNKNOWN, YES_AUTO -> YES_MANUAL
|
||||
YES_MANUAL -> SKIP
|
||||
SKIP -> NO
|
||||
else -> NO
|
||||
}
|
||||
}
|
||||
|
||||
fun nextToggleValueWithoutSkip(value: Int): Int {
|
||||
return when (value) {
|
||||
NO, UNKNOWN, YES_AUTO -> YES_MANUAL
|
||||
else -> NO
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,128 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* 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.apache.commons.lang3.builder.*;
|
||||
|
||||
import static org.isoron.uhabits.core.models.Checkmark.*;
|
||||
import static org.isoron.uhabits.core.utils.StringUtils.defaultToStringStyle;
|
||||
|
||||
/**
|
||||
* Represents a record that the user has performed or skipped a certain habit at a certain
|
||||
* date.
|
||||
*/
|
||||
public final class Repetition
|
||||
{
|
||||
|
||||
private final Timestamp timestamp;
|
||||
|
||||
/**
|
||||
* The value of the repetition.
|
||||
*
|
||||
* For boolean habits, this equals YES_MANUAL if performed or SKIP if skipped.
|
||||
* For numerical habits, this number is stored in thousandths. That is, if the user enters
|
||||
* value 1.50 on the app, it is here stored as 1500.
|
||||
*/
|
||||
private final int value;
|
||||
|
||||
/**
|
||||
* Creates a new repetition with given parameters.
|
||||
* <p>
|
||||
* The timestamp corresponds to the days this repetition occurred. Time of
|
||||
* day must be midnight (UTC).
|
||||
*
|
||||
* @param timestamp the time this repetition occurred.
|
||||
*/
|
||||
public Repetition(Timestamp timestamp, int value)
|
||||
{
|
||||
this.timestamp = timestamp;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static int nextToggleValueWithSkip(int value)
|
||||
{
|
||||
switch(value) {
|
||||
case NO:
|
||||
case UNKNOWN:
|
||||
case YES_AUTO:
|
||||
return YES_MANUAL;
|
||||
case YES_MANUAL:
|
||||
return SKIP;
|
||||
default:
|
||||
case SKIP:
|
||||
return NO;
|
||||
}
|
||||
}
|
||||
|
||||
public static int nextToggleValueWithoutSkip(int value)
|
||||
{
|
||||
switch(value) {
|
||||
case NO:
|
||||
case UNKNOWN:
|
||||
case YES_AUTO:
|
||||
return YES_MANUAL;
|
||||
default:
|
||||
return NO;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Repetition that = (Repetition) o;
|
||||
|
||||
return new EqualsBuilder()
|
||||
.append(timestamp, that.timestamp)
|
||||
.append(value, that.value)
|
||||
.isEquals();
|
||||
}
|
||||
|
||||
public Timestamp getTimestamp()
|
||||
{
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public int getValue()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return new HashCodeBuilder(17, 37)
|
||||
.append(timestamp)
|
||||
.append(value)
|
||||
.toHashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return new ToStringBuilder(this, defaultToStringStyle())
|
||||
.append("timestamp", timestamp)
|
||||
.append("value", value)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* 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
|
||||
|
||||
data class Repetition(
|
||||
/**
|
||||
* Time the repetition took place.
|
||||
*/
|
||||
val timestamp: Timestamp,
|
||||
/**
|
||||
* For boolean habits, [value] equals YES_MANUAL if performed or SKIP if skipped.
|
||||
* For numerical habits, this number is stored in thousandths. That is, if the user enters
|
||||
* value 1.50 on the app, it is here stored as 1500.
|
||||
*/
|
||||
val value: Int,
|
||||
);
|
||||
@@ -73,9 +73,9 @@ public class WidgetBehavior
|
||||
int currentValue = habit.getRepetitions().getValue(timestamp);
|
||||
int newValue;
|
||||
if(preferences.isSkipEnabled())
|
||||
newValue = Repetition.nextToggleValueWithSkip(currentValue);
|
||||
newValue = Checkmark.Companion.nextToggleValueWithSkip(currentValue);
|
||||
else
|
||||
newValue = Repetition.nextToggleValueWithoutSkip(currentValue);
|
||||
newValue = Checkmark.Companion.nextToggleValueWithoutSkip(currentValue);
|
||||
setValue(habit, timestamp, newValue);
|
||||
notificationTray.cancel(habit);
|
||||
}
|
||||
|
||||
@@ -161,11 +161,4 @@ public class RepetitionListTest extends BaseUnitTest
|
||||
verify(listener, times(2)).onModelChange();
|
||||
reset(listener);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() throws Exception
|
||||
{
|
||||
Repetition rep = new Repetition(Timestamp.ZERO.plus(100), 20);
|
||||
assertThat(rep.toString(), equalTo("{timestamp: 1970-04-11, value: 20}"));
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,6 @@ import org.junit.*;
|
||||
import java.util.*;
|
||||
|
||||
import static org.isoron.uhabits.core.models.Checkmark.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class WidgetBehaviorTest extends BaseUnitTest
|
||||
@@ -89,8 +88,8 @@ public class WidgetBehaviorTest extends BaseUnitTest
|
||||
when(preferences.isSkipEnabled()).thenReturn(skipEnabled);
|
||||
|
||||
int nextValue;
|
||||
if(skipEnabled) nextValue = Repetition.nextToggleValueWithSkip(currentValue);
|
||||
else nextValue = Repetition.nextToggleValueWithoutSkip(currentValue);
|
||||
if(skipEnabled) nextValue = Checkmark.Companion.nextToggleValueWithSkip(currentValue);
|
||||
else nextValue = Checkmark.Companion.nextToggleValueWithoutSkip(currentValue);
|
||||
|
||||
habit.getRepetitions().setValue(timestamp, currentValue);
|
||||
behavior.onToggleRepetition(habit, timestamp);
|
||||
|
||||
Reference in New Issue
Block a user