Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Essenty to 2.2.0-alpha02 #735

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading