Tim Malseed
03/30/2021, 11:15 AMTim Malseed
03/30/2021, 11:16 AMval screenState = appRepository
.getRoutines(listOf(routineId))
.mapNotNull { routines -> routines.firstOrNull() }
.map { routine -> ScreenState.Ready(routine) } // Seems to emit every time a field changes in Routine
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), ScreenState.Loading) // Seems to swallow subsequent emissions that are equal
The data class:
data class Routine(
val id: Long,
val order: Int,
val name: String,
val exercises: List<RoutineExercise>
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Routine
if (id != other.id) return false
return true
}
override fun hashCode(): Int {
return id.hashCode()
}
}
ScreenState:
sealed class ScreenState {
object Loading : ScreenState()
data class Ready(val routine: Routine) : ScreenState()
}
The ShareIn attempt:
val screenState = appRepository
.getRoutines(listOf(routineId))
.mapNotNull { routines -> routines.firstOrNull() }
.map { routine -> ScreenState.Ready(routine) }
.onStart { ScreenState.Loading }
.shareIn(viewModelScope, SharingStarted.WhileSubscribed(), 1) // Seems to swallow subsequent emissions that are equal as per StateIn
Tim Malseed
03/30/2021, 11:20 AMdistinctBy
policy (conflation policy?) But I’m a little out of my league here.Tim Malseed
03/30/2021, 11:57 AMshareIn
is not conflating subsequent emissions, which would mean the lack of recomposition is a Compose problem, rather than a Coroutine problem.Tim Malseed
03/30/2021, 11:58 AMval screenState: ScreenState by viewModel.screenState.collectAsState(ScreenState.Loading)
It looks like subsequent emissions of `viewModel.screenStat`` do not trigger a recomposition, possibly because of the abovementioned equality predicate..?