-
-
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
6 changed files
with
194 additions
and
3 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
44 changes: 44 additions & 0 deletions
44
...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,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 rememberBooleanSettingState( | ||
key: String, | ||
defaultValue: Boolean, | ||
settings: Settings = Settings() | ||
): BooleanSettingValueState { | ||
return remember { | ||
BooleanSettingValueState( | ||
settings = settings, | ||
key = key, | ||
defaultValue = defaultValue, | ||
) | ||
} | ||
} | ||
|
||
class BooleanSettingValueState( | ||
private val settings: Settings, | ||
val key: String, | ||
val defaultValue: Boolean = false, | ||
) : SettingValueState<Boolean> { | ||
|
||
private var _value by mutableStateOf(settings.getBoolean(key, defaultValue)) | ||
|
||
override var value: Boolean | ||
set(value) { | ||
_value = value | ||
settings.putBoolean(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/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,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 rememberFloatSettingState( | ||
key: String, | ||
defaultValue: Float = 0f, | ||
settings: Settings = Settings() | ||
): FloatSettingValueState { | ||
return remember { | ||
FloatSettingValueState( | ||
settings = settings, | ||
key = key, | ||
defaultValue = defaultValue, | ||
) | ||
} | ||
} | ||
|
||
class FloatSettingValueState( | ||
private val settings: Settings, | ||
val key: String, | ||
val defaultValue: Float = 0f, | ||
) : SettingValueState<Float> { | ||
|
||
private var _value by mutableStateOf(settings.getFloat(key, defaultValue)) | ||
|
||
override var value: Float | ||
set(value) { | ||
_value = value | ||
settings.putFloat(key, value) | ||
} | ||
get() = _value | ||
|
||
override fun reset() { | ||
value = defaultValue | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...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,56 @@ | ||
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 rememberIntSetSettingState( | ||
key: String, | ||
defaultValue: Set<Int> = emptySet(), | ||
delimiter: String = "|", | ||
settings: Settings = Settings() | ||
): IntSetSettingValueState { | ||
return remember { | ||
IntSetSettingValueState( | ||
settings = settings, | ||
key = key, | ||
defaultValue = defaultValue, | ||
delimiter = delimiter, | ||
) | ||
} | ||
} | ||
|
||
class IntSetSettingValueState( | ||
private val settings: Settings, | ||
val key: String, | ||
val defaultValue: Set<Int> = emptySet(), | ||
val delimiter: String = "|", | ||
) : SettingValueState<Set<Int>> { | ||
|
||
private var _value by mutableStateOf( | ||
settings.getString(key, defaultValue.toPrefString(delimiter)) | ||
.split(delimiter) | ||
.filter { it.isNotEmpty() } | ||
.map { it.toInt() } | ||
.toSet(), | ||
) | ||
|
||
override var value: Set<Int> | ||
set(value) { | ||
_value = value.sorted().toSet() | ||
settings.putString(key, value.toPrefString(delimiter)) | ||
} | ||
get() = _value | ||
|
||
private fun Set<Int>.toPrefString(delimiter: String) = | ||
joinToString(separator = delimiter) { it.toString() } | ||
|
||
override fun reset() { | ||
value = defaultValue | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...sk/src/commonMain/kotlin/com/alorma/compose/settings/storage/disk/IntSettingValueState.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 rememberIntSettingState( | ||
key: String, | ||
defaultValue: Int = -1, | ||
settings: Settings = Settings() | ||
): IntSettingValueState { | ||
return remember { | ||
IntSettingValueState( | ||
settings = settings, | ||
key = key, | ||
defaultValue = defaultValue, | ||
) | ||
} | ||
} | ||
|
||
class IntSettingValueState( | ||
private val settings: Settings, | ||
val key: String, | ||
val defaultValue: Int = 0, | ||
) : SettingValueState<Int> { | ||
|
||
private var _value by mutableStateOf(settings.getInt(key, defaultValue)) | ||
|
||
override var value: Int | ||
set(value) { | ||
_value = value | ||
settings.putInt(key, value) | ||
} | ||
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