-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serialize state to parcel for state restoration tester
- Loading branch information
Showing
4 changed files
with
162 additions
and
30 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
92 changes: 92 additions & 0 deletions
92
.../com/alexvanyo/composelife/kmpstaterestorationtester/KmpStateRestorationTester.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,92 @@ | ||
/* | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alexvanyo.composelife.kmpstaterestorationtester | ||
|
||
import android.os.Bundle | ||
import android.os.Parcel | ||
import android.os.Parcelable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.saveable.SaveableStateRegistry | ||
import androidx.compose.runtime.setValue | ||
|
||
private class RestorationRegistryImpl(private val original: SaveableStateRegistry) : | ||
RestorationRegistry { | ||
|
||
override var shouldEmitChildren by mutableStateOf(true) | ||
private set | ||
private var currentRegistry: SaveableStateRegistry = original | ||
private lateinit var savedParcel: Parcel | ||
|
||
override fun saveStateAndDisposeChildren() { | ||
savedParcel = Parcel.obtain().also { | ||
currentRegistry.performSave().toBundle().writeToParcel(it, Parcelable.PARCELABLE_WRITE_RETURN_VALUE) | ||
} | ||
shouldEmitChildren = false | ||
} | ||
|
||
override fun emitChildrenWithRestoredState() { | ||
savedParcel.setDataPosition(0) | ||
val restoredValues = Bundle.CREATOR.createFromParcel(savedParcel).apply { | ||
classLoader = this@RestorationRegistryImpl.javaClass.classLoader | ||
}.toMap() | ||
savedParcel.recycle() | ||
currentRegistry = SaveableStateRegistry( | ||
restoredValues = restoredValues, | ||
canBeSaved = { original.canBeSaved(it) }, | ||
) | ||
shouldEmitChildren = true | ||
} | ||
|
||
override fun consumeRestored(key: String) = currentRegistry.consumeRestored(key) | ||
|
||
override fun registerProvider(key: String, valueProvider: () -> Any?) = | ||
currentRegistry.registerProvider(key, valueProvider) | ||
|
||
override fun canBeSaved(value: Any) = currentRegistry.canBeSaved(value) | ||
|
||
override fun performSave() = currentRegistry.performSave() | ||
} | ||
|
||
internal actual fun RestorationRegistry( | ||
original: SaveableStateRegistry, | ||
): RestorationRegistry = RestorationRegistryImpl(original) | ||
|
||
// Copied from DisposableSaveableStateRegistry.android.kt | ||
@Suppress("DEPRECATION", "UNCHECKED_CAST") | ||
private fun Bundle.toMap(): Map<String, List<Any?>>? { | ||
val map = mutableMapOf<String, List<Any?>>() | ||
this.keySet().forEach { key -> | ||
val list = getParcelableArrayList<Parcelable?>(key) as ArrayList<Any?> | ||
map[key] = list | ||
} | ||
return map | ||
} | ||
|
||
// Copied from DisposableSaveableStateRegistry.android.kt | ||
@Suppress("UNCHECKED_CAST") | ||
private fun Map<String, List<Any?>>.toBundle(): Bundle { | ||
val bundle = Bundle() | ||
forEach { (key, list) -> | ||
val arrayList = if (list is ArrayList<Any?>) list else ArrayList(list) | ||
bundle.putParcelableArrayList( | ||
key, | ||
arrayList as ArrayList<Parcelable?>, | ||
) | ||
} | ||
return bundle | ||
} |
57 changes: 57 additions & 0 deletions
57
.../com/alexvanyo/composelife/kmpstaterestorationtester/KmpStateRestorationTester.desktop.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,57 @@ | ||
/* | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alexvanyo.composelife.kmpstaterestorationtester | ||
|
||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.saveable.SaveableStateRegistry | ||
import androidx.compose.runtime.setValue | ||
|
||
private class RestorationRegistryImpl(private val original: SaveableStateRegistry) : | ||
RestorationRegistry { | ||
|
||
override var shouldEmitChildren by mutableStateOf(true) | ||
private set | ||
private var currentRegistry: SaveableStateRegistry = original | ||
private var savedMap: Map<String, List<Any?>> = emptyMap() | ||
|
||
override fun saveStateAndDisposeChildren() { | ||
savedMap = currentRegistry.performSave() | ||
shouldEmitChildren = false | ||
} | ||
|
||
override fun emitChildrenWithRestoredState() { | ||
currentRegistry = SaveableStateRegistry( | ||
restoredValues = savedMap, | ||
canBeSaved = { original.canBeSaved(it) }, | ||
) | ||
shouldEmitChildren = true | ||
} | ||
|
||
override fun consumeRestored(key: String) = currentRegistry.consumeRestored(key) | ||
|
||
override fun registerProvider(key: String, valueProvider: () -> Any?) = | ||
currentRegistry.registerProvider(key, valueProvider) | ||
|
||
override fun canBeSaved(value: Any) = currentRegistry.canBeSaved(value) | ||
|
||
override fun performSave() = currentRegistry.performSave() | ||
} | ||
|
||
internal actual fun RestorationRegistry( | ||
original: SaveableStateRegistry, | ||
): RestorationRegistry = RestorationRegistryImpl(original) |
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