Simplify CommandRunner and convert it to Kotlin

pull/699/head
Alinson S. Xavier 5 years ago
parent e80292e75d
commit 373f21e247

@ -166,7 +166,7 @@ public class HistoryEditorDialog extends AppCompatDialogFragment
} }
@Override @Override
public void onCommandExecuted(@Nullable Command command, @Nullable Long refreshKey) public void onCommandFinished(@NonNull Command command)
{ {
refreshData(); refreshData();
} }

@ -228,7 +228,7 @@ class EditHabitActivity : AppCompatActivity() {
} else { } else {
component.createHabitCommandFactory.create(component.habitList, habit) component.createHabitCommandFactory.create(component.habitList, habit)
} }
component.commandRunner.execute(command, null) component.commandRunner.run(command)
finish() finish()
} }

@ -90,13 +90,11 @@ class ListHabitsScreen
commandRunner.removeListener(this) commandRunner.removeListener(this)
} }
override fun onCommandExecuted(command: Command?, refreshKey: Long?) { override fun onCommandFinished(command: Command) {
if (command != null) {
val stringId = getExecuteString(command) val stringId = getExecuteString(command)
if (stringId != null) if (stringId != null)
activity.showMessage(stringId) activity.showMessage(stringId)
} }
}
fun onResult(requestCode: Int, resultCode: Int, data: Intent?) { fun onResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) { when (requestCode) {

@ -115,7 +115,7 @@ class ShowHabitActivity : AppCompatActivity(), CommandRunner.Listener {
super.onPause() super.onPause()
} }
override fun onCommandExecuted(command: Command?, refreshKey: Long?) { override fun onCommandFinished(command: Command) {
refresh() refresh()
} }

@ -42,8 +42,12 @@ class WidgetUpdater
private val intentScheduler: IntentScheduler private val intentScheduler: IntentScheduler
) : CommandRunner.Listener { ) : CommandRunner.Listener {
override fun onCommandExecuted(command: Command?, refreshKey: Long?) { override fun onCommandFinished(command: Command) {
updateWidgets(refreshKey) 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()); habitRecord.id.toString());
Habit habit = habitList.getByUUID(habitRecord.uuid); Habit habit = habitList.getByUUID(habitRecord.uuid);
Command command;
if (habit == null) if (habit == null)
{ {
habit = modelFactory.buildHabit(); habit = modelFactory.buildHabit();
habitRecord.id = null; habitRecord.id = null;
habitRecord.copyTo(habit); habitRecord.copyTo(habit);
new CreateHabitCommand(modelFactory, habitList, habit).execute(); command = new CreateHabitCommand(modelFactory, habitList, habit);
command.execute();
} }
else else
{ {
Habit modified = modelFactory.buildHabit(); Habit modified = modelFactory.buildHabit();
habitRecord.id = habit.getId(); habitRecord.id = habit.getId();
habitRecord.copyTo(modified); 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 // Reload saved version of the habit
@ -134,9 +137,9 @@ public class LoopDBImporter extends AbstractImporter
if (existingEntry.getValue() != r.value) if (existingEntry.getValue() != r.value)
new CreateRepetitionCommand(habitList, habit, t, r.value).execute(); new CreateRepetitionCommand(habitList, habit, t, r.value).execute();
} }
}
runner.notifyListeners(null, null); runner.notifyListeners(command);
}
db.close(); db.close();
} }
} }

@ -56,8 +56,7 @@ public class ReminderScheduler implements CommandRunner.Listener
} }
@Override @Override
public synchronized void onCommandExecuted(@Nullable Command command, public synchronized void onCommandFinished(@Nullable Command command)
@Nullable Long refreshKey)
{ {
if (command instanceof CreateRepetitionCommand) return; if (command instanceof CreateRepetitionCommand) return;
if (command instanceof ChangeHabitColorCommand) return; if (command instanceof ChangeHabitColorCommand) return;

@ -160,7 +160,7 @@ class SyncManager @Inject constructor(
connected = false connected = false
} }
override fun onCommandExecuted(command: Command?, refreshKey: Long?) { override fun onCommandFinished(command: Command) {
if (!dirty) setCurrentVersion(currVersion + 1) if (!dirty) setCurrentVersion(currVersion + 1)
dirty = true dirty = true
} }

@ -74,8 +74,7 @@ public class NotificationTray
} }
@Override @Override
public void onCommandExecuted(@Nullable Command command, public void onCommandFinished(@Nullable Command command)
@Nullable Long refreshKey)
{ {
if (command instanceof CreateRepetitionCommand) if (command instanceof CreateRepetitionCommand)
{ {

@ -138,11 +138,15 @@ public class HabitCardListCache implements CommandRunner.Listener
} }
@Override @Override
public synchronized void onCommandExecuted(@Nullable Command command, public synchronized void onCommandFinished(@Nullable Command command)
@Nullable Long refreshKey)
{ {
if (refreshKey == null) refreshAllHabits(); if (command instanceof CreateRepetitionCommand) {
else refreshHabit(refreshKey); Habit h = ((CreateRepetitionCommand) command).getHabit();
Long id = h.getId();
if (id != null) refreshHabit(id);
} else {
refreshAllHabits();
}
} }
public synchronized void onDetached() public synchronized void onDetached()

@ -88,9 +88,9 @@ public class ListHabitsBehavior
screen.showNumberPicker(oldValue / 1000, habit.getUnit(), newValue -> screen.showNumberPicker(oldValue / 1000, habit.getUnit(), newValue ->
{ {
newValue = Math.round(newValue * 1000); newValue = Math.round(newValue * 1000);
commandRunner.execute( commandRunner.run(
new CreateRepetitionCommand(habitList, habit, timestamp, (int) newValue), new CreateRepetitionCommand(habitList, habit, timestamp, (int) newValue)
habit.getId()); );
}); });
} }
@ -153,9 +153,9 @@ public class ListHabitsBehavior
public void onToggle(@NonNull Habit habit, Timestamp timestamp, int value) public void onToggle(@NonNull Habit habit, Timestamp timestamp, int value)
{ {
commandRunner.execute( commandRunner.run(
new CreateRepetitionCommand(habitList, habit, timestamp, value), new CreateRepetitionCommand(habitList, habit, timestamp, value)
habit.getId()); );
} }
public void onSyncKeyOffer(@NotNull String syncKey, @NotNull String encryptionKey) public void onSyncKeyOffer(@NotNull String syncKey, @NotNull String encryptionKey)

@ -78,8 +78,8 @@ public class ListHabitsSelectionMenuBehavior
public void onArchiveHabits() public void onArchiveHabits()
{ {
commandRunner.execute( commandRunner.run(
new ArchiveHabitsCommand(habitList, adapter.getSelected()), null); new ArchiveHabitsCommand(habitList, adapter.getSelected()));
adapter.clearSelection(); adapter.clearSelection();
} }
@ -90,9 +90,9 @@ public class ListHabitsSelectionMenuBehavior
screen.showColorPicker(first.getColor(), selectedColor -> screen.showColorPicker(first.getColor(), selectedColor ->
{ {
commandRunner.execute( commandRunner.run(
new ChangeHabitColorCommand(habitList, selected, selectedColor), new ChangeHabitColorCommand(habitList, selected, selectedColor)
null); );
adapter.clearSelection(); adapter.clearSelection();
}); });
} }
@ -103,8 +103,8 @@ public class ListHabitsSelectionMenuBehavior
screen.showDeleteConfirmationScreen(() -> screen.showDeleteConfirmationScreen(() ->
{ {
adapter.performRemove(selected); adapter.performRemove(selected);
commandRunner.execute(new DeleteHabitsCommand(habitList, selected), commandRunner.run(new DeleteHabitsCommand(habitList, selected)
null); );
adapter.clearSelection(); adapter.clearSelection();
}); });
} }
@ -117,8 +117,8 @@ public class ListHabitsSelectionMenuBehavior
public void onUnarchiveHabits() public void onUnarchiveHabits()
{ {
commandRunner.execute( commandRunner.run(
new UnarchiveHabitsCommand(habitList, adapter.getSelected()), null); new UnarchiveHabitsCommand(habitList, adapter.getSelected()));
adapter.clearSelection(); adapter.clearSelection();
} }

@ -61,25 +61,23 @@ class ShowHabitBehavior(
val oldValue = entries.get(timestamp).value val oldValue = entries.get(timestamp).value
screen.showNumberPicker(oldValue / 1000.0, habit.unit) { newValue: Double -> screen.showNumberPicker(oldValue / 1000.0, habit.unit) { newValue: Double ->
val thousands = (newValue * 1000).roundToInt() val thousands = (newValue * 1000).roundToInt()
commandRunner.execute( commandRunner.run(
CreateRepetitionCommand( CreateRepetitionCommand(
habitList, habitList,
habit, habit,
timestamp, timestamp,
thousands, thousands,
), ),
habit.id,
) )
} }
} else { } else {
commandRunner.execute( commandRunner.run(
CreateRepetitionCommand( CreateRepetitionCommand(
habitList, habitList,
habit, habit,
timestamp, timestamp,
value, value,
), ),
null,
) )
} }
} }

@ -51,7 +51,7 @@ class ShowHabitMenuBehavior(
fun onDeleteHabit() { fun onDeleteHabit() {
screen.showDeleteConfirmationScreen { screen.showDeleteConfirmationScreen {
commandRunner.execute(DeleteHabitsCommand(habitList, listOf(habit)), null) commandRunner.run(DeleteHabitsCommand(habitList, listOf(habit)))
screen.close() screen.close()
} }
} }

@ -93,8 +93,8 @@ public class WidgetBehavior
} }
public void setValue(@NonNull Habit habit, Timestamp timestamp, int newValue) { public void setValue(@NonNull Habit habit, Timestamp timestamp, int newValue) {
commandRunner.execute( commandRunner.run(
new CreateRepetitionCommand(habitList, habit, timestamp, newValue), new CreateRepetitionCommand(habitList, habit, timestamp, newValue)
habit.getId()); );
} }
} }

@ -70,9 +70,9 @@ public class HabitCardListCacheTest extends BaseUnitTest
assertThat(cache.getHabitCount(), equalTo(10)); assertThat(cache.getHabitCount(), equalTo(10));
Habit h = habitList.getByPosition(0); Habit h = habitList.getByPosition(0);
commandRunner.execute( commandRunner.run(
new DeleteHabitsCommand(habitList, Collections.singletonList(h)), new DeleteHabitsCommand(habitList, Collections.singletonList(h))
null); );
verify(listener).onItemRemoved(0); verify(listener).onItemRemoved(0);
verify(listener).onRefreshFinished(); verify(listener).onRefreshFinished();
@ -84,7 +84,7 @@ public class HabitCardListCacheTest extends BaseUnitTest
{ {
Habit h2 = habitList.getByPosition(2); Habit h2 = habitList.getByPosition(2);
Timestamp today = DateUtils.getToday(); 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).onItemChanged(2);
verify(listener).onRefreshFinished(); verify(listener).onRefreshFinished();
verifyNoMoreInteractions(listener); verifyNoMoreInteractions(listener);

@ -61,9 +61,9 @@ public class WidgetBehaviorTest extends BaseUnitTest
public void testOnAddRepetition() public void testOnAddRepetition()
{ {
behavior.onAddRepetition(habit, timestamp); behavior.onAddRepetition(habit, timestamp);
verify(commandRunner).execute( verify(commandRunner).run(
new CreateRepetitionCommand(habitList, habit, timestamp, YES_MANUAL), new CreateRepetitionCommand(habitList, habit, timestamp, YES_MANUAL)
habit.getId()); );
verify(notificationTray).cancel(habit); verify(notificationTray).cancel(habit);
verifyZeroInteractions(preferences); verifyZeroInteractions(preferences);
} }
@ -72,9 +72,9 @@ public class WidgetBehaviorTest extends BaseUnitTest
public void testOnRemoveRepetition() public void testOnRemoveRepetition()
{ {
behavior.onRemoveRepetition(habit, timestamp); behavior.onRemoveRepetition(habit, timestamp);
verify(commandRunner).execute( verify(commandRunner).run(
new CreateRepetitionCommand(habitList, habit, timestamp, NO), new CreateRepetitionCommand(habitList, habit, timestamp, NO)
habit.getId()); );
verify(notificationTray).cancel(habit); verify(notificationTray).cancel(habit);
verifyZeroInteractions(preferences); verifyZeroInteractions(preferences);
} }
@ -94,9 +94,9 @@ public class WidgetBehaviorTest extends BaseUnitTest
habit.getOriginalEntries().add(new Entry(timestamp, currentValue)); habit.getOriginalEntries().add(new Entry(timestamp, currentValue));
behavior.onToggleRepetition(habit, timestamp); behavior.onToggleRepetition(habit, timestamp);
verify(preferences).isSkipEnabled(); verify(preferences).isSkipEnabled();
verify(commandRunner).execute( verify(commandRunner).run(
new CreateRepetitionCommand(habitList, habit, timestamp, nextValue), new CreateRepetitionCommand(habitList, habit, timestamp, nextValue)
habit.getId()); );
verify(notificationTray).cancel(habit); verify(notificationTray).cancel(habit);
reset(preferences, commandRunner, notificationTray); reset(preferences, commandRunner, notificationTray);
} }
@ -110,9 +110,9 @@ public class WidgetBehaviorTest extends BaseUnitTest
habit.recompute(); habit.recompute();
behavior.onIncrement(habit, timestamp, 100); behavior.onIncrement(habit, timestamp, 100);
verify(commandRunner).execute( verify(commandRunner).run(
new CreateRepetitionCommand(habitList, habit, timestamp, 600), new CreateRepetitionCommand(habitList, habit, timestamp, 600)
habit.getId()); );
verify(notificationTray).cancel(habit); verify(notificationTray).cancel(habit);
verifyZeroInteractions(preferences); verifyZeroInteractions(preferences);
} }
@ -125,9 +125,9 @@ public class WidgetBehaviorTest extends BaseUnitTest
habit.recompute(); habit.recompute();
behavior.onDecrement(habit, timestamp, 100); behavior.onDecrement(habit, timestamp, 100);
verify(commandRunner).execute( verify(commandRunner).run(
new CreateRepetitionCommand(habitList, habit, timestamp, 400), new CreateRepetitionCommand(habitList, habit, timestamp, 400)
habit.getId()); );
verify(notificationTray).cancel(habit); verify(notificationTray).cancel(habit);
verifyZeroInteractions(preferences); verifyZeroInteractions(preferences);
} }

Loading…
Cancel
Save