Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/doublesymmetry/multiplatform-viewmodel
Shared ViewModel in Kotlin Multiplatform
https://github.com/doublesymmetry/multiplatform-viewmodel
kotlin-multiplatform kotlin-multiplatform-mobile
Last synced: 3 days ago
JSON representation
Shared ViewModel in Kotlin Multiplatform
- Host: GitHub
- URL: https://github.com/doublesymmetry/multiplatform-viewmodel
- Owner: doublesymmetry
- License: apache-2.0
- Created: 2022-04-06T14:05:29.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-05-03T21:18:37.000Z (over 2 years ago)
- Last Synced: 2024-11-03T02:32:51.564Z (6 days ago)
- Topics: kotlin-multiplatform, kotlin-multiplatform-mobile
- Language: Kotlin
- Homepage:
- Size: 135 KB
- Stars: 29
- Watchers: 2
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-kotlin-multiplatform - multiplatform-viewmodel
README
# multiplatform-viewmodel 🗳
Create shared `ViewModel`'s for shared business logic using our `ViewModel` base class.
## Features
- [x] Uses Jetpack `ViewModel` on Android for lifecycled
- [x] Exposes a `CoroutineScope` to be used in your methods## Overview of Package
This package has 1 component to it:
A `ViewModel` class you can base your view models on.## Getting Started
**Add Dependency**
```gradle
commonMain {
dependencies {
// ...
api("com.doublesymmetry:multiplatform-viewmodel:0.0.1")
}
}
```**Expose it to iOS native side**
```gradle
ios {
binaries {
framework {
baseName = "shared"
export(Deps.viewmodel) // required to expose the class to iOS
}
}
}
```## Using the ViewModel
```kotlin
class ExampleViewModel: ViewModel() {
private val _viewState = MutableStateFlow(UIViewState())
val viewState: StateFlow = _viewState
fun onLaunched() {
scope.launch {
// fetch some data
_viewState.emit(newState)
}
}
}
```### Using your ViewModel on iOS with a `UIViewController`
When using it on iOS you'll want to make sure that you call `clear()` on your ViewModel on `deinit` to properly kill the `CoroutineScope`