Hi, everyone. I need some advice for this architec...
# multiplatform
h
Hi, everyone. I need some advice for this architecture sample I shared the sample previously. but I added some improvement. Sample supports • To share VIewModel Android, Ios both • To share Navigation stack by Android, Ios both • To use ‘function’ way to draw on Compose, SwiftUi both as able to share Navigation. View side code got simpler just drawing UI and deliver user action to viewModel. dependency between each Screen is removed. just ViewModel interact between viewModels. I’m wondering if this is fine approach. Android
Copy code
fun SampleScreen(val model: SampleViewModel) {
    Screen(model) {
        Column {
            Text("greeting : ${+model.greeting}")
            Text("reply result : ${+model.replyResult}")
            Button("Reply") { model.onClick() }
        }
    }
}
Ios • code looks similar though if ui is complicated, code also not be similar.
Copy code
func SampleScreen(_ model: ApiSingleViewModel) -> some View {
    Screen(model) {
        Column {
            Text("current value : \(+model.greeting ?? "")")
            Text("reply result : \(+model.replyResult ?? "")")
            Button("Reply") { model.onClick() }
        }
    }
}
Common
Copy code
class SampleViewModel(val api: SampleApi = serviceLocator.sampleApi) : BaseViewModel() {

    //[add] is for ios to watch each Flow, DataFlow will be migrated to SharedFlow
    val greeting by add { DataFlow<String>() }
    val replyResult by add { DataFlow<String>() }

    override fun onInitialized() {
        //initStatus handle error and loading ui, also retry on error
        greeting.load(initStatus) {
            api.getGreeting("Hyun", "Programmer")
        }
    }

    fun onClick() {
        //initStatus hide ui on loading, error.
        //status doesn't hide ui but show loading, error ui
        replyResult.load(status) {
            api.reply("Thanks")
            navigate(SomeOtherViewModel()) // you can navigate this way.
        }
    }
}
a
h
Thanks for sharing your thought 🙏. I agree to share navigation. in order to share same viewModel, I had to make some tricky code to navigate on ios and android. but after sharing navigation. swiftUI and compose code extremely got simpler with proper approach.