mirror of
https://github.com/fork-maintainers/iceraven-browser
synced 2024-11-19 09:25:34 +00:00
[fenix] For https://github.com/mozilla-mobile/fenix/issues/357 - Adds menu to delete a single item
This commit is contained in:
parent
502dc1257d
commit
d68e21c9ad
@ -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<HistoryAction>
|
||||
@ -28,7 +30,10 @@ class HistoryAdapter(
|
||||
private val favicon = view.findViewById<ImageView>(R.id.history_favicon)
|
||||
private val title = view.findViewById<TextView>(R.id.history_title)
|
||||
private val url = view.findViewById<TextView>(R.id.history_url)
|
||||
private val menuButton = view.findViewById<ImageButton>(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
|
||||
|
@ -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<HistoryItem>) : Delete()
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
@ -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"
|
||||
|
@ -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" />
|
||||
|
||||
<ImageButton
|
||||
android:id="@+id/overflow_button"
|
||||
android:id="@+id/history_item_overflow"
|
||||
android:layout_width="@dimen/glyph_button_height"
|
||||
android:layout_height="@dimen/glyph_button_height"
|
||||
android:background="?android:attr/selectableItemBackgroundBorderless"
|
||||
|
@ -147,10 +147,13 @@
|
||||
<string name="current_session_send_and_share">Send and Share</string>
|
||||
<string name="current_session_image">Current session image</string>
|
||||
|
||||
<!-- Text for the button to clear all history -->
|
||||
<string name="delete_history">Delete History</string>
|
||||
|
||||
|
||||
<!-- Text for the button to clear all history -->
|
||||
<string name="history_delete_all">Delete History</string>
|
||||
<!-- Text for the button to delete a single history item -->
|
||||
<string name="history_delete_item">Delete</string>
|
||||
<!-- Text for the button to clear selected history items. The first parameter
|
||||
is the number of items you have selected -->
|
||||
<string name="delete_history_items">Delete %1$d Items</string>
|
||||
<string name="history_delete_some">Delete %1$d Items</string>
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user