Move commands to uhabits-core

This commit is contained in:
2017-05-25 10:00:56 -04:00
parent acd653db70
commit 370e7343d7
20 changed files with 90 additions and 116 deletions

View File

@@ -90,6 +90,9 @@ public class ListHabitsScreen extends BaseScreen
@NonNull
private Preferences prefs;
@NonNull
private final CommandParser commandParser;
@Inject
public ListHabitsScreen(@NonNull BaseActivity activity,
@NonNull CommandRunner commandRunner,
@@ -99,9 +102,11 @@ public class ListHabitsScreen extends BaseScreen
@NonNull ConfirmDeleteDialogFactory confirmDeleteDialogFactory,
@NonNull ColorPickerDialogFactory colorPickerFactory,
@NonNull EditHabitDialogFactory editHabitDialogFactory,
@NonNull Preferences prefs)
@NonNull Preferences prefs,
@NonNull CommandParser commandParser)
{
super(activity);
this.commandParser = commandParser;
setRootView(rootView);
this.prefs = prefs;
this.colorPickerFactory = colorPickerFactory;
@@ -122,7 +127,7 @@ public class ListHabitsScreen extends BaseScreen
@Nullable Long refreshKey)
{
if(command.isRemote()) return;
showMessage(command.getExecuteStringId());
showMessage(commandParser.getExecuteString(command));
}
public void onDettached()

View File

@@ -1,112 +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.commands;
import android.support.annotation.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*;
import java.util.*;
/**
* Command to archive a list of habits.
*/
public class ArchiveHabitsCommand extends Command
{
final List<Habit> selected;
final HabitList habitList;
public ArchiveHabitsCommand(@NonNull HabitList habitList,
@NonNull List<Habit> selected)
{
super();
this.habitList = habitList;
this.selected = new LinkedList<>(selected);
}
@Override
public void execute()
{
for (Habit h : selected) h.setArchived(true);
habitList.update(selected);
}
@Override
public Integer getExecuteStringId()
{
return R.string.toast_habit_archived;
}
@Override
public Integer getUndoStringId()
{
return R.string.toast_habit_unarchived;
}
@Override
public void undo()
{
for (Habit h : selected) h.setArchived(false);
habitList.update(selected);
}
@NonNull
@Override
public Record toRecord()
{
return new Record(this);
}
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

@@ -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.commands;
import android.support.annotation.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*;
import java.util.*;
/**
* Command to change the color of a list of habits.
*/
public class ChangeHabitColorCommand extends Command
{
@NonNull
final HabitList habitList;
@NonNull
final List<Habit> selected;
@NonNull
final List<Integer> originalColors;
@NonNull
final Integer newColor;
public ChangeHabitColorCommand(@NonNull HabitList habitList,
@NonNull List<Habit> selected,
@NonNull Integer newColor)
{
this.habitList = habitList;
this.selected = selected;
this.newColor = newColor;
this.originalColors = new ArrayList<>(selected.size());
for (Habit h : selected) originalColors.add(h.getColor());
}
@Override
public void execute()
{
for (Habit h : selected) h.setColor(newColor);
habitList.update(selected);
}
@Override
public Integer getExecuteStringId()
{
return R.string.toast_habit_changed;
}
@Override
public Integer getUndoStringId()
{
return R.string.toast_habit_changed;
}
@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;
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, color);
command.setId(id);
return command;
}
}
}

View File

@@ -1,106 +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.commands;
import android.support.annotation.*;
import com.google.gson.*;
import org.isoron.uhabits.utils.*;
import org.json.*;
/**
* 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}.
*/
public abstract class Command
{
private String id;
private boolean isRemote;
public Command()
{
id = DatabaseUtils.getRandomId();
isRemote = false;
}
public Command(String id)
{
this.id = id;
isRemote = false;
}
public abstract void execute();
public Integer getExecuteStringId()
{
return null;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public Integer getUndoStringId()
{
return null;
}
public boolean isRemote()
{
return isRemote;
}
public void setRemote(boolean remote)
{
isRemote = remote;
}
@NonNull
public JSONObject toJson()
{
try
{
String json = new GsonBuilder().create().toJson(toRecord());
return new JSONObject(json);
}
catch (JSONException e)
{
throw new RuntimeException(e);
}
}
@NonNull
public abstract Object toRecord();
public abstract void undo();
}

View File

@@ -23,6 +23,7 @@ import android.support.annotation.*;
import com.google.gson.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*;
import org.json.*;
@@ -42,6 +43,30 @@ public class CommandParser
this.modelFactory = modelFactory;
}
@StringRes
public Integer getExecuteString(@NonNull Command command)
{
if(command instanceof ArchiveHabitsCommand)
return R.string.toast_habit_archived;
if(command instanceof ChangeHabitColorCommand)
return R.string.toast_habit_changed;
if(command instanceof CreateHabitCommand)
return R.string.toast_habit_created;
if(command instanceof DeleteHabitsCommand)
return R.string.toast_habit_deleted;
if(command instanceof EditHabitCommand)
return R.string.toast_habit_changed;
if(command instanceof UnarchiveHabitsCommand)
return R.string.toast_habit_unarchived;
return null;
}
@NonNull
public Command parse(@NonNull JSONObject json) throws JSONException
{

View File

@@ -1,89 +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.commands;
import android.support.annotation.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.tasks.*;
import java.util.*;
import javax.inject.*;
/**
* A CommandRunner executes and undoes commands.
* <p>
* CommandRunners also allows objects to subscribe to it, and receive events
* whenever a command is performed.
*/
@AppScope
public class CommandRunner
{
private TaskRunner taskRunner;
private LinkedList<Listener> listeners;
@Inject
public CommandRunner(@NonNull TaskRunner taskRunner)
{
this.taskRunner = taskRunner;
listeners = new LinkedList<>();
}
public void addListener(Listener l)
{
listeners.add(l);
}
public void execute(final Command command, final Long refreshKey)
{
taskRunner.execute(new Task()
{
@Override
public void doInBackground()
{
command.execute();
}
@Override
public void onPostExecute()
{
for (Listener l : listeners)
l.onCommandExecuted(command, refreshKey);
}
});
}
public void removeListener(Listener l)
{
listeners.remove(l);
}
/**
* Interface implemented by objects that want to receive an event whenever a
* command is executed.
*/
public interface Listener
{
void onCommandExecuted(@NonNull Command command,
@Nullable Long refreshKey);
}
}

View File

@@ -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.commands;
import android.support.annotation.*;
import com.google.auto.factory.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*;
/**
* Command to create a habit.
*/
@AutoFactory
public class CreateHabitCommand extends Command
{
ModelFactory modelFactory;
HabitList habitList;
@NonNull
Habit model;
@Nullable
Long savedId;
public CreateHabitCommand(@Provided @NonNull ModelFactory modelFactory,
@NonNull HabitList habitList,
@NonNull Habit model)
{
this.modelFactory = modelFactory;
this.habitList = habitList;
this.model = model;
}
@Override
public void execute()
{
Habit savedHabit = modelFactory.buildHabit();
savedHabit.copyFrom(model);
savedHabit.setId(savedId);
habitList.add(savedHabit);
savedId = savedHabit.getId();
}
@Override
public Integer getExecuteStringId()
{
return R.string.toast_habit_created;
}
@Override
public Integer getUndoStringId()
{
return R.string.toast_habit_deleted;
}
@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;
}
}
}

View File

@@ -1,126 +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.commands;
import android.support.annotation.*;
import org.isoron.uhabits.models.*;
/**
* Command to toggle a repetition.
*/
public class CreateRepetitionCommand extends Command
{
@NonNull
final Habit habit;
final long timestamp;
final int value;
@Nullable
Repetition previousRep;
@Nullable
Repetition newRep;
public CreateRepetitionCommand(@NonNull Habit habit,
long timestamp,
int value)
{
this.timestamp = timestamp;
this.habit = habit;
this.value = value;
}
@Override
public void execute()
{
RepetitionList reps = habit.getRepetitions();
previousRep = reps.getByTimestamp(timestamp);
if (previousRep != null) reps.remove(previousRep);
newRep = new Repetition(timestamp, value);
reps.add(newRep);
habit.invalidateNewerThan(timestamp);
}
@NonNull
public Habit getHabit()
{
return habit;
}
@Override
@NonNull
public Record toRecord()
{
return new Record(this);
}
@Override
public void undo()
{
if(newRep == null) throw new IllegalStateException();
habit.getRepetitions().remove(newRep);
if (previousRep != null) habit.getRepetitions().add(previousRep);
habit.invalidateNewerThan(timestamp);
}
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;
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(h, repTimestamp, value);
command.setId(id);
return command;
}
}
}

View File

@@ -1,118 +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.commands;
import android.support.annotation.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*;
import java.util.*;
/**
* Command to delete a list of habits.
*/
public class DeleteHabitsCommand extends Command
{
@NonNull
final HabitList habitList;
@NonNull
final List<Habit> selected;
public DeleteHabitsCommand(@NonNull HabitList habitList,
@NonNull List<Habit> selected)
{
this.selected = new LinkedList<>(selected);
this.habitList = habitList;
}
@Override
public void execute()
{
for (Habit h : selected)
habitList.remove(h);
}
@Override
public Integer getExecuteStringId()
{
return R.string.toast_habit_deleted;
}
public List<Habit> getSelected()
{
return Collections.unmodifiableList(selected);
}
@Override
public Integer getUndoStringId()
{
return R.string.toast_habit_restored;
}
@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

@@ -1,153 +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.commands;
import android.support.annotation.*;
import com.google.auto.factory.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*;
/**
* Command to modify a habit.
*/
@AutoFactory
public class EditHabitCommand extends Command
{
@NonNull
final HabitList habitList;
@NonNull
final Habit original;
@NonNull
final Habit modified;
final long savedId;
final boolean hasFrequencyChanged;
final boolean hasTargetChanged;
public EditHabitCommand(@Provided @NonNull ModelFactory modelFactory,
@NonNull HabitList habitList,
@NonNull Habit original,
@NonNull Habit modified)
{
Long habitId = original.getId();
if (habitId == null) throw new RuntimeException("Habit not saved");
this.savedId = habitId;
this.habitList = habitList;
this.modified = modelFactory.buildHabit();
this.original = modelFactory.buildHabit();
this.modified.copyFrom(modified);
this.original.copyFrom(original);
Frequency originalFreq = this.original.getFrequency();
Frequency modifiedFreq = this.modified.getFrequency();
hasFrequencyChanged = (!originalFreq.equals(modifiedFreq));
hasTargetChanged =
(original.getTargetType() != modified.getTargetType() ||
original.getTargetValue() != modified.getTargetValue());
}
@Override
public void execute()
{
copyAttributes(this.modified);
}
@Override
public Integer getExecuteStringId()
{
return R.string.toast_habit_changed;
}
@Override
public Integer getUndoStringId()
{
return R.string.toast_habit_changed_back;
}
@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);
if (habit == null) throw new RuntimeException("Habit not found");
habit.copyFrom(model);
habitList.update(habit);
if (hasFrequencyChanged || hasTargetChanged)
habit.invalidateNewerThan(0);
}
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

@@ -1,101 +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.commands;
import android.support.annotation.*;
import org.isoron.uhabits.models.*;
/**
* Command to toggle a repetition.
*/
public class ToggleRepetitionCommand extends Command
{
final long timestamp;
@NonNull
final Habit habit;
public ToggleRepetitionCommand(@NonNull Habit habit, long timestamp)
{
super();
this.timestamp = timestamp;
this.habit = habit;
}
@Override
public void execute()
{
habit.getRepetitions().toggleTimestamp(timestamp);
}
@NonNull
public Habit getHabit()
{
return habit;
}
@Override
@NonNull
public Record toRecord()
{
return new Record(this);
}
@Override
public void undo()
{
execute();
}
public static class Record
{
@NonNull
public String id;
@NonNull
public String event = "Toggle";
public long habit;
public long repTimestamp;
public Record(@NonNull ToggleRepetitionCommand command)
{
id = command.getId();
Long habitId = command.habit.getId();
if(habitId == null) throw new RuntimeException("Habit not saved");
this.repTimestamp = command.timestamp;
this.habit = habitId;
}
public ToggleRepetitionCommand toCommand(@NonNull HabitList habitList)
{
Habit h = habitList.getById(habit);
if(h == null) throw new HabitNotFoundException();
ToggleRepetitionCommand command;
command = new ToggleRepetitionCommand(h, repTimestamp);
command.setId(id);
return command;
}
}
}

View File

@@ -1,114 +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.commands;
import android.support.annotation.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*;
import java.util.*;
/**
* Command to unarchive a list of habits.
*/
public class UnarchiveHabitsCommand extends Command
{
@NonNull
final HabitList habitList;
@NonNull
final List<Habit> selected;
public UnarchiveHabitsCommand(@NonNull HabitList habitList,
@NonNull List<Habit> selected)
{
this.selected = new LinkedList<>(selected);
this.habitList = habitList;
}
@Override
public void execute()
{
for (Habit h : selected) h.setArchived(false);
habitList.update(selected);
}
@Override
public Integer getExecuteStringId()
{
return R.string.toast_habit_unarchived;
}
@Override
public Integer getUndoStringId()
{
return R.string.toast_habit_archived;
}
@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.commands.CreateHabitCommand}.
*/
package org.isoron.uhabits.commands;

View File

@@ -108,13 +108,25 @@ public class SyncManager implements CommandRunner.Listener
connect(context, serverURL);
}
private JSONObject toJSONObject(String json)
{
try
{
return new JSONObject(json);
}
catch (JSONException e)
{
throw new RuntimeException(e);
}
}
@Override
public void onCommandExecuted(@NonNull Command command,
@Nullable Long refreshKey)
{
if (command.isRemote()) return;
JSONObject msg = command.toJson();
JSONObject msg = toJSONObject(command.toJson());
Long now = new Date().getTime();
Event e = new Event(command.getId(), now, msg.toString());
e.save();

View File

@@ -30,9 +30,7 @@ import org.isoron.uhabits.models.sqlite.records.*;
import org.isoron.uhabits.sync.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
public abstract class DatabaseUtils
{
@@ -70,11 +68,6 @@ public abstract class DatabaseUtils
return databaseFilename;
}
public static String getRandomId()
{
return new BigInteger(260, new Random()).toString(32).substring(0, 32);
}
@SuppressWarnings("unchecked")
public static void initializeActiveAndroid(Context context)
{

View File

@@ -17,7 +17,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.activities;
package org.isoron.androidbase.activities;
import android.content.*;
import android.os.*;
@@ -27,8 +27,8 @@ import android.support.v7.widget.Toolbar;
import android.view.*;
import android.widget.*;
import org.isoron.androidbase.activities.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.activities.*;
import org.isoron.uhabits.activities.common.dialogs.*;
import org.junit.*;
import org.junit.runner.*;

View File

@@ -17,7 +17,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.activities;
package org.isoron.androidbase.activities;
import android.content.*;
import android.support.annotation.*;
@@ -25,7 +25,6 @@ import android.support.v7.view.ActionMode;
import android.support.v7.widget.*;
import android.view.*;
import org.isoron.androidbase.activities.*;
import org.isoron.uhabits.*;
import org.junit.*;
import org.junit.runner.*;
@@ -38,6 +37,7 @@ import static android.view.View.*;
import static junit.framework.Assert.assertNotNull;
import static org.hamcrest.core.IsEqual.*;
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
import static org.robolectric.Robolectric.*;

View File

@@ -71,6 +71,8 @@ public class ListHabitsScreenTest extends BaseUnitTest
private Preferences prefs;
private CommandParser commandParser;
@Before
@Override
public void setUp()
@@ -86,10 +88,11 @@ public class ListHabitsScreenTest extends BaseUnitTest
colorPickerDialogFactory = mock(ColorPickerDialogFactory.class);
dialogFactory = mock(EditHabitDialogFactory.class);
prefs = mock(Preferences.class);
commandParser = mock(CommandParser.class);
screen = spy(new ListHabitsScreen(activity, commandRunner, rootView,
intentFactory, themeSwitcher, confirmDeleteDialogFactory,
colorPickerDialogFactory, dialogFactory, prefs));
colorPickerDialogFactory, dialogFactory, prefs, commandParser));
doNothing().when(screen).showMessage(anyInt());
@@ -122,7 +125,8 @@ public class ListHabitsScreenTest extends BaseUnitTest
public void testOnCommand()
{
Command c = mock(Command.class);
when(c.getExecuteStringId()).thenReturn(R.string.toast_habit_deleted);
when(commandParser.getExecuteString(c)).thenReturn(
R.string.toast_habit_deleted);
screen.onCommandExecuted(c, null);
verify(screen).showMessage(R.string.toast_habit_deleted);
}