Skip to content

Commit

Permalink
chore: Remove reflect for toDestination (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBoulongne authored Dec 18, 2024
2 parents 9512ffd + 572f985 commit af15d16
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@
*/
package com.infomaniak.swisstransfer.ui.navigation

import android.os.Bundle
import androidx.compose.animation.AnimatedContentScope
import androidx.compose.runtime.Composable
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.navigation.navDeepLink
import androidx.navigation.toRoute
import com.infomaniak.sentry.SentryLog
import com.infomaniak.swisstransfer.BuildConfig
import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components.TransferTypeUi
import kotlinx.serialization.Serializable
import kotlin.reflect.KClass
import kotlin.reflect.full.primaryConstructor

/**
* Sealed class representing the navigation arguments for the main navigation flow.
Expand All @@ -37,9 +36,19 @@ import kotlin.reflect.full.primaryConstructor
sealed class MainNavigation : NavigationDestination() {
var enableTransition = true

/**
* DO NOT RENAME THIS.
* EVER.
* Because of the minification, it will break the `toMainDestination()` function.
*/
@Serializable
data object SentDestination : MainNavigation()

/**
* DO NOT RENAME THIS.
* EVER.
* Because of the minification, it will break the `toMainDestination()` function.
*/
@Serializable
data class ReceivedDestination(val transferUuid: String? = null) : MainNavigation() {

Expand All @@ -57,15 +66,45 @@ sealed class MainNavigation : NavigationDestination() {
}
}
}

@Serializable
data class TransferDetailsDestination(val transferUuid: String) : MainNavigation()

/**
* DO NOT RENAME THIS.
* EVER.
* Because of the minification, it will break the `toMainDestination()` function.
*/
@Serializable
data object SettingsDestination : MainNavigation()

companion object {
private val TAG = MainNavigation::class.java.simpleName
val startDestination = SentDestination

/**
* If these classes have to be renamed, they need to be renamed in this `list` too.
*/
val entries = listOf(
"SentDestination",
"ReceivedDestination",
"SettingsDestination",
)

fun NavBackStackEntry.toMainDestination(): MainNavigation? {
return runCatching {
val destinationRoute = destination.route ?: error("Destination route cannot be empty")
/**
* If these classes have to be renamed, they need to be renamed in this `when` too.
*/
when (entries.firstOrNull { destinationRoute.contains(it) }) {
"SentDestination" -> this.toRoute<SentDestination>()
"ReceivedDestination" -> this.toRoute<ReceivedDestination>()
"SettingsDestination" -> this.toRoute<SettingsDestination>()
else -> error("Destination $destinationRoute is not handled")
}
}.getOrElse { exception ->
SentryLog.e(TAG, "toMainDestination: Failure", exception)
null
}
}
}
}

Expand Down Expand Up @@ -98,34 +137,4 @@ sealed class NewTransferNavigation : NavigationDestination() {
* Sealed class representing navigation arguments with a title resource.
*/
@Serializable
sealed class NavigationDestination {

companion object {

inline fun <reified T : NavigationDestination> NavBackStackEntry.toDestination(): T? {
return toDestination(T::class, backStackEntry = this)
}

fun <T : NavigationDestination> toDestination(kClass: KClass<T>, backStackEntry: NavBackStackEntry?): T? {

fun kClassFromRoute(route: String) = kClass.sealedSubclasses.firstOrNull {
route.contains(it.qualifiedName.toString())
}

if (backStackEntry == null) return null

val route = backStackEntry.destination.route ?: ""
val args = backStackEntry.arguments
val subclass = kClassFromRoute(route) ?: return null

return createInstance(subclass, args)
}

private fun <T : NavigationDestination> createInstance(kClass: KClass<T>, bundle: Bundle?): T? {
return kClass.primaryConstructor?.let {
val args = it.parameters.associateWith { parameter -> bundle?.get(parameter.name) }
it.callBy(args)
} ?: kClass.objectInstance
}
}
}
sealed class NavigationDestination
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.infomaniak.swisstransfer.ui.components.BrandTopAppBar
import com.infomaniak.swisstransfer.ui.navigation.MainNavigation
import com.infomaniak.swisstransfer.ui.navigation.NavigationDestination.Companion.toDestination
import com.infomaniak.swisstransfer.ui.navigation.MainNavigation.Companion.toMainDestination
import com.infomaniak.swisstransfer.ui.screen.main.components.MainScaffold
import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
import com.infomaniak.swisstransfer.ui.utils.PreviewAllWindows
Expand All @@ -37,7 +37,7 @@ fun MainScreen() {
val navBackStackEntry by navController.currentBackStackEntryAsState()

val currentDestination by remember(navBackStackEntry) {
derivedStateOf { navBackStackEntry?.toDestination<MainNavigation>() ?: MainNavigation.startDestination }
derivedStateOf { navBackStackEntry?.toMainDestination() ?: MainNavigation.startDestination }
}

MainScaffold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package com.infomaniak.swisstransfer.ui.screen.main.components

import android.content.Intent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.HorizontalDivider
Expand All @@ -26,10 +25,8 @@ import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteType
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.navigation.NavController
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import com.infomaniak.core2.extensions.parcelableExtra
import com.infomaniak.swisstransfer.ui.components.BrandTopAppBar
import com.infomaniak.swisstransfer.ui.navigation.MainNavigation
import com.infomaniak.swisstransfer.ui.navigation.NavigationItem
Expand Down

0 comments on commit af15d16

Please sign in to comment.