For #357 - Adds ability to toggle checkbox by selecting item
parent
b7c7fd3100
commit
0cc44d1ee2
|
@ -48,6 +48,15 @@ class HistoryComponent(
|
||||||
state
|
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 Select(val item: HistoryItem) : HistoryAction()
|
||||||
data class EnterEditMode(val item: HistoryItem) : HistoryAction()
|
data class EnterEditMode(val item: HistoryItem) : HistoryAction()
|
||||||
data class AddItemForRemoval(val item: HistoryItem) : HistoryAction()
|
data class AddItemForRemoval(val item: HistoryItem) : HistoryAction()
|
||||||
|
data class RemoveItemForRemoval(val item: HistoryItem) : HistoryAction()
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed class HistoryChange : Change {
|
sealed class HistoryChange : Change {
|
||||||
data class Change(val list: List<HistoryItem>) : HistoryChange()
|
data class Change(val list: List<HistoryItem>) : HistoryChange()
|
||||||
data class EnterEditMode(val item: HistoryItem) : HistoryChange()
|
data class EnterEditMode(val item: HistoryItem) : HistoryChange()
|
||||||
data class AddItemForRemoval(val item: HistoryItem) : HistoryChange()
|
data class AddItemForRemoval(val item: HistoryItem) : HistoryChange()
|
||||||
|
data class RemoveItemForRemoval(val item: HistoryItem) : HistoryChange()
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,6 +62,8 @@ class HistoryFragment : Fragment(), CoroutineScope {
|
||||||
.onNext(HistoryChange.EnterEditMode(it.item))
|
.onNext(HistoryChange.EnterEditMode(it.item))
|
||||||
is HistoryAction.AddItemForRemoval -> getManagedEmitter<HistoryChange>()
|
is HistoryAction.AddItemForRemoval -> getManagedEmitter<HistoryChange>()
|
||||||
.onNext(HistoryChange.AddItemForRemoval(it.item))
|
.onNext(HistoryChange.AddItemForRemoval(it.item))
|
||||||
|
is HistoryAction.RemoveItemForRemoval -> getManagedEmitter<HistoryChange>()
|
||||||
|
.onNext(HistoryChange.RemoveItemForRemoval(it.item))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.widget.CheckBox
|
import android.widget.CheckBox
|
||||||
|
import android.widget.CompoundButton
|
||||||
import android.widget.ImageView
|
import android.widget.ImageView
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
|
@ -56,9 +57,29 @@ private class HistoryAdapter(
|
||||||
private val url = view.findViewById<TextView>(R.id.history_url)
|
private val url = view.findViewById<TextView>(R.id.history_url)
|
||||||
private var item: HistoryItem? = null
|
private var item: HistoryItem? = null
|
||||||
private var mode: HistoryState.Mode = HistoryState.Mode.Normal
|
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 {
|
init {
|
||||||
view.setOnClickListener {
|
view.setOnClickListener {
|
||||||
|
if (mode is HistoryState.Mode.Editing) {
|
||||||
|
checkbox.isChecked = !checkbox.isChecked
|
||||||
|
return@setOnClickListener
|
||||||
|
}
|
||||||
|
|
||||||
item?.apply {
|
item?.apply {
|
||||||
actionEmitter.onNext(HistoryAction.Select(this))
|
actionEmitter.onNext(HistoryAction.Select(this))
|
||||||
}
|
}
|
||||||
|
@ -72,11 +93,7 @@ private class HistoryAdapter(
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
checkbox.setOnClickListener {
|
checkbox.setOnCheckedChangeListener(checkListener)
|
||||||
item?.apply {
|
|
||||||
actionEmitter.onNext(HistoryAction.AddItemForRemoval(this))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun bind(item: HistoryItem, mode: HistoryState.Mode) {
|
fun bind(item: HistoryItem, mode: HistoryState.Mode) {
|
||||||
|
@ -91,7 +108,9 @@ private class HistoryAdapter(
|
||||||
favicon.visibility = if (isEditing) { View.INVISIBLE } else { View.VISIBLE }
|
favicon.visibility = if (isEditing) { View.INVISIBLE } else { View.VISIBLE }
|
||||||
|
|
||||||
if (mode is HistoryState.Mode.Editing) {
|
if (mode is HistoryState.Mode.Editing) {
|
||||||
|
checkbox.setOnCheckedChangeListener(null)
|
||||||
checkbox.isChecked = mode.selectedItems.contains(item)
|
checkbox.isChecked = mode.selectedItems.contains(item)
|
||||||
|
checkbox.setOnCheckedChangeListener(checkListener)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue