-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathNavigationUtils.kt
More file actions
121 lines (103 loc) · 4.88 KB
/
NavigationUtils.kt
File metadata and controls
121 lines (103 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.android.navigation
import android.annotation.SuppressLint
import android.content.Context
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.NavOptionsBuilder
import com.ramcosta.composedestinations.navigation.navigate
import com.ramcosta.composedestinations.spec.DestinationSpec
import com.ramcosta.composedestinations.spec.Direction
import com.ramcosta.composedestinations.spec.NavGraphSpec
import com.ramcosta.composedestinations.utils.findDestination
import com.ramcosta.composedestinations.utils.navGraph
import com.ramcosta.composedestinations.utils.route
import com.wire.android.appLogger
import com.wire.android.util.CustomTabsHelper
import com.wire.kalium.logger.obfuscateId
@SuppressLint("RestrictedApi")
internal fun NavController.navigateToItem(command: NavigationCommand) {
fun firstDestination() = currentBackStack.value.firstOrNull { it.route() is DestinationSpec<*> }
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?.getBaseRoute() == route.getBaseRoute() }
fun lastDestinationFromOtherGraph(graph: NavGraphSpec) = currentBackStack.value.lastOrNull { it.navGraph() != graph }
appLogger.d("[$TAG] -> command: ${command.destination.route.obfuscateId()} backStackMode:${command.backStackMode}")
navigate(command.destination) {
when (command.backStackMode) {
BackStackMode.CLEAR_WHOLE, BackStackMode.CLEAR_TILL_START -> {
val inclusive = command.backStackMode == BackStackMode.CLEAR_WHOLE
popUpTo(inclusive) { firstDestination() }
}
BackStackMode.REMOVE_CURRENT -> {
popUpTo(true) { lastDestination() }
}
BackStackMode.REMOVE_CURRENT_NESTED_GRAPH -> {
popUpTo(
getInclusive = { it.route() is NavGraphSpec },
getNavBackStackEntry = { lastNestedGraph()?.let { lastDestinationFromOtherGraph(it) } }
)
}
BackStackMode.REMOVE_CURRENT_AND_REPLACE -> {
popUpTo(true) { lastDestination() }
popUpTo(true) { firstDestinationWithRoute(command.destination.route) }
}
BackStackMode.UPDATE_EXISTED -> {
popUpTo(true) { firstDestinationWithRoute(command.destination.route) }
}
BackStackMode.NONE -> {
}
}
launchSingleTop = command.launchSingleTop
restoreState = true
}
}
private fun NavOptionsBuilder.popUpTo(
inclusive: Boolean,
getNavBackStackEntry: () -> NavBackStackEntry?,
) = popUpTo({ inclusive }, getNavBackStackEntry)
private fun NavOptionsBuilder.popUpTo(
getInclusive: (NavBackStackEntry) -> Boolean,
getNavBackStackEntry: () -> NavBackStackEntry?,
) {
getNavBackStackEntry()?.let { entry ->
appLogger.d("[$TAG] -> popUpTo:${entry.destination.route?.obfuscateId()} inclusive:${getInclusive(entry)}")
popUpTo(entry.destination.id) {
this.inclusive = getInclusive(entry)
}
}
}
internal fun NavDestination.toDestination(): DestinationSpec<*>? =
this.route?.let { currentRoute -> WireMainNavGraph.findDestination(currentRoute) }
fun String.getBaseRoute(): String =
this.indexOfAny(listOf("?", "/")).let {
if (it != -1) this.substring(0, it)
else this
}
fun Direction.handleNavigation(context: Context, handleOtherDirection: (Direction) -> Unit) = when (this) {
is ExternalUriDirection -> CustomTabsHelper.launchUri(context, this.uri)
is ExternalUriStringResDirection -> CustomTabsHelper.launchUri(context, this.getUri(context.resources))
is IntentDirection -> context.startActivity(this.intent(context))
else -> handleOtherDirection(this)
}
@SuppressLint("RestrictedApi")
fun NavController.startDestination() = currentBackStack.value.firstOrNull { it.route() is DestinationSpec<*> }
private const val TAG = "NavigationUtils"