Make habit deletions instantaneous

Fixes #133
pull/151/head
Alinson S. Xavier 9 years ago
parent 17423b3ecd
commit 06b5f89b7a

@ -79,22 +79,22 @@ public class ListHabitsSelectionMenu extends BaseSelectionMenu
switch (item.getItemId()) switch (item.getItemId())
{ {
case R.id.action_edit_habit: case R.id.action_edit_habit:
edit(firstHabit); showEditScreen(firstHabit);
finish(); finish();
return true; return true;
case R.id.action_archive_habit: case R.id.action_archive_habit:
archive(selected); performArchive(selected);
finish(); finish();
return true; return true;
case R.id.action_unarchive_habit: case R.id.action_unarchive_habit:
unarchive(selected); performUnarchive(selected);
finish(); finish();
return true; return true;
case R.id.action_delete: case R.id.action_delete:
delete(selected); performDelete(selected);
return true; return true;
case R.id.action_color: case R.id.action_color:
@ -164,24 +164,26 @@ public class ListHabitsSelectionMenu extends BaseSelectionMenu
return R.menu.list_habits_selection; return R.menu.list_habits_selection;
} }
private void archive(@NonNull List<Habit> selected) private void performArchive(@NonNull List<Habit> selected)
{ {
commandRunner.execute(new ArchiveHabitsCommand(habitList, selected), commandRunner.execute(new ArchiveHabitsCommand(habitList, selected),
null); null);
} }
private void delete(@NonNull List<Habit> selected) private void performDelete(@NonNull List<Habit> selected)
{ {
screen.showDeleteConfirmationScreen(() -> { screen.showDeleteConfirmationScreen(() -> {
listAdapter.performRemove(selected);
commandRunner.execute(new DeleteHabitsCommand(habitList, selected), commandRunner.execute(new DeleteHabitsCommand(habitList, selected),
null); null);
finish(); finish();
}); });
} }
private void edit(@NonNull Habit firstHabit) private void performUnarchive(@NonNull List<Habit> selected)
{ {
screen.showEditHabitScreen(firstHabit); commandRunner.execute(new UnarchiveHabitsCommand(habitList, selected),
null);
} }
private void showColorPicker(@NonNull List<Habit> selected, private void showColorPicker(@NonNull List<Habit> selected,
@ -194,9 +196,8 @@ public class ListHabitsSelectionMenu extends BaseSelectionMenu
}); });
} }
private void unarchive(@NonNull List<Habit> selected) private void showEditScreen(@NonNull Habit firstHabit)
{ {
commandRunner.execute(new UnarchiveHabitsCommand(habitList, selected), screen.showEditHabitScreen(firstHabit);
null);
} }
} }

@ -83,7 +83,7 @@ public class HabitCardListController implements HabitCardListView.Controller
Habit habitFrom = adapter.getItem(from); Habit habitFrom = adapter.getItem(from);
Habit habitTo = adapter.getItem(to); Habit habitTo = adapter.getItem(to);
adapter.reorder(from, to); adapter.performReorder(from, to);
if (habitListener != null) if (habitListener != null)
habitListener.onHabitReorder(habitFrom, habitTo); habitListener.onHabitReorder(habitFrom, habitTo);

@ -145,7 +145,7 @@ public class HabitCardListAdapter
if (listView == null) return; if (listView == null) return;
Habit habit = cache.getHabitByPosition(position); Habit habit = cache.getHabitByPosition(position);
int score = cache.getScore(habit.getId()); int score = cache.getScore(habit.getId());
int checkmarks[] = cache.getCheckmarks(habit.getId()); int checkmarks[] = cache.getCheckmarks(habit.getId());
boolean selected = this.selected.contains(habit); boolean selected = this.selected.contains(habit);
@ -199,9 +199,21 @@ public class HabitCardListAdapter
observable.notifyListeners(); observable.notifyListeners();
} }
public void refresh() /**
* Removes a list of habits from the adapter.
* <p>
* Note that this only has effect on the adapter cache. The database is not
* modified, and the change is lost when the cache is refreshed. This method
* is useful for making the ListView more responsive: while we wait for the
* database operation to finish, the cache can be modified to reflect the
* changes immediately.
*
* @param habits list of habits to be removed
*/
public void performRemove(List<Habit> habits)
{ {
cache.refreshAllHabits(); for (Habit h : habits)
cache.remove(h.getId());
} }
/** /**
@ -216,10 +228,14 @@ public class HabitCardListAdapter
* @param from the habit that should be moved * @param from the habit that should be moved
* @param to the habit that currently occupies the desired position * @param to the habit that currently occupies the desired position
*/ */
public void reorder(int from, int to) public void performReorder(int from, int to)
{ {
cache.reorder(from, to); cache.reorder(from, to);
notifyItemMoved(from, to); }
public void refresh()
{
cache.refreshAllHabits();
} }
public void setFilter(HabitMatcher matcher) public void setFilter(HabitMatcher matcher)

@ -165,6 +165,20 @@ public class HabitCardListCache implements CommandRunner.Listener
this.progressBar = progressBar; this.progressBar = progressBar;
} }
public void remove(@NonNull Long id)
{
Habit h = data.id_to_habit.get(id);
if(h == null) return;
int position = data.habits.indexOf(h);
data.habits.remove(position);
data.id_to_habit.remove(id);
data.checkmarks.remove(id);
data.scores.remove(id);
if (listener != null) listener.onItemRemoved(position);
}
/** /**
* Interface definition for a callback to be invoked when the data on the * Interface definition for a callback to be invoked when the data on the
* cache has been modified. * cache has been modified.
@ -298,6 +312,8 @@ public class HabitCardListCache implements CommandRunner.Listener
protected void onPreExecute() protected void onPreExecute()
{ {
super.onPreExecute(); super.onPreExecute();
progressBar.setTotal(0);
new Handler().postDelayed(() -> { new Handler().postDelayed(() -> {
if (getStatus() == Status.RUNNING) progressBar.show(); if (getStatus() == Status.RUNNING) progressBar.show();
}, 1000); }, 1000);
@ -367,16 +383,7 @@ public class HabitCardListCache implements CommandRunner.Listener
Set<Long> removed = new TreeSet<>(before); Set<Long> removed = new TreeSet<>(before);
removed.removeAll(after); removed.removeAll(after);
for (Long id : removed) for (Long id : removed) remove(id);
{
Habit h = data.id_to_habit.get(id);
int position = data.habits.indexOf(h);
data.habits.remove(position);
data.id_to_habit.remove(id);
data.checkmarks.remove(id);
data.scores.remove(id);
if (listener != null) listener.onItemRemoved(position);
}
} }
} }
} }

@ -119,7 +119,7 @@ public class HabitCardListControllerTest extends BaseUnitTest
controller.drop(1, 3); controller.drop(1, 3);
verify(habitListener).onHabitReorder(habits.get(1), habits.get(3)); verify(habitListener).onHabitReorder(habits.get(1), habits.get(3));
verify(selectionListener).onSelectionFinish(); verify(selectionListener).onSelectionFinish();
verify(adapter).reorder(1, 3); verify(adapter).performReorder(1, 3);
resetMocks(); resetMocks();
} }
@ -134,7 +134,7 @@ public class HabitCardListControllerTest extends BaseUnitTest
controller.drop(0, 3); controller.drop(0, 3);
verify(habitListener).onHabitReorder(habits.get(0), habits.get(3)); verify(habitListener).onHabitReorder(habits.get(0), habits.get(3));
verify(selectionListener).onSelectionFinish(); verify(selectionListener).onSelectionFinish();
verify(adapter).reorder(0, 3); verify(adapter).performReorder(0, 3);
verify(adapter).clearSelection(); verify(adapter).clearSelection();
} }

Loading…
Cancel
Save