-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support to use composeValueManager inside an AAC ViewModel and restore values using SavedStateHandle
- Loading branch information
1 parent
7475731
commit 6028fcd
Showing
11 changed files
with
278 additions
and
13 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
compose/android/src/dev/programadorthi/state/compose/ComposeValueManagerSavedStateHandle.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 dev.programadorthi.state.compose | ||
|
||
import androidx.compose.runtime.SnapshotMutationPolicy | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.saveable.Saver | ||
import androidx.compose.runtime.saveable.SaverScope | ||
import androidx.lifecycle.SavedStateHandle | ||
import dev.programadorthi.state.core.ValueManager | ||
|
||
internal class ComposeValueManagerSavedStateHandle<T>( | ||
private val valueManager: ValueManager<T>, | ||
private val policy: SnapshotMutationPolicy<T>, | ||
private val savedStateHandle: SavedStateHandle, | ||
stateRestorationKey: String, | ||
saver: Saver<T, out Any>, | ||
) : SaverScope, ValueManager<T> by valueManager { | ||
|
||
private val valueManagerSaver = ValueManagerSaver(saver) { valueManager } | ||
private val state = mutableStateOf(valueManager.value, policy) | ||
private val key = "$KEY:$stateRestorationKey" | ||
private val canBeSaved: Boolean | ||
|
||
init { | ||
val restoredValue = savedStateHandle.remove<Any>(key) | ||
|
||
// By default SaveStateHandle throw exceptions when can't save the value in a Bundle | ||
savedStateHandle[key] = valueManager.value | ||
canBeSaved = true | ||
|
||
valueManager.collect { newValue -> | ||
state.value = newValue | ||
savedStateHandle[key] = with(valueManagerSaver) { | ||
save(valueManager) | ||
} | ||
} | ||
|
||
if (restoredValue != null) { | ||
valueManagerSaver.restore(restoredValue) | ||
} | ||
} | ||
|
||
override var value: T | ||
get() = state.value | ||
set(value) { | ||
val current = state.value | ||
if (policy.equivalent(current, value).not()) { | ||
valueManager.value = value | ||
} | ||
} | ||
|
||
override fun canBeSaved(value: Any): Boolean = canBeSaved | ||
|
||
private companion object { | ||
private const val KEY = "dev.programadorthi.state.compose.ComposeValueManagerState.android" | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
compose/android/src/dev/programadorthi/state/compose/ValueManagerAsState.android.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,70 @@ | ||
package dev.programadorthi.state.compose | ||
|
||
import androidx.compose.runtime.SnapshotMutationPolicy | ||
import androidx.compose.runtime.saveable.Saver | ||
import androidx.compose.runtime.saveable.autoSaver | ||
import androidx.compose.runtime.structuralEqualityPolicy | ||
import androidx.lifecycle.SavedStateHandle | ||
import dev.programadorthi.state.core.ValueManager | ||
import dev.programadorthi.state.core.extension.basicValueManager | ||
import kotlin.properties.PropertyDelegateProvider | ||
import kotlin.properties.ReadWriteProperty | ||
import kotlin.reflect.KProperty | ||
|
||
public fun <T> composeValueManager( | ||
initialValue: T, | ||
stateRestorationKey: String, | ||
savedStateHandle: SavedStateHandle, | ||
policy: SnapshotMutationPolicy<T> = structuralEqualityPolicy(), | ||
saver: Saver<T, out Any> = autoSaver(), | ||
): ValueManager<T> = ComposeValueManagerSavedStateHandle( | ||
valueManager = basicValueManager(initialValue), | ||
policy = policy, | ||
savedStateHandle = savedStateHandle, | ||
stateRestorationKey = stateRestorationKey, | ||
saver = saver, | ||
) | ||
|
||
public fun <T> composeValueManager( | ||
initialValue: T, | ||
savedStateHandle: SavedStateHandle, | ||
policy: SnapshotMutationPolicy<T> = structuralEqualityPolicy(), | ||
saver: Saver<T, out Any> = autoSaver(), | ||
): ComposeValueManager<T> = basicValueManager(initialValue).asState( | ||
savedStateHandle = savedStateHandle, | ||
policy = policy, | ||
saver = saver, | ||
) | ||
|
||
public fun <T> ValueManager<T>.asState( | ||
savedStateHandle: SavedStateHandle, | ||
policy: SnapshotMutationPolicy<T> = structuralEqualityPolicy(), | ||
saver: Saver<T, out Any> = autoSaver(), | ||
): ComposeValueManager<T> = PropertyDelegateProvider { _, property -> | ||
val state = asState( | ||
stateRestorationKey = property.name, | ||
savedStateHandle = savedStateHandle, | ||
policy = policy, | ||
saver = saver, | ||
) | ||
object : ReadWriteProperty<Any?, T> { | ||
override fun getValue(thisRef: Any?, property: KProperty<*>): T = state.value | ||
|
||
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { | ||
state.value = value | ||
} | ||
} | ||
} | ||
|
||
public fun <T> ValueManager<T>.asState( | ||
stateRestorationKey: String, | ||
savedStateHandle: SavedStateHandle, | ||
policy: SnapshotMutationPolicy<T> = structuralEqualityPolicy(), | ||
saver: Saver<T, out Any> = autoSaver(), | ||
): ValueManager<T> = ComposeValueManagerSavedStateHandle( | ||
valueManager = this, | ||
policy = policy, | ||
savedStateHandle = savedStateHandle, | ||
stateRestorationKey = stateRestorationKey, | ||
saver = saver, | ||
) |
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
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
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
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
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
48 changes: 48 additions & 0 deletions
48
.../compose/norris-facts/android/src/main/java/dev/programadorthi/android/ComposeActivity.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,48 @@ | ||
package dev.programadorthi.android | ||
|
||
import android.os.Bundle | ||
import androidx.activity.compose.setContent | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
|
||
class ComposeActivity : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContent { | ||
Content() | ||
} | ||
} | ||
|
||
@Composable | ||
private fun Content( | ||
viewModel: ComposeViewModel = viewModel() | ||
) { | ||
Column( | ||
modifier = Modifier.fillMaxSize(), | ||
verticalArrangement = Arrangement.Center, | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
) { | ||
Text("Counter: ${viewModel.state}") | ||
Spacer(modifier = Modifier.height(16.dp)) | ||
Button(onClick = viewModel::decrement) { | ||
Text("Decrement") | ||
} | ||
Spacer(modifier = Modifier.height(16.dp)) | ||
Button(onClick = viewModel::increment) { | ||
Text("Increment") | ||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...compose/norris-facts/android/src/main/java/dev/programadorthi/android/ComposeViewModel.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 dev.programadorthi.android | ||
|
||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.ViewModel | ||
import dev.programadorthi.state.compose.composeValueManager | ||
|
||
class ComposeViewModel(savedStateHandle: SavedStateHandle) : ViewModel() { | ||
|
||
private var composeState by composeValueManager(0, savedStateHandle) | ||
|
||
val state: Int | ||
get() = composeState | ||
|
||
fun increment() { | ||
composeState++ | ||
} | ||
|
||
fun decrement() { | ||
composeState-- | ||
} | ||
} |
Oops, something went wrong.