Skip to content

Commit

Permalink
fix app bar navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
stslex committed Aug 20, 2024
1 parent 6019d69 commit 3080cf0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
9 changes: 4 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 @@ -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")
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T : Screen> KClass<T>.checkScreen(route: String): Boolean =
route.contains(simpleName.orEmpty())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <reified S : Screen> NavGraphBuilder.navScreen(
Expand Down

0 comments on commit 3080cf0

Please sign in to comment.