mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Simplify commands
This commit is contained in:
@@ -28,7 +28,7 @@ import java.util.*;
|
||||
/**
|
||||
* Command to archive a list of habits.
|
||||
*/
|
||||
public class ArchiveHabitsCommand extends Command
|
||||
public class ArchiveHabitsCommand implements Command
|
||||
{
|
||||
final List<Habit> selected;
|
||||
|
||||
@@ -48,52 +48,4 @@ public class ArchiveHabitsCommand extends Command
|
||||
for (Habit h : selected) h.setArchived(true);
|
||||
habitList.update(selected);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Record toRecord()
|
||||
{
|
||||
return new Record(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undo()
|
||||
{
|
||||
for (Habit h : selected) h.setArchived(false);
|
||||
habitList.update(selected);
|
||||
}
|
||||
|
||||
public static class Record
|
||||
{
|
||||
@NonNull
|
||||
public final String id;
|
||||
|
||||
@NonNull
|
||||
public final String event = "Archive";
|
||||
|
||||
@NonNull
|
||||
public final List<Long> habits;
|
||||
|
||||
public Record(@NonNull ArchiveHabitsCommand command)
|
||||
{
|
||||
id = command.getId();
|
||||
habits = new LinkedList<>();
|
||||
for (Habit h : command.selected)
|
||||
{
|
||||
habits.add(h.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public ArchiveHabitsCommand toCommand(@NonNull HabitList habitList)
|
||||
{
|
||||
List<Habit> selected = new LinkedList<>();
|
||||
for (Long id : this.habits) selected.add(habitList.getById(id));
|
||||
|
||||
ArchiveHabitsCommand command;
|
||||
command = new ArchiveHabitsCommand(habitList, selected);
|
||||
command.setId(id);
|
||||
return command;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ import java.util.*;
|
||||
/**
|
||||
* Command to change the color of a list of habits.
|
||||
*/
|
||||
public class ChangeHabitColorCommand extends Command
|
||||
public class ChangeHabitColorCommand implements Command
|
||||
{
|
||||
@NonNull
|
||||
final HabitList habitList;
|
||||
@@ -36,9 +36,6 @@ public class ChangeHabitColorCommand extends Command
|
||||
@NonNull
|
||||
final List<Habit> selected;
|
||||
|
||||
@NonNull
|
||||
final List<PaletteColor> originalColors;
|
||||
|
||||
@NonNull
|
||||
final PaletteColor newColor;
|
||||
|
||||
@@ -49,8 +46,6 @@ public class ChangeHabitColorCommand extends Command
|
||||
this.habitList = habitList;
|
||||
this.selected = selected;
|
||||
this.newColor = newColor;
|
||||
this.originalColors = new ArrayList<>(selected.size());
|
||||
for (Habit h : selected) originalColors.add(h.getColor());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -59,57 +54,4 @@ public class ChangeHabitColorCommand extends Command
|
||||
for (Habit h : selected) h.setColor(newColor);
|
||||
habitList.update(selected);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Record toRecord()
|
||||
{
|
||||
return new Record(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undo()
|
||||
{
|
||||
int k = 0;
|
||||
for (Habit h : selected) h.setColor(originalColors.get(k++));
|
||||
habitList.update(selected);
|
||||
}
|
||||
|
||||
public static class Record
|
||||
{
|
||||
@NonNull
|
||||
public String id;
|
||||
|
||||
@NonNull
|
||||
public String event = "ChangeColor";
|
||||
|
||||
@NonNull
|
||||
public List<Long> habits;
|
||||
|
||||
@NonNull
|
||||
public Integer color;
|
||||
|
||||
public Record(ChangeHabitColorCommand command)
|
||||
{
|
||||
id = command.getId();
|
||||
color = command.newColor.getPaletteIndex();
|
||||
habits = new LinkedList<>();
|
||||
for (Habit h : command.selected)
|
||||
{
|
||||
if (!h.hasId()) throw new RuntimeException("Habit not saved");
|
||||
habits.add(h.getId());
|
||||
}
|
||||
}
|
||||
|
||||
public ChangeHabitColorCommand toCommand(@NonNull HabitList habitList)
|
||||
{
|
||||
List<Habit> selected = new LinkedList<>();
|
||||
for (Long id : this.habits) selected.add(habitList.getById(id));
|
||||
|
||||
ChangeHabitColorCommand command;
|
||||
command = new ChangeHabitColorCommand(habitList, selected, new PaletteColor(color));
|
||||
command.setId(id);
|
||||
return command;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,55 +19,11 @@
|
||||
|
||||
package org.isoron.uhabits.core.commands;
|
||||
|
||||
import androidx.annotation.*;
|
||||
|
||||
import com.google.gson.*;
|
||||
|
||||
import org.isoron.uhabits.core.utils.*;
|
||||
|
||||
/**
|
||||
* A Command represents a desired set of changes that should be performed on the
|
||||
* models.
|
||||
* <p>
|
||||
* A command can be executed and undone. Each of these operations also provide
|
||||
* an string that should be displayed to the user upon their completion.
|
||||
* <p>
|
||||
* In general, commands should always be executed by a {@link CommandRunner}.
|
||||
* models. In general, commands should always be executed by a {@link CommandRunner}.
|
||||
*/
|
||||
public abstract class Command
|
||||
public interface Command
|
||||
{
|
||||
private String id;
|
||||
|
||||
public Command()
|
||||
{
|
||||
id = StringUtils.getRandomId();
|
||||
}
|
||||
|
||||
public Command(String id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public abstract void execute();
|
||||
|
||||
public String getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String toJson()
|
||||
{
|
||||
return new GsonBuilder().create().toJson(toRecord());
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public abstract Object toRecord();
|
||||
|
||||
public abstract void undo();
|
||||
void execute();
|
||||
}
|
||||
|
||||
@@ -1,81 +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.commands;
|
||||
|
||||
import androidx.annotation.*;
|
||||
|
||||
import com.google.gson.*;
|
||||
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
|
||||
import javax.inject.*;
|
||||
|
||||
public class CommandParser
|
||||
{
|
||||
private HabitList habitList;
|
||||
|
||||
private ModelFactory modelFactory;
|
||||
|
||||
@Inject
|
||||
public CommandParser(@NonNull HabitList habitList,
|
||||
@NonNull ModelFactory modelFactory)
|
||||
{
|
||||
this.habitList = habitList;
|
||||
this.modelFactory = modelFactory;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public Command parse(@NonNull String json)
|
||||
{
|
||||
JsonObject parsed = new JsonParser().parse(json).getAsJsonObject();
|
||||
String event = parsed.get("event").getAsString();
|
||||
Gson gson = new GsonBuilder().create();
|
||||
|
||||
if (event.equals("Archive")) return gson
|
||||
.fromJson(json, ArchiveHabitsCommand.Record.class)
|
||||
.toCommand(habitList);
|
||||
|
||||
if (event.equals("ChangeColor")) return gson
|
||||
.fromJson(json, ChangeHabitColorCommand.Record.class)
|
||||
.toCommand(habitList);
|
||||
|
||||
if (event.equals("CreateHabit")) return gson
|
||||
.fromJson(json, CreateHabitCommand.Record.class)
|
||||
.toCommand(modelFactory, habitList);
|
||||
|
||||
if (event.equals("CreateRep")) return gson
|
||||
.fromJson(json, CreateRepetitionCommand.Record.class)
|
||||
.toCommand(habitList);
|
||||
|
||||
if (event.equals("DeleteHabit")) return gson
|
||||
.fromJson(json, DeleteHabitsCommand.Record.class)
|
||||
.toCommand(habitList);
|
||||
|
||||
if (event.equals("EditHabit")) return gson
|
||||
.fromJson(json, EditHabitCommand.Record.class)
|
||||
.toCommand(modelFactory, habitList);
|
||||
|
||||
if (event.equals("Unarchive")) return gson
|
||||
.fromJson(json, UnarchiveHabitsCommand.Record.class)
|
||||
.toCommand(habitList);
|
||||
|
||||
throw new IllegalStateException("Unknown command");
|
||||
}
|
||||
}
|
||||
@@ -37,9 +37,9 @@ import javax.inject.*;
|
||||
@AppScope
|
||||
public class CommandRunner
|
||||
{
|
||||
private TaskRunner taskRunner;
|
||||
private final TaskRunner taskRunner;
|
||||
|
||||
private LinkedList<Listener> listeners;
|
||||
private final LinkedList<Listener> listeners;
|
||||
|
||||
@Inject
|
||||
public CommandRunner(@NonNull TaskRunner taskRunner)
|
||||
|
||||
@@ -25,11 +25,8 @@ import com.google.auto.factory.*;
|
||||
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
|
||||
/**
|
||||
* Command to create a habit.
|
||||
*/
|
||||
@AutoFactory
|
||||
public class CreateHabitCommand extends Command
|
||||
public class CreateHabitCommand implements Command
|
||||
{
|
||||
ModelFactory modelFactory;
|
||||
|
||||
@@ -38,9 +35,6 @@ public class CreateHabitCommand extends Command
|
||||
@NonNull
|
||||
Habit model;
|
||||
|
||||
@Nullable
|
||||
Long savedId;
|
||||
|
||||
public CreateHabitCommand(@Provided @NonNull ModelFactory modelFactory,
|
||||
@NonNull HabitList habitList,
|
||||
@NonNull Habit model)
|
||||
@@ -53,63 +47,8 @@ public class CreateHabitCommand extends Command
|
||||
@Override
|
||||
public void execute()
|
||||
{
|
||||
Habit savedHabit = modelFactory.buildHabit();
|
||||
savedHabit.copyFrom(model);
|
||||
savedHabit.setId(savedId);
|
||||
|
||||
habitList.add(savedHabit);
|
||||
savedId = savedHabit.getId();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Record toRecord()
|
||||
{
|
||||
return new Record(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undo()
|
||||
{
|
||||
if (savedId == null) throw new IllegalStateException();
|
||||
|
||||
Habit habit = habitList.getById(savedId);
|
||||
if (habit == null) throw new HabitNotFoundException();
|
||||
|
||||
habitList.remove(habit);
|
||||
}
|
||||
|
||||
public static class Record
|
||||
{
|
||||
@NonNull
|
||||
public String id;
|
||||
|
||||
@NonNull
|
||||
public String event = "CreateHabit";
|
||||
|
||||
@NonNull
|
||||
public Habit.HabitData habit;
|
||||
|
||||
@Nullable
|
||||
public Long savedId;
|
||||
|
||||
public Record(CreateHabitCommand command)
|
||||
{
|
||||
id = command.getId();
|
||||
habit = command.model.getData();
|
||||
savedId = command.savedId;
|
||||
}
|
||||
|
||||
public CreateHabitCommand toCommand(@NonNull ModelFactory modelFactory,
|
||||
@NonNull HabitList habitList)
|
||||
{
|
||||
Habit h = modelFactory.buildHabit(habit);
|
||||
|
||||
CreateHabitCommand command;
|
||||
command = new CreateHabitCommand(modelFactory, habitList, h);
|
||||
command.savedId = savedId;
|
||||
command.setId(id);
|
||||
return command;
|
||||
}
|
||||
Habit habit = modelFactory.buildHabit();
|
||||
habit.copyFrom(model);
|
||||
habitList.add(habit);
|
||||
}
|
||||
}
|
||||
@@ -25,10 +25,7 @@ import org.isoron.uhabits.core.models.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Command to toggle a repetition.
|
||||
*/
|
||||
public class CreateRepetitionCommand extends Command
|
||||
public class CreateRepetitionCommand implements Command
|
||||
{
|
||||
@NonNull
|
||||
final Habit habit;
|
||||
@@ -41,8 +38,6 @@ public class CreateRepetitionCommand extends Command
|
||||
|
||||
final int value;
|
||||
|
||||
int previousValue;
|
||||
|
||||
public CreateRepetitionCommand(@NonNull HabitList habitList,
|
||||
@NonNull Habit habit,
|
||||
@NonNull Timestamp timestamp,
|
||||
@@ -58,7 +53,6 @@ public class CreateRepetitionCommand extends Command
|
||||
public void execute()
|
||||
{
|
||||
RepetitionList reps = habit.getRepetitions();
|
||||
previousValue = reps.getValue(timestamp);
|
||||
reps.setValue(timestamp, value);
|
||||
habitList.resort();
|
||||
}
|
||||
@@ -69,57 +63,6 @@ public class CreateRepetitionCommand extends Command
|
||||
return habit;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public Record toRecord()
|
||||
{
|
||||
return new Record(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undo()
|
||||
{
|
||||
habit.getRepetitions().setValue(timestamp, previousValue);
|
||||
}
|
||||
|
||||
public static class Record
|
||||
{
|
||||
@NonNull
|
||||
public String id;
|
||||
|
||||
@NonNull
|
||||
public String event = "CreateRep";
|
||||
|
||||
public long habit;
|
||||
|
||||
public long repTimestamp;
|
||||
|
||||
public int value;
|
||||
|
||||
public Record(CreateRepetitionCommand command)
|
||||
{
|
||||
id = command.getId();
|
||||
Long habitId = command.habit.getId();
|
||||
if(habitId == null) throw new RuntimeException("Habit not saved");
|
||||
|
||||
this.habit = habitId;
|
||||
this.repTimestamp = command.timestamp.getUnixTime();
|
||||
this.value = command.value;
|
||||
}
|
||||
|
||||
public CreateRepetitionCommand toCommand(@NonNull HabitList habitList)
|
||||
{
|
||||
Habit h = habitList.getById(habit);
|
||||
if(h == null) throw new HabitNotFoundException();
|
||||
|
||||
CreateRepetitionCommand command;
|
||||
command = new CreateRepetitionCommand(
|
||||
habitList, h, new Timestamp(repTimestamp), value);
|
||||
command.setId(id);
|
||||
return command;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
@@ -137,16 +80,4 @@ public class CreateRepetitionCommand extends Command
|
||||
{
|
||||
return Objects.hash(habit, habitList, timestamp, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "CreateRepetitionCommand{" +
|
||||
"habit=" + habit +
|
||||
", habitList=" + habitList +
|
||||
", timestamp=" + timestamp +
|
||||
", value=" + value +
|
||||
", previousValue=" + previousValue +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -25,10 +25,7 @@ import org.isoron.uhabits.core.models.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Command to delete a list of habits.
|
||||
*/
|
||||
public class DeleteHabitsCommand extends Command
|
||||
public class DeleteHabitsCommand implements Command
|
||||
{
|
||||
@NonNull
|
||||
final HabitList habitList;
|
||||
@@ -55,51 +52,4 @@ public class DeleteHabitsCommand extends Command
|
||||
{
|
||||
return Collections.unmodifiableList(selected);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public Record toRecord()
|
||||
{
|
||||
return new Record(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undo()
|
||||
{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public static class Record
|
||||
{
|
||||
@NonNull
|
||||
public String id;
|
||||
|
||||
@NonNull
|
||||
public String event = "DeleteHabit";
|
||||
|
||||
@NonNull
|
||||
public List<Long> habits;
|
||||
|
||||
public Record(DeleteHabitsCommand command)
|
||||
{
|
||||
id = command.getId();
|
||||
habits = new LinkedList<>();
|
||||
for (Habit h : command.selected)
|
||||
{
|
||||
if (!h.hasId()) throw new RuntimeException("Habit not saved");
|
||||
habits.add(h.getId());
|
||||
}
|
||||
}
|
||||
|
||||
public DeleteHabitsCommand toCommand(@NonNull HabitList habitList)
|
||||
{
|
||||
List<Habit> selected = new LinkedList<>();
|
||||
for (Long id : this.habits) selected.add(habitList.getById(id));
|
||||
|
||||
DeleteHabitsCommand command;
|
||||
command = new DeleteHabitsCommand(habitList, selected);
|
||||
command.setId(id);
|
||||
return command;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,11 +25,8 @@ import com.google.auto.factory.*;
|
||||
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
|
||||
/**
|
||||
* Command to modify a habit.
|
||||
*/
|
||||
@AutoFactory
|
||||
public class EditHabitCommand extends Command
|
||||
public class EditHabitCommand implements Command
|
||||
{
|
||||
@NonNull
|
||||
final HabitList habitList;
|
||||
@@ -76,19 +73,6 @@ public class EditHabitCommand extends Command
|
||||
copyAttributes(this.modified);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Record toRecord()
|
||||
{
|
||||
return new Record(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undo()
|
||||
{
|
||||
copyAttributes(this.original);
|
||||
}
|
||||
|
||||
private void copyAttributes(Habit model)
|
||||
{
|
||||
Habit habit = habitList.getById(savedId);
|
||||
@@ -101,40 +85,4 @@ public class EditHabitCommand extends Command
|
||||
habit.invalidateNewerThan(Timestamp.ZERO);
|
||||
}
|
||||
|
||||
public static class Record
|
||||
{
|
||||
@NonNull
|
||||
public String id;
|
||||
|
||||
@NonNull
|
||||
public String event = "EditHabit";
|
||||
|
||||
@NonNull
|
||||
public Habit.HabitData habit;
|
||||
|
||||
public long habitId;
|
||||
|
||||
public Record(EditHabitCommand command)
|
||||
{
|
||||
id = command.getId();
|
||||
this.habitId = command.savedId;
|
||||
this.habit = command.modified.getData();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public EditHabitCommand toCommand(@NonNull ModelFactory modelFactory,
|
||||
@NonNull HabitList habitList)
|
||||
{
|
||||
Habit original = habitList.getById(habitId);
|
||||
if(original == null) throw new HabitNotFoundException();
|
||||
|
||||
Habit modified = modelFactory.buildHabit(habit);
|
||||
|
||||
EditHabitCommand command;
|
||||
command = new EditHabitCommand(modelFactory, habitList, original,
|
||||
modified);
|
||||
command.setId(id);
|
||||
return command;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,10 +25,7 @@ import org.isoron.uhabits.core.models.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Command to unarchive a list of habits.
|
||||
*/
|
||||
public class UnarchiveHabitsCommand extends Command
|
||||
public class UnarchiveHabitsCommand implements Command
|
||||
{
|
||||
@NonNull
|
||||
final HabitList habitList;
|
||||
@@ -49,53 +46,4 @@ public class UnarchiveHabitsCommand extends Command
|
||||
for (Habit h : selected) h.setArchived(false);
|
||||
habitList.update(selected);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public Record toRecord()
|
||||
{
|
||||
return new Record(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undo()
|
||||
{
|
||||
for (Habit h : selected) h.setArchived(true);
|
||||
habitList.update(selected);
|
||||
}
|
||||
|
||||
public static class Record
|
||||
{
|
||||
@NonNull
|
||||
public final String id;
|
||||
|
||||
@NonNull
|
||||
public final String event = "Unarchive";
|
||||
|
||||
@NonNull
|
||||
public final List<Long> habits;
|
||||
|
||||
public Record(@NonNull UnarchiveHabitsCommand command)
|
||||
{
|
||||
id = command.getId();
|
||||
habits = new LinkedList<>();
|
||||
for (Habit h : command.selected)
|
||||
{
|
||||
if (!h.hasId()) throw new RuntimeException("Habit not saved");
|
||||
habits.add(h.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public UnarchiveHabitsCommand toCommand(@NonNull HabitList habitList)
|
||||
{
|
||||
List<Habit> selected = new LinkedList<>();
|
||||
for (Long id : this.habits) selected.add(habitList.getById(id));
|
||||
|
||||
UnarchiveHabitsCommand command;
|
||||
command = new UnarchiveHabitsCommand(habitList, selected);
|
||||
command.setId(id);
|
||||
return command;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +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/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Provides commands to modify the models, such as {@link
|
||||
* org.isoron.uhabits.core.commands.CreateHabitCommand}.
|
||||
*/
|
||||
package org.isoron.uhabits.core.commands;
|
||||
@@ -49,26 +49,11 @@ public class ArchiveHabitsCommandTest extends BaseUnitTest
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteUndoRedo()
|
||||
public void testExecute()
|
||||
{
|
||||
assertFalse(habit.isArchived());
|
||||
|
||||
command.execute();
|
||||
assertTrue(habit.isArchived());
|
||||
|
||||
command.undo();
|
||||
assertFalse(habit.isArchived());
|
||||
|
||||
command.execute();
|
||||
assertTrue(habit.isArchived());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecord()
|
||||
{
|
||||
ArchiveHabitsCommand.Record rec = command.toRecord();
|
||||
ArchiveHabitsCommand other = rec.toCommand(habitList);
|
||||
assertThat(other.selected, equalTo(command.selected));
|
||||
assertThat(other.getId(), equalTo(command.getId()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,28 +54,11 @@ public class ChangeHabitColorCommandTest extends BaseUnitTest
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteUndoRedo()
|
||||
public void testExecute()
|
||||
{
|
||||
checkOriginalColors();
|
||||
|
||||
command.execute();
|
||||
checkNewColors();
|
||||
|
||||
command.undo();
|
||||
checkOriginalColors();
|
||||
|
||||
command.execute();
|
||||
checkNewColors();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecord()
|
||||
{
|
||||
ChangeHabitColorCommand.Record rec = command.toRecord();
|
||||
ChangeHabitColorCommand other = rec.toCommand(habitList);
|
||||
assertThat(other.getId(), equalTo(command.getId()));
|
||||
assertThat(other.newColor, equalTo(command.newColor));
|
||||
assertThat(other.selected, equalTo(command.selected));
|
||||
}
|
||||
|
||||
private void checkNewColors()
|
||||
|
||||
@@ -1,149 +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.commands;
|
||||
|
||||
import androidx.annotation.*;
|
||||
|
||||
import org.hamcrest.*;
|
||||
import org.isoron.uhabits.core.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.json.*;
|
||||
import org.junit.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
public class CommandParserTest extends BaseUnitTest
|
||||
{
|
||||
@NonNull
|
||||
private CommandParser parser;
|
||||
|
||||
private Habit habit;
|
||||
|
||||
private List<Habit> selected;
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
super.setUp();
|
||||
parser = new CommandParser(habitList, modelFactory);
|
||||
habit = fixtures.createShortHabit();
|
||||
selected = Collections.singletonList(habit);
|
||||
habitList.add(habit);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeArchiveCommand() throws JSONException
|
||||
{
|
||||
ArchiveHabitsCommand original, decoded;
|
||||
original = new ArchiveHabitsCommand(habitList, selected);
|
||||
decoded = (ArchiveHabitsCommand) parser.parse(original.toJson());
|
||||
|
||||
MatcherAssert.assertThat(decoded.getId(), equalTo(original.getId()));
|
||||
MatcherAssert.assertThat(decoded.selected, equalTo(original.selected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeChangeColorCommand() throws JSONException
|
||||
{
|
||||
ChangeHabitColorCommand original, decoded;
|
||||
original = new ChangeHabitColorCommand(habitList, selected, new PaletteColor(20));
|
||||
decoded = (ChangeHabitColorCommand) parser.parse(original.toJson());
|
||||
|
||||
MatcherAssert.assertThat(decoded.getId(), equalTo(original.getId()));
|
||||
MatcherAssert.assertThat(decoded.newColor, equalTo(original.newColor));
|
||||
MatcherAssert.assertThat(decoded.selected, equalTo(original.selected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeCreateHabitCommand() throws JSONException
|
||||
{
|
||||
Habit model = modelFactory.buildHabit();
|
||||
model.setName("JSON");
|
||||
|
||||
CreateHabitCommand original, decoded;
|
||||
original = new CreateHabitCommand(modelFactory, habitList, model);
|
||||
original.execute();
|
||||
|
||||
decoded = (CreateHabitCommand) parser.parse(original.toJson());
|
||||
|
||||
MatcherAssert.assertThat(decoded.getId(), equalTo(original.getId()));
|
||||
MatcherAssert.assertThat(decoded.savedId, equalTo(original.savedId));
|
||||
MatcherAssert.assertThat(decoded.model.getData(), equalTo(model
|
||||
.getData()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeCreateRepCommand() throws JSONException
|
||||
{
|
||||
CreateRepetitionCommand original, decoded;
|
||||
original = new CreateRepetitionCommand(habitList, habit, Timestamp.ZERO.plus(100), 5);
|
||||
decoded = (CreateRepetitionCommand) parser.parse(original.toJson());
|
||||
|
||||
MatcherAssert.assertThat(decoded.getId(), equalTo(original.getId()));
|
||||
MatcherAssert.assertThat(decoded.timestamp, equalTo(original
|
||||
.timestamp));
|
||||
MatcherAssert.assertThat(decoded.value, equalTo(original.value));
|
||||
MatcherAssert.assertThat(decoded.habit, equalTo(original.habit));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeDeleteCommand() throws JSONException
|
||||
{
|
||||
DeleteHabitsCommand original, decoded;
|
||||
original = new DeleteHabitsCommand(habitList, selected);
|
||||
decoded = (DeleteHabitsCommand) parser.parse(original.toJson());
|
||||
|
||||
MatcherAssert.assertThat(decoded.getId(), equalTo(original.getId()));
|
||||
MatcherAssert.assertThat(decoded.selected, equalTo(original.selected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeEditHabitCommand() throws JSONException
|
||||
{
|
||||
Habit modified = modelFactory.buildHabit();
|
||||
modified.setName("Edited JSON");
|
||||
modified.setColor(new PaletteColor(2));
|
||||
|
||||
EditHabitCommand original, decoded;
|
||||
original = new EditHabitCommand(modelFactory, habitList, habit, modified);
|
||||
original.execute();
|
||||
|
||||
decoded = (EditHabitCommand) parser.parse(original.toJson());
|
||||
|
||||
MatcherAssert.assertThat(decoded.getId(), equalTo(original.getId()));
|
||||
MatcherAssert.assertThat(decoded.savedId, equalTo(original.savedId));
|
||||
MatcherAssert.assertThat(decoded.modified.getData(), equalTo(modified
|
||||
.getData()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeUnarchiveCommand() throws JSONException
|
||||
{
|
||||
UnarchiveHabitsCommand original, decoded;
|
||||
original = new UnarchiveHabitsCommand(habitList, selected);
|
||||
decoded = (UnarchiveHabitsCommand) parser.parse(original.toJson());
|
||||
|
||||
MatcherAssert.assertThat(decoded.getId(), equalTo(original.getId()));
|
||||
MatcherAssert.assertThat(decoded.selected, equalTo(original.selected));
|
||||
}
|
||||
}
|
||||
@@ -23,9 +23,9 @@ import org.isoron.uhabits.core.*;
|
||||
import org.isoron.uhabits.core.models.*;
|
||||
import org.junit.*;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class CreateHabitCommandTest extends BaseUnitTest
|
||||
{
|
||||
@@ -47,40 +47,12 @@ public class CreateHabitCommandTest extends BaseUnitTest
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteUndoRedo()
|
||||
public void testExecute()
|
||||
{
|
||||
assertTrue(habitList.isEmpty());
|
||||
|
||||
command.execute();
|
||||
|
||||
assertThat(habitList.size(), equalTo(1));
|
||||
|
||||
Habit habit = habitList.getByPosition(0);
|
||||
Long id = habit.getId();
|
||||
assertThat(habit.getName(), equalTo(model.getName()));
|
||||
|
||||
command.undo();
|
||||
assertTrue(habitList.isEmpty());
|
||||
|
||||
command.execute();
|
||||
assertThat(habitList.size(), equalTo(1));
|
||||
|
||||
habit = habitList.getByPosition(0);
|
||||
Long newId = habit.getId();
|
||||
assertThat(id, equalTo(newId));
|
||||
assertThat(habit.getName(), equalTo(model.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecord()
|
||||
{
|
||||
command.execute();
|
||||
|
||||
CreateHabitCommand.Record rec = command.toRecord();
|
||||
CreateHabitCommand other = rec.toCommand(modelFactory, habitList);
|
||||
|
||||
assertThat(other.getId(), equalTo(command.getId()));
|
||||
assertThat(other.savedId, equalTo(command.savedId));
|
||||
assertThat(other.model.getData(), equalTo(command.model.getData()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,8 +24,6 @@ import org.isoron.uhabits.core.models.*;
|
||||
import org.isoron.uhabits.core.utils.*;
|
||||
import org.junit.*;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.isoron.uhabits.core.models.Checkmark.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -51,34 +49,15 @@ public class CreateRepetitionCommandTest extends BaseUnitTest
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteUndoRedo()
|
||||
public void testExecute()
|
||||
{
|
||||
RepetitionList reps = habit.getRepetitions();
|
||||
|
||||
Repetition rep = reps.getByTimestamp(today);
|
||||
assertNotNull(rep);
|
||||
assertEquals(YES_MANUAL, rep.getValue());
|
||||
|
||||
command.execute();
|
||||
rep = reps.getByTimestamp(today);
|
||||
assertNotNull(rep);
|
||||
assertEquals(100, rep.getValue());
|
||||
|
||||
command.undo();
|
||||
rep = reps.getByTimestamp(today);
|
||||
assertNotNull(rep);
|
||||
assertEquals(YES_MANUAL, rep.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecord()
|
||||
{
|
||||
CreateRepetitionCommand.Record rec = command.toRecord();
|
||||
CreateRepetitionCommand other = rec.toCommand(habitList);
|
||||
|
||||
assertThat(command.getId(), equalTo(other.getId()));
|
||||
assertThat(command.timestamp, equalTo(other.timestamp));
|
||||
assertThat(command.value, equalTo(other.value));
|
||||
assertThat(command.habit, equalTo(other.habit));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,24 +62,12 @@ public class DeleteHabitsCommandTest extends BaseUnitTest
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteUndoRedo()
|
||||
public void testExecute()
|
||||
{
|
||||
assertThat(habitList.size(), equalTo(4));
|
||||
|
||||
command.execute();
|
||||
assertThat(habitList.size(), equalTo(1));
|
||||
assertThat(habitList.getByPosition(0).getName(), equalTo("extra"));
|
||||
|
||||
thrown.expect(UnsupportedOperationException.class);
|
||||
command.undo();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecord()
|
||||
{
|
||||
DeleteHabitsCommand.Record rec = command.toRecord();
|
||||
DeleteHabitsCommand other = rec.toCommand(habitList);
|
||||
assertThat(other.getId(), equalTo(command.getId()));
|
||||
assertThat(other.selected, equalTo(command.selected));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,14 +63,6 @@ public class EditHabitCommandTest extends BaseUnitTest
|
||||
command.execute();
|
||||
assertThat(habit.getName(), equalTo("modified"));
|
||||
assertThat(habit.getScores().getTodayValue(), equalTo(originalScore));
|
||||
|
||||
command.undo();
|
||||
assertThat(habit.getName(), equalTo("original"));
|
||||
assertThat(habit.getScores().getTodayValue(), equalTo(originalScore));
|
||||
|
||||
command.execute();
|
||||
assertThat(habit.getName(), equalTo("modified"));
|
||||
assertThat(habit.getScores().getTodayValue(), equalTo(originalScore));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -87,29 +79,5 @@ public class EditHabitCommandTest extends BaseUnitTest
|
||||
assertThat(habit.getName(), equalTo("modified"));
|
||||
assertThat(habit.getScores().getTodayValue(),
|
||||
lessThan(originalScore));
|
||||
|
||||
command.undo();
|
||||
assertThat(habit.getName(), equalTo("original"));
|
||||
assertThat(habit.getScores().getTodayValue(), equalTo(originalScore));
|
||||
|
||||
command.execute();
|
||||
assertThat(habit.getName(), equalTo("modified"));
|
||||
assertThat(habit.getScores().getTodayValue(),
|
||||
lessThan(originalScore));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecord()
|
||||
{
|
||||
command =
|
||||
new EditHabitCommand(modelFactory, habitList, habit, modified);
|
||||
|
||||
EditHabitCommand.Record rec = command.toRecord();
|
||||
EditHabitCommand other = rec.toCommand(modelFactory, habitList);
|
||||
|
||||
assertThat(other.getId(), equalTo(command.getId()));
|
||||
assertThat(other.savedId, equalTo(command.savedId));
|
||||
assertThat(other.original.getData(), equalTo(command.original.getData()));
|
||||
assertThat(other.modified.getData(), equalTo(command.modified.getData()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,8 +26,6 @@ import org.junit.*;
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
public class UnarchiveHabitsCommandTest extends BaseUnitTest
|
||||
{
|
||||
@@ -53,23 +51,8 @@ public class UnarchiveHabitsCommandTest extends BaseUnitTest
|
||||
public void testExecuteUndoRedo()
|
||||
{
|
||||
assertTrue(habit.isArchived());
|
||||
|
||||
command.execute();
|
||||
assertFalse(habit.isArchived());
|
||||
|
||||
command.undo();
|
||||
assertTrue(habit.isArchived());
|
||||
|
||||
command.execute();
|
||||
assertFalse(habit.isArchived());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecord()
|
||||
{
|
||||
UnarchiveHabitsCommand.Record rec = command.toRecord();
|
||||
UnarchiveHabitsCommand other = rec.toCommand(habitList);
|
||||
assertThat(other.selected, equalTo(command.selected));
|
||||
assertThat(other.getId(), equalTo(command.getId()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user