https://kotlinlang.org logo
#compose
Title
# compose
h

Hyun

10/20/2020, 4:18 AM
Hi, All. Jetpack Compose uses function. but, I think there were some discussion on
function
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)
Copy code
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
Copy code
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.🙏
Screen Feature - loading, error UI for common use - use viewModel - navigation stack, navigate, navigateForResult, goBack, goBackWithResult BaseViewModel Feature - use SharedFlow for communicate with UI - use Resource to handle error, loading on UI - ViewModel Coroutine Scope (different lifecycle with composable) for loading data the sample exists here
v

Vinay Gaba

10/20/2020, 4:51 AM
h

Hyun

10/20/2020, 5:40 AM
Thanks for opinion from both of you 🙂 Actually, I tried
class
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.🙏
j

Javier

10/20/2020, 10:07 AM
I think fragment approach is just for architectures that are already using fragments so they can migrate easily but for new projects fragments should not be used
☝️ 9
h

Hyun

10/20/2020, 5:22 PM
@Javier Thanks for good answer 🙏 I checked the other samples again. my question got clearer.
👍 1
Detailed explanation previously I asked about using
function
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
3 Views