Skip to content

Commit a60bf72

Browse files
authored
Merge pull request #84 from stslex/dev
refactor Navigation
2 parents 671c6f0 + 3080cf0 commit a60bf72

File tree

42 files changed

+273
-357
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+273
-357
lines changed

app/src/main/java/st/slex/csplashscreen/ui/InitialApp.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,33 @@ import androidx.compose.ui.Modifier
2222
import androidx.compose.ui.graphics.Color
2323
import androidx.compose.ui.unit.dp
2424
import com.google.accompanist.systemuicontroller.rememberSystemUiController
25-
import st.slex.csplashscreen.core.navigation.AppDestination
26-
import st.slex.csplashscreen.core.navigation.navigator.NavigationTarget
25+
import st.slex.csplashscreen.core.core.Logger
26+
import st.slex.csplashscreen.core.navigation.Screen
2727
import st.slex.csplashscreen.ui.components.NavHostControllerHolder
2828
import st.slex.csplashscreen.ui.components.NavigationHost
2929
import st.slex.csplashscreen.ui.components.bottom_appbar.BottomAppBarResource
30+
import st.slex.csplashscreen.ui.components.bottom_appbar.BottomAppBarResource.Companion.getByRoute
3031
import st.slex.csplashscreen.ui.components.bottom_appbar.MainBottomAppBar
3132

3233
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
3334
@Composable
3435
@Stable
3536
fun InitialApp(
3637
navControllerHolder: NavHostControllerHolder,
37-
onBottomAppBarClick: (NavigationTarget.Screen) -> Unit,
38+
onBottomAppBarClick: (Screen) -> Unit,
3839
modifier: Modifier = Modifier,
3940
) {
4041
val systemUiController = rememberSystemUiController()
4142
val isDarkTheme = isSystemInDarkTheme()
4243

4344
var currentDestination by remember {
44-
mutableStateOf<AppDestination?>(AppDestination.HOME)
45+
mutableStateOf<Screen?>(Screen.Home)
4546
}
4647

4748
navControllerHolder.navController.addOnDestinationChangedListener { _, destination, _ ->
48-
currentDestination = AppDestination.findByRoute(destination.route)
49+
Logger.d("current route: ${destination.route}")
50+
currentDestination = destination.route?.let(::getByRoute)
51+
Logger.d("currentDestination: ${currentDestination}")
4952
}
5053

5154
DisposableEffect(systemUiController, isDarkTheme) {
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package st.slex.csplashscreen.ui
22

33
import androidx.lifecycle.ViewModel
4+
import st.slex.csplashscreen.core.navigation.Screen
45
import st.slex.csplashscreen.core.navigation.navigator.NavigationTarget
56
import st.slex.csplashscreen.core.navigation.navigator.Navigator
7+
import st.slex.csplashscreen.core.navigation.navigator.NavigatorOptions
68

79
class InitialAppViewModel(
810
private val navigator: Navigator
911
) : ViewModel() {
1012

11-
fun navigate(screen: NavigationTarget.Screen) {
12-
navigator.navigate(screen)
13+
fun navigate(screen: Screen) {
14+
navigator(
15+
NavigationTarget.Screen(
16+
screen = screen,
17+
options = NavigatorOptions(isSingleTop = true)
18+
),
19+
)
1320
}
1421
}

app/src/main/java/st/slex/csplashscreen/ui/MainActivity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ class MainActivity : ComponentActivity() {
3232
Need Research to find more efficient way */
3333
navControllerHolder = navHostControllerHolder,
3434
onBottomAppBarClick = remember {
35-
{ viewModel.navigate(it) }
35+
{ screen ->
36+
viewModel.navigate(screen)
37+
}
3638
}
3739
)
3840
}

app/src/main/java/st/slex/csplashscreen/ui/components/NavigationHost.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import androidx.compose.runtime.Stable
55
import androidx.compose.ui.Modifier
66
import androidx.navigation.NavHostController
77
import androidx.navigation.compose.NavHost
8-
import st.slex.csplashscreen.core.navigation.AppDestination
8+
import st.slex.csplashscreen.core.navigation.Screen
99
import st.slex.csplashscreen.feature.collection.navigation.singleCollectionGraph
1010
import st.slex.csplashscreen.feature.favourite.navigation.favouriteGraph
1111
import st.slex.csplashscreen.feature.feature_photo_detail.navigation.imageDetailGraph
@@ -21,11 +21,11 @@ class NavHostControllerHolder(val navController: NavHostController)
2121
fun NavigationHost(
2222
holder: NavHostControllerHolder,
2323
modifier: Modifier = Modifier,
24-
startDestination: AppDestination = AppDestination.HOME
24+
startDestination: Screen = Screen.Home
2525
) {
2626
NavHost(
2727
navController = holder.navController,
28-
startDestination = startDestination.navigationRoute
28+
startDestination = startDestination
2929
) {
3030
homeGraph(modifier)
3131
userGraph(modifier)

app/src/main/java/st/slex/csplashscreen/ui/components/bottom_appbar/BottomAppBarNavigation.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ import androidx.compose.runtime.Composable
1111
import androidx.compose.runtime.remember
1212
import androidx.compose.ui.Modifier
1313
import androidx.compose.ui.res.stringResource
14-
import st.slex.csplashscreen.core.navigation.AppDestination
15-
import st.slex.csplashscreen.core.navigation.navigator.NavigationTarget
14+
import st.slex.csplashscreen.core.navigation.Screen
1615

1716
@Composable
1817
fun MainBottomAppBar(
19-
onBottomAppBarClick: (NavigationTarget.Screen) -> Unit,
20-
currentDestination: AppDestination?
18+
onBottomAppBarClick: (Screen) -> Unit,
19+
currentDestination: Screen?
2120
) {
2221
NavigationBar(
2322
modifier = Modifier
@@ -27,7 +26,7 @@ fun MainBottomAppBar(
2726
BottomAppBarResource
2827
.entries
2928
.forEach { item ->
30-
val isSelected = currentDestination == item.appDestination
29+
val isSelected = currentDestination == item.screen
3130
BottomAppBarItem(
3231
item = item,
3332
isSelected = isSelected,
@@ -43,7 +42,7 @@ fun MainBottomAppBar(
4342
private fun RowScope.BottomAppBarItem(
4443
item: BottomAppBarResource,
4544
isSelected: Boolean,
46-
onBottomAppBarClick: (NavigationTarget.Screen) -> Unit
45+
onBottomAppBarClick: (Screen) -> Unit
4746
) {
4847
NavigationBarItem(
4948
selected = isSelected,
@@ -53,7 +52,7 @@ private fun RowScope.BottomAppBarItem(
5352
icon = {
5453
Icon(
5554
imageVector = item.getIcon(isSelected),
56-
contentDescription = item.appDestination.name
55+
contentDescription = item.screen.javaClass.simpleName
5756
)
5857
},
5958
label = {

app/src/main/java/st/slex/csplashscreen/ui/components/bottom_appbar/BottomAppBarResource.kt

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,51 @@ import androidx.compose.material.icons.outlined.Home
99
import androidx.compose.material.icons.outlined.Search
1010
import androidx.compose.ui.graphics.vector.ImageVector
1111
import st.slex.csplashscreen.R
12-
import st.slex.csplashscreen.core.navigation.AppDestination
13-
import st.slex.csplashscreen.core.navigation.navigator.NavigationTarget.Screen
12+
import st.slex.csplashscreen.core.navigation.Screen
13+
import st.slex.csplashscreen.core.navigation.Screen.Favourite
14+
import st.slex.csplashscreen.core.navigation.Screen.Home
15+
import st.slex.csplashscreen.core.navigation.Screen.SearchPhotosScreen
16+
import kotlin.reflect.KClass
1417

1518
enum class BottomAppBarResource(
1619
val unselectedIcon: ImageVector,
1720
val selectedIcon: ImageVector,
18-
val appDestination: AppDestination,
1921
val titleResource: Int,
2022
val screen: Screen
2123
) {
2224
FAVOURITE(
2325
unselectedIcon = Icons.Outlined.FavoriteBorder,
2426
selectedIcon = Icons.Filled.Favorite,
25-
appDestination = AppDestination.FAVOURITE,
2627
titleResource = R.string.nav_title_favourite,
27-
screen = Screen.Favourite
28+
screen = Favourite
2829
),
2930
HOME(
3031
unselectedIcon = Icons.Outlined.Home,
3132
selectedIcon = Icons.Filled.Home,
32-
appDestination = AppDestination.HOME,
3333
titleResource = R.string.nav_title_home,
34-
screen = Screen.Home
34+
screen = Home
3535
),
3636
SEARCH(
3737
unselectedIcon = Icons.Outlined.Search,
3838
selectedIcon = Icons.Filled.Search,
39-
appDestination = AppDestination.SEARCH_PHOTOS,
4039
titleResource = R.string.nav_title_search,
41-
screen = Screen.SearchPhotosScreen(query = " ")
40+
screen = SearchPhotosScreen(query = " ")
4241
);
4342

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

4645
companion object {
4746

48-
fun isAppbar(
49-
appDestination: AppDestination?
50-
): Boolean = entries.any { it.appDestination == appDestination }
47+
fun isAppbar(screen: Any?): Boolean = entries.any { it.screen == screen }
48+
49+
fun getByRoute(route: String): Screen? = when {
50+
Home::class.checkScreen(route) -> HOME
51+
SearchPhotosScreen::class.checkScreen(route) -> SEARCH
52+
Favourite::class.checkScreen(route) -> FAVOURITE
53+
else -> null
54+
}?.screen
55+
56+
private fun <T : Screen> KClass<T>.checkScreen(route: String): Boolean =
57+
route.contains(simpleName.orEmpty())
5158
}
5259
}

build-logic/dependencies/src/main/kotlin/st.slex.csplashscreen/ComposeAndroid.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ internal fun Project.configureAndroidCompose(
2626
androidTestImplementation("androidx-compose-ui-test-junit4")
2727
implementationBundle("accompanist", "compose", "lifecycle")
2828
implementation("appcompat", "material", "koin-androidx-compose")
29+
debugImplementation("androidx-compose-manifest")
2930
}
3031

3132
extensions.configure<VkomposeExtension>(action = ::configureVkompose)

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
alias(libs.plugins.application) apply false
44
alias(libs.plugins.kotlin) apply false
55
alias(libs.plugins.library) apply false
6-
alias(libs.plugins.serialization)
6+
alias(libs.plugins.serialization) apply false
77
alias(libs.plugins.ksp) apply false
88
alias(libs.plugins.room) apply false
99
alias(libs.plugins.vkompose) apply false

core/core/src/main/java/st/slex/csplashscreen/core/core/Logger.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ object Logger {
66

77
private const val DEFAULT_TAG = "GALLERY"
88

9-
fun exception(
9+
fun e(
1010
throwable: Throwable,
1111
tag: String? = null,
1212
message: String? = null
@@ -20,7 +20,7 @@ object Logger {
2020
)
2121
}
2222

23-
fun debug(
23+
fun d(
2424
message: String,
2525
tag: String? = null,
2626
) {

core/navigation/build.gradle.kts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
plugins {
2-
id("csplashscreen.android.library")
2+
alias(libs.plugins.convention.library)
3+
alias(libs.plugins.convention.library.compose)
4+
alias(libs.plugins.serialization)
35
}
46

57
dependencies {
68
implementation(project(":core:core"))
79
api(libs.androidx.compose.navigation)
10+
implementation(libs.kotlinx.serialization.json)
811
}
912

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

0 commit comments

Comments
 (0)