Hyun
10/25/2020, 4:13 PMfun 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.
func SampleScreen(_ model: ApiSingleViewModel) -> some View {
Screen(model) {
Column {
Text("current value : \(+model.greeting ?? "")")
Text("reply result : \(+model.replyResult ?? "")")
Button("Reply") { model.onClick() }
}
}
}
Common
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.
}
}
}
Hyun
10/25/2020, 4:14 PMArkadii Ivanov
10/25/2020, 5:51 PMHyun
10/26/2020, 12:40 AM