Hyun
10/20/2020, 4:18 AMfunction
vs `class`(as swiftUI also based on function but also able to use struct)
So, I just tried to use class
for Composable if it’s meaningful.
Kindly review if it’s reasonable or not.
the below is the sample code with class
Screen(Composable class)
class ApiSingleScreen(private val model: ApiSingleViewModel = ApiSingleViewModel()) : Screen(model) {
override val title: String = R.string.single_call.resourceToString()
//for Composer to recognize the class
@Composable
override fun compose() {
super.compose()
}
@Composable
override fun view() {
Column {
Text("current value : ${+model.result}")
SampleTextField("Input value", model.input)
Button("update") {
model.onClick()
}
}
}
}
ViewModel
class ApiSingleViewModel(private val api: PreferenceApi = serviceLocator.preferenceApi) : BaseViewModel() {
//DataFlow will be migrated to MutableSharedFlow. as coroutine-native-mt 1.4.0 is not yet released. I customized Flow.
val result by lazy { DataFlow<String>() }
//if result is changed, input also changed.
val input by add { DataFlow<String>().withSource(result) }
override fun onInit() {
//initStatus is connected with loading, error ui. so, when call api, loading, error ui is shown by the state
result.load(initStatus) {
api.getString(KEY) ?: ""
}
}
fun onClick() {
result.load(status) {
val text = input.value?: error("please input")
api.setString(KEY, text)
text//set changed text after api call is success
}
}
}
If There is something wrong on my opinion. kindly advise me to learn.🙏Vinay Gaba
10/20/2020, 4:51 AMHyun
10/20/2020, 5:40 AMclass
because I felt difficulty to manage api call.
Let’s say there are A,B,C page.
and user navigated to A then B then C.
when user press back button.
we show B.
when user navigate to C from B. if B page call some api. we should keep the api calling.
but if user go back to A from B. B page should cancel api calling. I couldn’t find the way to do that.
this was root reason I felt difficulty on function base.
as I didn’t that much understand Jetpack compose and usage,
I’ll really be appreciated if share me any hint to solve the problem I experienced.🙏Javier
10/20/2020, 10:07 AMHyun
10/20/2020, 5:22 PMfunction
vs class
because at that time, my project had to covers swiftUI and compose both with similar architecture form. so, I had to decide to use some tricky way to support both for developer to develop simple way.
but, when I asked the above, people’s answer gave me some insight. and I could managed to use function
on compose and swiftUI both.
the key was that navigation backstack is not need to be on View side on MVVM as backstack’s data doesn’t depend on ‘compose’ or UI at all.
so, I reflected this on SwiftUI and moved navigation logic from SwiftUi to kotlin.
then, all the logic got simpler and removed tricky code and followed SwiftUI and Compose’s proper approach.
github : https://github.com/dss99911/kotlin-simple-architecture
sample code on android : https://github.com/dss99911/kotlin-simple-architecture/tree/master/sample/sample-android