Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 54aa38b

Browse files
author
Suleyman Basaranoglu
committed
Add Nav Graph Builder, for features that use their own navigation graph
1 parent a365c92 commit 54aa38b

File tree

13 files changed

+160
-7
lines changed

13 files changed

+160
-7
lines changed

‎app/src/main/java/com/example/composefeaturebasedmultimodule/SingleActivity.kt‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import androidx.activity.ComponentActivity
55
import androidx.activity.compose.setContent
66
import com.example.composefeaturebasedmultimodule.ui.theme.ComposeFeatureBasedMultiModuleTheme
77
import com.example.detail.presentation.DetailScreen
8+
import com.example.detail.presentation.DetailSearchScreen
89
import com.example.home.presentation.HomeScreen
910
import com.example.list.presentation.ListScreen
1011
import com.example.navigation.AppNavigation
1112
import com.example.navigation.Navigator
13+
import com.example.navigation.graph.DetailScreens
1214
import dagger.hilt.android.AndroidEntryPoint
1315
import javax.inject.Inject
1416

@@ -32,7 +34,11 @@ class SingleActivity : ComponentActivity() {
3234
},
3335
detailScreen = {// We can get args with "it" if we need
3436
DetailScreen()
35-
}
37+
},
38+
detailScreenWithGraph = DetailScreens(
39+
detailMain = { DetailScreen() },
40+
detailSearch = { DetailSearchScreen() }
41+
)
3642
)
3743
}
3844
}

‎detail/src/main/java/com/example/detail/presentation/DetailScreen.kt‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ fun DetailScreen() {
2222
when {
2323
state.isLoading -> { LoadingComponent() }
2424
state.error != null -> { ErrorComponent(error = state.error) }
25-
state.itemData != null -> { DetailContent(state.itemData!!) }
25+
state.itemData != null -> { DetailContent(
26+
state.itemData!!,
27+
onSearchClicked = { viewModel.onEvent(DetailUIEvent.SearchDetailClick) }
28+
) }
2629
}
2730
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.example.detail.presentation
2+
3+
import androidx.compose.foundation.layout.Column
4+
import androidx.compose.foundation.layout.padding
5+
import androidx.compose.foundation.text.BasicTextField
6+
import androidx.compose.material3.Text
7+
import androidx.compose.runtime.Composable
8+
import androidx.compose.runtime.getValue
9+
import androidx.compose.runtime.mutableStateOf
10+
import androidx.compose.runtime.remember
11+
import androidx.compose.runtime.setValue
12+
import androidx.compose.ui.Modifier
13+
import androidx.compose.ui.unit.dp
14+
15+
/*The only function of this Composable class is to
16+
functionality of the Navigation module using its own nav graph(DetailGraph) within a module.
17+
For more understandable please check DetailScreens with DetailGraph
18+
*/
19+
@Composable
20+
fun DetailSearchScreen() {
21+
var searchText by remember { mutableStateOf("") }
22+
23+
Column(modifier = Modifier.padding(16.dp)) {
24+
BasicTextField(
25+
value = searchText,
26+
onValueChange = { searchText = it },
27+
decorationBox = { innerTextField ->
28+
if (searchText.isEmpty()) {
29+
Text("Search..")
30+
}
31+
innerTextField()
32+
}
33+
)
34+
}
35+
}

‎detail/src/main/java/com/example/detail/presentation/DetailViewModel.kt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ class DetailViewModel @Inject constructor(
3737
navigator.goBack()
3838
}
3939

40+
private fun handleSearchDetailClick() {
41+
navigator.navigateTo("detail/search")
42+
}
43+
4044
override suspend fun handleEvent(event: DetailUIEvent) {
4145
when (event) {
4246
is DetailUIEvent.Dismiss -> handleBack()
4347
is DetailUIEvent.LoadItemDetail -> loadItemDetail()
48+
is DetailUIEvent.SearchDetailClick -> handleSearchDetailClick()
4449
}
4550
}
4651

‎detail/src/main/java/com/example/detail/presentation/components/DetailContent.kt‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@ import androidx.compose.foundation.layout.Spacer
55
import androidx.compose.foundation.layout.fillMaxWidth
66
import androidx.compose.foundation.layout.height
77
import androidx.compose.foundation.layout.padding
8+
import androidx.compose.material3.Button
89
import androidx.compose.material3.MaterialTheme
910
import androidx.compose.material3.Text
1011
import androidx.compose.runtime.Composable
1112
import androidx.compose.ui.Modifier
1213
import androidx.compose.ui.unit.dp
1314
import com.example.core.components.CoilImageComponent
1415
import com.example.detail.domain.model.ItemDetail
16+
import com.example.detail.presentation.uievent.DetailUIEvent
1517

1618
@Composable
1719
fun DetailContent(
18-
itemData: ItemDetail
20+
itemData: ItemDetail,
21+
onSearchClicked: (DetailUIEvent) -> Unit
1922
) {
2023
Column(modifier = Modifier.padding(16.dp)) {
2124
CoilImageComponent(
@@ -29,5 +32,9 @@ fun DetailContent(
2932
Text(itemData.productName, style = MaterialTheme.typography.bodyLarge)
3033
Spacer(modifier = Modifier.height(4.dp))
3134
Text(itemData.subText, style = MaterialTheme.typography.bodyMedium)
35+
Button(
36+
onClick = { onSearchClicked(DetailUIEvent.SearchDetailClick) },
37+
modifier = Modifier.fillMaxWidth()
38+
) { Text("Route with Nav Graph to Search Detail") }
3239
}
3340
}

‎detail/src/main/java/com/example/detail/presentation/uievent/DetailUIEvent.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ package com.example.detail.presentation.uievent
33
sealed class DetailUIEvent {
44
data object Dismiss : DetailUIEvent()
55
data object LoadItemDetail : DetailUIEvent()
6+
data object SearchDetailClick: DetailUIEvent()
7+
68
}

‎home/src/main/java/com/example/home/presentation/HomeViewModel.kt‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class HomeViewModel @Inject constructor(
2929
}
3030

3131
is HomeUIEvent.OnProductClicked -> {
32-
onProductClicked(true)
32+
onProductClicked()
3333
}
3434

3535
is HomeUIEvent.OnVerticalProductClicked -> {
@@ -74,13 +74,24 @@ class HomeViewModel @Inject constructor(
7474
}
7575
}
7676

77-
private fun onProductClicked(isSheetOpen: Boolean) {
78-
navigator.navigateTo( "detail/$isSheetOpen") {
77+
/* Route with arguments
78+
private fun onProductClicked(isSheetOpen: Boolean) {
79+
navigator.navigateTo( "detail/$isSheetOpen") {
80+
launchSingleTop = true
81+
restoreState = true
82+
}
83+
}
84+
*/
85+
86+
// Route with Detail Graph
87+
private fun onProductClicked() {
88+
navigator.navigateTo("detailgraph") {
7989
launchSingleTop = true
8090
restoreState = true
8191
}
8292
}
8393

94+
8495
private fun handleBack() {
8596
navigator.goBack()
8697
}

‎navigation/src/main/java/com/example/navigation/AppNavigation.kt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import androidx.compose.runtime.LaunchedEffect
55
import androidx.navigation.compose.NavHost
66
import androidx.navigation.compose.composable
77
import androidx.navigation.compose.rememberNavController
8+
import com.example.navigation.graph.DetailScreens
9+
import com.example.navigation.graph.detailGraph
810
import com.example.navigation.screens.Detail
911
import kotlinx.coroutines.flow.collectLatest
1012

@@ -13,7 +15,8 @@ fun AppNavigation(
1315
navigator: Navigator,
1416
homeScreen: @Composable () -> Unit,
1517
listScreen: @Composable () -> Unit,
16-
detailScreen: @Composable (Boolean) -> Unit
18+
detailScreen: @Composable (Boolean) -> Unit,
19+
detailScreenWithGraph: DetailScreens
1720
) {
1821
val navController = rememberNavController()
1922

@@ -30,6 +33,7 @@ fun AppNavigation(
3033
}
3134

3235
NavHost(navController, startDestination = Destination.home.route) {
36+
detailGraph(detailScreenWithGraph)
3337
composable(Destination.home.route) {
3438
homeScreen()
3539
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.example.navigation.graph
2+
3+
import com.example.navigation.screens.detaiwithowngraph.DetailMain
4+
import com.example.navigation.screens.detaiwithowngraph.DetailSearch
5+
import com.example.navigation.utils.NavigationGraph
6+
7+
object DetailGraph : NavigationGraph {
8+
override val route: String
9+
get() = "detailgraph"
10+
override val startDestination: String
11+
get() = detailMain.destination(Unit)
12+
13+
val detailMain = DetailMain
14+
val detailSearch = DetailSearch
15+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.example.navigation.graph
2+
3+
import androidx.compose.runtime.Composable
4+
import androidx.compose.runtime.Immutable
5+
import androidx.navigation.NavGraphBuilder
6+
import androidx.navigation.compose.composable
7+
import androidx.navigation.navigation
8+
@Immutable
9+
data class DetailScreens(
10+
val detailMain: @Composable () -> Unit,
11+
val detailSearch: @Composable () -> Unit
12+
)
13+
14+
internal fun NavGraphBuilder.detailGraph(
15+
screens: DetailScreens
16+
) {
17+
navigation(
18+
startDestination = DetailGraph.startDestination,
19+
route = DetailGraph.route
20+
) {
21+
composable(DetailGraph.detailMain.route) {
22+
screens.detailMain()
23+
}
24+
composable(DetailGraph.detailSearch.route) {
25+
screens.detailSearch()
26+
}
27+
}
28+
}
29+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /