-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from stslex/feature/navigation
Refactor navigation
- Loading branch information
Showing
23 changed files
with
535 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...igation/src/main/java/com/stslex93/notes/core/navigation/di/NavigationComponentBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
package com.stslex93.notes.core.navigation.di | ||
|
||
import androidx.navigation.NavHostController | ||
import com.stslex93.notes.core.navigation.v2.controller.NavExtrasHostController | ||
import com.stslex93.notes.core.ui.di.NavigationApi | ||
|
||
object NavigationComponentBuilder { | ||
|
||
fun build( | ||
navHostController: NavHostController | ||
navController: NavExtrasHostController | ||
): NavigationApi = DaggerNavigationComponent | ||
.builder() | ||
.controller(navHostController) | ||
.controller(navController) | ||
.build() | ||
} |
2 changes: 1 addition & 1 deletion
2
core/navigation/src/main/java/com/stslex93/notes/core/navigation/di/NavigationModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 29 additions & 36 deletions
65
core/navigation/src/main/java/com/stslex93/notes/core/navigation/navigator/NavigatorImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,31 @@ | ||
package com.stslex93.notes.core.navigation.navigator | ||
|
||
import androidx.navigation.NavHostController | ||
import com.stslex.aproselection.core.core.Logger | ||
import com.stslex93.notes.core.navigation.model.NavigationScreen | ||
import com.stslex93.notes.core.ui.di.Navigator | ||
import com.stslex93.notes.core.ui.di.Screen | ||
import javax.inject.Inject | ||
|
||
class NavigatorImpl @Inject constructor( | ||
private val navController: NavHostController | ||
) : Navigator { | ||
|
||
override fun invoke(screen: Screen) { | ||
when (screen) { | ||
is NavigationScreen.PopBackStack -> navController.popBackStack() | ||
is NavigationScreen -> navigateScreen(screen) | ||
else -> { | ||
Logger.debug("unresolve navigation route", this::class.simpleName) | ||
} | ||
} | ||
} | ||
|
||
private fun navigateScreen(screen: NavigationScreen) { | ||
val currentRoute = navController.currentDestination?.route ?: return | ||
if (currentRoute == screen.screen.navigationRoute) return | ||
|
||
navController.navigate(screen.screenRoute) { | ||
if (screen.isSingleTop.not()) return@navigate | ||
|
||
popUpTo(currentRoute) { | ||
inclusive = true | ||
saveState = true | ||
} | ||
launchSingleTop = true | ||
} | ||
} | ||
} | ||
//class NavigatorImpl @Inject constructor( | ||
// private val navController: NavHostController | ||
//) : Navigator { | ||
// | ||
// override fun invoke(screen: Screen) { | ||
// when (screen) { | ||
// is NavigationScreen.PopBackStack -> navController.popBackStack() | ||
// is NavigationScreen -> navigateScreen(screen) | ||
// else -> { | ||
// Logger.debug("unresolve navigation route", this::class.simpleName) | ||
// } | ||
// } | ||
// } | ||
// | ||
// private fun navigateScreen(screen: NavigationScreen) { | ||
// val currentRoute = navController.currentDestination?.route ?: return | ||
// if (currentRoute == screen.screen.navigationRoute) return | ||
// | ||
// navController.navigate(screen.screenRoute) { | ||
// if (screen.isSingleTop.not()) return@navigate | ||
// | ||
// popUpTo(currentRoute) { | ||
// inclusive = true | ||
// saveState = true | ||
// } | ||
// launchSingleTop = true | ||
// } | ||
// } | ||
//} |
10 changes: 10 additions & 0 deletions
10
core/navigation/src/main/java/com/stslex93/notes/core/navigation/v2/NoteEditArgs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.stslex93.notes.core.navigation.v2 | ||
|
||
import android.os.Parcelable | ||
import kotlinx.parcelize.Parcelize | ||
|
||
@Parcelize | ||
data class NoteEditArgs( | ||
val noteId: Int, | ||
val isEdit: Boolean | ||
) : Parcelable |
64 changes: 64 additions & 0 deletions
64
core/navigation/src/main/java/com/stslex93/notes/core/navigation/v2/compose/ComposeGraph.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.stslex93.notes.core.navigation.v2.compose | ||
|
||
import android.os.Build | ||
import android.os.Bundle | ||
import android.os.Parcelable | ||
import androidx.compose.runtime.Composable | ||
import androidx.navigation.NamedNavArgument | ||
import androidx.navigation.NavBackStackEntry | ||
import androidx.navigation.NavDeepLink | ||
import androidx.navigation.NavGraphBuilder | ||
import androidx.navigation.get | ||
import com.stslex93.notes.core.navigation.v2.controller.ComposeNavigator | ||
import com.stslex93.notes.core.navigation.v2.controller.Destination | ||
import com.stslex93.notes.core.navigation.v2.screen.HostScreen | ||
|
||
fun NavGraphBuilder.composable( | ||
route: String, | ||
arguments: List<NamedNavArgument> = emptyList(), | ||
deepLinks: List<NavDeepLink> = emptyList(), | ||
content: @Composable (backStackEntry: NavBackStackEntry, arguments: Bundle?) -> Unit | ||
) { | ||
addDestination( | ||
Destination(provider[ComposeNavigator::class], content).apply { | ||
this.route = route | ||
arguments.forEach { (argumentName, argument) -> | ||
addArgument(argumentName, argument) | ||
} | ||
deepLinks.forEach { deepLink -> | ||
addDeepLink(deepLink) | ||
} | ||
} | ||
) | ||
} | ||
|
||
inline fun <reified T : Parcelable> NavGraphBuilder.composable( | ||
screen: HostScreen, | ||
arguments: List<NamedNavArgument> = emptyList(), | ||
deepLinks: List<NavDeepLink> = emptyList(), | ||
crossinline content: @Composable (backStackEntry: NavBackStackEntry, arguments: T?) -> Unit | ||
) { | ||
composable( | ||
route = screen.route, | ||
arguments = arguments, | ||
deepLinks = deepLinks, | ||
) { backStackEntry, args -> | ||
val data = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
args?.getParcelable(screen.key, T::class.java) | ||
} else { | ||
args?.getParcelable(screen.key) | ||
} | ||
content(backStackEntry, data) | ||
} | ||
} | ||
|
||
inline fun <reified T : Parcelable> NavGraphBuilder.composable( | ||
screen: HostScreen, | ||
crossinline content: @Composable (data: T) -> Unit | ||
) { | ||
composable<T>(screen) { _, arguments -> | ||
arguments?.let { data -> | ||
content(data) | ||
} | ||
} | ||
} |
Oops, something went wrong.