2
0
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 ability to toggle checkbox by selecting item

This commit is contained in:
Jeff Boek 2019-02-13 15:14:10 -08:00
parent 53851d38fc
commit 54d8091a0f
3 changed files with 37 additions and 5 deletions

View File

@ -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<HistoryItem>) : HistoryChange()
data class EnterEditMode(val item: HistoryItem) : HistoryChange()
data class AddItemForRemoval(val item: HistoryItem) : HistoryChange()
data class RemoveItemForRemoval(val item: HistoryItem) : HistoryChange()
}

View File

@ -62,6 +62,8 @@ class HistoryFragment : Fragment(), CoroutineScope {
.onNext(HistoryChange.EnterEditMode(it.item))
is HistoryAction.AddItemForRemoval -> getManagedEmitter<HistoryChange>()
.onNext(HistoryChange.AddItemForRemoval(it.item))
is HistoryAction.RemoveItemForRemoval -> getManagedEmitter<HistoryChange>()
.onNext(HistoryChange.RemoveItemForRemoval(it.item))
}
}
}

View File

@ -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<TextView>(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)
}
}