diff --git a/app/src/main/kotlin/com/x8bit/bitwarden/ui/auth/feature/accountsetup/SetupAutoFillNavigation.kt b/app/src/main/kotlin/com/x8bit/bitwarden/ui/auth/feature/accountsetup/SetupAutoFillNavigation.kt index f2688f25fd..69c2ab0036 100644 --- a/app/src/main/kotlin/com/x8bit/bitwarden/ui/auth/feature/accountsetup/SetupAutoFillNavigation.kt +++ b/app/src/main/kotlin/com/x8bit/bitwarden/ui/auth/feature/accountsetup/SetupAutoFillNavigation.kt @@ -1,37 +1,59 @@ package com.x8bit.bitwarden.ui.auth.feature.accountsetup +import android.os.Parcelable import androidx.lifecycle.SavedStateHandle import androidx.navigation.NavController import androidx.navigation.NavGraphBuilder import androidx.navigation.NavOptions +import androidx.navigation.toRoute import com.bitwarden.ui.platform.base.util.composableWithPushTransitions import com.bitwarden.ui.platform.base.util.composableWithSlideTransitions -import com.x8bit.bitwarden.ui.platform.util.toObjectRoute +import com.bitwarden.ui.platform.util.ParcelableRouteSerializer +import kotlinx.parcelize.Parcelize import kotlinx.serialization.Serializable /** * The type-safe route for the setup autofill screen. */ -sealed class SetupAutofillRoute { +@Parcelize +@Serializable(with = SetupAutofillRoute.Serializer::class) +sealed class SetupAutofillRoute : Parcelable { /** * The [isInitialSetup] value used in the setup autofill screen. */ abstract val isInitialSetup: Boolean + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(SetupAutofillRoute::class) + /** * The type-safe route for the standard setup autofill screen. */ - @Serializable + @Parcelize + @Serializable(with = Standard.Serializer::class) data object Standard : SetupAutofillRoute() { override val isInitialSetup: Boolean get() = false + + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(Standard::class) } /** * The type-safe route for the root setup autofill screen. */ - @Serializable + @Parcelize + @Serializable(with = AsRoot.Serializer::class) data object AsRoot : SetupAutofillRoute() { override val isInitialSetup: Boolean get() = true + + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(AsRoot::class) } } @@ -44,11 +66,8 @@ data class SetupAutoFillScreenArgs(val isInitialSetup: Boolean) * Constructs a [SetupAutoFillScreenArgs] from the [SavedStateHandle] and internal route data. */ fun SavedStateHandle.toSetupAutoFillArgs(): SetupAutoFillScreenArgs { - val route = (this.toObjectRoute() - ?: this.toObjectRoute()) - return route - ?.let { SetupAutoFillScreenArgs(isInitialSetup = it.isInitialSetup) } - ?: throw IllegalStateException("Missing correct route for SetupAutofillScreen") + val route = this.toRoute() + return SetupAutoFillScreenArgs(isInitialSetup = route.isInitialSetup) } /** diff --git a/app/src/main/kotlin/com/x8bit/bitwarden/ui/auth/feature/accountsetup/SetupUnlockNavigation.kt b/app/src/main/kotlin/com/x8bit/bitwarden/ui/auth/feature/accountsetup/SetupUnlockNavigation.kt index 25d3d7c680..72fd63cf23 100644 --- a/app/src/main/kotlin/com/x8bit/bitwarden/ui/auth/feature/accountsetup/SetupUnlockNavigation.kt +++ b/app/src/main/kotlin/com/x8bit/bitwarden/ui/auth/feature/accountsetup/SetupUnlockNavigation.kt @@ -1,37 +1,59 @@ package com.x8bit.bitwarden.ui.auth.feature.accountsetup +import android.os.Parcelable import androidx.lifecycle.SavedStateHandle import androidx.navigation.NavController import androidx.navigation.NavGraphBuilder import androidx.navigation.NavOptions +import androidx.navigation.toRoute import com.bitwarden.ui.platform.base.util.composableWithPushTransitions import com.bitwarden.ui.platform.base.util.composableWithSlideTransitions -import com.x8bit.bitwarden.ui.platform.util.toObjectRoute +import com.bitwarden.ui.platform.util.ParcelableRouteSerializer +import kotlinx.parcelize.Parcelize import kotlinx.serialization.Serializable /** * The type-safe route for the setup unlock screen. */ -sealed class SetupUnlockRoute { +@Parcelize +@Serializable(with = SetupUnlockRoute.Serializer::class) +sealed class SetupUnlockRoute : Parcelable { /** * The [isInitialSetup] value used in the setup unlock screen. */ abstract val isInitialSetup: Boolean + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(SetupUnlockRoute::class) + /** * The type-safe route for the standard setup unlock screen. */ - @Serializable + @Parcelize + @Serializable(with = Standard.Serializer::class) data object Standard : SetupUnlockRoute() { override val isInitialSetup: Boolean get() = false + + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(Standard::class) } /** * The type-safe route for the root setup unlock screen. */ - @Serializable + @Parcelize + @Serializable(with = AsRoot.Serializer::class) data object AsRoot : SetupUnlockRoute() { override val isInitialSetup: Boolean get() = true + + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(AsRoot::class) } } @@ -46,11 +68,8 @@ data class SetupUnlockArgs( * Constructs a [SetupUnlockArgs] from the [SavedStateHandle] and internal route data. */ fun SavedStateHandle.toSetupUnlockArgs(): SetupUnlockArgs { - val route = this.toObjectRoute() - ?: this.toObjectRoute() - return route - ?.let { SetupUnlockArgs(isInitialSetup = it.isInitialSetup) } - ?: throw IllegalStateException("Missing correct route for SetupUnlockScreen") + val route = this.toRoute() + return SetupUnlockArgs(isInitialSetup = route.isInitialSetup) } /** diff --git a/app/src/main/kotlin/com/x8bit/bitwarden/ui/auth/feature/vaultunlock/VaultUnlockNavigation.kt b/app/src/main/kotlin/com/x8bit/bitwarden/ui/auth/feature/vaultunlock/VaultUnlockNavigation.kt index cce06f5f9a..1abc5bc3b4 100644 --- a/app/src/main/kotlin/com/x8bit/bitwarden/ui/auth/feature/vaultunlock/VaultUnlockNavigation.kt +++ b/app/src/main/kotlin/com/x8bit/bitwarden/ui/auth/feature/vaultunlock/VaultUnlockNavigation.kt @@ -1,38 +1,59 @@ package com.x8bit.bitwarden.ui.auth.feature.vaultunlock +import android.os.Parcelable import androidx.lifecycle.SavedStateHandle import androidx.navigation.NavController import androidx.navigation.NavGraphBuilder import androidx.navigation.NavOptions import androidx.navigation.compose.composable +import androidx.navigation.toRoute +import com.bitwarden.ui.platform.util.ParcelableRouteSerializer import com.x8bit.bitwarden.ui.auth.feature.vaultunlock.model.UnlockType -import com.x8bit.bitwarden.ui.platform.util.toObjectRoute +import kotlinx.parcelize.Parcelize import kotlinx.serialization.Serializable /** * The type-safe route for the vault unlock screen. */ -@Serializable -sealed class VaultUnlockRoute { +@Parcelize +@Serializable(with = VaultUnlockRoute.Serializer::class) +sealed class VaultUnlockRoute : Parcelable { /** * The underlying [UnlockType] used in the vault unlock screen. */ abstract val unlockType: UnlockType + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(VaultUnlockRoute::class) + /** * The type-safe route for the standard vault unlock screen. */ - @Serializable + @Parcelize + @Serializable(with = Standard.Serializer::class) data object Standard : VaultUnlockRoute() { override val unlockType: UnlockType get() = UnlockType.STANDARD + + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(Standard::class) } /** * The type-safe route for the TDE vault unlock screen. */ - @Serializable + @Parcelize + @Serializable(with = Tde.Serializer::class) data object Tde : VaultUnlockRoute() { override val unlockType: UnlockType get() = UnlockType.TDE + + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(Tde::class) } } @@ -47,11 +68,8 @@ data class VaultUnlockArgs( * Constructs a [VaultUnlockArgs] from the [SavedStateHandle] and internal route data. */ fun SavedStateHandle.toVaultUnlockArgs(): VaultUnlockArgs { - val route = this.toObjectRoute() - ?: this.toObjectRoute() - return route - ?.let { VaultUnlockArgs(unlockType = it.unlockType) } - ?: throw IllegalStateException("Missing correct route for VaultUnlockScreen") + val route = this.toRoute() + return VaultUnlockArgs(unlockType = route.unlockType) } /** diff --git a/app/src/main/kotlin/com/x8bit/bitwarden/ui/platform/feature/settings/SettingsNavigation.kt b/app/src/main/kotlin/com/x8bit/bitwarden/ui/platform/feature/settings/SettingsNavigation.kt index 0bb5182790..fd350f52ef 100644 --- a/app/src/main/kotlin/com/x8bit/bitwarden/ui/platform/feature/settings/SettingsNavigation.kt +++ b/app/src/main/kotlin/com/x8bit/bitwarden/ui/platform/feature/settings/SettingsNavigation.kt @@ -1,5 +1,6 @@ package com.x8bit.bitwarden.ui.platform.feature.settings +import android.os.Parcelable import androidx.lifecycle.SavedStateHandle import androidx.navigation.NavController import androidx.navigation.NavGraph.Companion.findStartDestination @@ -7,8 +8,10 @@ import androidx.navigation.NavGraphBuilder import androidx.navigation.NavOptions import androidx.navigation.navOptions import androidx.navigation.navigation +import androidx.navigation.toRoute import com.bitwarden.ui.platform.base.util.composableWithRootPushTransitions import com.bitwarden.ui.platform.base.util.composableWithSlideTransitions +import com.bitwarden.ui.platform.util.ParcelableRouteSerializer import com.x8bit.bitwarden.ui.platform.feature.settings.about.aboutDestination import com.x8bit.bitwarden.ui.platform.feature.settings.about.navigateToAbout import com.x8bit.bitwarden.ui.platform.feature.settings.accountsecurity.accountSecurityDestination @@ -27,7 +30,7 @@ import com.x8bit.bitwarden.ui.platform.feature.settings.other.navigateToOther import com.x8bit.bitwarden.ui.platform.feature.settings.other.otherDestination import com.x8bit.bitwarden.ui.platform.feature.settings.vault.navigateToVaultSettings import com.x8bit.bitwarden.ui.platform.feature.settings.vault.vaultSettingsDestination -import com.x8bit.bitwarden.ui.platform.util.toObjectRoute +import kotlinx.parcelize.Parcelize import kotlinx.serialization.Serializable /** @@ -39,27 +42,45 @@ data object SettingsGraphRoute /** * The type-safe route for the settings screen. */ -@Serializable -sealed class SettingsRoute { +@Parcelize +@Serializable(with = SettingsRoute.Serializer::class) +sealed class SettingsRoute : Parcelable { /** * Indicates that the settings screen should be shown as a pre-authentication. */ abstract val isPreAuth: Boolean + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(SettingsRoute::class) + /** * The type-safe route for the settings screen in the settings graph. */ - @Serializable + @Parcelize + @Serializable(with = Standard.Serializer::class) data object Standard : SettingsRoute() { override val isPreAuth: Boolean get() = false + + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(Standard::class) } /** * The type-safe route for the pre-auth settings screen. */ - @Serializable + @Parcelize + @Serializable(with = PreAuth.Serializer::class) data object PreAuth : SettingsRoute() { override val isPreAuth: Boolean get() = true + + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(PreAuth::class) } } @@ -72,12 +93,8 @@ data class SettingsArgs(val isPreAuth: Boolean) * Constructs a [SettingsArgs] from the [SavedStateHandle] and internal route data. */ fun SavedStateHandle.toSettingsArgs(): SettingsArgs { - val route = this.toObjectRoute() - ?: this.toObjectRoute() - return route - ?.let { SettingsArgs(isPreAuth = it.isPreAuth) } - ?: this.toObjectRoute()?.let { SettingsArgs(isPreAuth = false) } - ?: throw IllegalStateException("Missing correct route for SettingsScreen") + val route = this.toRoute() + return SettingsArgs(isPreAuth = route.isPreAuth) } /** diff --git a/app/src/main/kotlin/com/x8bit/bitwarden/ui/platform/feature/settings/other/OtherNavigation.kt b/app/src/main/kotlin/com/x8bit/bitwarden/ui/platform/feature/settings/other/OtherNavigation.kt index 639952e98f..325042a74b 100644 --- a/app/src/main/kotlin/com/x8bit/bitwarden/ui/platform/feature/settings/other/OtherNavigation.kt +++ b/app/src/main/kotlin/com/x8bit/bitwarden/ui/platform/feature/settings/other/OtherNavigation.kt @@ -1,37 +1,58 @@ package com.x8bit.bitwarden.ui.platform.feature.settings.other +import android.os.Parcelable import androidx.lifecycle.SavedStateHandle import androidx.navigation.NavController import androidx.navigation.NavGraphBuilder import androidx.navigation.NavOptions +import androidx.navigation.toRoute import com.bitwarden.ui.platform.base.util.composableWithPushTransitions -import com.x8bit.bitwarden.ui.platform.util.toObjectRoute +import com.bitwarden.ui.platform.util.ParcelableRouteSerializer +import kotlinx.parcelize.Parcelize import kotlinx.serialization.Serializable /** * The type-safe route for the settings other screen. */ -@Serializable -sealed class SettingsOtherRoute { +@Parcelize +@Serializable(with = SettingsOtherRoute.Serializer::class) +sealed class SettingsOtherRoute : Parcelable { /** * Indicates that the settings other screen should be shown as a pre-authentication. */ abstract val isPreAuth: Boolean + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(SettingsOtherRoute::class) + /** * The type-safe route for the settings other screen. */ - @Serializable + @Parcelize + @Serializable(with = Standard.Serializer::class) data object Standard : SettingsOtherRoute() { override val isPreAuth: Boolean get() = false + + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(Standard::class) } /** * The type-safe route for the pre-auth settings other screen. */ - @Serializable + @Parcelize + @Serializable(with = PreAuth.Serializer::class) data object PreAuth : SettingsOtherRoute() { override val isPreAuth: Boolean get() = true + + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(PreAuth::class) } } @@ -44,11 +65,8 @@ data class OtherArgs(val isPreAuth: Boolean) * Constructs a [OtherArgs] from the [SavedStateHandle] and internal route data. */ fun SavedStateHandle.toOtherArgs(): OtherArgs { - val route = this.toObjectRoute() - ?: this.toObjectRoute() - return route - ?.let { OtherArgs(isPreAuth = it.isPreAuth) } - ?: throw IllegalStateException("Missing correct route for SettingsOtherScreen") + val route = this.toRoute() + return OtherArgs(isPreAuth = route.isPreAuth) } /** diff --git a/app/src/main/kotlin/com/x8bit/bitwarden/ui/platform/util/SavedStateHandleExtensions.kt b/app/src/main/kotlin/com/x8bit/bitwarden/ui/platform/util/SavedStateHandleExtensions.kt deleted file mode 100644 index d1965fdfb8..0000000000 --- a/app/src/main/kotlin/com/x8bit/bitwarden/ui/platform/util/SavedStateHandleExtensions.kt +++ /dev/null @@ -1,25 +0,0 @@ -package com.x8bit.bitwarden.ui.platform.util - -import android.content.Intent -import androidx.lifecycle.SavedStateHandle -import androidx.navigation.NavController -import androidx.navigation.toRoute -import com.bitwarden.ui.platform.util.toObjectKClassNavigationRoute - -/** - * Determines if the [SavedStateHandle] contains a route for the specified object class. - * - * This will return the object instance if the route is correct, `null` otherwise. - */ -inline fun SavedStateHandle.toObjectRoute(): T? = - this - .get(key = NavController.KEY_DEEP_LINK_INTENT) - ?.data - ?.pathSegments - .orEmpty() - .takeIf { segments -> segments.any { it == T::class.toObjectKClassNavigationRoute() } } - ?.let { _ -> - // This will get the instance for us. We only do this after the checks above as it - // will always return the object instance even if it is not the correct one. - this.toRoute() - } diff --git a/app/src/main/kotlin/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorNavigation.kt b/app/src/main/kotlin/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorNavigation.kt index ab7edc6e66..a1ee6077de 100644 --- a/app/src/main/kotlin/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorNavigation.kt +++ b/app/src/main/kotlin/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorNavigation.kt @@ -1,5 +1,6 @@ package com.x8bit.bitwarden.ui.tools.feature.generator +import android.os.Parcelable import androidx.lifecycle.SavedStateHandle import androidx.navigation.NavController import androidx.navigation.NavGraphBuilder @@ -7,28 +8,49 @@ import androidx.navigation.NavOptions import androidx.navigation.compose.composable import androidx.navigation.toRoute import com.bitwarden.ui.platform.base.util.composableWithSlideTransitions +import com.bitwarden.ui.platform.util.ParcelableRouteSerializer import com.x8bit.bitwarden.ui.tools.feature.generator.model.GeneratorMode +import kotlinx.parcelize.Parcelize import kotlinx.serialization.Serializable /** * The type-safe route for the generator screen. */ -@Serializable -sealed class GeneratorRoute { +@Parcelize +@Serializable(with = GeneratorRoute.Serializer::class) +sealed class GeneratorRoute : Parcelable { + + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(GeneratorRoute::class) + /** * The type-safe route for the standard generator screen. */ - @Serializable - data object Standard : GeneratorRoute() + @Parcelize + @Serializable(with = Standard.Serializer::class) + data object Standard : GeneratorRoute() { + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(Standard::class) + } /** * The type-safe route for the modal generator screen. */ - @Serializable + @Parcelize + @Serializable(with = Modal.Serializer::class) data class Modal( val type: ModalType, val website: String?, - ) : GeneratorRoute() + ) : GeneratorRoute() { + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(Modal::class) + } } /** @@ -52,22 +74,17 @@ data class GeneratorArgs( */ fun SavedStateHandle.toGeneratorArgs(): GeneratorArgs { return GeneratorArgs( - type = try { - this.toModalGeneratorMode() - } catch (_: Exception) { - GeneratorMode.Default + type = when (val route = this.toRoute()) { + is GeneratorRoute.Modal -> when (route.type) { + ModalType.PASSWORD -> GeneratorMode.Modal.Password + ModalType.USERNAME -> GeneratorMode.Modal.Username(website = route.website) + } + + GeneratorRoute.Standard -> GeneratorMode.Default }, ) } -private fun SavedStateHandle.toModalGeneratorMode(): GeneratorMode.Modal { - val route = this.toRoute() - return when (route.type) { - ModalType.PASSWORD -> GeneratorMode.Modal.Password - ModalType.USERNAME -> GeneratorMode.Modal.Username(website = route.website) - } -} - /** * Add generator destination to the root nav graph. */ diff --git a/app/src/main/kotlin/com/x8bit/bitwarden/ui/vault/feature/itemlisting/VaultItemListingNavigation.kt b/app/src/main/kotlin/com/x8bit/bitwarden/ui/vault/feature/itemlisting/VaultItemListingNavigation.kt index b203ede1e3..f22cd94240 100644 --- a/app/src/main/kotlin/com/x8bit/bitwarden/ui/vault/feature/itemlisting/VaultItemListingNavigation.kt +++ b/app/src/main/kotlin/com/x8bit/bitwarden/ui/vault/feature/itemlisting/VaultItemListingNavigation.kt @@ -1,5 +1,6 @@ package com.x8bit.bitwarden.ui.vault.feature.itemlisting +import android.os.Parcelable import androidx.lifecycle.SavedStateHandle import androidx.navigation.NavController import androidx.navigation.NavGraphBuilder @@ -7,19 +8,22 @@ import androidx.navigation.NavOptions import androidx.navigation.toRoute import com.bitwarden.ui.platform.base.util.composableWithPushTransitions import com.bitwarden.ui.platform.base.util.composableWithStayTransitions +import com.bitwarden.ui.platform.util.ParcelableRouteSerializer import com.x8bit.bitwarden.ui.platform.feature.search.model.SearchType import com.x8bit.bitwarden.ui.tools.feature.send.addedit.AddEditSendRoute import com.x8bit.bitwarden.ui.tools.feature.send.viewsend.ViewSendRoute import com.x8bit.bitwarden.ui.vault.feature.addedit.VaultAddEditArgs import com.x8bit.bitwarden.ui.vault.feature.item.VaultItemArgs import com.x8bit.bitwarden.ui.vault.model.VaultItemListingType +import kotlinx.parcelize.Parcelize import kotlinx.serialization.Serializable /** * The type-safe route for the vault item listing screen. */ -@Serializable -sealed class VaultItemListingRoute { +@Parcelize +@Serializable(with = VaultItemListingRoute.Serializer::class) +sealed class VaultItemListingRoute : Parcelable { /** * The type of item to be displayed. */ @@ -30,32 +34,57 @@ sealed class VaultItemListingRoute { */ abstract val itemId: String? + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer( + kClass = VaultItemListingRoute::class, + ) + /** * The type-safe route for the cipher specific vault item listing screen. */ - @Serializable + @Parcelize + @Serializable(with = CipherItemListing.Serializer::class) data class CipherItemListing( override val type: ItemListingType, override val itemId: String?, - ) : VaultItemListingRoute() + ) : VaultItemListingRoute() { + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(CipherItemListing::class) + } /** * The type-safe route for the send specific vault item listing screen. */ - @Serializable + @Parcelize + @Serializable(with = SendItemListing.Serializer::class) data class SendItemListing( override val type: ItemListingType, override val itemId: String?, - ) : VaultItemListingRoute() + ) : VaultItemListingRoute() { + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(SendItemListing::class) + } /** * The type-safe route for the root vault item listing screen. */ - @Serializable + @Parcelize + @Serializable(with = AsRoot.Serializer::class) data class AsRoot( override val type: ItemListingType, override val itemId: String?, - ) : VaultItemListingRoute() + ) : VaultItemListingRoute() { + /** + * Custom serializer to support polymorphic routes. + */ + class Serializer : ParcelableRouteSerializer(AsRoot::class) + } } /** @@ -86,9 +115,7 @@ data class VaultItemListingArgs( * Constructs a [VaultItemListingArgs] from the [SavedStateHandle] and internal route data. */ fun SavedStateHandle.toVaultItemListingArgs(): VaultItemListingArgs { - // We just need to pull the serializable data out of the route, since they are always the same - // it does not matter which instance we fetch. - val route = this.toRoute() + val route = this.toRoute() return VaultItemListingArgs( vaultItemListingType = when (route.type) { ItemListingType.LOGIN -> VaultItemListingType.Login diff --git a/ui/src/main/kotlin/com/bitwarden/ui/platform/util/ParcelableRouteSerializer.kt b/ui/src/main/kotlin/com/bitwarden/ui/platform/util/ParcelableRouteSerializer.kt new file mode 100644 index 0000000000..b01564b626 --- /dev/null +++ b/ui/src/main/kotlin/com/bitwarden/ui/platform/util/ParcelableRouteSerializer.kt @@ -0,0 +1,159 @@ +package com.bitwarden.ui.platform.util + +import android.os.Build +import android.os.Parcel +import android.os.Parcelable +import android.util.Base64 +import com.bitwarden.annotation.OmitFromCoverage +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.InternalSerializationApi +import kotlinx.serialization.KSerializer +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.descriptors.buildClassSerialDescriptor +import kotlinx.serialization.descriptors.element +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.encoding.decodeStructure +import kotlinx.serialization.encoding.encodeStructure +import kotlin.reflect.KClass + +/** + * A custom [KSerializer] for serializing and deserializing [Parcelable] classes. + * + * This serializer is compatible with Jetpack Compose type-safe navigation routes. It allows for + * complex [Parcelable] types to be easily serialized without needing to specify a `NavType` for + * all non-primitive properties and properties nested within those types. + * + * For example: + * + * ``` + * @Parcelize + * @Serializable(with = MyCustomRoute.Serializer::class) + * data class MyCustomRoute( + * val data: Data, + * ): Parcelable { + * class Serializer : ParcelableSerializer(MyCustomRoute::class) + * } + * ``` + * + * Where `Data` is a complex type implementing `Parcelable`. + * + * In addition, this serializer provides support for directly serializing to the parent types of a + * sealed class when using `SavedStateHandle.toRoute()` or `NavBackStackEntry.toRoute()`. In order + * to achieve this while also ensuring each route is unique, a subclass of this serializer should + * be defined for each parent and child type. + * + * Given the following type: + * + * ``` + * @Parcelize + * @Serializable(with = ParentRoute.Serializer::class) + * sealed class ParentRoute : Parcelable { + * class Serializer : ParcelableRouteSerializer(ParentRoute::class) + * + * @Parcelize + * @Serializable(with = Child1.Serializer::class) + * data class Child1( + * val data1: Data1, + * ) : ParentRoute() { + * class Serializer : ParcelableRouteSerializer(Child1::class) + * } + * + * @Parcelize + * @Serializable(with = Child2.Serializer::class) + * data class Child2( + * val data2: Data2, + * ) : ParentRoute() { + * class Serializer : ParcelableRouteSerializer(Child2::class) + * } + * } + * ``` + * + * The route information for a navigation to the `Child1` destination could be derived using both: + * ``` + * savedStateHandle.toRoute() + * ``` + * + * As well as: + * ``` + * when (savedStateHandle.toRoute()) { + * is Child1 -> // ... + * is Child2 -> // ... + * } + * ``` + * + * The latter is useful in cases where the same `ViewModel` is used to handle these routes. + */ +@OmitFromCoverage +@OptIn(InternalSerializationApi::class, ExperimentalSerializationApi::class) +open class ParcelableRouteSerializer( + private val kClass: KClass, +) : KSerializer { + + override val descriptor: SerialDescriptor + get() = buildClassSerialDescriptor(serialName = requireNotNull(kClass.qualifiedName)) { + element(elementName = "encodedData") + } + + override fun deserialize(decoder: Decoder): T = + decoder.decodeStructure(descriptor) { + var encodedString: String? = null + while (true) { + when (decodeElementIndex(descriptor)) { + 0 -> encodedString = decodeStringElement(descriptor = descriptor, index = 0) + else -> break + } + } + encodedString + ?.toParcelable() + ?: throw IllegalStateException("Invalid decoding for ${kClass.qualifiedName}.") + } + + override fun serialize(encoder: Encoder, value: T) { + val valueAsString = value.toEncodedString() + encoder.encodeStructure(descriptor = descriptor) { + encodeStringElement(descriptor = descriptor, index = 0, value = valueAsString) + } + } + + // Helpers for encoding Parcelable data + + private fun Parcelable.toBytes(): ByteArray { + val parcel = Parcel.obtain().apply { + writeParcelable(this@toBytes, 0) + } + return parcel + .marshall() + .also { parcel.recycle() } + } + + private fun Parcelable.toEncodedString(): String = + Base64.encodeToString(toBytes(), Base64.URL_SAFE) + + private fun ByteArray.toParcelable(): T? { + val parcel = Parcel.obtain().apply { + unmarshall(this@toParcelable, 0, this@toParcelable.size) + setDataPosition(0) + } + val value = try { + @Suppress("UNCHECKED_CAST") + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + parcel.readParcelable( + ParcelableRouteSerializer::class.java.classLoader, + kClass.java, + ) + } else { + @Suppress("DEPRECATION") + parcel.readParcelable(ParcelableRouteSerializer::class.java.classLoader) + } as T? + } catch (_: IllegalArgumentException) { + null + } catch (_: IllegalStateException) { + null + } + parcel.recycle() + return value + } + + private fun String.toParcelable(): T? = Base64.decode(this, Base64.URL_SAFE).toParcelable() +}