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)
|
@Nullable ListHabitsSelectionMenu menu)
|
||||||
{
|
{
|
||||||
listView.setController(null);
|
listView.setController(null);
|
||||||
if (controller == null || listAdapter == null) return;
|
if (controller == null || menu == null || listAdapter == null) return;
|
||||||
|
|
||||||
HabitCardListController listController =
|
HabitCardListController listController =
|
||||||
new HabitCardListController(listAdapter, listView);
|
new HabitCardListController(listAdapter, listView);
|
||||||
|
|
||||||
listController.setHabitListener(controller);
|
listController.setHabitListener(controller);
|
||||||
listController.setSelectionListener(menu);
|
listController.setSelectionListener(menu);
|
||||||
listView.setController(listController);
|
listView.setController(listController);
|
||||||
|
menu.setListController(listController);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setListAdapter(@NonNull HabitCardListAdapter listAdapter)
|
public void setListAdapter(@NonNull HabitCardListAdapter listAdapter)
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ public class ListHabitsScreen extends BaseScreen
|
|||||||
HabitCardListAdapter adapter = new HabitCardListAdapter(
|
HabitCardListAdapter adapter = new HabitCardListAdapter(
|
||||||
ListHabitsRootView.MAX_CHECKMARK_COUNT);
|
ListHabitsRootView.MAX_CHECKMARK_COUNT);
|
||||||
rootView.setListAdapter(adapter);
|
rootView.setListAdapter(adapter);
|
||||||
selectionMenu.setAdapter(adapter);
|
selectionMenu.setListAdapter(adapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -19,26 +19,19 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.ui.habits.list;
|
package org.isoron.uhabits.ui.habits.list;
|
||||||
|
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.*;
|
||||||
import android.support.annotation.Nullable;
|
import android.view.*;
|
||||||
import android.view.Menu;
|
|
||||||
import android.view.MenuItem;
|
|
||||||
|
|
||||||
import org.isoron.uhabits.HabitsApplication;
|
import org.isoron.uhabits.*;
|
||||||
import org.isoron.uhabits.R;
|
import org.isoron.uhabits.commands.*;
|
||||||
import org.isoron.uhabits.commands.ArchiveHabitsCommand;
|
import org.isoron.uhabits.models.*;
|
||||||
import org.isoron.uhabits.commands.ChangeHabitColorCommand;
|
import org.isoron.uhabits.ui.*;
|
||||||
import org.isoron.uhabits.commands.CommandRunner;
|
import org.isoron.uhabits.ui.habits.list.controllers.*;
|
||||||
import org.isoron.uhabits.commands.DeleteHabitsCommand;
|
import org.isoron.uhabits.ui.habits.list.model.*;
|
||||||
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 java.util.List;
|
import java.util.*;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.*;
|
||||||
|
|
||||||
public class ListHabitsSelectionMenu extends BaseSelectionMenu
|
public class ListHabitsSelectionMenu extends BaseSelectionMenu
|
||||||
implements HabitCardListController.SelectionListener
|
implements HabitCardListController.SelectionListener
|
||||||
@@ -50,7 +43,10 @@ public class ListHabitsSelectionMenu extends BaseSelectionMenu
|
|||||||
CommandRunner commandRunner;
|
CommandRunner commandRunner;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private HabitCardListAdapter adapter;
|
private HabitCardListAdapter listAdapter;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private HabitCardListController listController;
|
||||||
|
|
||||||
public ListHabitsSelectionMenu(@NonNull ListHabitsScreen screen)
|
public ListHabitsSelectionMenu(@NonNull ListHabitsScreen screen)
|
||||||
{
|
{
|
||||||
@@ -61,16 +57,16 @@ public class ListHabitsSelectionMenu extends BaseSelectionMenu
|
|||||||
@Override
|
@Override
|
||||||
public void onFinish()
|
public void onFinish()
|
||||||
{
|
{
|
||||||
if (adapter != null) adapter.clearSelection();
|
if (listController != null) listController.onSelectionFinished();
|
||||||
super.onFinish();
|
super.onFinish();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onItemClicked(@NonNull MenuItem item)
|
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;
|
if (selected.isEmpty()) return false;
|
||||||
|
|
||||||
Habit firstHabit = selected.get(0);
|
Habit firstHabit = selected.get(0);
|
||||||
@@ -108,8 +104,8 @@ public class ListHabitsSelectionMenu extends BaseSelectionMenu
|
|||||||
@Override
|
@Override
|
||||||
public boolean onPrepare(@NonNull Menu menu)
|
public boolean onPrepare(@NonNull Menu menu)
|
||||||
{
|
{
|
||||||
if (adapter == null) return false;
|
if (listAdapter == null) return false;
|
||||||
List<Habit> selected = adapter.getSelected();
|
List<Habit> selected = listAdapter.getSelected();
|
||||||
|
|
||||||
boolean showEdit = (selected.size() == 1);
|
boolean showEdit = (selected.size() == 1);
|
||||||
boolean showArchive = true;
|
boolean showArchive = true;
|
||||||
@@ -153,10 +149,21 @@ public class ListHabitsSelectionMenu extends BaseSelectionMenu
|
|||||||
screen.startSelection();
|
screen.startSelection();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAdapter(@Nullable HabitCardListAdapter adapter)
|
public void setListAdapter(@Nullable HabitCardListAdapter listAdapter)
|
||||||
{
|
{
|
||||||
if (adapter == null) return;
|
if (listAdapter == null) return;
|
||||||
this.adapter = adapter;
|
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)
|
private void archive(@NonNull List<Habit> selected)
|
||||||
@@ -177,12 +184,6 @@ public class ListHabitsSelectionMenu extends BaseSelectionMenu
|
|||||||
screen.showEditHabitScreen(firstHabit);
|
screen.showEditHabitScreen(firstHabit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected int getResourceId()
|
|
||||||
{
|
|
||||||
return R.menu.list_habits_selection;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showColorPicker(@NonNull List<Habit> selected,
|
private void showColorPicker(@NonNull List<Habit> selected,
|
||||||
@NonNull Habit firstHabit)
|
@NonNull Habit firstHabit)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -19,14 +19,13 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.ui.habits.list.controllers;
|
package org.isoron.uhabits.ui.habits.list.controllers;
|
||||||
|
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.*;
|
||||||
import android.support.annotation.Nullable;
|
|
||||||
|
|
||||||
import com.mobeta.android.dslv.DragSortListView;
|
import com.mobeta.android.dslv.*;
|
||||||
|
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.models.*;
|
||||||
import org.isoron.uhabits.ui.habits.list.model.HabitCardListAdapter;
|
import org.isoron.uhabits.ui.habits.list.model.*;
|
||||||
import org.isoron.uhabits.ui.habits.list.views.HabitCardListView;
|
import org.isoron.uhabits.ui.habits.list.views.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controller responsible for receiving and processing the events generated by a
|
* Controller responsible for receiving and processing the events generated by a
|
||||||
@@ -131,6 +130,16 @@ public class HabitCardListController implements DragSortListView.DropListener,
|
|||||||
activeMode.onItemLongClick(position);
|
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.
|
* Called when the user wants to toggle a checkmark.
|
||||||
*
|
*
|
||||||
@@ -153,7 +162,6 @@ public class HabitCardListController implements DragSortListView.DropListener,
|
|||||||
this.selectionListener = listener;
|
this.selectionListener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when the user starts dragging an item.
|
* Called when the user starts dragging an item.
|
||||||
*
|
*
|
||||||
@@ -165,6 +173,17 @@ public class HabitCardListController implements DragSortListView.DropListener,
|
|||||||
activeMode.startDrag(position);
|
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.
|
* 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();
|
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
|
public interface HabitListener extends CheckmarkButtonController.Listener
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@@ -306,12 +314,11 @@ public class HabitCardListController implements DragSortListView.DropListener,
|
|||||||
|
|
||||||
protected void notifyListener()
|
protected void notifyListener()
|
||||||
{
|
{
|
||||||
if(selectionListener == null) return;
|
if (selectionListener == null) return;
|
||||||
|
|
||||||
if (activeMode == SELECTION_MODE)
|
if (activeMode == SELECTION_MODE)
|
||||||
selectionListener.onSelectionChange();
|
selectionListener.onSelectionChange();
|
||||||
else
|
else selectionListener.onSelectionFinish();
|
||||||
selectionListener.onSelectionFinish();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user