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())
{
case R.id.action_edit_habit:
edit(firstHabit);
showEditScreen(firstHabit);
finish();
return true;
case R.id.action_archive_habit:
archive(selected);
performArchive(selected);
finish();
return true;
case R.id.action_unarchive_habit:
unarchive(selected);
performUnarchive(selected);
finish();
return true;
case R.id.action_delete:
delete(selected);
performDelete(selected);
return true;
case R.id.action_color:
@ -164,24 +164,26 @@ public class ListHabitsSelectionMenu extends BaseSelectionMenu
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),
null);
}
private void delete(@NonNull List<Habit> selected)
private void performDelete(@NonNull List<Habit> selected)
{
screen.showDeleteConfirmationScreen(() -> {
listAdapter.performRemove(selected);
commandRunner.execute(new DeleteHabitsCommand(habitList, selected),
null);
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,
@ -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),
null);
screen.showEditHabitScreen(firstHabit);
}
}

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

@ -199,9 +199,21 @@ public class HabitCardListAdapter
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 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);
notifyItemMoved(from, to);
}
public void refresh()
{
cache.refreshAllHabits();
}
public void setFilter(HabitMatcher matcher)

@ -165,6 +165,20 @@ public class HabitCardListCache implements CommandRunner.Listener
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
* cache has been modified.
@ -298,6 +312,8 @@ public class HabitCardListCache implements CommandRunner.Listener
protected void onPreExecute()
{
super.onPreExecute();
progressBar.setTotal(0);
new Handler().postDelayed(() -> {
if (getStatus() == Status.RUNNING) progressBar.show();
}, 1000);
@ -367,16 +383,7 @@ public class HabitCardListCache implements CommandRunner.Listener
Set<Long> removed = new TreeSet<>(before);
removed.removeAll(after);
for (Long id : removed)
{
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);
}
for (Long id : removed) remove(id);
}
}
}

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

Loading…
Cancel
Save