Skip to content

Commit a673f48

Browse files
committed
Address Dany's feedback
1 parent ab24fc3 commit a673f48

File tree

5 files changed

+22
-17
lines changed

5 files changed

+22
-17
lines changed
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
package com.example.nav3recipes.modular
22

3-
import androidx.compose.runtime.snapshots.SnapshotStateList
43
import dagger.Module
54
import dagger.Provides
65
import dagger.hilt.InstallIn
76
import dagger.hilt.android.components.ActivityRetainedComponent
87
import dagger.hilt.android.scopes.ActivityRetainedScoped
9-
import javax.inject.Singleton
108

119
@Module
1210
@InstallIn(ActivityRetainedComponent::class)
1311
object AppModule {
1412

1513
@Provides
1614
@ActivityRetainedScoped
17-
fun provideBackStack(backStackFactory: BackStackFactory) : SnapshotStateList<Any> =
18-
backStackFactory.create(startDestination = ConversationList)
19-
15+
fun provideNavigator() : Navigator = Navigator(startDestination = ConversationList)
2016
}

app/src/main/java/com/example/nav3recipes/modular/CommonModule.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ import javax.inject.Inject
1414
typealias EntryProviderInstaller = EntryProviderBuilder<Any>.() -> Unit
1515

1616
@ActivityRetainedScoped
17-
class BackStackFactory @Inject constructor() {
18-
fun create(startDestination: Any) : SnapshotStateList<Any> = mutableStateListOf(startDestination)
19-
}
17+
class Navigator @Inject constructor(startDestination: Any) {
18+
val backStack : SnapshotStateList<Any> = mutableStateListOf(startDestination)
19+
20+
fun goTo(destination: Any){
21+
backStack.add(destination)
22+
}
23+
24+
fun goBack(){
25+
backStack.removeLastOrNull()
26+
}
27+
}

app/src/main/java/com/example/nav3recipes/modular/ConversationModule.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ object ConversationModule {
4343

4444
@IntoSet
4545
@Provides
46-
fun provideEntryProviderInstaller(backStack: SnapshotStateList<Any>): EntryProviderInstaller =
46+
fun provideEntryProviderInstaller(navigator: Navigator): EntryProviderInstaller =
4747
{
4848
entry<ConversationList> {
4949
ConversationListScreen(
5050
onConversationClicked = { conversationDetail ->
51-
backStack.add(conversationDetail)
51+
navigator.goTo(conversationDetail)
5252
}
5353
)
5454
}
5555
entry<ConversationDetail> { key ->
56-
ConversationDetailScreen(key) { backStack.add(Profile) }
56+
ConversationDetailScreen(key) { navigator.goTo(Profile) }
5757
}
5858
}
5959
}

app/src/main/java/com/example/nav3recipes/modular/ModularActivity.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ import javax.inject.Inject
2222
* - api: defines the public facing routes for this feature
2323
* - impl: defines the entryProviders for this feature, these are injected into the app's main activity
2424
* The common module defines:
25-
* - how the back stack should be created
25+
* - a common navigator class that exposes a back stack and methods to modify that back stack
2626
* - a type that should be used by feature modules to inject entryProviders into the app's main activity
27-
* The app module creates the back stack by supplying a start destination and provides this back stack to the rest of the app module (i.e. MainActivity) and the feature modules.
27+
* The app module creates the navigator by supplying a start destination and provides this navigator
28+
* to the rest of the app module (i.e. MainActivity) and the feature modules.
2829
*/
2930
@AndroidEntryPoint
3031
class ModularActivity : ComponentActivity() {
3132

3233
@Inject
33-
lateinit var backStack: SnapshotStateList<Any>
34+
lateinit var navigator: Navigator
3435

3536
@Inject
3637
lateinit var entryProviderBuilders: Set<@JvmSuppressWildcards EntryProviderInstaller>
@@ -41,9 +42,9 @@ class ModularActivity : ComponentActivity() {
4142
setContent {
4243
Scaffold { paddingValues ->
4344
NavDisplay(
44-
backStack = backStack,
45+
backStack = navigator.backStack,
4546
modifier = Modifier.padding(paddingValues),
46-
onBack = { backStack.removeLastOrNull() },
47+
onBack = { navigator.goBack() },
4748
entryProvider = entryProvider {
4849
entryProviderBuilders.forEach { builder -> this.builder() }
4950
}

app/src/main/java/com/example/nav3recipes/modular/ProfileModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ object ProfileModule {
2929

3030
@IntoSet
3131
@Provides
32-
fun provideEntryProviderInstaller(backStack: SnapshotStateList<Any>) : EntryProviderInstaller = {
32+
fun provideEntryProviderInstaller() : EntryProviderInstaller = {
3333
entry<Profile>{
3434
ProfileScreen()
3535
}

0 commit comments

Comments
 (0)