touchlab / KaMPKit

KaMP Kit by Touchlab. A collection of code & tools designed to get your mobile team started quickly w/Kotlin Multiplatform
https://touchlab.co/
Apache License 2.0
2.2k stars 199 forks source link

MVVM -> MVI, LiveData -> StateFlow #161

Closed brady-aiello closed 3 years ago

brady-aiello commented 3 years ago

MVVM to MVI

We're currently using MVVM, which doesn't specify the state of the view (loading, error, success, empty), only the data (List), and the error state, which is a separate value to observe. This means that the state of the view is implicit vs. explicit. By default, we assume that data will load successfully, and if it doesn't, then we'll show the error view. Instead of putting this custom logic in the view, we can emit the view state from the repository layer, which iOS and Android both share. This allows the code to be more testable, while also allowing us to share more business logic between iOS and Android platforms.

LiveData to StateFlow

We're already using Flow on the repository layer, but we can now use StateFlow on the ViewModel layer, which is now the best tool available. LiveData is Android platform specific, whereas Flow and StateFlow are available to Android and iOS platforms. If we convert to StateFlow, we can share more code between platforms, have a more succinct "emit" API, and have access to the powerful Flow operators such as debounce, map, filter, and transform.