@@ -4,12 +4,13 @@ A library that allows you to share ViewModels between Android and iOS.
44
55## Compatibility
66
7- The latest version of the library uses Kotlin version ` 1.8.20 ` .
7+ The latest version of the library uses Kotlin version ` 1.8.21 ` .
88Compatibility versions for newer Kotlin versions are also available:
99
1010| Version | Version suffix | Kotlin | Coroutines |
1111| ---------------| -----------------| :----------:| :----------:|
12- | ** _ latest_ ** | ** _ no suffix_ ** | ** 1.8.20** | ** 1.6.4** |
12+ | ** _ latest_ ** | ** _ no suffix_ ** | ** 1.8.21** | ** 1.6.4** |
13+ | 1.0.0-ALPHA-6 | _ no suffix_ | 1.8.20 | 1.6.4 |
1314| 1.0.0-ALPHA-4 | _ no suffix_ | 1.8.10 | 1.6.4 |
1415| 1.0.0-ALPHA-3 | _ no suffix_ | 1.8.0 | 1.6.4 |
1516
@@ -22,13 +23,12 @@ dependencies {
2223}
2324```
2425
25- Create your ViewModels almost as you would in Android :
26+ And create your ViewModels:
2627``` kotlin
2728import com.rickclephas.kmm.viewmodel.KMMViewModel
2829import com.rickclephas.kmm.viewmodel.MutableStateFlow
2930import com.rickclephas.kmm.viewmodel.stateIn
3031
31- // 1: use KMMViewModel instead of ViewModel
3232open class TimeTravelViewModel : KMMViewModel () {
3333
3434 private val clockTime = Clock .time
@@ -37,10 +37,8 @@ open class TimeTravelViewModel: KMMViewModel() {
3737 * A [StateFlow] that emits the actual time.
3838 */
3939 val actualTime = clockTime.map { formatTime(it) }
40- // 2: supply viewModelScope to your stateIn calls
4140 .stateIn(viewModelScope, SharingStarted .WhileSubscribed (), " N/A" )
4241
43- // 3: supply viewModelScope to your MutableStateFlows
4442 private val _travelEffect = MutableStateFlow <TravelEffect ?>(viewModelScope, null )
4543 /* *
4644 * A [StateFlow] that emits the applied [TravelEffect].
@@ -49,21 +47,55 @@ open class TimeTravelViewModel: KMMViewModel() {
4947}
5048```
5149
52- You need to use ` viewModelScope ` wherever possible to propagate state changes to iOS.
53- In other cases you can access the ` ViewModelScope.coroutineScope ` property directly.
50+ As you might notice it isn't much different from an Android ViewModel.
51+ The most obvious difference is the ` KMMViewModel ` superclass:
52+
53+ ``` diff
54+ - import androidx.lifecycle.ViewModel
55+ + import com.rickclephas.kmm.viewmodel.KMMViewModel
56+
57+ - open class TimeTravelViewModel: ViewModel() {
58+ + open class TimeTravelViewModel: KMMViewModel() {
59+ ```
60+
61+ Besides that there are only 2 minor differences.
62+ The first being a different import for ` stateIn ` :
63+
64+ ``` diff
65+ - import kotlinx.coroutines.flow.stateIn
66+ + import com.rickclephas.kmm.viewmodel.stateIn
67+
68+ .stateIn(viewModelScope, SharingStarted.WhileSubscribed(), "N/A")
69+ ```
70+
71+ And the second being a different ` MutableStateFlow ` constructor:
72+
73+ ``` diff
74+ - import kotlinx.coroutines.flow.MutableStateFlow
75+ + import com.rickclephas.kmm.viewmodel.MutableStateFlow
76+
77+ - private val _travelEffect = MutableStateFlow<TravelEffect?>(null)
78+ + private val _travelEffect = MutableStateFlow<TravelEffect?>(viewModelScope, null)
79+ ```
80+
81+ These minor differences will make sure that any state changes are propagate to iOS.
82+
83+ > ** Note** : ` viewModelScope ` is a wrapper around the actual ` CoroutineScope ` which can be accessed
84+ > via the ` ViewModelScope.coroutineScope ` property.
5485
5586### KMP-NativeCoroutines
5687
57- Use the ` @NativeCoroutinesState ` annotation from [ KMP-NativeCoroutines] ( https://github.com/rickclephas/KMP-NativeCoroutines )
88+ I highly recommend you to use the ` @NativeCoroutinesState ` annotation from
89+ [ KMP-NativeCoroutines] ( https://github.com/rickclephas/KMP-NativeCoroutines )
5890to turn your ` StateFlow ` s into properties in Swift:
5991
6092``` kotlin
6193@NativeCoroutinesState
6294val travelEffect = _travelEffect .asStateFlow()
6395```
6496
65- Checkout the [ README] ( https://github.com/rickclephas/KMP-NativeCoroutines/blob/master/README.md )
66- for more information and installation instructions for KMP-NativeCoroutines .
97+ Checkout the KMP-NativeCoroutines [ README] ( https://github.com/rickclephas/KMP-NativeCoroutines/blob/master/README.md )
98+ for more information and installation instructions.
6799
68100<details ><summary >Alternative</summary >
69101<p >
@@ -92,7 +124,7 @@ class TimeTravelFragment: Fragment(R.layout.fragment_time_travel) {
92124}
93125```
94126
95- > ** Note:** support for Jetpack Compose is coming soon.
127+ > ** Note:** improved support for Jetpack Compose is coming soon.
96128
97129## Swift
98130
0 commit comments