-
-
Notifications
You must be signed in to change notification settings - Fork 29
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
Showing
12 changed files
with
311 additions
and
83 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
86 changes: 8 additions & 78 deletions
86
...age-base/src/commonMain/kotlin/com/alorma/compose/settings/storage/base/ValueProviders.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,92 +1,22 @@ | ||
package com.alorma.compose.settings.storage.base | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import kotlin.reflect.KProperty | ||
|
||
@Composable | ||
fun rememberBooleanSettingState(defaultValue: Boolean = false): SettingValueState<Boolean> { | ||
return remember { InMemoryBooleanSettingValueState(defaultValue) } | ||
} | ||
|
||
@Composable | ||
fun rememberFloatSettingState(defaultValue: Float = 0f): SettingValueState<Float> { | ||
return remember { InMemoryFloatSettingValueState(defaultValue) } | ||
} | ||
|
||
@Composable | ||
fun rememberIntSettingState(defaultValue: Int = -1): SettingValueState<Int> { | ||
return remember { InMemoryIntSettingValueState(defaultValue) } | ||
} | ||
|
||
@Composable | ||
fun rememberStringSettingState(defaultValue: String? = null): SettingValueState<String?> { | ||
return remember { InMemoryStringSettingValueState(defaultValue) } | ||
} | ||
|
||
@Composable | ||
fun rememberIntSetSettingState(defaultValue: Set<Int> = emptySet()): SettingValueState<Set<Int>> { | ||
return remember { InMemoryIntSetSettingValueState(defaultValue) } | ||
} | ||
|
||
@Suppress("NOTHING_TO_INLINE") | ||
inline operator fun <T> SettingValueState<T>.getValue(thisObj: Any?, property: KProperty<*>): T = | ||
value | ||
value | ||
|
||
@Suppress("NOTHING_TO_INLINE") | ||
inline operator fun <T> SettingValueState<T>.setValue( | ||
thisObj: Any?, | ||
property: KProperty<*>, | ||
value: T, | ||
thisObj: Any?, | ||
property: KProperty<*>, | ||
value: T, | ||
) { | ||
this.value = value | ||
this.value = value | ||
} | ||
|
||
interface SettingValueState<T> { | ||
fun reset() | ||
fun reset() | ||
|
||
var value: T | ||
} | ||
|
||
internal class InMemoryBooleanSettingValueState(private val defaultValue: Boolean) : | ||
SettingValueState<Boolean> { | ||
override var value: Boolean by mutableStateOf(defaultValue) | ||
override fun reset() { | ||
value = defaultValue | ||
} | ||
} | ||
|
||
internal class InMemoryFloatSettingValueState(private val defaultValue: Float) : | ||
SettingValueState<Float> { | ||
override var value: Float by mutableStateOf(defaultValue) | ||
override fun reset() { | ||
value = defaultValue | ||
} | ||
} | ||
|
||
internal class InMemoryIntSettingValueState(private val defaultValue: Int) : | ||
SettingValueState<Int> { | ||
override var value: Int by mutableStateOf(defaultValue) | ||
override fun reset() { | ||
value = defaultValue | ||
} | ||
} | ||
|
||
internal class InMemoryStringSettingValueState(private val defaultValue: String?) : | ||
SettingValueState<String?> { | ||
override var value: String? by mutableStateOf(defaultValue) | ||
override fun reset() { | ||
value = defaultValue | ||
} | ||
} | ||
|
||
internal class InMemoryIntSetSettingValueState(private val defaultValue: Set<Int>) : | ||
SettingValueState<Set<Int>> { | ||
override var value: Set<Int> by mutableStateOf(defaultValue) | ||
override fun reset() { | ||
value = defaultValue | ||
} | ||
} | ||
var value: 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
47 changes: 47 additions & 0 deletions
47
...Main/kotlin/com/alorma/compose/settings/storage/disk/StringPreferenceSettingValueState.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,47 @@ | ||
package com.alorma.compose.settings.storage.disk | ||
|
||
import android.content.SharedPreferences | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.core.content.edit | ||
import androidx.preference.PreferenceManager | ||
import com.alorma.compose.settings.storage.base.SettingValueState | ||
|
||
@Composable | ||
fun rememberPreferenceStringSettingState( | ||
key: String, | ||
defaultValue: String? = null, | ||
preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(LocalContext.current), | ||
): StringPreferenceSettingValueState { | ||
return remember { | ||
StringPreferenceSettingValueState( | ||
key = key, | ||
preferences = preferences, | ||
defaultValue = defaultValue, | ||
) | ||
} | ||
} | ||
|
||
class StringPreferenceSettingValueState( | ||
private val preferences: SharedPreferences, | ||
val key: String, | ||
val defaultValue: String?, | ||
) : SettingValueState<String?> { | ||
|
||
private var _value by mutableStateOf(preferences.getString(key, defaultValue)) | ||
|
||
override var value: String? | ||
set(value) { | ||
_value = value | ||
preferences.edit { putString(key, value) } | ||
} | ||
get() = _value | ||
|
||
override fun reset() { | ||
value = defaultValue | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...src/commonMain/kotlin/com/alorma/compose/settings/storage/disk/StringSettingValueState.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,44 @@ | ||
package com.alorma.compose.settings.storage.disk | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import com.alorma.compose.settings.storage.base.SettingValueState | ||
import com.russhwolf.settings.Settings | ||
|
||
@Composable | ||
fun rememberStringSettingState( | ||
key: String, | ||
defaultValue: String? = null, | ||
settings: Settings = Settings() | ||
): StringSettingValueState { | ||
return remember { | ||
StringSettingValueState( | ||
settings = settings, | ||
key = key, | ||
defaultValue = defaultValue, | ||
) | ||
} | ||
} | ||
|
||
class StringSettingValueState( | ||
private val settings: Settings, | ||
val key: String, | ||
val defaultValue: String?, | ||
) : SettingValueState<String?> { | ||
|
||
private var _value by mutableStateOf(settings.getStringOrNull(key)) | ||
|
||
override var value: String? | ||
set(value) { | ||
_value = value | ||
settings.putString(key, value.orEmpty()) | ||
} | ||
get() = _value | ||
|
||
override fun reset() { | ||
value = defaultValue | ||
} | ||
} |
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,102 @@ | ||
import org.jetbrains.compose.ExperimentalComposeLibrary | ||
import org.jetbrains.compose.desktop.application.dsl.TargetFormat | ||
|
||
plugins { | ||
alias(libs.plugins.kotlinMultiplatform) | ||
alias(libs.plugins.androidLibrary) | ||
alias(libs.plugins.jetbrainsCompose) | ||
} | ||
|
||
apply(from = "${rootProject.projectDir}/scripts/publish-module.gradle") | ||
|
||
kotlin { | ||
applyDefaultHierarchyTemplate() | ||
|
||
withSourcesJar() | ||
|
||
androidTarget { | ||
publishLibraryVariants("release") | ||
compilations.all { | ||
kotlinOptions { | ||
jvmTarget = JavaVersion.VERSION_17.toString() | ||
} | ||
} | ||
} | ||
|
||
jvm("desktop") | ||
|
||
listOf( | ||
iosX64(), | ||
iosArm64(), | ||
iosSimulatorArm64() | ||
).forEach { iosTarget -> | ||
iosTarget.binaries.framework { | ||
baseName = "ComposeApp" | ||
binaryOption("bundleId", libs.versions.namespace.get() + ".memory") | ||
} | ||
} | ||
|
||
sourceSets { | ||
androidMain.dependencies { | ||
implementation(libs.androidx.preference.preference) | ||
implementation(libs.androidx.preference.ktx) | ||
} | ||
|
||
commonMain.dependencies { | ||
api(projects.composeSettingsStorageBase) | ||
|
||
implementation(compose.runtime) | ||
implementation(compose.foundation) | ||
} | ||
} | ||
} | ||
|
||
android { | ||
namespace = libs.versions.namespace.get() + ".memory" | ||
compileSdk = libs.versions.android.compileSdk.get().toInt() | ||
|
||
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml") | ||
sourceSets["main"].res.srcDirs("src/androidMain/res") | ||
sourceSets["main"].resources.srcDirs("src/commonMain/resources") | ||
|
||
defaultConfig { | ||
minSdk = libs.versions.android.minSdk.get().toInt() | ||
} | ||
buildFeatures { | ||
compose = true | ||
} | ||
composeOptions { | ||
kotlinCompilerExtensionVersion = libs.versions.compose.compiler.get() | ||
} | ||
packaging { | ||
resources { | ||
excludes += "/META-INF/{AL2.0,LGPL2.1}" | ||
} | ||
} | ||
lint { | ||
checkReleaseBuilds = false | ||
abortOnError = false | ||
} | ||
buildTypes { | ||
getByName("release") { | ||
isMinifyEnabled = false | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
targetCompatibility = JavaVersion.VERSION_17 | ||
} | ||
dependencies { | ||
debugImplementation(libs.compose.ui.tooling) | ||
} | ||
} | ||
|
||
compose.desktop { | ||
application { | ||
nativeDistributions { | ||
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb) | ||
packageName = libs.versions.namespace.get() + ".memory" | ||
packageVersion = "1.0.0" | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...rc/commonMain/kotlin/com/alorma/compose/settings/storage/disk/BooleanSettingValueState.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,21 @@ | ||
package com.alorma.compose.settings.storage.disk | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import com.alorma.compose.settings.storage.base.SettingValueState | ||
|
||
@Composable | ||
fun rememberBooleanSettingState(defaultValue: Boolean = false): SettingValueState<Boolean> { | ||
return remember { InMemoryBooleanSettingValueState(defaultValue) } | ||
} | ||
|
||
internal class InMemoryBooleanSettingValueState(private val defaultValue: Boolean) : | ||
SettingValueState<Boolean> { | ||
override var value: Boolean by mutableStateOf(defaultValue) | ||
override fun reset() { | ||
value = defaultValue | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
.../src/commonMain/kotlin/com/alorma/compose/settings/storage/disk/FloatSettingValueState.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,21 @@ | ||
package com.alorma.compose.settings.storage.disk | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import com.alorma.compose.settings.storage.base.SettingValueState | ||
|
||
@Composable | ||
fun rememberFloatSettingState(defaultValue: Float = 0f): SettingValueState<Float> { | ||
return remember { InMemoryFloatSettingValueState(defaultValue) } | ||
} | ||
|
||
internal class InMemoryFloatSettingValueState(private val defaultValue: Float) : | ||
SettingValueState<Float> { | ||
override var value: Float by mutableStateOf(defaultValue) | ||
override fun reset() { | ||
value = defaultValue | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...src/commonMain/kotlin/com/alorma/compose/settings/storage/disk/IntSetSettingValueState.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,21 @@ | ||
package com.alorma.compose.settings.storage.disk | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import com.alorma.compose.settings.storage.base.SettingValueState | ||
|
||
@Composable | ||
fun rememberIntSetSettingState(defaultValue: Set<Int> = emptySet()): SettingValueState<Set<Int>> { | ||
return remember { InMemoryIntSetSettingValueState(defaultValue) } | ||
} | ||
|
||
internal class InMemoryIntSetSettingValueState(private val defaultValue: Set<Int>) : | ||
SettingValueState<Set<Int>> { | ||
override var value: Set<Int> by mutableStateOf(defaultValue) | ||
override fun reset() { | ||
value = defaultValue | ||
} | ||
} |
Oops, something went wrong.