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 ac011f761b..6cb027ed06 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 @@ -48,6 +48,15 @@ class HistoryComponent( state } } + is HistoryChange.RemoveItemForRemoval -> { + val mode = state.mode + if (mode is HistoryState.Mode.Editing) { + val items = mode.selectedItems.filter { it.id != change.item.id } + state.copy(mode = mode.copy(selectedItems = items)) + } else { + state + } + } } } @@ -69,10 +78,12 @@ sealed class HistoryAction : Action { data class Select(val item: HistoryItem) : HistoryAction() data class EnterEditMode(val item: HistoryItem) : HistoryAction() data class AddItemForRemoval(val item: HistoryItem) : HistoryAction() + data class RemoveItemForRemoval(val item: HistoryItem) : HistoryAction() } sealed class HistoryChange : Change { data class Change(val list: List) : HistoryChange() data class EnterEditMode(val item: HistoryItem) : HistoryChange() data class AddItemForRemoval(val item: HistoryItem) : HistoryChange() + data class RemoveItemForRemoval(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 43e1372cbf..0e3b6a777a 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 @@ -62,6 +62,8 @@ class HistoryFragment : Fragment(), CoroutineScope { .onNext(HistoryChange.EnterEditMode(it.item)) is HistoryAction.AddItemForRemoval -> getManagedEmitter() .onNext(HistoryChange.AddItemForRemoval(it.item)) + is HistoryAction.RemoveItemForRemoval -> getManagedEmitter() + .onNext(HistoryChange.RemoveItemForRemoval(it.item)) } } } 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 25370527a5..dcf4fbfebc 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 @@ -9,6 +9,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.CheckBox +import android.widget.CompoundButton import android.widget.ImageView import android.widget.TextView import androidx.recyclerview.widget.LinearLayoutManager @@ -56,9 +57,29 @@ private class HistoryAdapter( private val url = view.findViewById(R.id.history_url) private var item: HistoryItem? = null private var mode: HistoryState.Mode = HistoryState.Mode.Normal + private val checkListener = object : CompoundButton.OnCheckedChangeListener { + override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) { + if (mode is HistoryState.Mode.Normal) { return } + + item?.apply { + val action = if (isChecked) { + HistoryAction.AddItemForRemoval(this) + } else { + HistoryAction.RemoveItemForRemoval(this) + } + + actionEmitter.onNext(action) + } + } + } init { view.setOnClickListener { + if (mode is HistoryState.Mode.Editing) { + checkbox.isChecked = !checkbox.isChecked + return@setOnClickListener + } + item?.apply { actionEmitter.onNext(HistoryAction.Select(this)) } @@ -72,11 +93,7 @@ private class HistoryAdapter( true } - checkbox.setOnClickListener { - item?.apply { - actionEmitter.onNext(HistoryAction.AddItemForRemoval(this)) - } - } + checkbox.setOnCheckedChangeListener(checkListener) } fun bind(item: HistoryItem, mode: HistoryState.Mode) { @@ -91,7 +108,9 @@ private class HistoryAdapter( favicon.visibility = if (isEditing) { View.INVISIBLE } else { View.VISIBLE } if (mode is HistoryState.Mode.Editing) { + checkbox.setOnCheckedChangeListener(null) checkbox.isChecked = mode.selectedItems.contains(item) + checkbox.setOnCheckedChangeListener(checkListener) } }