mirror of
https://github.com/bitwarden/android.git
synced 2026-11-19 17:40:12 -06:00
Replace toObjectRoute with custom ParcelableRouteSerializer (#5393)
This commit is contained in:
+28
-9
@@ -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>(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>(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>(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<SetupAutofillRoute.AsRoot>()
|
||||
?: this.toObjectRoute<SetupAutofillRoute.Standard>())
|
||||
return route
|
||||
?.let { SetupAutoFillScreenArgs(isInitialSetup = it.isInitialSetup) }
|
||||
?: throw IllegalStateException("Missing correct route for SetupAutofillScreen")
|
||||
val route = this.toRoute<SetupAutofillRoute>()
|
||||
return SetupAutoFillScreenArgs(isInitialSetup = route.isInitialSetup)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+28
-9
@@ -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>(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>(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>(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<SetupUnlockRoute.AsRoot>()
|
||||
?: this.toObjectRoute<SetupUnlockRoute.Standard>()
|
||||
return route
|
||||
?.let { SetupUnlockArgs(isInitialSetup = it.isInitialSetup) }
|
||||
?: throw IllegalStateException("Missing correct route for SetupUnlockScreen")
|
||||
val route = this.toRoute<SetupUnlockRoute>()
|
||||
return SetupUnlockArgs(isInitialSetup = route.isInitialSetup)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+28
-10
@@ -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>(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>(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>(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<VaultUnlockRoute.Tde>()
|
||||
?: this.toObjectRoute<VaultUnlockRoute.Standard>()
|
||||
return route
|
||||
?.let { VaultUnlockArgs(unlockType = it.unlockType) }
|
||||
?: throw IllegalStateException("Missing correct route for VaultUnlockScreen")
|
||||
val route = this.toRoute<VaultUnlockRoute>()
|
||||
return VaultUnlockArgs(unlockType = route.unlockType)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+28
-11
@@ -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>(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>(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>(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<SettingsRoute.PreAuth>()
|
||||
?: this.toObjectRoute<SettingsRoute.Standard>()
|
||||
return route
|
||||
?.let { SettingsArgs(isPreAuth = it.isPreAuth) }
|
||||
?: this.toObjectRoute<SettingsGraphRoute>()?.let { SettingsArgs(isPreAuth = false) }
|
||||
?: throw IllegalStateException("Missing correct route for SettingsScreen")
|
||||
val route = this.toRoute<SettingsRoute>()
|
||||
return SettingsArgs(isPreAuth = route.isPreAuth)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+28
-10
@@ -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>(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>(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>(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<SettingsOtherRoute.PreAuth>()
|
||||
?: this.toObjectRoute<SettingsOtherRoute.Standard>()
|
||||
return route
|
||||
?.let { OtherArgs(isPreAuth = it.isPreAuth) }
|
||||
?: throw IllegalStateException("Missing correct route for SettingsOtherScreen")
|
||||
val route = this.toRoute<SettingsOtherRoute>()
|
||||
return OtherArgs(isPreAuth = route.isPreAuth)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
-25
@@ -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 <reified T : Any> SavedStateHandle.toObjectRoute(): T? =
|
||||
this
|
||||
.get<Intent>(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<T>()
|
||||
}
|
||||
+35
-18
@@ -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>(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>(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>(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<GeneratorRoute>()) {
|
||||
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<GeneratorRoute.Modal>()
|
||||
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.
|
||||
*/
|
||||
|
||||
+38
-11
@@ -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<VaultItemListingRoute>(
|
||||
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>(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>(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>(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<VaultItemListingRoute.SendItemListing>()
|
||||
val route = this.toRoute<VaultItemListingRoute>()
|
||||
return VaultItemListingArgs(
|
||||
vaultItemListingType = when (route.type) {
|
||||
ItemListingType.LOGIN -> VaultItemListingType.Login
|
||||
|
||||
@@ -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>(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>(ParentRoute::class)
|
||||
*
|
||||
* @Parcelize
|
||||
* @Serializable(with = Child1.Serializer::class)
|
||||
* data class Child1(
|
||||
* val data1: Data1,
|
||||
* ) : ParentRoute() {
|
||||
* class Serializer : ParcelableRouteSerializer<Child1>(Child1::class)
|
||||
* }
|
||||
*
|
||||
* @Parcelize
|
||||
* @Serializable(with = Child2.Serializer::class)
|
||||
* data class Child2(
|
||||
* val data2: Data2,
|
||||
* ) : ParentRoute() {
|
||||
* class Serializer : ParcelableRouteSerializer<Child2>(Child2::class)
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* The route information for a navigation to the `Child1` destination could be derived using both:
|
||||
* ```
|
||||
* savedStateHandle.toRoute<ParentRoute.Child1>()
|
||||
* ```
|
||||
*
|
||||
* As well as:
|
||||
* ```
|
||||
* when (savedStateHandle.toRoute<ParentRoute>()) {
|
||||
* 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<T : Parcelable>(
|
||||
private val kClass: KClass<T>,
|
||||
) : KSerializer<T> {
|
||||
|
||||
override val descriptor: SerialDescriptor
|
||||
get() = buildClassSerialDescriptor(serialName = requireNotNull(kClass.qualifiedName)) {
|
||||
element<String>(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<T>()
|
||||
?: 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 <T> 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 <T> String.toParcelable(): T? = Base64.decode(this, Base64.URL_SAFE).toParcelable()
|
||||
}
|
||||
Reference in New Issue
Block a user