Hey :wave: Im a student whos new to kotlin multip...
# multiplatform
j
Hey đź‘‹ Im a student whos new to kotlin multiplatform. I am builing a KMM app and was wondering if anyone has used any projects that allow for shared view models in KMM
v
just use flow instead of livedata and make your viewmodel just a simple class handling the flow logic.
example:
Copy code
class LoginViewModel(
    private val login: Login = Login()
) {

    private val _state = MutableStateFlow<ComponentState>(ComponentState.Idle)
    val state = _state.asStateFlow()

    private val _effects: Channel<LoginEffect> = Channel()
    val effects = _effects.receiveAsFlow()

    fun sendLogin(email: String, password: String) {
        MainScope().launch {
            _state.emit(login(LoginCredentials(email, password, "password"))))
        }
    }
}
j
🤯
v
for compose you just need the
state.asState()
… for swift you need to create a
watch()
method and pass a simple lambda to listen flow changes
j
and the watch method could use combine to update the view’s data automatically?
v
if you decide to use compose and swift ui .. you can split logic in state and effects.. state just hold the current component state and effects dispatch events to be handle (show toast / route to screen/ etc) .. effects is everything that cannot be a screen/component state.. everything that “happens”
you can write an extension inside your swift project to transform flow events in combine streams
j
oh wow thats awesome! I was hoping that there was a way to keep the combine/flow code in kmm
but this is still super helpful and likely the approach I will take! Thanks so much!
v
my current problem is: state in my kotlin module is a sealed class (i need to describe state and hold a value for loaded state).. in android it’s perfect.. but in swift the better solution is to use a enum (enum can hold values) .. so in my ios project i create a extension to translate my state into swift enums.
if you find a solution to translate flows in combine streams or a way to transform flow output in swiftui binding using property wrapper.. send to me plx.
today i use the example of kotlin conf app and use a watch methods like it:
Copy code
fun <T> Flow<T>.watch(block: (T) -> Unit): Closeable {
    val scope = MainScope()
    onEach {
        block(it)
    }.launchIn(scope)

    return object : Closeable {
        override fun close() {
            try {
                scope.cancel()
            } catch (e: Exception) {}
        }
    }
}
j
I looooove enums in swift - theyre awesome but I can see how that issue would be a pain
I will definitely reply back here once ive got more context if im able to help you out 🙂
v
I think the kotlin compiler should evolve in future to transform sealed classes into enums.. makes more sense in my mind.
j
yeah that would be ideal. I feel like a more likely solution would be a kmm dependency that can convert the sealed class into enums in the ios lib so you dont need to know the sealed class existed in the ios app