-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3c7743
commit 33c87e7
Showing
29 changed files
with
538 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...androidJvmMain/kotlin/com/joaomanaia/game2048/core/datastore/PreferencesKey.androidJvm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.joaomanaia.game2048.core.datastore | ||
|
||
import androidx.datastore.preferences.core.Preferences as AndroidxPreferences | ||
import androidx.datastore.preferences.core.MutablePreferences as AndroidxMutablePreferences | ||
|
||
actual typealias Preferences = AndroidxPreferences | ||
actual typealias MutablePreferences = AndroidxMutablePreferences | ||
|
||
actual typealias PreferencesKey <T> = AndroidxPreferences.Key<T> | ||
actual typealias PreferencesPair <T> = AndroidxPreferences.Pair<T> |
40 changes: 40 additions & 0 deletions
40
.../kotlin/com/joaomanaia/game2048/core/datastore/manager/DataStoreManagerImpl.androidJvm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.joaomanaia.game2048.core.datastore.manager | ||
|
||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.firstOrNull | ||
import kotlinx.coroutines.flow.map | ||
|
||
actual class DataStoreManagerImpl( | ||
private val dataStore: DataStore<Preferences> | ||
) : DataStoreManager { | ||
override val preferenceFlow = dataStore.data | ||
|
||
override suspend fun <T> getPreference( | ||
preferenceEntry: PreferenceRequest<T> | ||
) = preferenceFlow | ||
.firstOrNull() | ||
?.get(preferenceEntry.key) ?: preferenceEntry.defaultValue | ||
|
||
override fun <T> getPreferenceFlow(request: PreferenceRequest<T>) = preferenceFlow.map { | ||
it[request.key] ?: request.defaultValue | ||
}.distinctUntilChanged() | ||
|
||
override suspend fun <T> editPreference(key: Preferences.Key<T>, newValue: T) { | ||
dataStore.edit { preferences -> preferences[key] = newValue } | ||
} | ||
|
||
override suspend fun editPreferences(vararg prefs: Preferences.Pair<*>) { | ||
dataStore.edit { preferences -> | ||
prefs.forEach { | ||
preferences.plusAssign(it) | ||
} | ||
} | ||
} | ||
|
||
override suspend fun clearPreferences() { | ||
dataStore.edit { preferences -> preferences.clear() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...nMain/kotlin/com/joaomanaia/game2048/core/common/preferences/GameDataPreferencesCommon.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
composeApp/src/commonMain/kotlin/com/joaomanaia/game2048/core/datastore/PreferencesKey.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.joaomanaia.game2048.core.datastore | ||
|
||
expect abstract class Preferences internal constructor() { | ||
abstract fun asMap(): Map<PreferencesKey<*>, Any> | ||
|
||
abstract operator fun <T> contains(key: PreferencesKey<T>): Boolean | ||
|
||
abstract operator fun <T> get(key: PreferencesKey<T>): T? | ||
|
||
fun toMutablePreferences(): MutablePreferences | ||
|
||
fun toPreferences(): Preferences | ||
} | ||
|
||
expect class PreferencesKey<T> internal constructor(name: String) { | ||
val name: String | ||
|
||
override operator fun equals(other: Any?): Boolean | ||
|
||
override fun hashCode(): Int | ||
|
||
infix fun to(value: T): PreferencesPair<T> | ||
|
||
override fun toString(): String | ||
} | ||
|
||
expect class PreferencesPair<T> internal constructor(key: PreferencesKey<T>, value: T) { | ||
internal val key: PreferencesKey<T> | ||
|
||
internal val value: T | ||
} | ||
|
||
|
||
expect class MutablePreferences : Preferences | ||
|
||
fun booleanPreferencesKey(name: String): PreferencesKey<Boolean> = PreferencesKey(name) | ||
|
||
fun intPreferencesKey(name: String): PreferencesKey<Int> = PreferencesKey(name) | ||
|
||
fun floatPreferencesKey(name: String): PreferencesKey<Float> = PreferencesKey(name) | ||
|
||
fun stringPreferencesKey(name: String): PreferencesKey<String> = PreferencesKey(name) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 1 addition & 38 deletions
39
.../commonMain/kotlin/com/joaomanaia/game2048/core/datastore/manager/DataStoreManagerImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,3 @@ | ||
package com.joaomanaia.game2048.core.datastore.manager | ||
|
||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.firstOrNull | ||
import kotlinx.coroutines.flow.map | ||
|
||
class DataStoreManagerImpl( | ||
private val dataStore: DataStore<Preferences> | ||
) : DataStoreManager { | ||
override val preferenceFlow = dataStore.data | ||
|
||
override suspend fun <T> getPreference( | ||
preferenceEntry: PreferenceRequest<T> | ||
) = preferenceFlow | ||
.firstOrNull() | ||
?.get(preferenceEntry.key) ?: preferenceEntry.defaultValue | ||
|
||
override fun <T> getPreferenceFlow(request: PreferenceRequest<T>) = preferenceFlow.map { | ||
it[request.key] ?: request.defaultValue | ||
}.distinctUntilChanged() | ||
|
||
override suspend fun <T> editPreference(key: Preferences.Key<T>, newValue: T) { | ||
dataStore.edit { preferences -> preferences[key] = newValue } | ||
} | ||
|
||
override suspend fun editPreferences(vararg prefs: Preferences.Pair<*>) { | ||
dataStore.edit { preferences -> | ||
prefs.forEach { | ||
preferences.plusAssign(it) | ||
} | ||
} | ||
} | ||
|
||
override suspend fun clearPreferences() { | ||
dataStore.edit { preferences -> preferences.clear() } | ||
} | ||
} | ||
expect class DataStoreManagerImpl : DataStoreManager |
4 changes: 2 additions & 2 deletions
4
...src/commonMain/kotlin/com/joaomanaia/game2048/core/datastore/manager/PreferenceRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package com.joaomanaia.game2048.core.datastore.manager | ||
|
||
import androidx.datastore.preferences.core.Preferences | ||
import com.joaomanaia.game2048.core.datastore.PreferencesKey | ||
|
||
open class PreferenceRequest<T>( | ||
val key: Preferences.Key<T>, | ||
val key: PreferencesKey<T>, | ||
val defaultValue: T | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
composeApp/src/commonMain/kotlin/com/joaomanaia/game2048/di/KoinStarter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
composeApp/src/commonMain/kotlin/com/joaomanaia/game2048/model/Cell.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
composeApp/src/commonMain/kotlin/com/joaomanaia/game2048/model/GridTile.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
composeApp/src/commonMain/kotlin/com/joaomanaia/game2048/model/GridTileMovement.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.