From 28cead33c45e58f77c63cbe664d3ba654422cee2 Mon Sep 17 00:00:00 2001 From: AndroidBob Date: Mon, 30 Oct 2023 09:42:57 +0100 Subject: [PATCH] fix: navigating with UPDATE_EXISTED, extracting base route [WPB-5225] (#2376) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: GitHub Actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: MichaƂ Saleniuk --- .../android/navigation/HomeDestination.kt | 2 +- .../android/navigation/NavigationUtils.kt | 16 +++---- .../android/navigation/NavigationUtilsTest.kt | 43 +++++++++++++++++++ 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/app/src/main/kotlin/com/wire/android/navigation/HomeDestination.kt b/app/src/main/kotlin/com/wire/android/navigation/HomeDestination.kt index bea848e7b88..7e152e6b43f 100644 --- a/app/src/main/kotlin/com/wire/android/navigation/HomeDestination.kt +++ b/app/src/main/kotlin/com/wire/android/navigation/HomeDestination.kt @@ -112,7 +112,7 @@ sealed class HomeDestination( private const val ITEM_NAME_PREFIX = "HomeNavigationItem." fun fromRoute(fullRoute: String): HomeDestination? = - values().find { it.direction.route.getPrimaryRoute() == fullRoute.getPrimaryRoute() } + values().find { it.direction.route.getBaseRoute() == fullRoute.getBaseRoute() } fun values(): Array = arrayOf(Conversations, Calls, Mentions, Settings, Vault, Archive, Support, WhatsNew) } diff --git a/app/src/main/kotlin/com/wire/android/navigation/NavigationUtils.kt b/app/src/main/kotlin/com/wire/android/navigation/NavigationUtils.kt index e4c28cce428..183aa352462 100644 --- a/app/src/main/kotlin/com/wire/android/navigation/NavigationUtils.kt +++ b/app/src/main/kotlin/com/wire/android/navigation/NavigationUtils.kt @@ -45,7 +45,7 @@ internal fun NavController.navigateToItem(command: NavigationCommand) { fun lastDestination() = currentBackStack.value.lastOrNull { it.route() is DestinationSpec<*> } fun lastNestedGraph() = lastDestination()?.takeIf { it.navGraph() != navGraph }?.navGraph() fun firstDestinationWithRoute(route: String) = - currentBackStack.value.firstOrNull { it.destination.route?.getPrimaryRoute() == route.getPrimaryRoute() } + currentBackStack.value.firstOrNull { it.destination.route?.getBaseRoute() == route.getBaseRoute() } fun lastDestinationFromOtherGraph(graph: NavGraphSpec) = currentBackStack.value.lastOrNull { it.navGraph() != graph } appLogger.d("[$TAG] -> command: ${command.destination.route.obfuscateId()}") @@ -104,17 +104,11 @@ private fun NavOptionsBuilder.popUpTo( internal fun NavDestination.toDestination(): Destination? = this.route?.let { currentRoute -> NavGraphs.root.destinationsByRoute[currentRoute] } -fun String.getPrimaryRoute(): String { - val splitByQuestion = this.split("?") - val splitBySlash = this.split("/") - - val primaryRoute = when { - splitByQuestion.size > 1 -> splitByQuestion[0] - splitBySlash.size > 1 -> splitBySlash[0] - else -> this +fun String.getBaseRoute(): String = + this.indexOfAny(listOf("?", "/")).let { + if (it != -1) this.substring(0, it) + else this } - return primaryRoute -} fun Direction.handleNavigation(context: Context, handleOtherDirection: (Direction) -> Unit) = when (this) { is ExternalUriDirection -> CustomTabsHelper.launchUri(context, this.uri) diff --git a/app/src/test/kotlin/com/wire/android/navigation/NavigationUtilsTest.kt b/app/src/test/kotlin/com/wire/android/navigation/NavigationUtilsTest.kt index 88397803c4f..fbc47c9d6f2 100644 --- a/app/src/test/kotlin/com/wire/android/navigation/NavigationUtilsTest.kt +++ b/app/src/test/kotlin/com/wire/android/navigation/NavigationUtilsTest.kt @@ -103,4 +103,47 @@ internal class NavigationUtilsTest { // Then assertEquals(mappedImagePrivateAsset, expectedPrivateAssetImage) } + + @Test + fun `given route without segments and parameters, when getting primary route, then return only host part which is the same route`() { + // Given + val route = "route" + // When + val result = route.getBaseRoute() + // Then + assertEquals(route, result) + } + + @Test + fun `given route with segments but without parameters, when getting primary route, then return only host part`() { + // Given + val host = "route" + val route = "$host/segment1/segment2" + // When + val result = route.getBaseRoute() + // Then + assertEquals(host, result) + } + + @Test + fun `given route without segments but with parameters, when getting primary route, then return only host part`() { + // Given + val host = "route" + val route = "$host?param1=value1¶m2=value2" + // When + val result = route.getBaseRoute() + // Then + assertEquals(host, result) + } + + @Test + fun `given route with segments and parameters, when getting primary route, then return only host part`() { + // Given + val host = "route" + val route = "$host/segment1/segment2?param1=value1¶m2=value2" + // When + val result = route.getBaseRoute() + // Then + assertEquals(host, result) + } }