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

jsonNavigatorSaver for kotlinx.Serialization for state restoration #497

Open
osrl opened this issue Oct 9, 2024 · 0 comments
Open

jsonNavigatorSaver for kotlinx.Serialization for state restoration #497

osrl opened this issue Oct 9, 2024 · 0 comments

Comments

@osrl
Copy link
Contributor

osrl commented Oct 9, 2024

This is not an issue but I didn't know where to put this. I've been trying to implement a NavigatorSaver to save and restore state of @Serializable data classes. I needed it because my multiplatform data classes had kotlinx.DateTime classes and they're not java.io.Serializable. I'm open to change my approach if you suggest me some other way to serialize DateTime classes.

If anyone needs it, here is how I've implemented it:

val screenSerializersModule = SerializersModule {
    polymorphic(Screen::class) {
        subclass(MyScreen::class, MyScreen.serializer())
        // Register other subclasses here
    }
}
val screenJson: Json
    get() = Json(defaultJson) {
        serializersModule = screenSerializersModule // Include the module here
    }

@OptIn(InternalSerializationApi::class)
fun isKotlinxSerializable(screenClass: KClass<*>): Boolean {
    return try {
        screenClass.serializerOrNull() != null
    } catch (e: Exception) {
        false
    }
}

@OptIn(
    ExperimentalVoyagerApi::class,
    InternalVoyagerApi::class
)
fun jsonNavigatorSaver() =
    NavigatorSaver { _, key, stateHolder, disposeBehavior, parent ->
        listSaver(
            save = { navigator ->
                navigator.items.map { screen ->
                    if (isKotlinxSerializable(screen::class)) {
                        screenJson.encodeToString(screen)
                    } else {
                        screen
                    }
                }
            },
            restore = { items ->
                // Deserialize the list of screens using custom Json configuration
                val restoredScreens = items.map { item ->
                    when (item) {
                        is String -> {
                            screenJson.decodeFromString(item) as Screen
                        }
                        else -> {
                            item as Screen
                        }
                    }
                }
                Navigator(restoredScreens, key, stateHolder, disposeBehavior, parent)
            }
        )
    }


CompositionLocalProvider(
    LocalNavigatorSaver provides jsonNavigatorSaver()
) { .... }

And your screen:

@Serializable
data class MyScreen(val myDataThatHasDateTimeField: MyData): Screen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant