mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Move commands to uhabits-core
This commit is contained in:
@@ -26,6 +26,7 @@ dependencies {
|
||||
implementation 'com.google.auto.factory:auto-factory:1.0-beta3'
|
||||
implementation 'com.google.code.findbugs:jsr305:3.0.2'
|
||||
implementation 'org.apache.commons:commons-lang3:3.5'
|
||||
implementation 'com.google.code.gson:gson:2.7'
|
||||
|
||||
implementation ('com.opencsv:opencsv:3.9') {
|
||||
exclude group: 'commons-logging', module: 'commons-logging'
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* 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.*;
|
||||
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* 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.*;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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.*;
|
||||
|
||||
/**
|
||||
* 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 = StringUtils.getRandomId();
|
||||
isRemote = false;
|
||||
}
|
||||
|
||||
public Command(String id)
|
||||
{
|
||||
this.id = id;
|
||||
isRemote = false;
|
||||
}
|
||||
|
||||
public abstract void execute();
|
||||
|
||||
public String getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public boolean isRemote()
|
||||
{
|
||||
return isRemote;
|
||||
}
|
||||
|
||||
public void setRemote(boolean remote)
|
||||
{
|
||||
isRemote = remote;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String toJson()
|
||||
{
|
||||
return new GsonBuilder().create().toJson(toRecord());
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public abstract Object toRecord();
|
||||
|
||||
public abstract void undo();
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* 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.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();
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* 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.*;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public List<Habit> getSelected()
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* 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.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);
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* 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.*;
|
||||
|
||||
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
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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;
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Á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.utils;
|
||||
|
||||
import java.math.*;
|
||||
import java.util.*;
|
||||
|
||||
public class StringUtils
|
||||
{
|
||||
public static String getRandomId()
|
||||
{
|
||||
return new BigInteger(260, new Random()).toString(32).substring(0, 32);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user