Skip to content

Commit

Permalink
Merge pull request #735 from arkivanov/update-essenty
Browse files Browse the repository at this point in the history
Updated Essenty to 2.2.0-alpha02
  • Loading branch information
arkivanov authored Jul 8, 2024
2 parents 170b65c + e674b87 commit d314f1d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
2 changes: 1 addition & 1 deletion deps.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

decompose = "3.2.0-alpha01"
kotlin = "2.0.0"
essenty = "2.1.0"
essenty = "2.2.0-alpha02"
reaktive = "1.2.3"
junit = "4.13.2"
jetbrainsCompose = "1.6.10"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import com.arkivanov.decompose.ComponentContext
import com.arkivanov.decompose.value.MutableValue
import com.arkivanov.decompose.value.Value
import com.arkivanov.decompose.value.update
import com.arkivanov.essenty.instancekeeper.ExperimentalInstanceKeeperApi
import com.arkivanov.essenty.instancekeeper.InstanceKeeper
import com.arkivanov.essenty.instancekeeper.getOrCreate
import com.arkivanov.essenty.instancekeeper.retainedInstance
import com.arkivanov.essenty.lifecycle.subscribe
import com.arkivanov.essenty.statekeeper.ExperimentalStateKeeperApi
import com.arkivanov.essenty.statekeeper.saveable
import com.arkivanov.sample.shared.cards.card.CardComponent.Model
import com.badoo.reaktive.disposable.Disposable
import com.badoo.reaktive.disposable.scope.DisposableScope
Expand All @@ -16,7 +19,7 @@ import com.badoo.reaktive.scheduler.Scheduler
import com.badoo.reaktive.scheduler.mainScheduler
import com.badoo.reaktive.subject.behavior.BehaviorObservable
import com.badoo.reaktive.subject.behavior.BehaviorSubject
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer

class DefaultCardComponent(
componentContext: ComponentContext,
Expand All @@ -25,20 +28,17 @@ class DefaultCardComponent(
tickScheduler: Scheduler = mainScheduler,
) : CardComponent, ComponentContext by componentContext, DisposableScope by DisposableScope() {

private val handler =
instanceKeeper.getOrCreate {
Handler(
initialCount = stateKeeper.consume(key = KEY_SAVED_STATE, strategy = SavedState.serializer())?.count ?: 0,
tickScheduler = tickScheduler,
)
@OptIn(ExperimentalStateKeeperApi::class, ExperimentalInstanceKeeperApi::class)
private val handler: Handler by saveable(serializer = Int.serializer(), state = { it.count.value }) { savedState ->
retainedInstance {
Handler(initialCount = savedState ?: 0, tickScheduler = tickScheduler)
}
}

private val _model = MutableValue(Model(color = color, title = number.toString()))
override val model: Value<Model> = _model

init {
stateKeeper.register(key = KEY_SAVED_STATE, strategy = SavedState.serializer()) { SavedState(count = handler.count.value) }

handler.count.subscribeScoped { count ->
_model.update { it.copy(text = "Count: $count") }
}
Expand All @@ -62,10 +62,6 @@ class DefaultCardComponent(
_model.update { it.copy(status = "Status: $status") }
}

private companion object {
const val KEY_SAVED_STATE: String = "SAVED_STATE"
}

private class Handler(
initialCount: Int,
private val tickScheduler: Scheduler,
Expand All @@ -89,7 +85,4 @@ class DefaultCardComponent(
stop()
}
}

@Serializable
private class SavedState(val count: Int)
}

0 comments on commit d314f1d

Please sign in to comment.