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

Added PersistableBundle extensions #174

Merged
merged 1 commit into from
Jul 20, 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
7 changes: 7 additions & 0 deletions state-keeper/api/android/state-keeper.api
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ public final class com/arkivanov/essenty/statekeeper/BundleExtKt {
public abstract interface annotation class com/arkivanov/essenty/statekeeper/ExperimentalStateKeeperApi : java/lang/annotation/Annotation {
}

public final class com/arkivanov/essenty/statekeeper/PersistableBundleExtKt {
public static final fun getSerializable (Landroid/os/PersistableBundle;Ljava/lang/String;Lkotlinx/serialization/DeserializationStrategy;)Ljava/lang/Object;
public static final fun getSerializableContainer (Landroid/os/PersistableBundle;Ljava/lang/String;)Lcom/arkivanov/essenty/statekeeper/SerializableContainer;
public static final fun putSerializable (Landroid/os/PersistableBundle;Ljava/lang/String;Ljava/lang/Object;Lkotlinx/serialization/SerializationStrategy;)V
public static final fun putSerializableContainer (Landroid/os/PersistableBundle;Ljava/lang/String;Lcom/arkivanov/essenty/statekeeper/SerializableContainer;)V
}

public final class com/arkivanov/essenty/statekeeper/PolymorphicSerializerKt {
public static final fun polymorphicSerializer (Lkotlin/reflect/KClass;Lkotlinx/serialization/modules/SerializersModule;)Lkotlinx/serialization/KSerializer;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.arkivanov.essenty.statekeeper

import android.os.Build
import android.os.Bundle
import android.os.PersistableBundle
import androidx.annotation.RequiresApi
import com.arkivanov.essenty.statekeeper.base64.base64ToByteArray
import com.arkivanov.essenty.statekeeper.base64.toBase64
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerializationStrategy

/**
* Inserts the provided `kotlinx-serialization` [Serializable][kotlinx.serialization.Serializable] value
* into this [PersistableBundle], replacing any existing value for the given [key].
* Either [key] or [value] may be `null`.
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
fun <T : Any> PersistableBundle.putSerializable(key: String?, value: T?, strategy: SerializationStrategy<T>) {
putString(key, value?.serialize(strategy)?.toBase64())
}

/**
* Returns a `kotlinx-serialization` [Serializable][kotlinx.serialization.Serializable] associated with
* the given [key], or `null` if no mapping exists for the given [key] or a `null` value is explicitly
* associated with the [key].
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
fun <T : Any> PersistableBundle.getSerializable(key: String?, strategy: DeserializationStrategy<T>): T? =
getString(key)?.base64ToByteArray()?.deserialize(strategy)

/**
* Inserts the provided [SerializableContainer] into this [Bundle],
* replacing any existing value for the given [key]. Either [key] or [value] may be `null`.
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
fun PersistableBundle.putSerializableContainer(key: String?, value: SerializableContainer?) {
putSerializable(key = key, value = value, strategy = SerializableContainer.serializer())
}

/**
* Returns a [SerializableContainer] associated with the given [key],
* or `null` if no mapping exists for the given [key] or a `null` value
* is explicitly associated with the [key].
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
fun PersistableBundle.getSerializableContainer(key: String?): SerializableContainer? =
getSerializable(key = key, strategy = SerializableContainer.serializer())
Loading