From d68e21c9add33d40d51baa2b8f57540d01a2ca5c Mon Sep 17 00:00:00 2001 From: Jeff Boek Date: Fri, 15 Feb 2019 14:19:15 -0800 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/357 - Adds menu to delete a single item --- .../fenix/library/history/HistoryAdapter.kt | 27 ++++++++++++++-- .../fenix/library/history/HistoryComponent.kt | 1 + .../fenix/library/history/HistoryItemMenu.kt | 31 +++++++++++++++++++ app/src/main/res/layout/history_delete.xml | 2 +- app/src/main/res/layout/history_list_item.xml | 4 +-- app/src/main/res/values/strings.xml | 9 ++++-- 6 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/org/mozilla/fenix/library/history/HistoryItemMenu.kt diff --git a/app/src/main/java/org/mozilla/fenix/library/history/HistoryAdapter.kt b/app/src/main/java/org/mozilla/fenix/library/history/HistoryAdapter.kt index 01f308391c..2abc5e21f9 100644 --- a/app/src/main/java/org/mozilla/fenix/library/history/HistoryAdapter.kt +++ b/app/src/main/java/org/mozilla/fenix/library/history/HistoryAdapter.kt @@ -9,12 +9,14 @@ import android.view.View import android.view.ViewGroup import android.widget.CheckBox import android.widget.CompoundButton +import android.widget.ImageButton import android.widget.ImageView import android.widget.TextView import androidx.recyclerview.widget.RecyclerView import io.reactivex.Observer import org.mozilla.fenix.R import androidx.core.content.ContextCompat +import mozilla.components.browser.menu.BrowserMenu class HistoryAdapter( private val actionEmitter: Observer @@ -28,7 +30,10 @@ class HistoryAdapter( private val favicon = view.findViewById(R.id.history_favicon) private val title = view.findViewById(R.id.history_title) private val url = view.findViewById(R.id.history_url) + private val menuButton = view.findViewById(R.id.history_item_overflow) + private var item: HistoryItem? = null + private lateinit var historyMenu: HistoryItemMenu private var mode: HistoryState.Mode = HistoryState.Mode.Normal private val checkListener = CompoundButton.OnCheckedChangeListener { _, isChecked -> if (mode is HistoryState.Mode.Normal) { @@ -47,6 +52,8 @@ class HistoryAdapter( } init { + setupMenu() + view.setOnClickListener { if (mode is HistoryState.Mode.Editing) { checkbox.isChecked = !checkbox.isChecked @@ -66,6 +73,12 @@ class HistoryAdapter( true } + menuButton.setOnClickListener { + historyMenu.menuBuilder.build(view.context).show( + anchor = it, + orientation = BrowserMenu.Orientation.DOWN) + } + checkbox.setOnCheckedChangeListener(checkListener) } @@ -93,6 +106,16 @@ class HistoryAdapter( } } + private fun setupMenu() { + this.historyMenu = HistoryItemMenu(itemView.context) { + when (it) { + is HistoryItemMenu.Item.Delete -> { + item?.apply { actionEmitter.onNext(HistoryAction.Delete.One(this)) } + } + } + } + } + companion object { const val LAYOUT_ID = R.layout.history_list_item } @@ -140,9 +163,9 @@ class HistoryAdapter( this.mode = mode val text = if (mode is HistoryState.Mode.Editing && mode.selectedItems.isNotEmpty()) { - text.context.resources.getString(R.string.delete_history_items, mode.selectedItems.size) + text.context.resources.getString(R.string.history_delete_some, mode.selectedItems.size) } else { - text.context.resources.getString(R.string.delete_history) + text.context.resources.getString(R.string.history_delete_all) } button.contentDescription = text diff --git a/app/src/main/java/org/mozilla/fenix/library/history/HistoryComponent.kt b/app/src/main/java/org/mozilla/fenix/library/history/HistoryComponent.kt index e5b75055d2..5197e722b8 100644 --- a/app/src/main/java/org/mozilla/fenix/library/history/HistoryComponent.kt +++ b/app/src/main/java/org/mozilla/fenix/library/history/HistoryComponent.kt @@ -84,6 +84,7 @@ sealed class HistoryAction : Action { sealed class Delete : HistoryAction() { object All : Delete() + data class One(val item: HistoryItem) : Delete() data class Some(val items: List) : Delete() } } diff --git a/app/src/main/java/org/mozilla/fenix/library/history/HistoryItemMenu.kt b/app/src/main/java/org/mozilla/fenix/library/history/HistoryItemMenu.kt new file mode 100644 index 0000000000..2ba9e6d0a9 --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/library/history/HistoryItemMenu.kt @@ -0,0 +1,31 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.fenix.library.history + +import android.content.Context +import mozilla.components.browser.menu.BrowserMenuBuilder +import mozilla.components.browser.menu.item.SimpleBrowserMenuItem +import org.mozilla.fenix.R + +class HistoryItemMenu( + private val context: Context, + private val onItemTapped: (Item) -> Unit = {} +) { + sealed class Item { + object Delete : Item() + } + + val menuBuilder by lazy { BrowserMenuBuilder(menuItems) } + + private val menuItems by lazy { + listOf( + SimpleBrowserMenuItem( + context.getString(R.string.history_delete_item) + ) { + onItemTapped.invoke(Item.Delete) + } + ) + } +} diff --git a/app/src/main/res/layout/history_delete.xml b/app/src/main/res/layout/history_delete.xml index abefe9f8bc..5cf27420ea 100644 --- a/app/src/main/res/layout/history_delete.xml +++ b/app/src/main/res/layout/history_delete.xml @@ -17,7 +17,7 @@ android:id="@+id/delete_history_button_text" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="@string/delete_history" + android:text="@string/history_delete_all" android:textColor="@color/photonRed60" android:drawablePadding="8dp" android:textSize="16sp" diff --git a/app/src/main/res/layout/history_list_item.xml b/app/src/main/res/layout/history_list_item.xml index c52978e7f6..086a92d709 100644 --- a/app/src/main/res/layout/history_list_item.xml +++ b/app/src/main/res/layout/history_list_item.xml @@ -56,11 +56,11 @@ android:ellipsize="end" app:layout_constraintStart_toEndOf="@id/history_favicon" app:layout_constraintTop_toBottomOf="@id/history_title" - app:layout_constraintEnd_toStartOf="@id/overflow_button" + app:layout_constraintEnd_toStartOf="@id/history_item_overflow" app:layout_constraintBottom_toBottomOf="parent" /> Send and Share Current session image - - Delete History + + + Delete History + + Delete - Delete %1$d Items + Delete %1$d Items