ayodele
10/31/2022, 3:48 PMstreetsofboston
10/31/2022, 3:58 PMayodele
10/31/2022, 4:05 PMstreetsofboston
10/31/2022, 4:07 PMstreetsofboston
10/31/2022, 4:09 PMactual class KmpViewModel : ViewModel() { .....}
and on iOS actual class KmpViewModel { ... }
and create them (not having them injected) in my native Android or iOS code.streetsofboston
10/31/2022, 4:14 PM/**
* A [ViewModel] expects to be implemented by a platform specific
* implementation of a View-Model.
*/
expect abstract class ViewModel() {
/**
* This method must be called when this [ViewModel] is no longer needed.
* It cleans up its resources and cancels its scope.
*/
abstract fun onDestroy()
}
...
...
abstract class BaseViewModel(protected val scope: CoroutineScope, protected val log: Kermit): ViewModel() {
override fun onDestroy() {
log.v { "onDestroy called" }
scope.cancel()
}
...
}
androidMain:
/**
* Android implements the [ViewModel] through its Android lifecycle [androidx.lifecycle.ViewModel].
*/
actual abstract class ViewModel : androidx.lifecycle.ViewModel() {
override fun onCleared() = onDestroy()
actual abstract fun onDestroy()
}
iosMain:
/**
* iOS does not provide any platform-specific implementation for a [ViewModel].
*/
actual abstract class ViewModel {
actual abstract fun onDestroy()
}
ayodele
10/31/2022, 4:24 PM