mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Simplify CommandRunner and convert it to Kotlin
This commit is contained in:
@@ -166,7 +166,7 @@ public class HistoryEditorDialog extends AppCompatDialogFragment
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommandExecuted(@Nullable Command command, @Nullable Long refreshKey)
|
||||
public void onCommandFinished(@NonNull Command command)
|
||||
{
|
||||
refreshData();
|
||||
}
|
||||
|
||||
@@ -228,7 +228,7 @@ class EditHabitActivity : AppCompatActivity() {
|
||||
} else {
|
||||
component.createHabitCommandFactory.create(component.habitList, habit)
|
||||
}
|
||||
component.commandRunner.execute(command, null)
|
||||
component.commandRunner.run(command)
|
||||
finish()
|
||||
}
|
||||
|
||||
|
||||
@@ -90,12 +90,10 @@ class ListHabitsScreen
|
||||
commandRunner.removeListener(this)
|
||||
}
|
||||
|
||||
override fun onCommandExecuted(command: Command?, refreshKey: Long?) {
|
||||
if (command != null) {
|
||||
val stringId = getExecuteString(command)
|
||||
if (stringId != null)
|
||||
activity.showMessage(stringId)
|
||||
}
|
||||
override fun onCommandFinished(command: Command) {
|
||||
val stringId = getExecuteString(command)
|
||||
if (stringId != null)
|
||||
activity.showMessage(stringId)
|
||||
}
|
||||
|
||||
fun onResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
|
||||
@@ -115,7 +115,7 @@ class ShowHabitActivity : AppCompatActivity(), CommandRunner.Listener {
|
||||
super.onPause()
|
||||
}
|
||||
|
||||
override fun onCommandExecuted(command: Command?, refreshKey: Long?) {
|
||||
override fun onCommandFinished(command: Command) {
|
||||
refresh()
|
||||
}
|
||||
|
||||
|
||||
@@ -42,8 +42,12 @@ class WidgetUpdater
|
||||
private val intentScheduler: IntentScheduler
|
||||
) : CommandRunner.Listener {
|
||||
|
||||
override fun onCommandExecuted(command: Command?, refreshKey: Long?) {
|
||||
updateWidgets(refreshKey)
|
||||
override fun onCommandFinished(command: Command) {
|
||||
if (command is CreateRepetitionCommand) {
|
||||
updateWidgets(command.habit.id)
|
||||
} else {
|
||||
updateWidgets()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,94 +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.isoron.uhabits.core.*;
|
||||
import org.isoron.uhabits.core.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 final TaskRunner taskRunner;
|
||||
|
||||
private final 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()
|
||||
{
|
||||
notifyListeners(command, refreshKey);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void notifyListeners(Command command, Long refreshKey)
|
||||
{
|
||||
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(@Nullable Command command,
|
||||
@Nullable Long refreshKey);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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 org.isoron.uhabits.core.*
|
||||
import org.isoron.uhabits.core.tasks.*
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
|
||||
@AppScope
|
||||
open class CommandRunner
|
||||
@Inject constructor(
|
||||
private val taskRunner: TaskRunner,
|
||||
) {
|
||||
private val listeners: LinkedList<Listener> = LinkedList()
|
||||
|
||||
open fun run(command: Command) {
|
||||
taskRunner.execute(object : Task {
|
||||
override fun doInBackground() {
|
||||
command.execute()
|
||||
}
|
||||
override fun onPostExecute() {
|
||||
notifyListeners(command)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fun addListener(l: Listener) {
|
||||
listeners.add(l)
|
||||
}
|
||||
|
||||
fun notifyListeners(command: Command) {
|
||||
for (l in listeners) l.onCommandFinished(command)
|
||||
}
|
||||
|
||||
fun removeListener(l: Listener) {
|
||||
listeners.remove(l)
|
||||
}
|
||||
|
||||
interface Listener {
|
||||
fun onCommandFinished(command: Command)
|
||||
}
|
||||
}
|
||||
@@ -109,19 +109,22 @@ public class LoopDBImporter extends AbstractImporter
|
||||
habitRecord.id.toString());
|
||||
|
||||
Habit habit = habitList.getByUUID(habitRecord.uuid);
|
||||
Command command;
|
||||
if (habit == null)
|
||||
{
|
||||
habit = modelFactory.buildHabit();
|
||||
habitRecord.id = null;
|
||||
habitRecord.copyTo(habit);
|
||||
new CreateHabitCommand(modelFactory, habitList, habit).execute();
|
||||
command = new CreateHabitCommand(modelFactory, habitList, habit);
|
||||
command.execute();
|
||||
}
|
||||
else
|
||||
{
|
||||
Habit modified = modelFactory.buildHabit();
|
||||
habitRecord.id = habit.getId();
|
||||
habitRecord.copyTo(modified);
|
||||
new EditHabitCommand(modelFactory, habitList, habit, modified).execute();
|
||||
command = new EditHabitCommand(modelFactory, habitList, habit, modified);
|
||||
command.execute();
|
||||
}
|
||||
|
||||
// Reload saved version of the habit
|
||||
@@ -134,9 +137,9 @@ public class LoopDBImporter extends AbstractImporter
|
||||
if (existingEntry.getValue() != r.value)
|
||||
new CreateRepetitionCommand(habitList, habit, t, r.value).execute();
|
||||
}
|
||||
}
|
||||
|
||||
runner.notifyListeners(null, null);
|
||||
runner.notifyListeners(command);
|
||||
}
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,8 +56,7 @@ public class ReminderScheduler implements CommandRunner.Listener
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void onCommandExecuted(@Nullable Command command,
|
||||
@Nullable Long refreshKey)
|
||||
public synchronized void onCommandFinished(@Nullable Command command)
|
||||
{
|
||||
if (command instanceof CreateRepetitionCommand) return;
|
||||
if (command instanceof ChangeHabitColorCommand) return;
|
||||
|
||||
@@ -160,7 +160,7 @@ class SyncManager @Inject constructor(
|
||||
connected = false
|
||||
}
|
||||
|
||||
override fun onCommandExecuted(command: Command?, refreshKey: Long?) {
|
||||
override fun onCommandFinished(command: Command) {
|
||||
if (!dirty) setCurrentVersion(currVersion + 1)
|
||||
dirty = true
|
||||
}
|
||||
|
||||
@@ -74,8 +74,7 @@ public class NotificationTray
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommandExecuted(@Nullable Command command,
|
||||
@Nullable Long refreshKey)
|
||||
public void onCommandFinished(@Nullable Command command)
|
||||
{
|
||||
if (command instanceof CreateRepetitionCommand)
|
||||
{
|
||||
|
||||
@@ -138,11 +138,15 @@ public class HabitCardListCache implements CommandRunner.Listener
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void onCommandExecuted(@Nullable Command command,
|
||||
@Nullable Long refreshKey)
|
||||
public synchronized void onCommandFinished(@Nullable Command command)
|
||||
{
|
||||
if (refreshKey == null) refreshAllHabits();
|
||||
else refreshHabit(refreshKey);
|
||||
if (command instanceof CreateRepetitionCommand) {
|
||||
Habit h = ((CreateRepetitionCommand) command).getHabit();
|
||||
Long id = h.getId();
|
||||
if (id != null) refreshHabit(id);
|
||||
} else {
|
||||
refreshAllHabits();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void onDetached()
|
||||
|
||||
@@ -88,9 +88,9 @@ public class ListHabitsBehavior
|
||||
screen.showNumberPicker(oldValue / 1000, habit.getUnit(), newValue ->
|
||||
{
|
||||
newValue = Math.round(newValue * 1000);
|
||||
commandRunner.execute(
|
||||
new CreateRepetitionCommand(habitList, habit, timestamp, (int) newValue),
|
||||
habit.getId());
|
||||
commandRunner.run(
|
||||
new CreateRepetitionCommand(habitList, habit, timestamp, (int) newValue)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -153,9 +153,9 @@ public class ListHabitsBehavior
|
||||
|
||||
public void onToggle(@NonNull Habit habit, Timestamp timestamp, int value)
|
||||
{
|
||||
commandRunner.execute(
|
||||
new CreateRepetitionCommand(habitList, habit, timestamp, value),
|
||||
habit.getId());
|
||||
commandRunner.run(
|
||||
new CreateRepetitionCommand(habitList, habit, timestamp, value)
|
||||
);
|
||||
}
|
||||
|
||||
public void onSyncKeyOffer(@NotNull String syncKey, @NotNull String encryptionKey)
|
||||
|
||||
@@ -78,8 +78,8 @@ public class ListHabitsSelectionMenuBehavior
|
||||
|
||||
public void onArchiveHabits()
|
||||
{
|
||||
commandRunner.execute(
|
||||
new ArchiveHabitsCommand(habitList, adapter.getSelected()), null);
|
||||
commandRunner.run(
|
||||
new ArchiveHabitsCommand(habitList, adapter.getSelected()));
|
||||
adapter.clearSelection();
|
||||
}
|
||||
|
||||
@@ -90,9 +90,9 @@ public class ListHabitsSelectionMenuBehavior
|
||||
|
||||
screen.showColorPicker(first.getColor(), selectedColor ->
|
||||
{
|
||||
commandRunner.execute(
|
||||
new ChangeHabitColorCommand(habitList, selected, selectedColor),
|
||||
null);
|
||||
commandRunner.run(
|
||||
new ChangeHabitColorCommand(habitList, selected, selectedColor)
|
||||
);
|
||||
adapter.clearSelection();
|
||||
});
|
||||
}
|
||||
@@ -103,8 +103,8 @@ public class ListHabitsSelectionMenuBehavior
|
||||
screen.showDeleteConfirmationScreen(() ->
|
||||
{
|
||||
adapter.performRemove(selected);
|
||||
commandRunner.execute(new DeleteHabitsCommand(habitList, selected),
|
||||
null);
|
||||
commandRunner.run(new DeleteHabitsCommand(habitList, selected)
|
||||
);
|
||||
adapter.clearSelection();
|
||||
});
|
||||
}
|
||||
@@ -117,8 +117,8 @@ public class ListHabitsSelectionMenuBehavior
|
||||
|
||||
public void onUnarchiveHabits()
|
||||
{
|
||||
commandRunner.execute(
|
||||
new UnarchiveHabitsCommand(habitList, adapter.getSelected()), null);
|
||||
commandRunner.run(
|
||||
new UnarchiveHabitsCommand(habitList, adapter.getSelected()));
|
||||
adapter.clearSelection();
|
||||
}
|
||||
|
||||
|
||||
@@ -61,25 +61,23 @@ class ShowHabitBehavior(
|
||||
val oldValue = entries.get(timestamp).value
|
||||
screen.showNumberPicker(oldValue / 1000.0, habit.unit) { newValue: Double ->
|
||||
val thousands = (newValue * 1000).roundToInt()
|
||||
commandRunner.execute(
|
||||
commandRunner.run(
|
||||
CreateRepetitionCommand(
|
||||
habitList,
|
||||
habit,
|
||||
timestamp,
|
||||
thousands,
|
||||
),
|
||||
habit.id,
|
||||
)
|
||||
}
|
||||
} else {
|
||||
commandRunner.execute(
|
||||
commandRunner.run(
|
||||
CreateRepetitionCommand(
|
||||
habitList,
|
||||
habit,
|
||||
timestamp,
|
||||
value,
|
||||
),
|
||||
null,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ class ShowHabitMenuBehavior(
|
||||
|
||||
fun onDeleteHabit() {
|
||||
screen.showDeleteConfirmationScreen {
|
||||
commandRunner.execute(DeleteHabitsCommand(habitList, listOf(habit)), null)
|
||||
commandRunner.run(DeleteHabitsCommand(habitList, listOf(habit)))
|
||||
screen.close()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,8 +93,8 @@ public class WidgetBehavior
|
||||
}
|
||||
|
||||
public void setValue(@NonNull Habit habit, Timestamp timestamp, int newValue) {
|
||||
commandRunner.execute(
|
||||
new CreateRepetitionCommand(habitList, habit, timestamp, newValue),
|
||||
habit.getId());
|
||||
commandRunner.run(
|
||||
new CreateRepetitionCommand(habitList, habit, timestamp, newValue)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,9 +70,9 @@ public class HabitCardListCacheTest extends BaseUnitTest
|
||||
assertThat(cache.getHabitCount(), equalTo(10));
|
||||
|
||||
Habit h = habitList.getByPosition(0);
|
||||
commandRunner.execute(
|
||||
new DeleteHabitsCommand(habitList, Collections.singletonList(h)),
|
||||
null);
|
||||
commandRunner.run(
|
||||
new DeleteHabitsCommand(habitList, Collections.singletonList(h))
|
||||
);
|
||||
|
||||
verify(listener).onItemRemoved(0);
|
||||
verify(listener).onRefreshFinished();
|
||||
@@ -84,7 +84,7 @@ public class HabitCardListCacheTest extends BaseUnitTest
|
||||
{
|
||||
Habit h2 = habitList.getByPosition(2);
|
||||
Timestamp today = DateUtils.getToday();
|
||||
commandRunner.execute(new CreateRepetitionCommand(habitList, h2, today, Entry.NO), h2.getId());
|
||||
commandRunner.run(new CreateRepetitionCommand(habitList, h2, today, Entry.NO));
|
||||
verify(listener).onItemChanged(2);
|
||||
verify(listener).onRefreshFinished();
|
||||
verifyNoMoreInteractions(listener);
|
||||
|
||||
@@ -61,9 +61,9 @@ public class WidgetBehaviorTest extends BaseUnitTest
|
||||
public void testOnAddRepetition()
|
||||
{
|
||||
behavior.onAddRepetition(habit, timestamp);
|
||||
verify(commandRunner).execute(
|
||||
new CreateRepetitionCommand(habitList, habit, timestamp, YES_MANUAL),
|
||||
habit.getId());
|
||||
verify(commandRunner).run(
|
||||
new CreateRepetitionCommand(habitList, habit, timestamp, YES_MANUAL)
|
||||
);
|
||||
verify(notificationTray).cancel(habit);
|
||||
verifyZeroInteractions(preferences);
|
||||
}
|
||||
@@ -72,9 +72,9 @@ public class WidgetBehaviorTest extends BaseUnitTest
|
||||
public void testOnRemoveRepetition()
|
||||
{
|
||||
behavior.onRemoveRepetition(habit, timestamp);
|
||||
verify(commandRunner).execute(
|
||||
new CreateRepetitionCommand(habitList, habit, timestamp, NO),
|
||||
habit.getId());
|
||||
verify(commandRunner).run(
|
||||
new CreateRepetitionCommand(habitList, habit, timestamp, NO)
|
||||
);
|
||||
verify(notificationTray).cancel(habit);
|
||||
verifyZeroInteractions(preferences);
|
||||
}
|
||||
@@ -94,9 +94,9 @@ public class WidgetBehaviorTest extends BaseUnitTest
|
||||
habit.getOriginalEntries().add(new Entry(timestamp, currentValue));
|
||||
behavior.onToggleRepetition(habit, timestamp);
|
||||
verify(preferences).isSkipEnabled();
|
||||
verify(commandRunner).execute(
|
||||
new CreateRepetitionCommand(habitList, habit, timestamp, nextValue),
|
||||
habit.getId());
|
||||
verify(commandRunner).run(
|
||||
new CreateRepetitionCommand(habitList, habit, timestamp, nextValue)
|
||||
);
|
||||
verify(notificationTray).cancel(habit);
|
||||
reset(preferences, commandRunner, notificationTray);
|
||||
}
|
||||
@@ -110,9 +110,9 @@ public class WidgetBehaviorTest extends BaseUnitTest
|
||||
habit.recompute();
|
||||
|
||||
behavior.onIncrement(habit, timestamp, 100);
|
||||
verify(commandRunner).execute(
|
||||
new CreateRepetitionCommand(habitList, habit, timestamp, 600),
|
||||
habit.getId());
|
||||
verify(commandRunner).run(
|
||||
new CreateRepetitionCommand(habitList, habit, timestamp, 600)
|
||||
);
|
||||
verify(notificationTray).cancel(habit);
|
||||
verifyZeroInteractions(preferences);
|
||||
}
|
||||
@@ -125,9 +125,9 @@ public class WidgetBehaviorTest extends BaseUnitTest
|
||||
habit.recompute();
|
||||
|
||||
behavior.onDecrement(habit, timestamp, 100);
|
||||
verify(commandRunner).execute(
|
||||
new CreateRepetitionCommand(habitList, habit, timestamp, 400),
|
||||
habit.getId());
|
||||
verify(commandRunner).run(
|
||||
new CreateRepetitionCommand(habitList, habit, timestamp, 400)
|
||||
);
|
||||
verify(notificationTray).cancel(habit);
|
||||
verifyZeroInteractions(preferences);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user