Hyun
10/25/2020, 4:01 PMclass
to function
. and structure got improved much. Thanks all.
Sample supports
- To share Navigation stack by Android, Ios both
- To share VIewModel Android, Ios both
- To use ‘function’ way on Android, Ios both.
I used ViewModel(not android component) as navigation stack.(so, for supporting savedState, need some additional implementation.)
is this fine for Jetpack compose approach?
Android
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.
func SampleScreen(_ model: ApiSingleViewModel) -> some View {
Screen(model) {
Column {
Text("current value : \(+model.greeting ?? "")")
Text("reply result : \(+model.replyResult ?? "")")
Button("Reply") { model.onClick() }
}
}
}
Common
• able to navigate between ViewModel.
• so on MVVM, View side do drawing UI only. without navigation.
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 here.
}
}
}