For #357 - Adds an editing mode to the history component
parent
9bf0354d02
commit
0cbcd93208
|
@ -28,7 +28,7 @@ data class HistoryItem(val url: String) {
|
|||
class HistoryComponent(
|
||||
private val container: ViewGroup,
|
||||
bus: ActionBusFactory,
|
||||
override var initialState: HistoryState = HistoryState(emptyList())
|
||||
override var initialState: HistoryState = HistoryState(emptyList(), HistoryState.Mode.Normal)
|
||||
) :
|
||||
UIComponent<HistoryState, HistoryAction, HistoryChange>(
|
||||
bus.getManagedEmitter(HistoryAction::class.java),
|
||||
|
@ -38,6 +38,7 @@ 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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,12 +49,19 @@ class HistoryComponent(
|
|||
}
|
||||
}
|
||||
|
||||
data class HistoryState(val items: List<HistoryItem>) : ViewState
|
||||
data class HistoryState(val items: List<HistoryItem>, val mode: Mode) : ViewState {
|
||||
sealed class Mode {
|
||||
object Normal : Mode()
|
||||
object Editing : Mode()
|
||||
}
|
||||
}
|
||||
|
||||
sealed class HistoryAction : Action {
|
||||
data class Select(val item: HistoryItem) : HistoryAction()
|
||||
data class Edit(val item: HistoryItem) : HistoryAction()
|
||||
}
|
||||
|
||||
sealed class HistoryChange : Change {
|
||||
data class Change(val list: List<HistoryItem>) : HistoryChange()
|
||||
data class Mode(val mode: HistoryState.Mode) : HistoryChange()
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ import org.mozilla.fenix.HomeActivity
|
|||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.ext.requireComponents
|
||||
import org.mozilla.fenix.mvi.ActionBusFactory
|
||||
import org.mozilla.fenix.mvi.getManagedEmitter
|
||||
import org.mozilla.fenix.mvi.getSafeManagedObservable
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
|
@ -40,7 +41,7 @@ class HistoryFragment : Fragment(), CoroutineScope {
|
|||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
val view = inflater.inflate(R.layout.fragment_history, container, false)
|
||||
HistoryComponent(view.history_layout, ActionBusFactory.get(this), initialState = HistoryState(emptyList()))
|
||||
HistoryComponent(view.history_layout, ActionBusFactory.get(this))
|
||||
|
||||
return view
|
||||
}
|
||||
|
@ -55,20 +56,25 @@ class HistoryFragment : Fragment(), CoroutineScope {
|
|||
|
||||
getSafeManagedObservable<HistoryAction>()
|
||||
.subscribe {
|
||||
if (it is HistoryAction.Select) {
|
||||
Navigation.findNavController(requireActivity(), R.id.container).apply {
|
||||
navigate(
|
||||
HistoryFragmentDirections.actionGlobalBrowser(null,
|
||||
(activity as HomeActivity).browsingModeManager.isPrivate),
|
||||
NavOptions.Builder().setPopUpTo(R.id.homeFragment, false).build()
|
||||
)
|
||||
}
|
||||
|
||||
requireComponents.useCases.sessionUseCases.loadUrl.invoke(it.item.url)
|
||||
when (it) {
|
||||
is HistoryAction.Select -> selectItem(it.item)
|
||||
is HistoryAction.Edit -> getManagedEmitter<HistoryChange>().onNext(HistoryChange.Mode(HistoryState.Mode.Editing))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun selectItem(item: HistoryItem) {
|
||||
Navigation.findNavController(requireActivity(), R.id.container).apply {
|
||||
navigate(
|
||||
HistoryFragmentDirections.actionGlobalBrowser(null,
|
||||
(activity as HomeActivity).browsingModeManager.isPrivate),
|
||||
NavOptions.Builder().setPopUpTo(R.id.homeFragment, false).build()
|
||||
)
|
||||
}
|
||||
|
||||
requireComponents.useCases.sessionUseCases.loadUrl.invoke(item.url)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
job.cancel()
|
||||
|
|
|
@ -8,6 +8,8 @@ package org.mozilla.fenix.library.history
|
|||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.CheckBox
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
|
@ -36,7 +38,9 @@ class HistoryUIView(
|
|||
}
|
||||
|
||||
override fun updateView() = Consumer<HistoryState> {
|
||||
(view.adapter as HistoryAdapter).updateData(it.items)
|
||||
val isEditing = it.mode == HistoryState.Mode.Editing
|
||||
|
||||
(view.adapter as HistoryAdapter).updateData(it.items, it.mode)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,9 +52,12 @@ private class HistoryAdapter(
|
|||
private val actionEmitter: Observer<HistoryAction>
|
||||
) : RecyclerView.ViewHolder(view) {
|
||||
|
||||
private var title = view.findViewById<TextView>(R.id.history_title)
|
||||
private var url = view.findViewById<TextView>(R.id.history_url)
|
||||
private val checkbox = view.findViewById<CheckBox>(R.id.should_remove_checkbox)
|
||||
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 var item: HistoryItem? = null
|
||||
private var mode: HistoryState.Mode = HistoryState.Mode.Normal
|
||||
|
||||
init {
|
||||
view.setOnClickListener {
|
||||
|
@ -58,13 +65,26 @@ private class HistoryAdapter(
|
|||
actionEmitter.onNext(HistoryAction.Select(this))
|
||||
}
|
||||
}
|
||||
|
||||
view.setOnLongClickListener {
|
||||
item?.apply {
|
||||
actionEmitter.onNext(HistoryAction.Edit(this))
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fun bind(item: HistoryItem) {
|
||||
fun bind(item: HistoryItem, mode: HistoryState.Mode) {
|
||||
this.item = item
|
||||
this.mode = mode
|
||||
|
||||
title.text = item.title
|
||||
url.text = item.url
|
||||
|
||||
val isEditing = mode == HistoryState.Mode.Editing
|
||||
checkbox.visibility = if (isEditing) { View.VISIBLE } else { View.GONE }
|
||||
favicon.visibility = if (isEditing) { View.INVISIBLE } else { View.VISIBLE }
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
@ -87,9 +107,11 @@ private class HistoryAdapter(
|
|||
}
|
||||
|
||||
private var items: List<HistoryItem> = emptyList()
|
||||
private var mode: HistoryState.Mode = HistoryState.Mode.Normal
|
||||
|
||||
fun updateData(items: List<HistoryItem>) {
|
||||
fun updateData(items: List<HistoryItem>, mode: HistoryState.Mode) {
|
||||
this.items = items
|
||||
this.mode = mode
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
|
@ -116,7 +138,7 @@ private class HistoryAdapter(
|
|||
|
||||
when (holder) {
|
||||
is HistoryHeaderViewHolder -> holder.bind("Today")
|
||||
is HistoryListItemViewHolder -> holder.bind(items[position - NUMBER_OF_SECTIONS])
|
||||
is HistoryListItemViewHolder -> holder.bind(items[position - NUMBER_OF_SECTIONS], mode)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,15 @@
|
|||
android:paddingStart="20dp"
|
||||
android:background="?android:attr/selectableItemBackground"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/should_remove_checkbox"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="2dp"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@id/history_title" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/history_favicon"
|
||||
android:layout_width="@dimen/history_favicon_width_height"
|
||||
|
|
Loading…
Reference in New Issue