first commit
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package com.shaarit.presentation.nav
|
||||
|
||||
import android.app.Activity
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.navigation.NavType
|
||||
import androidx.navigation.compose.NavHost
|
||||
import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import androidx.navigation.navArgument
|
||||
import java.net.URLEncoder
|
||||
|
||||
sealed class Screen(val route: String) {
|
||||
object Login : Screen("login")
|
||||
object Feed : Screen("feed?tag={tag}") {
|
||||
fun createRoute(tag: String? = null): String {
|
||||
return if (tag != null) "feed?tag=$tag" else "feed"
|
||||
}
|
||||
}
|
||||
object Add : Screen("add?url={url}&title={title}&isShare={isShare}")
|
||||
object Tags : Screen("tags")
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun AppNavGraph(
|
||||
startDestination: String = Screen.Login.route,
|
||||
shareUrl: String? = null,
|
||||
shareTitle: String? = null
|
||||
) {
|
||||
val navController = rememberNavController()
|
||||
val context = LocalContext.current
|
||||
val isShareIntent = shareUrl != null
|
||||
|
||||
NavHost(navController = navController, startDestination = startDestination) {
|
||||
composable(Screen.Login.route) {
|
||||
com.shaarit.presentation.auth.LoginScreen(
|
||||
onLoginSuccess = {
|
||||
if (shareUrl != null) {
|
||||
// Use proper URL encoding that handles spaces correctly
|
||||
val encodedUrl = URLEncoder.encode(shareUrl, "UTF-8")
|
||||
val encodedTitle =
|
||||
if (shareTitle != null) {
|
||||
URLEncoder.encode(shareTitle, "UTF-8")
|
||||
} else ""
|
||||
navController.navigate("add?url=$encodedUrl&title=$encodedTitle&isShare=true") {
|
||||
popUpTo(Screen.Login.route) { inclusive = true }
|
||||
}
|
||||
} else {
|
||||
navController.navigate(Screen.Feed.createRoute()) {
|
||||
popUpTo(Screen.Login.route) { inclusive = true }
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
composable(
|
||||
route = "feed?tag={tag}",
|
||||
arguments =
|
||||
listOf(
|
||||
navArgument("tag") {
|
||||
type = NavType.StringType
|
||||
nullable = true
|
||||
defaultValue = null
|
||||
}
|
||||
)
|
||||
) { backStackEntry ->
|
||||
val tag = backStackEntry.arguments?.getString("tag")
|
||||
com.shaarit.presentation.feed.FeedScreen(
|
||||
onNavigateToAdd = { navController.navigate("add?url=&title=&isShare=false") },
|
||||
onNavigateToTags = { navController.navigate(Screen.Tags.route) },
|
||||
initialTagFilter = tag
|
||||
)
|
||||
}
|
||||
|
||||
composable(
|
||||
route = "add?url={url}&title={title}&isShare={isShare}",
|
||||
arguments =
|
||||
listOf(
|
||||
navArgument("url") {
|
||||
type = NavType.StringType
|
||||
defaultValue = ""
|
||||
nullable = true
|
||||
},
|
||||
navArgument("title") {
|
||||
type = NavType.StringType
|
||||
defaultValue = ""
|
||||
nullable = true
|
||||
},
|
||||
navArgument("isShare") {
|
||||
type = NavType.BoolType
|
||||
defaultValue = false
|
||||
}
|
||||
)
|
||||
) { backStackEntry ->
|
||||
val isShare = backStackEntry.arguments?.getBoolean("isShare") ?: false
|
||||
com.shaarit.presentation.add.AddLinkScreen(
|
||||
onNavigateBack = { navController.popBackStack() },
|
||||
onShareSuccess = if (isShare) {
|
||||
{ (context as? Activity)?.finish() }
|
||||
} else null
|
||||
)
|
||||
}
|
||||
|
||||
composable(Screen.Tags.route) {
|
||||
com.shaarit.presentation.tags.TagsScreen(
|
||||
onNavigateBack = { navController.popBackStack() },
|
||||
onNavigateToFeedWithTag = { tag ->
|
||||
navController.navigate(Screen.Feed.createRoute(tag)) {
|
||||
popUpTo(Screen.Tags.route) { inclusive = true }
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user