Skip to content

Commit

Permalink
Use androidx.core-bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
ogaclejapan committed May 25, 2024
1 parent 4e95c20 commit dd945ff
Show file tree
Hide file tree
Showing 9 changed files with 373 additions and 315 deletions.
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ androidx-lifecycle = "2.7.0"
compose = "1.6.7"
compose-multiplatform = "1.6.10"
dokka = "1.9.20"
jbx-core-bundle = "1.0.0"
jbx-savedstate = "1.2.0"
kotlin = "1.9.23"
kotlinx-coroutines = "1.8.0"
kotlinx-serialization = "1.6.3"
Expand All @@ -28,6 +30,8 @@ compose-ui-test-junit4-android = { module = "androidx.compose.ui:ui-test-junit4-
compose-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest", version.ref = "compose" }
compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "compose" }
compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview", version.ref = "compose" }
jbx-core-bundle = { module = "org.jetbrains.androidx.core:core-bundle", version.ref = "jbx-core-bundle" }
jbx-savedstate = { module = "org.jetbrains.androidx.savedstate:savedstate", version.ref = "jbx-savedstate" }
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinx-coroutines" }
Expand Down
2 changes: 2 additions & 0 deletions soil-space/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ kotlin {
commonMain.dependencies {
implementation(compose.runtime)
implementation(compose.runtimeSaveable)
api(libs.jbx.savedstate)
api(libs.jbx.core.bundle)
}

androidMain.dependencies {
Expand Down
99 changes: 87 additions & 12 deletions soil-space/src/androidMain/kotlin/soil/space/Atom.android.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,132 @@

package soil.space

import android.os.Parcelable
import androidx.core.bundle.Bundle
import androidx.core.os.BundleCompat
import java.io.Serializable

/**
* Creates an [Atom] using [AtomSaverKey] for [Parcelable].
*
* @param T The type of the value to be stored.
* @param initialValue The initial value to be stored.
* @param saverKey The key to be used to save and restore the value.
* @param scope The scope to be used to manage the value.
* @return The created Atom.
*/
@JvmName("atomWithParcelable")
inline fun <reified T : Parcelable> atom(
initialValue: T,
saverKey: AtomSaverKey,
scope: AtomScope? = null
): Atom<T> {
return atom(initialValue, parcelableSaver(saverKey), scope)
}

/**
* Creates an [Atom] using [AtomSaverKey] for [ArrayList] with [Parcelable][T].
*
* @param T The type of the value to be stored.
* @param initialValue The initial value to be stored.
* @param saverKey The key to be used to save and restore the value.
* @param scope The scope to be used to manage the value.
* @return The created Atom.
*/
@JvmName("atomWithParcelableArrayList")
inline fun <reified T : Parcelable> atom(
initialValue: ArrayList<T>,
saverKey: AtomSaverKey,
scope: AtomScope? = null
): Atom<ArrayList<T>> {
return atom(initialValue, parcelableArrayListSaver(saverKey), scope)
}

/**
* Creates an [Atom] using [AtomSaverKey] for [Array] with [Parcelable][T].
*
* @param T The type of the value to be stored.
* @param initialValue The initial value to be stored.
* @param saverKey The key to be used to save and restore the value.
* @param scope The scope to be used to manage the value.
* @return The created Atom.
*/
@JvmName("atomWithParcelableArray")
inline fun <reified T : Parcelable> atom(
initialValue: Array<T>,
saverKey: AtomSaverKey,
scope: AtomScope? = null
): Atom<Array<T>> {
return atom(initialValue, parcelableArraySaver(saverKey), scope)
}

/**
* Creates an [Atom] using [AtomSaverKey] for [Serializable].
*
* @param T The type of the value to be stored.
* @param initialValue The initial value to be stored.
* @param saverKey The key to be used to save and restore the value.
* @param scope The scope to be used to manage the value.
* @return The created Atom.
*/
@JvmName("atomWithSerializable")
inline fun <reified T : Serializable> atom(
initialValue: T,
saverKey: AtomSaverKey,
scope: AtomScope? = null
): Atom<T> {
return atom(initialValue, serializableSaver(saverKey), scope)
}

@PublishedApi
internal actual inline fun <reified T : CommonParcelable> parcelableSaver(key: AtomSaverKey): AtomSaver<T> {
internal inline fun <reified T : Parcelable> parcelableSaver(key: AtomSaverKey): AtomSaver<T> {
return object : AtomSaver<T> {
override fun save(bundle: CommonBundle, value: T) {
override fun save(bundle: Bundle, value: T) {
bundle.putParcelable(key, value)
}

override fun restore(bundle: CommonBundle): T? {
override fun restore(bundle: Bundle): T? {
return if (bundle.containsKey(key)) BundleCompat.getParcelable(bundle, key, T::class.java) else null
}
}
}

@PublishedApi
internal actual inline fun <reified T : CommonParcelable> parcelableArrayListSaver(key: AtomSaverKey): AtomSaver<ArrayList<T>> {
internal inline fun <reified T : Parcelable> parcelableArrayListSaver(key: AtomSaverKey): AtomSaver<ArrayList<T>> {
return object : AtomSaver<ArrayList<T>> {
override fun save(bundle: CommonBundle, value: ArrayList<T>) {
override fun save(bundle: Bundle, value: ArrayList<T>) {
bundle.putParcelableArrayList(key, value)
}

override fun restore(bundle: CommonBundle): ArrayList<T>? {
override fun restore(bundle: Bundle): ArrayList<T>? {
return BundleCompat.getParcelableArrayList(bundle, key, T::class.java)
}
}
}

@Suppress("UNCHECKED_CAST")
@PublishedApi
internal actual inline fun <reified T : CommonParcelable> parcelableArraySaver(key: AtomSaverKey): AtomSaver<Array<T>> {
internal inline fun <reified T : Parcelable> parcelableArraySaver(key: AtomSaverKey): AtomSaver<Array<T>> {
return object : AtomSaver<Array<T>> {
override fun save(bundle: CommonBundle, value: Array<T>) {
override fun save(bundle: Bundle, value: Array<T>) {
bundle.putParcelableArray(key, value)
}

override fun restore(bundle: CommonBundle): Array<T>? {
override fun restore(bundle: Bundle): Array<T>? {
return BundleCompat.getParcelableArray(bundle, key, T::class.java) as? Array<T>
}
}
}

@Suppress("DEPRECATION")
@PublishedApi
internal actual inline fun <reified T : CommonSerializable> serializableSaver(key: AtomSaverKey): AtomSaver<T> {
internal inline fun <reified T : Serializable> serializableSaver(key: AtomSaverKey): AtomSaver<T> {
return object : AtomSaver<T> {
override fun save(bundle: CommonBundle, value: T) {
override fun save(bundle: Bundle, value: T) {
bundle.putSerializable(key, value)
}

override fun restore(bundle: CommonBundle): T? {
override fun restore(bundle: Bundle): T? {
// TODO: Compat package starting with Core-ktx Version 1.13
// ref. https://issuetracker.google.com/issues/317403466
return if (bundle.containsKey(key)) bundle.getSerializable(key) as T else null
Expand Down
14 changes: 0 additions & 14 deletions soil-space/src/androidMain/kotlin/soil/space/Platform.android.kt

This file was deleted.

Loading

0 comments on commit dd945ff

Please sign in to comment.