Guilherme Delgado
08/10/2023, 2:07 PMstruct SampleUIViewController: UIViewControllerRepresentable {
@Binding var status: String
let action: () -> Void
func makeUIViewController(context: Context) -> UIViewController {
return SharedViewControllers().SampleComposable(status: status, click: action)
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
//how to update the composable state when binding values changes?
}
}
struct ComposeScreen: View {
@StateObject private var viewModel = ViewModels.sampleViewModel()
@State private var status: String = ""
var body: some View {
SampleUIViewController(
status: $status,
action: { viewModel.doSomething() }
)
.onReceive(viewModel.$state) { new in
status = new.label()
}
.ignoresSafeArea()
}
}
I know from logging that the ViewModel state emission is working (also the viewModel.doSomething()
) and reaching iOS. If I’m using SwiftUi this updates de View, but now on Compose (probably because we’re using a generic ViewController?) this is not updating my screen.
How can I solve this? We must have the possibility to use the updateUIViewController
.Dima Avdeev
08/10/2023, 5:41 PMGuilherme Delgado
08/10/2023, 5:46 PMGuilherme Delgado
08/10/2023, 5:54 PMGuilherme Delgado
08/10/2023, 6:02 PMGuilherme Delgado
08/10/2023, 6:20 PMGuilherme Delgado
08/11/2023, 12:06 AMobject SharedViewControllers {
private data class ComposeUIViewState(val status: String = "")
private val _state = MutableStateFlow(ComposeUIViewState())
fun sampleComposable(status: String, click: () -> Unit): UIViewController {
return ComposeUIViewController {
with(_state.collectAsState().value) {
Composable(state.status, click)
}
}
}
fun updateSampleComposable(status: String) {
_state.update { ComposeUIViewState(status = status) }
}
}
struct SampleUIViewController: UIViewControllerRepresentable {
@Binding var status: String
let action: () -> Void
func makeUIViewController(context: Context) -> UIViewController {
return SharedViewControllers().sampleComposable(click: action)
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
SharedViewControllers().updateSampleComposable(status: status)
}
}
Not very graceful though.Davgdafa
08/18/2023, 9:53 PMGuilherme Delgado
08/19/2023, 8:38 AM