Skip to content

Commit 725256f

Browse files
committed
feat: Expose a canNavigate() directly accessible on any NavController
1 parent 60f8dd2 commit 725256f

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Legacy/src/main/java/com/infomaniak/lib/core/utils/Extensions.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Infomaniak Core - Android
3-
* Copyright (C) 2022-2024 Infomaniak Network SA
3+
* Copyright (C) 2022-2025 Infomaniak Network SA
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
@@ -53,6 +53,7 @@ import androidx.lifecycle.DefaultLifecycleObserver
5353
import androidx.lifecycle.Lifecycle.Event
5454
import androidx.lifecycle.LifecycleEventObserver
5555
import androidx.lifecycle.LifecycleOwner
56+
import androidx.navigation.NavController
5657
import androidx.navigation.NavDirections
5758
import androidx.navigation.NavOptions
5859
import androidx.navigation.Navigator
@@ -362,14 +363,18 @@ fun ImageView.loadAvatar(
362363
}
363364

364365
fun Fragment.canNavigate(currentClassName: String? = null): Boolean {
365-
val className = currentClassName ?: when (val currentDestination = findNavController().currentDestination) {
366+
return findNavController().canNavigate(allowedStartingClassName = javaClass.name, currentClassName)
367+
}
368+
369+
fun NavController.canNavigate(allowedStartingClassName: String, currentClassName: String? = null): Boolean {
370+
val className = currentClassName ?: when (val currentDestination = currentDestination) {
366371
is FragmentNavigator.Destination -> currentDestination.className
367372
is DialogFragmentNavigator.Destination -> currentDestination.className
368-
null -> javaClass.name
373+
null -> allowedStartingClassName
369374
else -> null
370375
}
371376

372-
return javaClass.name == className
377+
return allowedStartingClassName == className
373378
}
374379

375380
fun Fragment.safeNavigate(directions: NavDirections, currentClassName: String? = null) = with(findNavController()) {

0 commit comments

Comments
 (0)