mirror of
https://github.com/fork-maintainers/iceraven-browser
synced 2024-11-17 15:26:23 +00:00
[fenix] For https://github.com/mozilla-mobile/fenix/issues/357 - Adds selected items to the history component state
This commit is contained in:
parent
b9d0109592
commit
53851d38fc
@ -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<HistoryItem>, val mode: Mode) : ViewState {
|
||||
sealed class Mode {
|
||||
object Normal : Mode()
|
||||
object Editing : Mode()
|
||||
data class Editing(val selectedItems: List<HistoryItem>) : 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<HistoryItem>) : HistoryChange()
|
||||
data class Mode(val mode: HistoryState.Mode) : HistoryChange()
|
||||
data class EnterEditMode(val item: HistoryItem) : HistoryChange()
|
||||
data class AddItemForRemoval(val item: HistoryItem) : HistoryChange()
|
||||
}
|
||||
|
@ -58,7 +58,10 @@ class HistoryFragment : Fragment(), CoroutineScope {
|
||||
.subscribe {
|
||||
when (it) {
|
||||
is HistoryAction.Select -> selectItem(it.item)
|
||||
is HistoryAction.Edit -> getManagedEmitter<HistoryChange>().onNext(HistoryChange.Mode(HistoryState.Mode.Editing))
|
||||
is HistoryAction.EnterEditMode -> getManagedEmitter<HistoryChange>()
|
||||
.onNext(HistoryChange.EnterEditMode(it.item))
|
||||
is HistoryAction.AddItemForRemoval -> getManagedEmitter<HistoryChange>()
|
||||
.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))
|
||||
|
@ -38,8 +38,6 @@ class HistoryUIView(
|
||||
}
|
||||
|
||||
override fun updateView() = Consumer<HistoryState> {
|
||||
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 {
|
||||
|
@ -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" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/history_title"
|
||||
|
Loading…
Reference in New Issue
Block a user