Skip to content

Commit

Permalink
Merge pull request #84 from stslex/dev
Browse files Browse the repository at this point in the history
refactor Navigation
  • Loading branch information
stslex authored Aug 20, 2024
2 parents 671c6f0 + 3080cf0 commit a60bf72
Show file tree
Hide file tree
Showing 42 changed files with 273 additions and 357 deletions.
13 changes: 8 additions & 5 deletions app/src/main/java/st/slex/csplashscreen/ui/InitialApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,33 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import st.slex.csplashscreen.core.navigation.AppDestination
import st.slex.csplashscreen.core.navigation.navigator.NavigationTarget
import st.slex.csplashscreen.core.core.Logger
import st.slex.csplashscreen.core.navigation.Screen
import st.slex.csplashscreen.ui.components.NavHostControllerHolder
import st.slex.csplashscreen.ui.components.NavigationHost
import st.slex.csplashscreen.ui.components.bottom_appbar.BottomAppBarResource
import st.slex.csplashscreen.ui.components.bottom_appbar.BottomAppBarResource.Companion.getByRoute
import st.slex.csplashscreen.ui.components.bottom_appbar.MainBottomAppBar

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
@Stable
fun InitialApp(
navControllerHolder: NavHostControllerHolder,
onBottomAppBarClick: (NavigationTarget.Screen) -> Unit,
onBottomAppBarClick: (Screen) -> Unit,
modifier: Modifier = Modifier,
) {
val systemUiController = rememberSystemUiController()
val isDarkTheme = isSystemInDarkTheme()

var currentDestination by remember {
mutableStateOf<AppDestination?>(AppDestination.HOME)
mutableStateOf<Screen?>(Screen.Home)
}

navControllerHolder.navController.addOnDestinationChangedListener { _, destination, _ ->
currentDestination = AppDestination.findByRoute(destination.route)
Logger.d("current route: ${destination.route}")
currentDestination = destination.route?.let(::getByRoute)
Logger.d("currentDestination: ${currentDestination}")
}

DisposableEffect(systemUiController, isDarkTheme) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package st.slex.csplashscreen.ui

import androidx.lifecycle.ViewModel
import st.slex.csplashscreen.core.navigation.Screen
import st.slex.csplashscreen.core.navigation.navigator.NavigationTarget
import st.slex.csplashscreen.core.navigation.navigator.Navigator
import st.slex.csplashscreen.core.navigation.navigator.NavigatorOptions

class InitialAppViewModel(
private val navigator: Navigator
) : ViewModel() {

fun navigate(screen: NavigationTarget.Screen) {
navigator.navigate(screen)
fun navigate(screen: Screen) {
navigator(
NavigationTarget.Screen(
screen = screen,
options = NavigatorOptions(isSingleTop = true)
),
)
}
}
4 changes: 3 additions & 1 deletion app/src/main/java/st/slex/csplashscreen/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class MainActivity : ComponentActivity() {
Need Research to find more efficient way */
navControllerHolder = navHostControllerHolder,
onBottomAppBarClick = remember {
{ viewModel.navigate(it) }
{ screen ->
viewModel.navigate(screen)
}
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.compose.runtime.Stable
import androidx.compose.ui.Modifier
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import st.slex.csplashscreen.core.navigation.AppDestination
import st.slex.csplashscreen.core.navigation.Screen
import st.slex.csplashscreen.feature.collection.navigation.singleCollectionGraph
import st.slex.csplashscreen.feature.favourite.navigation.favouriteGraph
import st.slex.csplashscreen.feature.feature_photo_detail.navigation.imageDetailGraph
Expand All @@ -21,11 +21,11 @@ class NavHostControllerHolder(val navController: NavHostController)
fun NavigationHost(
holder: NavHostControllerHolder,
modifier: Modifier = Modifier,
startDestination: AppDestination = AppDestination.HOME
startDestination: Screen = Screen.Home
) {
NavHost(
navController = holder.navController,
startDestination = startDestination.navigationRoute
startDestination = startDestination
) {
homeGraph(modifier)
userGraph(modifier)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import st.slex.csplashscreen.core.navigation.AppDestination
import st.slex.csplashscreen.core.navigation.navigator.NavigationTarget
import st.slex.csplashscreen.core.navigation.Screen

@Composable
fun MainBottomAppBar(
onBottomAppBarClick: (NavigationTarget.Screen) -> Unit,
currentDestination: AppDestination?
onBottomAppBarClick: (Screen) -> Unit,
currentDestination: Screen?
) {
NavigationBar(
modifier = Modifier
Expand All @@ -27,7 +26,7 @@ fun MainBottomAppBar(
BottomAppBarResource
.entries
.forEach { item ->
val isSelected = currentDestination == item.appDestination
val isSelected = currentDestination == item.screen
BottomAppBarItem(
item = item,
isSelected = isSelected,
Expand All @@ -43,7 +42,7 @@ fun MainBottomAppBar(
private fun RowScope.BottomAppBarItem(
item: BottomAppBarResource,
isSelected: Boolean,
onBottomAppBarClick: (NavigationTarget.Screen) -> Unit
onBottomAppBarClick: (Screen) -> Unit
) {
NavigationBarItem(
selected = isSelected,
Expand All @@ -53,7 +52,7 @@ private fun RowScope.BottomAppBarItem(
icon = {
Icon(
imageVector = item.getIcon(isSelected),
contentDescription = item.appDestination.name
contentDescription = item.screen.javaClass.simpleName
)
},
label = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,51 @@ import androidx.compose.material.icons.outlined.Home
import androidx.compose.material.icons.outlined.Search
import androidx.compose.ui.graphics.vector.ImageVector
import st.slex.csplashscreen.R
import st.slex.csplashscreen.core.navigation.AppDestination
import st.slex.csplashscreen.core.navigation.navigator.NavigationTarget.Screen
import st.slex.csplashscreen.core.navigation.Screen
import st.slex.csplashscreen.core.navigation.Screen.Favourite
import st.slex.csplashscreen.core.navigation.Screen.Home
import st.slex.csplashscreen.core.navigation.Screen.SearchPhotosScreen
import kotlin.reflect.KClass

enum class BottomAppBarResource(
val unselectedIcon: ImageVector,
val selectedIcon: ImageVector,
val appDestination: AppDestination,
val titleResource: Int,
val screen: Screen
) {
FAVOURITE(
unselectedIcon = Icons.Outlined.FavoriteBorder,
selectedIcon = Icons.Filled.Favorite,
appDestination = AppDestination.FAVOURITE,
titleResource = R.string.nav_title_favourite,
screen = Screen.Favourite
screen = Favourite
),
HOME(
unselectedIcon = Icons.Outlined.Home,
selectedIcon = Icons.Filled.Home,
appDestination = AppDestination.HOME,
titleResource = R.string.nav_title_home,
screen = Screen.Home
screen = Home
),
SEARCH(
unselectedIcon = Icons.Outlined.Search,
selectedIcon = Icons.Filled.Search,
appDestination = AppDestination.SEARCH_PHOTOS,
titleResource = R.string.nav_title_search,
screen = Screen.SearchPhotosScreen(query = " ")
screen = SearchPhotosScreen(query = " ")
);

fun getIcon(isSelected: Boolean) = if (isSelected) selectedIcon else unselectedIcon

companion object {

fun isAppbar(
appDestination: AppDestination?
): Boolean = entries.any { it.appDestination == appDestination }
fun isAppbar(screen: Any?): Boolean = entries.any { it.screen == screen }

fun getByRoute(route: String): Screen? = when {
Home::class.checkScreen(route) -> HOME
SearchPhotosScreen::class.checkScreen(route) -> SEARCH
Favourite::class.checkScreen(route) -> FAVOURITE
else -> null
}?.screen

private fun <T : Screen> KClass<T>.checkScreen(route: String): Boolean =
route.contains(simpleName.orEmpty())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal fun Project.configureAndroidCompose(
androidTestImplementation("androidx-compose-ui-test-junit4")
implementationBundle("accompanist", "compose", "lifecycle")
implementation("appcompat", "material", "koin-androidx-compose")
debugImplementation("androidx-compose-manifest")
}

extensions.configure<VkomposeExtension>(action = ::configureVkompose)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
alias(libs.plugins.application) apply false
alias(libs.plugins.kotlin) apply false
alias(libs.plugins.library) apply false
alias(libs.plugins.serialization)
alias(libs.plugins.serialization) apply false
alias(libs.plugins.ksp) apply false
alias(libs.plugins.room) apply false
alias(libs.plugins.vkompose) apply false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object Logger {

private const val DEFAULT_TAG = "GALLERY"

fun exception(
fun e(
throwable: Throwable,
tag: String? = null,
message: String? = null
Expand All @@ -20,7 +20,7 @@ object Logger {
)
}

fun debug(
fun d(
message: String,
tag: String? = null,
) {
Expand Down
5 changes: 4 additions & 1 deletion core/navigation/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
plugins {
id("csplashscreen.android.library")
alias(libs.plugins.convention.library)
alias(libs.plugins.convention.library.compose)
alias(libs.plugins.serialization)
}

dependencies {
implementation(project(":core:core"))
api(libs.androidx.compose.navigation)
implementation(libs.kotlinx.serialization.json)
}

android.namespace = "st.slex.csplashscreen.core.navigation"

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package st.slex.csplashscreen.core.navigation

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.navigation.toRoute
import kotlinx.serialization.Serializable

@Serializable
@Stable
sealed interface Screen {

@Serializable
data object Home : Screen

@Serializable
data class ImageDetailScreen(val imageId: String) : Screen

@Serializable
data class CollectionScreen(val collectionId: String) : Screen

@Serializable
data class SearchPhotosScreen(val query: String) : Screen

@Serializable
data class UserScreen(val username: String) : Screen

@Serializable
data object Favourite : Screen

}

inline fun <reified S : Screen> NavGraphBuilder.navScreen(
noinline content: @Composable (S) -> Unit
) {
composable<S> { backStackEntry ->
content(backStackEntry.toRoute())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@ import st.slex.csplashscreen.core.navigation.navigator.Navigator
import st.slex.csplashscreen.core.navigation.navigator.NavigatorImpl

fun moduleCoreNavigation(navController: NavHostController) = module {
single<Navigator> {
NavigatorImpl(navController)
}
single<Navigator> { NavigatorImpl(navController) }
}
Loading

0 comments on commit a60bf72

Please sign in to comment.