Skip to content

Commit fa49834

Browse files
authored
Using SKIE (android#14)
* Use SKIE * Android view model changes * Change import to Foundation and remove try from loop
1 parent 182c3ee commit fa49834

File tree

8 files changed

+23
-46
lines changed

8 files changed

+23
-46
lines changed

DiceRoller/androidApp/src/main/kotlin/com/google/samples/apps/diceroller/DiceViewModel.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import kotlinx.coroutines.flow.SharingStarted
2222
import kotlinx.coroutines.flow.StateFlow
2323
import kotlinx.coroutines.flow.asStateFlow
2424
import kotlinx.coroutines.flow.stateIn
25+
import kotlinx.coroutines.launch
2526

2627
class DiceViewModel(
2728
private val roller: DiceRoller,
@@ -42,7 +43,7 @@ class DiceViewModel(
4243
number: Int,
4344
sides: Int,
4445
unique: Boolean,
45-
) = settingsRepository.saveSettings(number, sides, unique)
46+
) = viewModelScope.launch { settingsRepository.saveSettings(number, sides, unique) }
4647

4748
fun rollDice() {
4849
// Ignore attempted rolls before settings are available

DiceRoller/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ plugins {
2020
alias(libs.plugins.kotlin.cocoapods) apply false
2121
alias(libs.plugins.kotlin.multiplatform) apply false
2222
alias(libs.plugins.ksp) apply false
23-
alias(libs.plugins.nativecoroutines) apply false
2423
}
2524

2625
tasks.register<Delete>("clean") {

DiceRoller/gradle/libs.versions.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ minSdk = "24"
1111
androidx-core = "1.10.1"
1212
androidx-lifecycle = "2.6.1"
1313
activityCompose = "1.7.2"
14+
skie = "0.4.19"
1415

1516
[plugins]
1617
android-application = { id = "com.android.application", version.ref = "agp" }
@@ -19,7 +20,7 @@ kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
1920
kotlin-cocoapods = { id = "org.jetbrains.kotlin.native.cocoapods", version.ref = "kotlin" }
2021
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
2122
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
22-
nativecoroutines = { id = "com.rickclephas.kmp.nativecoroutines", version.ref = "nativecoroutines" }
23+
skie = { id = "co.touchlab.skie", version.ref = "skie" }
2324

2425
[libraries]
2526
androidx-lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "androidx-lifecycle" }

DiceRoller/iosApp/Podfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ target 'iosApp' do
22
use_frameworks!
33
platform :ios, '14.1'
44
pod 'shared', :path => '../shared'
5-
pod 'KMPNativeCoroutinesAsync', '1.0.0-ALPHA-10'
65
end

DiceRoller/iosApp/Podfile.lock

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
PODS:
2-
- KMPNativeCoroutinesAsync (1.0.0-ALPHA-10):
3-
- KMPNativeCoroutinesCore (= 1.0.0-ALPHA-10)
4-
- KMPNativeCoroutinesCore (1.0.0-ALPHA-10)
52
- shared (1.0)
63

74
DEPENDENCIES:
8-
- KMPNativeCoroutinesAsync (= 1.0.0-ALPHA-10)
95
- shared (from `../shared`)
106

11-
SPEC REPOS:
12-
trunk:
13-
- KMPNativeCoroutinesAsync
14-
- KMPNativeCoroutinesCore
15-
167
EXTERNAL SOURCES:
178
shared:
189
:path: "../shared"
1910

2011
SPEC CHECKSUMS:
21-
KMPNativeCoroutinesAsync: c941aeffc6fef5ab52fa6448221d130c050a9574
22-
KMPNativeCoroutinesCore: 826573240f36e70f257c5215acab10a72b05ba82
2312
shared: d3e936b2c1df570cf43bf984e725cd24b68be937
2413

25-
PODFILE CHECKSUM: b69254d89a5e04a64d7a1076a395426a967d0720
14+
PODFILE CHECKSUM: cb976e7e275a85ac0f87a2394c4de9268ead2adf
2615

27-
COCOAPODS: 1.11.3
16+
COCOAPODS: 1.12.1

DiceRoller/iosApp/iosApp/SettingsViewModel.swift

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import shared
1818
import Combine
19-
import KMPNativeCoroutinesAsync
19+
import Foundation
2020

2121
@MainActor
2222
final class SettingsViewModel: ObservableObject {
@@ -47,22 +47,19 @@ final class SettingsViewModel: ObservableObject {
4747
private var currentSettings: DiceSettings? = nil
4848

4949
func startObservingSettings() async {
50-
do {
51-
let stream = asyncSequence(for: repository.settings)
52-
for try await settings in stream {
53-
self.diceCount = Int(settings.diceCount)
54-
self.sideCount = Int(settings.sideCount)
55-
self.uniqueRollsOnly = settings.uniqueRollsOnly
56-
self.rollButtonLabel = String.localizedStringWithFormat(NSLocalizedString("game_roll_button", comment: ""), settings.diceCount, settings.sideCount)
57-
self.currentSettings = settings
58-
}
59-
} catch {
60-
print("Failed with error: \(error)")
61-
}
50+
for await settings in repository.settings {
51+
self.diceCount = Int(settings.diceCount)
52+
self.sideCount = Int(settings.sideCount)
53+
self.uniqueRollsOnly = settings.uniqueRollsOnly
54+
self.rollButtonLabel = String.localizedStringWithFormat(NSLocalizedString("game_roll_button", comment: ""), settings.diceCount, settings.sideCount)
55+
self.currentSettings = settings
56+
}
6257
}
6358

6459
func saveSettings() {
65-
repository.saveSettings(diceCount: Int32(diceCount), sideCount: Int32(sideCount), uniqueRollsOnly: uniqueRollsOnly)
60+
Task {
61+
try? await repository.saveSettings(diceCount: Int32(diceCount), sideCount: Int32(sideCount), uniqueRollsOnly: uniqueRollsOnly)
62+
}
6663
}
6764

6865
func rollDice() {

DiceRoller/shared/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
*/
1616
plugins {
1717
alias(libs.plugins.kotlin.multiplatform)
18-
alias(libs.plugins.nativecoroutines)
1918
alias(libs.plugins.ksp)
2019
alias(libs.plugins.kotlin.cocoapods)
2120
alias(libs.plugins.android.library)
21+
alias(libs.plugins.skie)
2222
}
2323

2424
version = "1.0"

DiceRoller/shared/src/commonMain/kotlin/com/google/samples/apps/diceroller/DiceSettingsRepository.kt

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,8 @@ import androidx.datastore.preferences.core.Preferences
2020
import androidx.datastore.preferences.core.booleanPreferencesKey
2121
import androidx.datastore.preferences.core.edit
2222
import androidx.datastore.preferences.core.intPreferencesKey
23-
import com.rickclephas.kmp.nativecoroutines.NativeCoroutines
24-
import kotlinx.coroutines.CoroutineScope
25-
import kotlinx.coroutines.Dispatchers
2623
import kotlinx.coroutines.flow.Flow
2724
import kotlinx.coroutines.flow.map
28-
import kotlinx.coroutines.launch
2925

3026
class DiceSettingsRepository(
3127
private val dataStore: DataStore<Preferences>
@@ -36,13 +32,10 @@ class DiceSettingsRepository(
3632
const val DEFAULT_UNIQUE_ROLLS_ONLY = false
3733
}
3834

39-
private val scope = CoroutineScope(Dispatchers.Default)
40-
4135
private val diceCountKey = intPreferencesKey("dice_count")
4236
private val sideCountKey = intPreferencesKey("side_count")
4337
private val uniqueRollsOnlyKey = booleanPreferencesKey("unique_rolls_only")
4438

45-
@NativeCoroutines
4639
val settings: Flow<DiceSettings> = dataStore.data.map {
4740
DiceSettings(
4841
it[diceCountKey] ?: DEFAULT_DICE_COUNT,
@@ -51,17 +44,15 @@ class DiceSettingsRepository(
5144
)
5245
}
5346

54-
fun saveSettings(
47+
suspend fun saveSettings(
5548
diceCount: Int,
5649
sideCount: Int,
5750
uniqueRollsOnly: Boolean,
5851
) {
59-
scope.launch {
60-
dataStore.edit {
61-
it[diceCountKey] = diceCount
62-
it[sideCountKey] = sideCount
63-
it[uniqueRollsOnlyKey] = uniqueRollsOnly
64-
}
52+
dataStore.edit {
53+
it[diceCountKey] = diceCount
54+
it[sideCountKey] = sideCount
55+
it[uniqueRollsOnlyKey] = uniqueRollsOnly
6556
}
6657
}
6758
}

0 commit comments

Comments
 (0)