https://kotlinlang.org logo
Title
m

Mehmet Burak Akgün

04/20/2023, 4:34 PM
Hi all, I was trying out Compose ios (alpha) and got stuck at one point. I needed to consult you for your opinion. I am attaching a screenshot that shows a small part of both iOSApp and Android App project. Attached you can see viewModels , Activity/ContentView. Just like Android, I am trying to update the content on iOS, but it’s not working by using binding<state> etc. How can I get the redraw action on iOS? Any ideas or examples would be very helpful :) Have a nice :kotlin: / :compose-multiplatform: :cheers:
m

Magdalena Tsolaki

04/20/2023, 5:38 PM
d

Dima Avdeev

04/20/2023, 6:02 PM
you can create a state and store it in
val stateFlow = MutableStateFlow<YourState>(...)
After that - you can subscribe to state changes with in Composable function with:
val state = stateFlow.collectAsState()
And draw that state:
Text(state.value.toString())
state.value will have a type of YourState
t

tylerwilson

04/20/2023, 6:59 PM
It appears you are using KMPNativeCoroutines. Then on the Kotlin side, put @NativeCoroutinesState above your val state. Then on the iOS side wrap the generated stateFlow with the KMPNativecoroutines.createObservable method, then use the RxSwift observe methods...
m

Mehmet Burak Akgün

04/20/2023, 9:03 PM
Thanks all, The requirement is , updating the ComposeUIView by using ViewModel.swift: ObservableObject. Fetching data is OK but updating it. I’m able to draw a composeView with default state , and it never gets updated 🙂 I need a trick which refreshes my rootComposable
d

Dima Avdeev

04/21/2023, 4:58 AM
Also, you can simply use in Kotlin Composable functions:
val state = remember{ mutableStateOf(MyState()) }
Read and change this state
state.value