mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 17:18:52 -06:00
Cancel selection with back button correctly
This commit is contained in:
@@ -119,13 +119,15 @@ public class ListHabitsRootView extends BaseRootView
|
||||
@Nullable ListHabitsSelectionMenu menu)
|
||||
{
|
||||
listView.setController(null);
|
||||
if (controller == null || listAdapter == null) return;
|
||||
if (controller == null || menu == null || listAdapter == null) return;
|
||||
|
||||
HabitCardListController listController =
|
||||
new HabitCardListController(listAdapter, listView);
|
||||
|
||||
listController.setHabitListener(controller);
|
||||
listController.setSelectionListener(menu);
|
||||
listView.setController(listController);
|
||||
menu.setListController(listController);
|
||||
}
|
||||
|
||||
public void setListAdapter(@NonNull HabitCardListAdapter listAdapter)
|
||||
|
||||
@@ -75,7 +75,7 @@ public class ListHabitsScreen extends BaseScreen
|
||||
HabitCardListAdapter adapter = new HabitCardListAdapter(
|
||||
ListHabitsRootView.MAX_CHECKMARK_COUNT);
|
||||
rootView.setListAdapter(adapter);
|
||||
selectionMenu.setAdapter(adapter);
|
||||
selectionMenu.setListAdapter(adapter);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -19,26 +19,19 @@
|
||||
|
||||
package org.isoron.uhabits.ui.habits.list;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.support.annotation.*;
|
||||
import android.view.*;
|
||||
|
||||
import org.isoron.uhabits.HabitsApplication;
|
||||
import org.isoron.uhabits.R;
|
||||
import org.isoron.uhabits.commands.ArchiveHabitsCommand;
|
||||
import org.isoron.uhabits.commands.ChangeHabitColorCommand;
|
||||
import org.isoron.uhabits.commands.CommandRunner;
|
||||
import org.isoron.uhabits.commands.DeleteHabitsCommand;
|
||||
import org.isoron.uhabits.commands.UnarchiveHabitsCommand;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
import org.isoron.uhabits.ui.BaseSelectionMenu;
|
||||
import org.isoron.uhabits.ui.habits.list.controllers.HabitCardListController;
|
||||
import org.isoron.uhabits.ui.habits.list.model.HabitCardListAdapter;
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.commands.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.ui.*;
|
||||
import org.isoron.uhabits.ui.habits.list.controllers.*;
|
||||
import org.isoron.uhabits.ui.habits.list.model.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.*;
|
||||
|
||||
public class ListHabitsSelectionMenu extends BaseSelectionMenu
|
||||
implements HabitCardListController.SelectionListener
|
||||
@@ -50,7 +43,10 @@ public class ListHabitsSelectionMenu extends BaseSelectionMenu
|
||||
CommandRunner commandRunner;
|
||||
|
||||
@Nullable
|
||||
private HabitCardListAdapter adapter;
|
||||
private HabitCardListAdapter listAdapter;
|
||||
|
||||
@Nullable
|
||||
private HabitCardListController listController;
|
||||
|
||||
public ListHabitsSelectionMenu(@NonNull ListHabitsScreen screen)
|
||||
{
|
||||
@@ -61,16 +57,16 @@ public class ListHabitsSelectionMenu extends BaseSelectionMenu
|
||||
@Override
|
||||
public void onFinish()
|
||||
{
|
||||
if (adapter != null) adapter.clearSelection();
|
||||
if (listController != null) listController.onSelectionFinished();
|
||||
super.onFinish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onItemClicked(@NonNull MenuItem item)
|
||||
{
|
||||
if (adapter == null) return false;
|
||||
if (listAdapter == null) return false;
|
||||
|
||||
List<Habit> selected = adapter.getSelected();
|
||||
List<Habit> selected = listAdapter.getSelected();
|
||||
if (selected.isEmpty()) return false;
|
||||
|
||||
Habit firstHabit = selected.get(0);
|
||||
@@ -108,8 +104,8 @@ public class ListHabitsSelectionMenu extends BaseSelectionMenu
|
||||
@Override
|
||||
public boolean onPrepare(@NonNull Menu menu)
|
||||
{
|
||||
if (adapter == null) return false;
|
||||
List<Habit> selected = adapter.getSelected();
|
||||
if (listAdapter == null) return false;
|
||||
List<Habit> selected = listAdapter.getSelected();
|
||||
|
||||
boolean showEdit = (selected.size() == 1);
|
||||
boolean showArchive = true;
|
||||
@@ -153,10 +149,21 @@ public class ListHabitsSelectionMenu extends BaseSelectionMenu
|
||||
screen.startSelection();
|
||||
}
|
||||
|
||||
public void setAdapter(@Nullable HabitCardListAdapter adapter)
|
||||
public void setListAdapter(@Nullable HabitCardListAdapter listAdapter)
|
||||
{
|
||||
if (adapter == null) return;
|
||||
this.adapter = adapter;
|
||||
if (listAdapter == null) return;
|
||||
this.listAdapter = listAdapter;
|
||||
}
|
||||
|
||||
public void setListController(HabitCardListController listController)
|
||||
{
|
||||
this.listController = listController;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getResourceId()
|
||||
{
|
||||
return R.menu.list_habits_selection;
|
||||
}
|
||||
|
||||
private void archive(@NonNull List<Habit> selected)
|
||||
@@ -177,12 +184,6 @@ public class ListHabitsSelectionMenu extends BaseSelectionMenu
|
||||
screen.showEditHabitScreen(firstHabit);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getResourceId()
|
||||
{
|
||||
return R.menu.list_habits_selection;
|
||||
}
|
||||
|
||||
private void showColorPicker(@NonNull List<Habit> selected,
|
||||
@NonNull Habit firstHabit)
|
||||
{
|
||||
|
||||
@@ -19,14 +19,13 @@
|
||||
|
||||
package org.isoron.uhabits.ui.habits.list.controllers;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.*;
|
||||
|
||||
import com.mobeta.android.dslv.DragSortListView;
|
||||
import com.mobeta.android.dslv.*;
|
||||
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
import org.isoron.uhabits.ui.habits.list.model.HabitCardListAdapter;
|
||||
import org.isoron.uhabits.ui.habits.list.views.HabitCardListView;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.ui.habits.list.model.*;
|
||||
import org.isoron.uhabits.ui.habits.list.views.*;
|
||||
|
||||
/**
|
||||
* Controller responsible for receiving and processing the events generated by a
|
||||
@@ -131,6 +130,16 @@ public class HabitCardListController implements DragSortListView.DropListener,
|
||||
activeMode.onItemLongClick(position);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the selection operation is cancelled externally, by something
|
||||
* other than this controller. This happens, for example, when the user
|
||||
* presses the back button.
|
||||
*/
|
||||
public void onSelectionFinished()
|
||||
{
|
||||
cancelSelection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the user wants to toggle a checkmark.
|
||||
*
|
||||
@@ -153,7 +162,6 @@ public class HabitCardListController implements DragSortListView.DropListener,
|
||||
this.selectionListener = listener;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called when the user starts dragging an item.
|
||||
*
|
||||
@@ -165,6 +173,17 @@ public class HabitCardListController implements DragSortListView.DropListener,
|
||||
activeMode.startDrag(position);
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects or deselects the item at a given position
|
||||
*
|
||||
* @param position the position of the item to be selected/deselected
|
||||
*/
|
||||
protected void toggleSelection(int position)
|
||||
{
|
||||
adapter.toggleSelection(position);
|
||||
activeMode = adapter.isSelectionEmpty() ? NORMAL_MODE : SELECTION_MODE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks all items as not selected and finishes the selection operation.
|
||||
*/
|
||||
@@ -177,17 +196,6 @@ public class HabitCardListController implements DragSortListView.DropListener,
|
||||
if (selectionListener != null) selectionListener.onSelectionFinish();
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects or deselects the item at a given position
|
||||
*
|
||||
* @param position the position of the item to be selected/deselected
|
||||
*/
|
||||
protected void toggleSelection(int position)
|
||||
{
|
||||
adapter.toggleSelection(position);
|
||||
activeMode = adapter.isSelectionEmpty() ? NORMAL_MODE : SELECTION_MODE;
|
||||
}
|
||||
|
||||
public interface HabitListener extends CheckmarkButtonController.Listener
|
||||
{
|
||||
/**
|
||||
@@ -310,8 +318,7 @@ public class HabitCardListController implements DragSortListView.DropListener,
|
||||
|
||||
if (activeMode == SELECTION_MODE)
|
||||
selectionListener.onSelectionChange();
|
||||
else
|
||||
selectionListener.onSelectionFinish();
|
||||
else selectionListener.onSelectionFinish();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user