Skip to content

Commit

Permalink
Merge pull request #72 from stslex/dev
Browse files Browse the repository at this point in the history
Refactor navigation
  • Loading branch information
stslex authored Nov 22, 2023
2 parents c9eed61 + 3490e9c commit 94b3ee3
Show file tree
Hide file tree
Showing 47 changed files with 279 additions and 359 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package st.slex.csplashscreen.di.main
import androidx.lifecycle.ViewModelProvider
import dagger.Component
import st.slex.csplashscreen.core.core.api.AppApi
import st.slex.csplashscreen.core.navigation.di.NavigationApi
import st.slex.csplashscreen.core.ui.di.MainUiApi
import st.slex.csplashscreen.core.ui.di.NavigationApi

@Component(
dependencies = [MainDependencies::class],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import androidx.navigation.NavHostController
import st.slex.csplashscreen.core.core.api.AppApi
import st.slex.csplashscreen.core.core.api.ApplicationApiProvider
import st.slex.csplashscreen.core.core.coroutine.AppDispatcher
import st.slex.csplashscreen.core.navigation.di.NavigationApi
import st.slex.csplashscreen.core.navigation.di.NavigationComponentBuilder
import st.slex.csplashscreen.core.ui.di.NavigationApi

object MainComponentBuilder {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package st.slex.csplashscreen.di.main

import android.content.Context
import st.slex.csplashscreen.core.core.coroutine.AppDispatcher
import st.slex.csplashscreen.core.ui.di.Navigator
import st.slex.csplashscreen.core.navigation.navigator.Navigator

interface MainDependencies {

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/st/slex/csplashscreen/ui/InitialApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import st.slex.csplashscreen.core.navigation.AppDestination
import st.slex.csplashscreen.core.navigation.NavigationScreen
import st.slex.csplashscreen.core.navigation.navigator.NavigationTarget
import st.slex.csplashscreen.ui.components.NavigationHost
import st.slex.csplashscreen.ui.components.bottom_appbar.BottomAppBarResource
import st.slex.csplashscreen.ui.components.bottom_appbar.MainBottomAppBar
Expand All @@ -32,7 +32,7 @@ import st.slex.csplashscreen.ui.components.bottom_appbar.MainBottomAppBar
@Composable
fun InitialApp(
navController: NavHostController,
onBottomAppBarClick: (NavigationScreen) -> Unit,
onBottomAppBarClick: (NavigationTarget.Screen) -> Unit,
modifier: Modifier = Modifier,
) {
val systemUiController = rememberSystemUiController()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package st.slex.csplashscreen.ui

import androidx.lifecycle.ViewModel
import st.slex.csplashscreen.core.navigation.NavigationScreen
import st.slex.csplashscreen.core.ui.di.Navigator
import st.slex.csplashscreen.core.navigation.navigator.NavigationTarget
import st.slex.csplashscreen.core.navigation.navigator.Navigator
import javax.inject.Inject

class InitialAppViewModel @Inject constructor(
private val navigator: Navigator
) : ViewModel() {

fun navigate(screen: NavigationScreen) {
navigator(screen)
fun navigate(screen: NavigationTarget.Screen) {
navigator.navigate(screen)
}
}
3 changes: 3 additions & 0 deletions app/src/main/java/st/slex/csplashscreen/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class MainActivity : ComponentActivity(), MainUiProvider {
val viewModel = buildViewModel()

InitialApp(
/*TODO AFTER reconfiguration controller in VM don't change it State,
so it need to have latest instance.
Need Research to find more efficient way */
navController = api.navigator.controller,
onBottomAppBarClick = remember {
{ viewModel.navigate(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fun NavigationHost(
) {
NavHost(
navController = navController,
startDestination = startDestination.route
startDestination = startDestination.navigationRoute
) {
homeGraph(modifier)
userGraph(modifier)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ 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.NavigationScreen
import st.slex.csplashscreen.core.navigation.navigator.NavigationTarget

@Composable
fun MainBottomAppBar(
onBottomAppBarClick: (NavigationScreen) -> Unit,
onBottomAppBarClick: (NavigationTarget.Screen) -> Unit,
currentDestination: AppDestination?
) {
NavigationBar(
Expand All @@ -43,7 +43,7 @@ fun MainBottomAppBar(
private fun RowScope.BottomAppBarItem(
item: BottomAppBarResource,
isSelected: Boolean,
onBottomAppBarClick: (NavigationScreen) -> Unit
onBottomAppBarClick: (NavigationTarget.Screen) -> Unit
) {
NavigationBarItem(
selected = isSelected,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,35 @@ 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.NavigationScreen
import st.slex.csplashscreen.core.navigation.navigator.NavigationTarget.Screen

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

fun getIcon(isSelected: Boolean) = if (isSelected) selectedIcon else unselectedIcon
Expand Down
4 changes: 2 additions & 2 deletions build-logic/dependencies/src/main/kotlin/AppVersions.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
object AppVersions {
const val VERSION_NAME = "1.69"
const val VERSION_CODE = 15
const val VERSION_NAME = "1.70"
const val VERSION_CODE = 16
}
1 change: 0 additions & 1 deletion core/navigation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ plugins {

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,34 @@
package st.slex.csplashscreen.core.navigation

sealed class AppArguments {
sealed class AppArguments(
vararg val arguments: String
) {

abstract val arguments: List<String>
val argumentsForRoute: String
get() = when (arguments.isEmpty()) {
true -> String()
false -> arguments.joinToString(separator = "/", prefix = "/")
}

open val argumentsForRoute: String
get() = arguments.joinToString(separator = "/", prefix = "/")

object Empty : AppArguments() {
override val arguments: List<String>
get() = emptyList()
override val argumentsForRoute: String
get() = String()
}
data object Empty : AppArguments()

data class ImageDetailScreen(
val imageId: String
) : AppArguments() {
override val arguments: List<String>
get() = listOf(imageId)
}
) : AppArguments(imageId)

data class CollectionScreen(
val collectionId: String
) : AppArguments() {

override val arguments: List<String>
get() = listOf(collectionId)
}
) : AppArguments(collectionId)

data class SearchPhotosScreen(
private val query: String
) : AppArguments() {

override val arguments: List<String>
get() = listOf(query)
) : AppArguments(query) {

val checkedQuery: String
get() = arguments.firstOrNull().orEmpty().trimEnd()
}

data class UserScreen(
val username: String
) : AppArguments() {
override val arguments: List<String>
get() = listOf(username)
}
}
) : AppArguments(username)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package st.slex.csplashscreen.core.navigation

enum class AppDestination(vararg val argsNames: String) {
import androidx.navigation.NamedNavArgument
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavType
import androidx.navigation.navArgument

enum class AppDestination(
private vararg val argsNames: String
) {
HOME,
IMAGE_DETAIL("imageId"),
COLLECTION("collection_id"),
Expand All @@ -9,35 +16,36 @@ enum class AppDestination(vararg val argsNames: String) {
USER("username"),
UNDEFINED;

val route: String
get() = StringBuilder()
.append(name, SEPARATOR_ROUTE, TAG_ROUTE)
.toString()
.lowercase()
val destinationName: String = "${name}_route"
val navigationRoute: String = "$destinationName${argsNames.argumentsRoute}"
val composableArguments: List<NamedNavArgument> = argsNames.map { name ->
navArgument(name) { NavType.StringType }
}

val navigationRoute: String
get() = "$route${argsNames.argumentsRoute}"
// TODO Arguments always String - Need research for Parcelable implementation
fun parseArguments(
navBackStackEntry: NavBackStackEntry
): List<String> = argsNames.map { name ->
navBackStackEntry.arguments?.getString(name).orEmpty()
}

private val Array<out String>.argumentsRoute: String
get() = if (isEmpty()) {
String()
} else {
joinToString(separator = "}/{", prefix = "/{", postfix = "}")
}
get() = if (isEmpty()) "" else joinToString(
separator = "}/{",
prefix = "/{",
postfix = "}"
)

companion object {

private const val SEPARATOR_ROUTE = "_"
private const val TAG_ROUTE = "route"

fun findByRoute(route: String?) = if (route == null) {
fun findByRoute(
route: String?
) = if (route.isNullOrBlank()) {
UNDEFINED
} else {
AppDestination
.entries
.firstOrNull { destination ->
destination.navigationRoute == route
}
AppDestination.entries.firstOrNull {
it.navigationRoute == route
}
}
}
}
}

This file was deleted.

Loading

0 comments on commit 94b3ee3

Please sign in to comment.