Hey all, I have a query related specifically to th...
# multiplatform
g
Hey all, I have a query related specifically to the Voyager ScreenModel library (v1.0.0). Is it a requirement to have the property to be observed as a class property instead of a function? If I have the flow defined as a property it works as expected, however it unexpectedly emits multiple items when defined as a function. Attaching code snippets in thread
Code emitting single item, as expected:
Copy code
internal data object HomeScreen : Screen {

    private val requests: Flow<GetRandomRant?> = MutableStateFlow(null)

    @Composable
    override fun Content() {
        val navigator = LocalNavigator.currentOrThrow
        val viewModel: HomeScreenViewModel = getScreenModel()

        val state by viewModel.models.collectAsState()

        Box(
            contentAlignment = Alignment.Center,
            modifier = Modifier.fillMaxSize()
        ) {
            Text(state)
        }
    }
}


internal class HomeScreenViewModel(
    private val repository: RantsRepository
) : ScreenModel {

    val models: StateFlow<Rant> = repository
        .getRandom()
        .retry(3)
        .catch { emit("") }
        .stateIn(screenModelScope, SharingStarted.WhileSubscribed(5000), "")
}
Code emitting an undefined (but not always same) amount of items:
Copy code
internal data object HomeScreen : Screen {

    private val requests: Flow<GetRandomRant?> = MutableStateFlow(null)

    @Composable
    override fun Content() {
        val navigator = LocalNavigator.currentOrThrow
        val viewModel: HomeScreenViewModel = getScreenModel()

        val state by viewModel.models().collectAsState()

        Box(
            contentAlignment = Alignment.Center,
            modifier = Modifier.fillMaxSize()
        ) {
            Text(state)
        }
    }
}


internal class HomeScreenViewModel(
    private val repository: RantsRepository
) : ScreenModel {

    fun models(): StateFlow<Rant> = repository
        .getRandom()
        .retry(3)
        .catch { emit("") }
        .stateIn(screenModelScope, SharingStarted.WhileSubscribed(5000), "")
}
RantsRepository#getRandom()
returns a random rant of type string.