icerockdev / moko-mvvm

Model-View-ViewModel architecture components for mobile (android & ios) Kotlin Multiplatform development
https://moko.icerock.dev/
Apache License 2.0
995 stars 95 forks source link

collect CStateFlow in ios #197

Closed periva101 closed 1 year ago

periva101 commented 1 year ago

val state: CStateFlow<BookingUiState> get() = _state.cStateFlow() how to collect this state in ios, also I want to bind the value to a function call that will fill tableIVew

Alex009 commented 1 year ago

just call method subscribe https://github.com/icerockdev/moko-mvvm/blob/cae693d73c571e835abf23061937c219ab63f300/mvvm-flow/src/nonAndroidMain/kotlin/dev/icerock/moko/mvvm/flow/CFlow.kt#L34

periva101 commented 1 year ago

@Alex009 you are fantastic, please keep supporting those libraries, kmm is coming beta soon, and your libraries will be core for the team, keep going

RageshAntony commented 1 year ago

@Alex009

How to observe a CFlow in Moko viewmodel, directly from iOS side inside the Swift UI View ?

val movies: CFlow<MoviesList> get() = _movies.receiveAsFlow().cFlow() // in moko viewmodel Want to observe in iOS Swift UI view, something like...

HStack {

   ForEach(viewmodel.movies.results, id : 100) { movie in
   Text(movie.title)
}

}

Is that possible ?

Alex009 commented 1 year ago

@RageshAntony here post about it - https://medium.com/p/8158e98c091d

RageshAntony commented 1 year ago

@RageshAntony here post about it - https://medium.com/p/8158e98c091d

@Alex009

Thanks,

But I already followed the tutorial. But my problem is with CStateFlow is, I am not getting the Types of the objects in iOS

For example , a snippet from a Viewmodel

    private val _cgeneres: MutableStateFlow<GenreList?> = MutableStateFlow(null)
    val cgeneres: CStateFlow<GenreList?> = _cgeneres.cStateFlow()  // not working

    private val _isLoading: MutableStateFlow<Boolean> = MutableStateFlow(false)
    val isLoading: CStateFlow<Boolean> = _isLoading.cStateFlow()
   .....
@Serializable
data class GenreList(
    var genres: ArrayList<Genres> = arrayListOf()
)

When I am trying to access the isLoading, I am able to get the Data type as Bool

But,Now the problem is, when I am trying to access the cgeneres, I am unable to get the Data type . It showing some <<error type>> and I am getting this error : Type of expression is ambiguous without more context

image

Why is this ?

Swift able to infer the type of a primitive type such as bool but not a data class GenreList. Why ?

I need to manually subscribe to it to get the data. But I need to use them directly like a state variable like that isLoading boolean variable ?

Please help me!