Remove calls to Repetition.add and Repetition.remove

pull/699/head
Alinson S. Xavier 5 years ago
parent f97fed3b9b
commit 61414d62f4

@ -128,8 +128,7 @@ public class HabitFixtures
Timestamp timestamp = DateUtils.getToday(); Timestamp timestamp = DateUtils.getToday();
for (int value : LONG_NUMERICAL_HABIT_CHECKS) for (int value : LONG_NUMERICAL_HABIT_CHECKS)
{ {
Repetition r = new Repetition(timestamp, value); habit.getRepetitions().setValue(timestamp, value);
habit.getRepetitions().add(r);
timestamp = timestamp.minus(1); timestamp = timestamp.minus(1);
} }

@ -58,7 +58,7 @@ class NumericalCheckmarkWidgetActivity : Activity(), ListHabitsBehavior.NumberPi
} }
override fun onNumberPicked(newValue: Double) { override fun onNumberPicked(newValue: Double) {
behavior.setNumericValue(data.habit, data.timestamp, (newValue * 1000).toInt()) behavior.setValue(data.habit, data.timestamp, (newValue * 1000).toInt())
widgetUpdater.updateWidgets() widgetUpdater.updateWidgets()
finish() finish()
} }

@ -23,6 +23,8 @@ import androidx.annotation.*;
import org.isoron.uhabits.core.models.*; import org.isoron.uhabits.core.models.*;
import java.util.*;
/** /**
* Command to toggle a repetition. * Command to toggle a repetition.
*/ */
@ -31,20 +33,19 @@ public class CreateRepetitionCommand extends Command
@NonNull @NonNull
final Habit habit; final Habit habit;
private HabitList habitList; @NonNull
final HabitList habitList;
@NonNull
final Timestamp timestamp; final Timestamp timestamp;
final int value; final int value;
@Nullable int previousValue;
Repetition previousRep;
@Nullable
Repetition newRep;
public CreateRepetitionCommand(@NonNull HabitList habitList, public CreateRepetitionCommand(@NonNull HabitList habitList,
@NonNull Habit habit, @NonNull Habit habit,
Timestamp timestamp, @NonNull Timestamp timestamp,
int value) int value)
{ {
this.habitList = habitList; this.habitList = habitList;
@ -57,18 +58,8 @@ public class CreateRepetitionCommand extends Command
public void execute() public void execute()
{ {
RepetitionList reps = habit.getRepetitions(); RepetitionList reps = habit.getRepetitions();
previousValue = reps.getValue(timestamp);
previousRep = reps.getByTimestamp(timestamp); reps.setValue(timestamp, value);
if (previousRep != null) reps.remove(previousRep);
if (value > 0)
{
newRep = new Repetition(timestamp, value);
reps.add(newRep);
}
habit.invalidateNewerThan(timestamp);
habitList.update(habit);
} }
@NonNull @NonNull
@ -87,9 +78,7 @@ public class CreateRepetitionCommand extends Command
@Override @Override
public void undo() public void undo()
{ {
if(newRep != null) habit.getRepetitions().remove(newRep); habit.getRepetitions().setValue(timestamp, previousValue);
if (previousRep != null) habit.getRepetitions().add(previousRep);
habit.invalidateNewerThan(timestamp);
} }
public static class Record public static class Record
@ -129,4 +118,34 @@ public class CreateRepetitionCommand extends Command
return command; return command;
} }
} }
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CreateRepetitionCommand that = (CreateRepetitionCommand) o;
return value == that.value &&
habit.equals(that.habit) &&
habitList.equals(that.habitList) &&
timestamp.equals(that.timestamp);
}
@Override
public int hashCode()
{
return Objects.hash(habit, habitList, timestamp, value);
}
@Override
public String toString()
{
return "CreateRepetitionCommand{" +
"habit=" + habit +
", habitList=" + habitList +
", timestamp=" + timestamp +
", value=" + value +
", previousValue=" + previousValue +
'}';
}
} }

@ -89,7 +89,7 @@ public class HabitFixtures
for (int i = 0; i < times.length; i++) for (int i = 0; i < times.length; i++)
{ {
Timestamp timestamp = today.minus(times[i]); Timestamp timestamp = today.minus(times[i]);
habit.getRepetitions().add(new Repetition(timestamp, values[i])); habit.getRepetitions().setValue(timestamp, values[i]);
} }
return habit; return habit;
@ -125,7 +125,7 @@ public class HabitFixtures
for (int i = 0; i < times.length; i++) for (int i = 0; i < times.length; i++)
{ {
Timestamp timestamp = reference.minus(times[i]); Timestamp timestamp = reference.minus(times[i]);
habit.getRepetitions().add(new Repetition(timestamp, values[i])); habit.getRepetitions().setValue(timestamp, values[i]);
} }
return habit; return habit;

@ -114,7 +114,7 @@ public class ShowHabitMenuBehavior
if (habit.isNumerical()) if (habit.isNumerical())
value = (int) (1000 + 250 * random.nextGaussian() * strength / 100) * 1000; value = (int) (1000 + 250 * random.nextGaussian() * strength / 100) * 1000;
habit.getRepetitions().add(new Repetition(DateUtils.getToday().minus(i), value)); habit.getRepetitions().setValue(DateUtils.getToday().minus(i), value);
} }
habit.invalidateNewerThan(Timestamp.ZERO); habit.invalidateNewerThan(Timestamp.ZERO);

Loading…
Cancel
Save