diff --git a/app/src/main/java/st/slex/csplashscreen/ui/InitialApp.kt b/app/src/main/java/st/slex/csplashscreen/ui/InitialApp.kt index f49f4d6..e185c2e 100644 --- a/app/src/main/java/st/slex/csplashscreen/ui/InitialApp.kt +++ b/app/src/main/java/st/slex/csplashscreen/ui/InitialApp.kt @@ -27,6 +27,7 @@ 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") @@ -45,11 +46,9 @@ fun InitialApp( } navControllerHolder.navController.addOnDestinationChangedListener { _, destination, _ -> - Logger.d("Destination: ${destination.route}") - // todo need reflection to get Screen by route - currentDestination = destination.route?.let { - Screen.getByRoute(it) - } + Logger.d("current route: ${destination.route}") + currentDestination = destination.route?.let(::getByRoute) + Logger.d("currentDestination: ${currentDestination}") } DisposableEffect(systemUiController, isDarkTheme) { diff --git a/app/src/main/java/st/slex/csplashscreen/ui/components/bottom_appbar/BottomAppBarNavigation.kt b/app/src/main/java/st/slex/csplashscreen/ui/components/bottom_appbar/BottomAppBarNavigation.kt index c689229..9b24d8a 100644 --- a/app/src/main/java/st/slex/csplashscreen/ui/components/bottom_appbar/BottomAppBarNavigation.kt +++ b/app/src/main/java/st/slex/csplashscreen/ui/components/bottom_appbar/BottomAppBarNavigation.kt @@ -26,7 +26,7 @@ fun MainBottomAppBar( BottomAppBarResource .entries .forEach { item -> - val isSelected = currentDestination == item.appDestination + val isSelected = currentDestination == item.screen BottomAppBarItem( item = item, isSelected = isSelected, diff --git a/app/src/main/java/st/slex/csplashscreen/ui/components/bottom_appbar/BottomAppBarResource.kt b/app/src/main/java/st/slex/csplashscreen/ui/components/bottom_appbar/BottomAppBarResource.kt index 9339309..679b659 100644 --- a/app/src/main/java/st/slex/csplashscreen/ui/components/bottom_appbar/BottomAppBarResource.kt +++ b/app/src/main/java/st/slex/csplashscreen/ui/components/bottom_appbar/BottomAppBarResource.kt @@ -10,40 +10,50 @@ 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.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: Screen, val titleResource: Int, val screen: Screen ) { FAVOURITE( unselectedIcon = Icons.Outlined.FavoriteBorder, selectedIcon = Icons.Filled.Favorite, - appDestination = Screen.Favourite, titleResource = R.string.nav_title_favourite, - screen = Screen.Favourite + screen = Favourite ), HOME( unselectedIcon = Icons.Outlined.Home, selectedIcon = Icons.Filled.Home, - appDestination = Screen.Home, titleResource = R.string.nav_title_home, - screen = Screen.Home + screen = Home ), SEARCH( unselectedIcon = Icons.Outlined.Search, selectedIcon = Icons.Filled.Search, - appDestination = Screen.SearchPhotosScreen(" "), 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(screen: Any?): Boolean = entries.any { it.appDestination == screen } + 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 KClass.checkScreen(route: String): Boolean = + route.contains(simpleName.orEmpty()) } } \ No newline at end of file diff --git a/core/navigation/src/main/java/st/slex/csplashscreen/core/navigation/Screen.kt b/core/navigation/src/main/java/st/slex/csplashscreen/core/navigation/Screen.kt index b477239..b1f4559 100644 --- a/core/navigation/src/main/java/st/slex/csplashscreen/core/navigation/Screen.kt +++ b/core/navigation/src/main/java/st/slex/csplashscreen/core/navigation/Screen.kt @@ -29,14 +29,6 @@ sealed interface Screen { @Serializable data object Favourite : Screen - - companion object { - fun getByRoute(route: String): Screen? = this::class.sealedSubclasses - .firstOrNull { screenClass -> - screenClass.simpleName.orEmpty() - .contains(route, ignoreCase = true) - }?.objectInstance as Screen? - } } inline fun NavGraphBuilder.navScreen(