ursus
03/10/2020, 3:21 AMdata class State(
val items: List<Item> = emptyList(),
...
)
sealed class Item
data class HeaderItem(val titleRes: Int) : Item()
sealed class ServiceItem : Item() { ..
data class SimpleItem(..) : ServiceItem
data class FormItem(val textItems: List<TextItem> = emptyList()) : ServiceItem
-- lets say now I need to change text item inside a form item
with mutable models code is pretty neat in kotlin
state.items
.firstOrNull { it is FormItem && item.service.id == serviceId }
?.let { it as FormItem }
?.textItems
?.firstOrNull { it.id == textItemId }
?.apply {
value = text
validity = FormItem.TextItem.Validity.PENDING
}
with immutable obviously you need to create copies
setState {
copy(
items = items.map { item ->
if (item is FormItem && item.service.id == serviceId) {
item.copy(
textItems = item.textItems.map {
if (it.id == textItemId) {
it.copy(value = text, validity = FormItem.TextItem.Validity.PENDING)
} else {
it
}
})
} else {
item
}
}
)
}
elizarov
03/10/2020, 8:14 AMcarbaj0
03/10/2020, 11:52 AMJavier Troconis
03/10/2020, 2:12 PM