Pablo
02/10/2025, 11:28 AMPablo
02/10/2025, 11:28 AMdata class UiState(
val loading: Boolean = false,
val data: List<Item> = emptyList(),
val favorites: List<Int> = emptyList(),
val selectedItem: Item? = null
)
Into this:
data class UiState(
val loading: Boolean = false,
val mapState: MapState = MapState()
)
data class MapState(
val data: List<Item> = emptyList(),
val favorites: List<Int> = emptyList(),
val selectedItem: Item? = null
)
Pablo
02/10/2025, 11:31 AM_uiState.value = _uiState.value.copy(
favorites = _uiState.value.favorites + item
)
now, with the separated class... is this the correct approach?
_uiState.value = _uiState.value.copy(
mapState = _uiState.value.mapState.copy(
favorites = _uiState.value.mapState.favorites + item
)
)
Zach Klippenstein (he/him) [MOD]
02/10/2025, 7:33 PMIs it correctIt's valid, "correctness" is more a matter of preference here. You're free to break up your values however you see fit
dorche
02/10/2025, 7:34 PM.value
call there this is a StateFlow) you might want to use .update
as described here https://www.droidcon.com/2021/08/25/make-sure-to-update-your-stateflow-safely-in-kotlin/Pablo
02/10/2025, 7:42 PMZach Klippenstein (he/him) [MOD]
02/10/2025, 7:47 PMZach Klippenstein (he/him) [MOD]
02/10/2025, 7:47 PMPablo
02/10/2025, 7:50 PMZach Klippenstein (he/him) [MOD]
02/10/2025, 8:22 PMZach Klippenstein (he/him) [MOD]
02/10/2025, 8:23 PMPablo
02/10/2025, 8:43 PMMitch Ware
02/13/2025, 3:00 PMZach Klippenstein (he/him) [MOD]
02/13/2025, 4:52 PMdo you mean having the variables inside the nested mapState class vars with mutablestateof? and not being the nested class itself a data class? but if I can remember that doesn't follows the recommended compose principlesYes that is what I mean and it absolutely follows compose principles, it’s intentionally designed to support exactly that.
Pablo
02/13/2025, 5:12 PMdata class UiState(
val loading: Boolean = false,
val mapState: MutableMapState = MutableMapState()
)
data class MutableMapState(
var data: MutableState<List<Item>> = mutableStateOf(emptyList()),
var favorites: MutableState<List<Int>> = mutableStateOf(emptyList()),
var selectedItem: MutableState<Item?> = mutableStateOf(null)
)
If you refeer to this, Is not breaking the compose immutable recomendation for uistates? Or you mean another thing? can you show me please?Zach Klippenstein (he/him) [MOD]
02/13/2025, 5:29 PMclass MutableMapState(
data: List<Item>,
favorites: List<Int>,
selectedItem: Item?
) {
var data by mutableStateOf(data)
var favorites by mutableStateOf(favorites)
var selectedItem by mutableStateOf(selectedItem)
}
Pablo
02/13/2025, 8:48 PMdata class UiState(
val loading: Boolean = false,
val nutableMapState: MutableMapState= MutableMapState()
)
class MutableMapState(
data: List<Item>,
favorites: List<Int>,
selectedItem: Item?
) {
var data by mutableStateOf(data)
var favorites by mutableStateOf(favorites)
var selectedItem by mutableStateOf(selectedItem)
}
And having this uistate:
private val _uiState = MutableStateFlow<UiState>(UiState(loading = true))
val uiState: StateFlow<UiState> = _uiState
for example, being on the viewmodel, how to update data list with one more item? or how to set selectedItem?Zach Klippenstein (he/him) [MOD]
02/13/2025, 9:59 PMZach Klippenstein (he/him) [MOD]
02/13/2025, 10:00 PM_uiState.update { apply { mutableMapState.data += newItem } }
Zach Klippenstein (he/him) [MOD]
02/13/2025, 10:02 PMupdate
prevents race conditions, but wouldn't be needed if _uiState
were also snapshot state.Pablo
02/14/2025, 4:45 PMPablo
02/14/2025, 4:46 PMZach Klippenstein (he/him) [MOD]
02/15/2025, 5:47 PMprivate var _uiState by mutableStateOf(UiState(loading = true))
val uiState: UiState get() = _uiState
Pablo
02/15/2025, 7:16 PMPablo
02/15/2025, 7:16 PMZach Klippenstein (he/him) [MOD]
02/16/2025, 6:14 PM