Skip to content

Commit

Permalink
update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
makeevrserg committed Jun 22, 2024
1 parent ff1d992 commit b2f34b2
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SettingsApi(private val settings: Settings) {
class IntKrate(
key: String,
settings: Settings
) : MutableKrate<Int?> by DefaultMutableKrate<Int?>(
) : Krate.Mutable<Int?> by DefaultMutableKrate<Int?>(
factory = { null },
saver = { value -> settings[key] = value },
loader = { settings[key] }
Expand All @@ -62,15 +62,15 @@ class SettingsApi(private val settings: Settings) {
// Create custom type-safe parser for your krate
private class CustomClassKrate(
key: String,
factory: DefaultValueFactory<CustomClass>
) : MutableKrate<CustomClass> by DefaultMutableKrate(
factory: ValueFactory<CustomClass>
) : Krate.Mutable<CustomClass> by DefaultMutableKrate(
factory = factory,
loader = { settings[key]?.let(::CustomClass) },
saver = { customClass -> settings[key] = customClass.customInt }
)

// Register krate
val customKrate: MutableKrate<CustomClass> = CustomClassKrate(
val customKrate: Krate.Mutable<CustomClass> = CustomClassKrate(
key = "CUSTOM_KEY",
factory = { CustomClass(100) }
)
Expand All @@ -92,17 +92,16 @@ class SettingsApi(private val settings: Settings) {
// Create custom nullable parser for your krate
private class CustomClassKrate(
key: String,
factory: DefaultValueFactory<CustomClass?>
) : MutableKrate<CustomClass?> by DefaultMutableKrate(
factory = factory,
) : Krate.Mutable<CustomClass?> by DefaultMutableKrate(
factory = { null },
loader = { settings[key]?.let(::CustomClass) },
saver = { customClass -> settings[key] = customClass?.customInt }
)

// Register krate with default parameter
val customKrate: MutableKrate<CustomClass> = CustomClassKrate(
val customKrate: Krate.Mutable<CustomClass> = CustomClassKrate(
key = "CUSTOM_KEY",
).withDefault(15)
).withDefault { CustomClass(15) }
}
```

Expand All @@ -115,8 +114,8 @@ class SettingsApi(private val dataStore: DataStore<Preferences>) {
internal class DataStoreFlowMutableKrate<T>(
key: Preferences.Key<T>,
dataStore: DataStore<Preferences>,
factory: SuspendValueFactory<T>,
) : FlowMutableKrate<T> by DefaultFlowMutableKrate(
factory: ValueFactory<T>,
) : FlowKrate.Mutable<T> by DefaultFlowMutableKrate(
factory = factory,
loader = { dataStore.data.map { it[key] } },
saver = { value ->
Expand Down

0 comments on commit b2f34b2

Please sign in to comment.