Skip to content

Commit b2d79bd

Browse files
committed
refactor: extract nav method
1 parent 960c751 commit b2d79bd

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

app/src/main/java/to/bitkit/ui/ContentView.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,16 @@ fun NavController.navigateToHome() {
13851385
}
13861386
}
13871387

1388+
/**
1389+
* Navigates to the specified route only if not already on that route.
1390+
*/
1391+
inline fun <reified T : Any> NavController.navigateIfNotCurrent(route: T) {
1392+
val isOnRoute = currentBackStackEntry?.destination?.hasRoute<T>() ?: false
1393+
if (!isOnRoute) {
1394+
navigate(route)
1395+
}
1396+
}
1397+
13881398
fun NavController.navigateToSettings() = navigate(
13891399
route = Routes.Settings,
13901400
)

app/src/main/java/to/bitkit/ui/components/DrawerMenu.kt

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ import androidx.compose.ui.unit.dp
3939
import androidx.compose.ui.unit.sp
4040
import androidx.compose.ui.zIndex
4141
import androidx.navigation.NavController
42+
import androidx.navigation.NavDestination.Companion.hasRoute
4243
import androidx.navigation.compose.rememberNavController
4344
import kotlinx.coroutines.launch
4445
import to.bitkit.R
4546
import to.bitkit.ui.Routes
47+
import to.bitkit.ui.navigateIfNotCurrent
4648
import to.bitkit.ui.navigateToHome
47-
import to.bitkit.ui.navigateToSettings
4849
import to.bitkit.ui.screens.wallets.HomeRoutes
4950
import to.bitkit.ui.shared.util.blockPointerInputPassthrough
5051
import to.bitkit.ui.shared.util.clickableAlpha
@@ -102,16 +103,16 @@ fun DrawerMenu(
102103
drawerState = drawerState,
103104
onClickAddWidget = {
104105
if (!hasSeenWidgetsIntro) {
105-
rootNavController.navigate(Routes.WidgetsIntro)
106+
rootNavController.navigateIfNotCurrent(Routes.WidgetsIntro)
106107
} else {
107-
rootNavController.navigate(Routes.AddWidget)
108+
rootNavController.navigateIfNotCurrent(Routes.AddWidget)
108109
}
109110
},
110111
onClickShop = {
111112
if (!hasSeenShopIntro) {
112-
rootNavController.navigate(Routes.ShopIntro)
113+
rootNavController.navigateIfNotCurrent(Routes.ShopIntro)
113114
} else {
114-
rootNavController.navigate(Routes.ShopDiscover)
115+
rootNavController.navigateIfNotCurrent(Routes.ShopDiscover)
115116
}
116117
},
117118
)
@@ -141,7 +142,11 @@ private fun Menu(
141142
label = stringResource(R.string.wallet__drawer__wallet),
142143
iconRes = R.drawable.ic_coins,
143144
onClick = {
144-
rootNavController.navigateToHome()
145+
val isOnHome = rootNavController.currentBackStackEntry
146+
?.destination?.hasRoute<Routes.Home>() ?: false
147+
if (!isOnHome) {
148+
rootNavController.navigateToHome()
149+
}
145150
scope.launch { drawerState.close() }
146151
},
147152
modifier = Modifier.testTag("DrawerWallet")
@@ -152,9 +157,9 @@ private fun Menu(
152157
iconRes = R.drawable.ic_heartbeat,
153158
onClick = {
154159
if (walletNavController != null) {
155-
walletNavController.navigate(HomeRoutes.AllActivity)
160+
walletNavController.navigateIfNotCurrent(HomeRoutes.AllActivity)
156161
} else {
157-
rootNavController.navigate(Routes.Home)
162+
rootNavController.navigateIfNotCurrent(Routes.Home)
158163
}
159164
scope.launch { drawerState.close() }
160165
},
@@ -199,7 +204,7 @@ private fun Menu(
199204
label = stringResource(R.string.wallet__drawer__settings),
200205
iconRes = R.drawable.ic_settings,
201206
onClick = {
202-
rootNavController.navigateToSettings()
207+
rootNavController.navigateIfNotCurrent(Routes.Settings)
203208
scope.launch { drawerState.close() }
204209
},
205210
modifier = Modifier.testTag("DrawerSettings")
@@ -212,15 +217,17 @@ private fun Menu(
212217
modifier = Modifier
213218
.fillMaxWidth()
214219
.clickableAlpha {
215-
rootNavController.navigate(Routes.AppStatus)
220+
rootNavController.navigateIfNotCurrent(Routes.AppStatus)
216221
scope.launch { drawerState.close() }
217222
}
218223
) {
219224
AppStatus(
220225
showText = true,
221226
showReady = true,
222227
color = Colors.Black,
223-
modifier = Modifier.padding(vertical = 16.dp).testTag("DrawerAppStatus")
228+
modifier = Modifier
229+
.padding(vertical = 16.dp)
230+
.testTag("DrawerAppStatus")
224231
)
225232
}
226233
}

0 commit comments

Comments
 (0)