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 e685c3e9c9..ac011f761b 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 @@ -11,7 +11,7 @@ import org.mozilla.fenix.mvi.UIComponent import org.mozilla.fenix.mvi.ViewState import java.net.URL -data class HistoryItem(val url: String) { +data class HistoryItem(val id: Int, val url: String) { val title: String get() = siteTitle() @@ -38,7 +38,16 @@ class HistoryComponent( override val reducer: (HistoryState, HistoryChange) -> HistoryState = { state, change -> when (change) { is HistoryChange.Change -> state.copy(items = change.list) - is HistoryChange.Mode-> state.copy(mode = change.mode) + is HistoryChange.EnterEditMode -> state.copy(mode = HistoryState.Mode.Editing(listOf(change.item))) + is HistoryChange.AddItemForRemoval -> { + val mode = state.mode + if (mode is HistoryState.Mode.Editing) { + val items = mode.selectedItems + listOf(change.item) + state.copy(mode = mode.copy(selectedItems = items)) + } else { + state + } + } } } @@ -52,16 +61,18 @@ class HistoryComponent( data class HistoryState(val items: List, val mode: Mode) : ViewState { sealed class Mode { object Normal : Mode() - object Editing : Mode() + data class Editing(val selectedItems: List) : Mode() } } sealed class HistoryAction : Action { data class Select(val item: HistoryItem) : HistoryAction() - data class Edit(val item: HistoryItem) : HistoryAction() + data class EnterEditMode(val item: HistoryItem) : HistoryAction() + data class AddItemForRemoval(val item: HistoryItem) : HistoryAction() } sealed class HistoryChange : Change { data class Change(val list: List) : HistoryChange() - data class Mode(val mode: HistoryState.Mode) : HistoryChange() + data class EnterEditMode(val item: HistoryItem) : HistoryChange() + data class AddItemForRemoval(val item: HistoryItem) : HistoryChange() } diff --git a/app/src/main/java/org/mozilla/fenix/library/history/HistoryFragment.kt b/app/src/main/java/org/mozilla/fenix/library/history/HistoryFragment.kt index 0dbc1a658e..43e1372cbf 100644 --- a/app/src/main/java/org/mozilla/fenix/library/history/HistoryFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/library/history/HistoryFragment.kt @@ -58,7 +58,10 @@ class HistoryFragment : Fragment(), CoroutineScope { .subscribe { when (it) { is HistoryAction.Select -> selectItem(it.item) - is HistoryAction.Edit -> getManagedEmitter().onNext(HistoryChange.Mode(HistoryState.Mode.Editing)) + is HistoryAction.EnterEditMode -> getManagedEmitter() + .onNext(HistoryChange.EnterEditMode(it.item)) + is HistoryAction.AddItemForRemoval -> getManagedEmitter() + .onNext(HistoryChange.AddItemForRemoval(it.item)) } } } @@ -90,7 +93,8 @@ class HistoryFragment : Fragment(), CoroutineScope { val eventEmitter = ActionBusFactory.get(this) launch(Dispatchers.IO) { - val items = requireComponents.core.historyStorage.getVisited().map { HistoryItem(it) } + val items = requireComponents.core.historyStorage.getVisited() + .mapIndexed { id, item -> HistoryItem(id, item) } launch(Dispatchers.Main) { eventEmitter.emit(HistoryChange::class.java, HistoryChange.Change(items)) diff --git a/app/src/main/java/org/mozilla/fenix/library/history/HistoryUIView.kt b/app/src/main/java/org/mozilla/fenix/library/history/HistoryUIView.kt index 2115f41e11..25370527a5 100644 --- a/app/src/main/java/org/mozilla/fenix/library/history/HistoryUIView.kt +++ b/app/src/main/java/org/mozilla/fenix/library/history/HistoryUIView.kt @@ -38,8 +38,6 @@ class HistoryUIView( } override fun updateView() = Consumer { - val isEditing = it.mode == HistoryState.Mode.Editing - (view.adapter as HistoryAdapter).updateData(it.items, it.mode) } } @@ -68,11 +66,17 @@ private class HistoryAdapter( view.setOnLongClickListener { item?.apply { - actionEmitter.onNext(HistoryAction.Edit(this)) + actionEmitter.onNext(HistoryAction.EnterEditMode(this)) } true } + + checkbox.setOnClickListener { + item?.apply { + actionEmitter.onNext(HistoryAction.AddItemForRemoval(this)) + } + } } fun bind(item: HistoryItem, mode: HistoryState.Mode) { @@ -82,9 +86,13 @@ private class HistoryAdapter( title.text = item.title url.text = item.url - val isEditing = mode == HistoryState.Mode.Editing + val isEditing = mode is HistoryState.Mode.Editing checkbox.visibility = if (isEditing) { View.VISIBLE } else { View.GONE } favicon.visibility = if (isEditing) { View.INVISIBLE } else { View.VISIBLE } + + if (mode is HistoryState.Mode.Editing) { + checkbox.isChecked = mode.selectedItems.contains(item) + } } companion object { diff --git a/app/src/main/res/layout/history_list_item.xml b/app/src/main/res/layout/history_list_item.xml index ca4606fbbc..efd9da46de 100644 --- a/app/src/main/res/layout/history_list_item.xml +++ b/app/src/main/res/layout/history_list_item.xml @@ -23,7 +23,7 @@ android:layout_height="@dimen/history_favicon_width_height" android:background="@drawable/history_favicon_background" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toTopOf="parent"/> + app:layout_constraintTop_toTopOf="parent" />