From e0fbe841c10308f04b08684f01a2a7180b8d89f2 Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Wed, 23 Dec 2020 12:43:29 -0600 Subject: [PATCH] Remove BaseSelectionMenu --- .../androidbase/activities/BaseScreen.kt | 44 ------- .../activities/BaseSelectionMenu.kt | 107 ------------------ .../habits/list/ListHabitsScreen.kt | 4 +- .../habits/list/ListHabitsSelectionMenu.kt | 69 ++++++----- .../src/main/res/values/styles.xml | 3 +- 5 files changed, 44 insertions(+), 183 deletions(-) delete mode 100644 android/android-base/src/main/java/org/isoron/androidbase/activities/BaseSelectionMenu.kt diff --git a/android/android-base/src/main/java/org/isoron/androidbase/activities/BaseScreen.kt b/android/android-base/src/main/java/org/isoron/androidbase/activities/BaseScreen.kt index dd3395671..de64575d5 100644 --- a/android/android-base/src/main/java/org/isoron/androidbase/activities/BaseScreen.kt +++ b/android/android-base/src/main/java/org/isoron/androidbase/activities/BaseScreen.kt @@ -44,7 +44,6 @@ import java.io.* open class BaseScreen(@JvmField protected var activity: BaseActivity) { private var rootView: BaseRootView? = null - private var selectionMenu: BaseSelectionMenu? = null private var snackbar: Snackbar? = null /** @@ -100,15 +99,6 @@ open class BaseScreen(@JvmField protected var activity: BaseActivity) { } } - /** - * Sets the menu to be shown when a selection is active on the screen. - * - * @param menu the menu to be shown during a selection - */ - fun setSelectionMenu(menu: BaseSelectionMenu?) { - selectionMenu = menu - } - /** * Shows a message on the screen. * @@ -155,15 +145,6 @@ open class BaseScreen(@JvmField protected var activity: BaseActivity) { }) } - /** - * Instructs the screen to start a selection. - * - * If a selection menu was provided, this menu will be shown instead of the regular one. - */ - fun startSelection() { - activity.startSupportActionMode(ActionModeWrapper()) - } - private fun setActionBarColor(actionBar: ActionBar, color: Int) { val drawable = ColorDrawable(color) actionBar.setBackgroundDrawable(drawable) @@ -174,31 +155,6 @@ open class BaseScreen(@JvmField protected var activity: BaseActivity) { activity.window.statusBarColor = darkerColor } - private inner class ActionModeWrapper : ActionMode.Callback { - override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean { - val selectionMenu = selectionMenu - if (item == null || selectionMenu == null) return false - return selectionMenu.onItemClicked(item) - } - - override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean { - if (mode == null || menu == null) return false - val selectionMenu = selectionMenu ?: return false - selectionMenu.onCreate(activity.menuInflater, mode, menu) - return true - } - - override fun onDestroyActionMode(mode: ActionMode?) { - selectionMenu?.onFinish() - } - - override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean { - val selectionMenu = selectionMenu - if (selectionMenu == null || menu == null) return false - return selectionMenu.onPrepare(menu) - } - } - companion object { @JvmStatic @Deprecated("") diff --git a/android/android-base/src/main/java/org/isoron/androidbase/activities/BaseSelectionMenu.kt b/android/android-base/src/main/java/org/isoron/androidbase/activities/BaseSelectionMenu.kt deleted file mode 100644 index 5d4b9af11..000000000 --- a/android/android-base/src/main/java/org/isoron/androidbase/activities/BaseSelectionMenu.kt +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * 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 . - */ -package org.isoron.androidbase.activities - -import android.view.* -import androidx.appcompat.view.ActionMode - -/** - * Base class for all the selection menus in the application. - * - * A selection menu is a menu that appears when the screen starts a selection - * operation. It contains actions that modify the selected items, such as delete - * or archive. Since it replaces the toolbar, it also has a title. - * - * This class hides many implementation details of creating such menus in - * Android. The interface is supposed to look very similar to [BaseMenu], - * with a few additional methods, such as finishing the selection operation. - * Internally, it uses an [ActionMode]. - */ -abstract class BaseSelectionMenu { - private var actionMode: ActionMode? = null - - /** - * Finishes the selection operation. - */ - fun finish() { - actionMode?.finish() - } - - /** - * Declare that the menu has changed, and should be recreated. - */ - fun invalidate() { - actionMode?.invalidate() - } - - /** - * Called when the menu is first displayed. - * - * This method should not be overridden. The application should override - * the methods onCreate(Menu) and getMenuResourceId instead. - * - * @param inflater a menu inflater, for creating the menu - * @param mode the action mode associated with this menu. - * @param menu the menu that is being created. - */ - fun onCreate(inflater: MenuInflater, mode: ActionMode, menu: Menu) { - actionMode = mode - inflater.inflate(getResourceId(), menu) - onCreate(menu) - } - - /** - * Called when the selection operation is about to finish. - */ - open fun onFinish() {} - - /** - * Called whenever an item on the menu is selected. - * - * @param item the item that was selected. - * @return true if the event was consumed, or false otherwise - */ - open fun onItemClicked(item: MenuItem): Boolean = false - - /** - * Called whenever the menu is invalidated. - * - * @param menu the menu to be refreshed - * @return true if the menu has changes, false otherwise - */ - open fun onPrepare(menu: Menu): Boolean = false - - /** - * Sets the title of the selection menu. - * - * @param title the new title. - */ - fun setTitle(title: String?) { - actionMode?.title = title - } - - protected abstract fun getResourceId(): Int - - /** - * Called when the menu is first created. - * - * @param menu the menu being created - */ - protected fun onCreate(menu: Menu) {} -} \ No newline at end of file diff --git a/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsScreen.kt b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsScreen.kt index 9bde84fe8..c2362641a 100644 --- a/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsScreen.kt +++ b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsScreen.kt @@ -66,8 +66,7 @@ class ListHabitsScreen private val confirmSyncKeyDialogFactory: ConfirmSyncKeyDialogFactory, private val colorPickerFactory: ColorPickerDialogFactory, private val numberPickerFactory: NumberPickerFactory, - private val behavior: Lazy, - private val selectionMenu: Lazy + private val behavior: Lazy ) : BaseScreen(activity), CommandRunner.Listener, ListHabitsBehavior.Screen, @@ -79,7 +78,6 @@ class ListHabitsScreen } fun onAttached() { - setSelectionMenu(selectionMenu.get()) commandRunner.addListener(this) if(activity.intent.action == "android.intent.action.VIEW") { val uri = activity.intent.data!!.toString() diff --git a/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsSelectionMenu.kt b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsSelectionMenu.kt index 65c75cc01..3bcc08a31 100644 --- a/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsSelectionMenu.kt +++ b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsSelectionMenu.kt @@ -20,6 +20,7 @@ package org.isoron.uhabits.activities.habits.list import android.view.* +import androidx.appcompat.view.ActionMode import dagger.* import org.isoron.androidbase.activities.* import org.isoron.uhabits.* @@ -33,21 +34,55 @@ import javax.inject.* @ActivityScope class ListHabitsSelectionMenu @Inject constructor( - private val screen: ListHabitsScreen, + private val activity: BaseActivity, private val listAdapter: HabitCardListAdapter, var commandRunner: CommandRunner, private val prefs: Preferences, private val behavior: ListHabitsSelectionMenuBehavior, private val listController: Lazy, private val notificationTray: NotificationTray -) : BaseSelectionMenu() { +) : ActionMode.Callback { - override fun onFinish() { + var activeActionMode: ActionMode? = null + + fun onSelectionStart() { + activity.startSupportActionMode(this) + } + + fun onSelectionChange() { + activeActionMode?.invalidate() + } + + fun onSelectionFinish() { + activeActionMode?.finish() + } + + override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean { + activeActionMode = mode + activity.menuInflater.inflate(R.menu.list_habits_selection, menu) + return true + } + + override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean { + val itemEdit = menu.findItem(R.id.action_edit_habit) + val itemColor = menu.findItem(R.id.action_color) + val itemArchive = menu.findItem(R.id.action_archive_habit) + val itemUnarchive = menu.findItem(R.id.action_unarchive_habit) + val itemNotify = menu.findItem(R.id.action_notify) + + itemColor.isVisible = true + itemEdit.isVisible = behavior.canEdit() + itemArchive.isVisible = behavior.canArchive() + itemUnarchive.isVisible = behavior.canUnarchive() + itemNotify.isVisible = prefs.isDeveloper + activeActionMode?.title = listAdapter.selected.size.toString() + return true + } + override fun onDestroyActionMode(mode: ActionMode?) { listController.get().onSelectionFinished() - super.onFinish() } - override fun onItemClicked(item: MenuItem): Boolean { + override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { when (item.itemId) { R.id.action_edit_habit -> { behavior.onEditHabits() @@ -75,7 +110,7 @@ class ListHabitsSelectionMenu @Inject constructor( } R.id.action_notify -> { - for(h in listAdapter.selected) + for (h in listAdapter.selected) notificationTray.show(h, DateUtils.getToday(), 0) return true } @@ -83,26 +118,4 @@ class ListHabitsSelectionMenu @Inject constructor( else -> return false } } - - override fun onPrepare(menu: Menu): Boolean { - val itemEdit = menu.findItem(R.id.action_edit_habit) - val itemColor = menu.findItem(R.id.action_color) - val itemArchive = menu.findItem(R.id.action_archive_habit) - val itemUnarchive = menu.findItem(R.id.action_unarchive_habit) - val itemNotify = menu.findItem(R.id.action_notify) - - itemColor.isVisible = true - itemEdit.isVisible = behavior.canEdit() - itemArchive.isVisible = behavior.canArchive() - itemUnarchive.isVisible = behavior.canUnarchive() - setTitle(Integer.toString(listAdapter.selected.size)) - itemNotify.isVisible = prefs.isDeveloper - - return true - } - - fun onSelectionStart() = screen.startSelection() - fun onSelectionChange() = invalidate() - fun onSelectionFinish() = finish() - override fun getResourceId() = R.menu.list_habits_selection } diff --git a/android/uhabits-android/src/main/res/values/styles.xml b/android/uhabits-android/src/main/res/values/styles.xml index 3199ce797..f5471b3b4 100644 --- a/android/uhabits-android/src/main/res/values/styles.xml +++ b/android/uhabits-android/src/main/res/values/styles.xml @@ -25,8 +25,9 @@ ?attr/colorPrimary ?attr/highContrastReverseTextColor + true - @color/blue_grey_700 + @color/grey_700 @style/ThemeOverlay.AppCompat.Light @drawable/selected_box_light