Repetition: Replace toggle by setValue

This commit is contained in:
2020-11-26 13:29:12 -06:00
parent d45281d137
commit f97fed3b9b
12 changed files with 96 additions and 139 deletions

View File

@@ -31,6 +31,8 @@ import java.util.*;
import javax.inject.*;
import static org.isoron.uhabits.core.models.Checkmark.*;
/**
* Class that imports data from HabitBull CSV files.
@@ -93,8 +95,7 @@ public class HabitBullCSVImporter extends AbstractImporter
map.put(name, h);
}
if (!h.getRepetitions().containsTimestamp(timestamp))
h.getRepetitions().toggle(timestamp);
h.getRepetitions().setValue(timestamp, YES_MANUAL);
}
}
}

View File

@@ -109,7 +109,7 @@ public class LoopDBImporter extends AbstractImporter
habitRecord.id.toString());
for (RepetitionRecord r : reps)
h.getRepetitions().toggle(new Timestamp(r.timestamp), r.value);
h.getRepetitions().setValue(new Timestamp(r.timestamp), r.value);
}
}
}

View File

@@ -30,6 +30,8 @@ import java.util.*;
import javax.inject.*;
import static org.isoron.uhabits.core.models.Checkmark.*;
/**
* Class that imports database files exported by Rewire.
*/
@@ -165,7 +167,7 @@ public class RewireDBImporter extends AbstractImporter
GregorianCalendar cal = DateUtils.getStartOfTodayCalendar();
cal.set(year, month - 1, day);
habit.getRepetitions().toggle(new Timestamp(cal));
habit.getRepetitions().setValue(new Timestamp(cal), YES_MANUAL);
} while (c.moveToNext());
}
finally

View File

@@ -30,6 +30,8 @@ import java.util.*;
import javax.inject.*;
import static org.isoron.uhabits.core.models.Checkmark.*;
/**
* Class that imports data from database files exported by Tickmate.
*/
@@ -100,7 +102,7 @@ public class TickmateDBImporter extends AbstractImporter
GregorianCalendar cal = DateUtils.getStartOfTodayCalendar();
cal.set(year, month, day);
habit.getRepetitions().toggle(new Timestamp(cal));
habit.getRepetitions().setValue(new Timestamp(cal), YES_MANUAL);
} while (c.moveToNext());
}
finally

View File

@@ -52,19 +52,6 @@ public abstract class RepetitionList
*/
public abstract void add(Repetition repetition);
/**
* Returns true if the list contains a repetition that has the given
* timestamp.
*
* @param timestamp the timestamp to find.
* @return true if list contains repetition with given timestamp, false
* otherwise.
*/
public boolean containsTimestamp(Timestamp timestamp)
{
return (getByTimestamp(timestamp) != null);
}
/**
* Returns the list of repetitions that happened within the given time
* interval.
@@ -90,6 +77,18 @@ public abstract class RepetitionList
@Nullable
public abstract Repetition getByTimestamp(Timestamp timestamp);
/**
* If a repetition with the given timestamp exists, return its value. Otherwise, returns
* Checkmark.NO for boolean habits and zero for numerical habits.
*/
@NonNull
public int getValue(Timestamp timestamp)
{
Repetition rep = getByTimestamp(timestamp);
if (rep == null) return Checkmark.NO;
return rep.getValue();
}
@NonNull
public ModelObservable getObservable()
{
@@ -175,39 +174,9 @@ public abstract class RepetitionList
*/
public abstract void remove(@NonNull Repetition repetition);
/**
* Adds or remove a repetition at a certain timestamp.
* <p>
* If there exists a repetition on the list with the given timestamp, the
* method removes this repetition from the list and returns it. If there are
* no repetitions with the given timestamp, creates and adds one to the
* list, then returns it.
*
* @param timestamp the timestamp for the timestamp that should be added or
* removed.
* @return the repetition that has been added or removed.
*/
@NonNull
public synchronized Repetition toggle(Timestamp timestamp)
{
if (habit.isNumerical())
throw new IllegalStateException("habit must NOT be numerical");
Repetition rep = getByTimestamp(timestamp);
if (rep != null) remove(rep);
else
{
rep = new Repetition(timestamp, Checkmark.YES_MANUAL);
add(rep);
}
habit.invalidateNewerThan(timestamp);
return rep;
}
public abstract long getTotalCount();
public void toggle(Timestamp timestamp, int value)
public void setValue(Timestamp timestamp, int value)
{
Repetition rep = getByTimestamp(timestamp);
if (rep != null) remove(rep);

View File

@@ -23,6 +23,8 @@ import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.models.sqlite.*;
import org.isoron.uhabits.core.utils.*;
import static org.isoron.uhabits.core.models.Checkmark.*;
public class HabitFixtures
{
public boolean NON_DAILY_HABIT_CHECKS[] = {
@@ -63,7 +65,7 @@ public class HabitFixtures
81, 83, 89, 90, 91, 95, 102, 103, 108, 109, 120};
for (int mark : marks)
habit.getRepetitions().toggle(today.minus(mark));
habit.getRepetitions().setValue(today.minus(mark), YES_MANUAL);
return habit;
}
@@ -140,7 +142,7 @@ public class HabitFixtures
Timestamp timestamp = DateUtils.getToday();
for (boolean c : NON_DAILY_HABIT_CHECKS)
{
if (c) habit.getRepetitions().toggle(timestamp);
if (c) habit.getRepetitions().setValue(timestamp, YES_MANUAL);
timestamp = timestamp.minus(1);
}