Skip to content

Commit

Permalink
Created MutableNavigationStateSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
chRyNaN committed May 14, 2023
1 parent f5d111e commit ef1b167
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ sealed interface NavigationState<T> {
/**
* A mutable version of [NavigationState] that allows changing of the underlying wrapped state value.
*/
@Serializable(with = MutableNavigationStateSerializer::class)
internal sealed interface MutableNavigationState<T> : NavigationState<T> {

/**
Expand Down Expand Up @@ -179,3 +180,44 @@ internal class NavigationStateSerializer<T> internal constructor(
override fun toString(): String =
"NavigationStateSerializer(delegateSerializer=$delegateSerializer)"
}

/**
* A [KSerializer] for a [MutableNavigationState].
*/
internal class MutableNavigationStateSerializer<T> internal constructor(
elementSerializer: KSerializer<T>
) : KSerializer<MutableNavigationState<T>> {

private val delegateSerializer = PersistedNavigationStateSnapshot.serializer(elementSerializer)

override val descriptor: SerialDescriptor
get() = delegateSerializer.descriptor

override fun serialize(encoder: Encoder, value: MutableNavigationState<T>) {
val snapshot = PersistedNavigationStateSnapshot(initial = value.initial, current = value.current)

delegateSerializer.serialize(encoder = encoder, value = snapshot)
}

override fun deserialize(decoder: Decoder): MutableNavigationState<T> {
val snapshot = delegateSerializer.deserialize(decoder = decoder)

return mutableNavigationStateOf(initial = snapshot.initial).apply {
this.update(state = snapshot.current)
}
}

override fun equals(other: Any?): Boolean {
if (this === other) return true

if (other !is MutableNavigationStateSerializer<*>) return false

return delegateSerializer == other.delegateSerializer
}

override fun hashCode(): Int =
delegateSerializer.hashCode()

override fun toString(): String =
"MutableNavigationStateSerializer(delegateSerializer=$delegateSerializer)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,8 @@ internal class StackSerializer<E> internal constructor(
return delegateSerializer == other.delegateSerializer
}

override fun hashCode(): Int {
return delegateSerializer.hashCode()
}
override fun hashCode(): Int =
delegateSerializer.hashCode()

override fun toString(): String =
"StackSerializer(delegateSerializer=$delegateSerializer)"
Expand Down Expand Up @@ -329,9 +328,8 @@ internal class MutableStackSerializer<E> internal constructor(
return delegateSerializer == other.delegateSerializer
}

override fun hashCode(): Int {
return delegateSerializer.hashCode()
}
override fun hashCode(): Int =
delegateSerializer.hashCode()

override fun toString(): String =
"MutableStackSerializer(delegateSerializer=$delegateSerializer)"
Expand Down

0 comments on commit ef1b167

Please sign in to comment.