Skip to content

Commit

Permalink
fix: navigating with UPDATE_EXISTED, extracting base route [WPB-5225] (
Browse files Browse the repository at this point in the history
…#2376)

Co-authored-by: GitHub Actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Michał Saleniuk <[email protected]>
  • Loading branch information
3 people authored Oct 30, 2023
1 parent 3c608e9 commit 28cead3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<HomeDestination> =
arrayOf(Conversations, Calls, Mentions, Settings, Vault, Archive, Support, WhatsNew)
}
Expand Down
16 changes: 5 additions & 11 deletions app/src/main/kotlin/com/wire/android/navigation/NavigationUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()}")
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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&param2=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&param2=value2"
// When
val result = route.getBaseRoute()
// Then
assertEquals(host, result)
}
}

0 comments on commit 28cead3

Please sign in to comment.