Simplify commands

This commit is contained in:
2020-12-21 21:49:12 -06:00
parent 5d2ff40dc9
commit ac1441aba5
19 changed files with 22 additions and 852 deletions

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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();
}

View File

@@ -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");
}
}

View File

@@ -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)

View File

@@ -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);
}
}

View File

@@ -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 +
'}';
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;