Move commands to their own files

pull/30/head
Alinson S. Xavier 10 years ago
parent ac8e78ff24
commit 8acbc63914

@ -0,0 +1,66 @@
/* Copyright (C) 2016 Alinson Santos Xavier
*
* This program 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.
*
* This program 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 org.isoron.helpers.Command;
import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit;
public class CreateHabitCommand extends Command
{
private Habit model;
private Long savedId;
public CreateHabitCommand(Habit model)
{
this.model = model;
}
@Override
public void execute()
{
Habit savedHabit = new Habit(model);
if (savedId == null)
{
savedHabit.save();
savedId = savedHabit.getId();
}
else
{
savedHabit.save(savedId);
}
}
@Override
public void undo()
{
Habit.get(savedId).delete();
}
@Override
public Integer getExecuteStringId()
{
return R.string.toast_habit_created;
}
@Override
public Integer getUndoStringId()
{
return R.string.toast_habit_deleted;
}
}

@ -0,0 +1,75 @@
/* Copyright (C) 2016 Alinson Santos Xavier
*
* This program 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.
*
* This program 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 org.isoron.helpers.Command;
import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit;
public class EditHabitCommand extends Command
{
private Habit original;
private Habit modified;
private long savedId;
private boolean hasIntervalChanged;
public EditHabitCommand(Habit original, Habit modified)
{
this.savedId = original.getId();
this.modified = new Habit(modified);
this.original = new Habit(original);
hasIntervalChanged = (this.original.freqDen != this.modified.freqDen ||
this.original.freqNum != this.modified.freqNum);
}
public void execute()
{
Habit habit = Habit.get(savedId);
habit.copyAttributes(modified);
habit.save();
if (hasIntervalChanged)
{
habit.deleteCheckmarksNewerThan(0);
habit.deleteStreaksNewerThan(0);
habit.deleteScoresNewerThan(0);
}
}
public void undo()
{
Habit habit = Habit.get(savedId);
habit.copyAttributes(original);
habit.save();
if (hasIntervalChanged)
{
habit.deleteCheckmarksNewerThan(0);
habit.deleteStreaksNewerThan(0);
habit.deleteScoresNewerThan(0);
}
}
public Integer getExecuteStringId()
{
return R.string.toast_habit_changed;
}
public Integer getUndoStringId()
{
return R.string.toast_habit_changed_back;
}
}

@ -0,0 +1,44 @@
/* Copyright (C) 2016 Alinson Santos Xavier
*
* This program 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.
*
* This program 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 org.isoron.helpers.Command;
import org.isoron.uhabits.models.Habit;
public class ToggleRepetitionCommand extends Command
{
private Long offset;
private Habit habit;
public ToggleRepetitionCommand(Habit habit, long offset)
{
this.offset = offset;
this.habit = habit;
}
@Override
public void execute()
{
habit.toggleRepetition(offset);
}
@Override
public void undo()
{
execute();
}
}

@ -40,6 +40,8 @@ import org.isoron.helpers.Command;
import org.isoron.helpers.DateHelper; import org.isoron.helpers.DateHelper;
import org.isoron.helpers.DialogHelper.OnSavedListener; import org.isoron.helpers.DialogHelper.OnSavedListener;
import org.isoron.uhabits.R; import org.isoron.uhabits.R;
import org.isoron.uhabits.commands.CreateHabitCommand;
import org.isoron.uhabits.commands.EditHabitCommand;
import org.isoron.uhabits.dialogs.WeekdayPickerDialog; import org.isoron.uhabits.dialogs.WeekdayPickerDialog;
import org.isoron.uhabits.models.Habit; import org.isoron.uhabits.models.Habit;
@ -241,11 +243,11 @@ public class EditHabitFragment extends DialogFragment
if (mode == EDIT_MODE) if (mode == EDIT_MODE)
{ {
command = originalHabit.new EditCommand(modifiedHabit); command = new EditHabitCommand(originalHabit, modifiedHabit);
savedHabit = originalHabit; savedHabit = originalHabit;
} }
if (mode == CREATE_MODE) command = new Habit.CreateCommand(modifiedHabit); if (mode == CREATE_MODE) command = new CreateHabitCommand(modifiedHabit);
if (onSavedListener != null) onSavedListener.onSaved(command, savedHabit); if (onSavedListener != null) onSavedListener.onSaved(command, savedHabit);

@ -69,6 +69,7 @@ import org.isoron.uhabits.R;
import org.isoron.uhabits.commands.ArchiveHabitsCommand; import org.isoron.uhabits.commands.ArchiveHabitsCommand;
import org.isoron.uhabits.commands.ChangeHabitColorCommand; import org.isoron.uhabits.commands.ChangeHabitColorCommand;
import org.isoron.uhabits.commands.DeleteHabitsCommand; import org.isoron.uhabits.commands.DeleteHabitsCommand;
import org.isoron.uhabits.commands.ToggleRepetitionCommand;
import org.isoron.uhabits.commands.UnarchiveHabitsCommand; import org.isoron.uhabits.commands.UnarchiveHabitsCommand;
import org.isoron.uhabits.helpers.ReminderHelper; import org.isoron.uhabits.helpers.ReminderHelper;
import org.isoron.uhabits.io.CSVExporter; import org.isoron.uhabits.io.CSVExporter;
@ -545,7 +546,7 @@ public class ListHabitsFragment extends Fragment
if (v.getTag(R.string.toggle_key).equals(2)) updateCheckmark(habit.color, (TextView) v, 0); if (v.getTag(R.string.toggle_key).equals(2)) updateCheckmark(habit.color, (TextView) v, 0);
else updateCheckmark(habit.color, (TextView) v, 2); else updateCheckmark(habit.color, (TextView) v, 2);
executeCommand(habit.new ToggleRepetitionCommand(timestamp), habit.getId()); executeCommand(new ToggleRepetitionCommand(habit, timestamp), habit.getId());
} }
private void executeCommand(Command c, Long refreshKey) private void executeCommand(Command c, Long refreshKey)

@ -666,125 +666,4 @@ public class Habit extends Model
ActiveAndroid.endTransaction(); ActiveAndroid.endTransaction();
} }
} }
public static class CreateCommand extends Command
{
private Habit model;
private Long savedId;
public CreateCommand(Habit model)
{
this.model = model;
}
@Override
public void execute()
{
Habit savedHabit = new Habit(model);
if (savedId == null)
{
savedHabit.save();
savedId = savedHabit.getId();
}
else
{
savedHabit.save(savedId);
}
}
@Override
public void undo()
{
Habit.get(savedId).delete();
}
@Override
public Integer getExecuteStringId()
{
return R.string.toast_habit_created;
}
@Override
public Integer getUndoStringId()
{
return R.string.toast_habit_deleted;
}
}
public class EditCommand extends Command
{
private Habit original;
private Habit modified;
private long savedId;
private boolean hasIntervalChanged;
public EditCommand(Habit modified)
{
this.savedId = getId();
this.modified = new Habit(modified);
this.original = new Habit(Habit.this);
hasIntervalChanged = (this.original.freqDen != this.modified.freqDen ||
this.original.freqNum != this.modified.freqNum);
}
public void execute()
{
Habit habit = Habit.get(savedId);
habit.copyAttributes(modified);
habit.save();
if (hasIntervalChanged)
{
habit.deleteCheckmarksNewerThan(0);
habit.deleteStreaksNewerThan(0);
habit.deleteScoresNewerThan(0);
}
}
public void undo()
{
Habit habit = Habit.get(savedId);
habit.copyAttributes(original);
habit.save();
if (hasIntervalChanged)
{
habit.deleteCheckmarksNewerThan(0);
habit.deleteStreaksNewerThan(0);
habit.deleteScoresNewerThan(0);
}
}
public Integer getExecuteStringId()
{
return R.string.toast_habit_changed;
}
public Integer getUndoStringId()
{
return R.string.toast_habit_changed_back;
}
}
public class ToggleRepetitionCommand extends Command
{
private Long offset;
public ToggleRepetitionCommand(long offset)
{
this.offset = offset;
}
@Override
public void execute()
{
toggleRepetition(offset);
}
@Override
public void undo()
{
execute();
}
}
} }

Loading…
Cancel
Save