-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Main 화면 네비게이션 구조 feature 모듈을 의존하는 형태로 개선 #56
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
9 changes: 0 additions & 9 deletions
9
core/navigation/src/main/java/com/droidknights/app2023/core/navigation/HomeNavigation.kt
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
core/navigation/src/main/java/com/droidknights/app2023/core/navigation/MainDestination.kt
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
...ure/home/src/main/java/com/droidknights/app2023/feature/home/navigation/HomeNavigation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.droidknights.app2023.feature.home.navigation | ||
|
||
import androidx.navigation.NavController | ||
import androidx.navigation.NavGraphBuilder | ||
import androidx.navigation.compose.composable | ||
import com.droidknights.app2023.feature.home.HomeScreen | ||
|
||
fun NavController.navigateHome() { | ||
navigate(HomeRoute.route) | ||
} | ||
|
||
fun NavGraphBuilder.homeNavGraph() { | ||
composable(route = HomeRoute.route) { | ||
HomeScreen() | ||
} | ||
} | ||
|
||
object HomeRoute { | ||
const val route = "home" | ||
} | ||
16 changes: 0 additions & 16 deletions
16
...home/src/main/java/com/droidknights/app2023/feature/home/navigation/HomeNavigationImpl.kt
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
...e/home/src/main/java/com/droidknights/app2023/feature/home/navigation/NavigationModule.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 6 additions & 15 deletions
21
feature/main/src/androidTest/java/com/droidknights/app2023/feature/main/MainScreenTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,47 @@ | ||
package com.droidknights.app2023.feature.main | ||
|
||
import androidx.activity.ComponentActivity | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.test.junit4.createAndroidComposeRule | ||
import androidx.compose.ui.test.onNodeWithContentDescription | ||
import androidx.compose.ui.test.performClick | ||
import androidx.navigation.compose.ComposeNavigator | ||
import androidx.navigation.testing.TestNavHostController | ||
import com.droidknights.app2023.core.navigation.HomeNavigation | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class MainScreenTest { | ||
|
||
@get:Rule | ||
val composeTestRule = createAndroidComposeRule<ComponentActivity>() | ||
private lateinit var navController: TestNavHostController | ||
|
||
@Before | ||
fun setup() { | ||
composeTestRule.setContent { | ||
navController = TestNavHostController(LocalContext.current) | ||
navController.navigatorProvider.addNavigator(ComposeNavigator()) | ||
MainScreen(MainNavigator(navController, FakeHomeNavigation())) | ||
MainScreen(rememberMainNavigator(navController = navController)) | ||
} | ||
} | ||
|
||
@Test | ||
fun 시작_화면은_홈이다() { | ||
val actual = navController.currentBackStackEntry?.destination?.route | ||
assertEquals("home", actual) | ||
} | ||
|
||
@Test | ||
fun 설정_탭을_클릭하면_설정으로_이동한다() { | ||
// when | ||
composeTestRule | ||
.onNodeWithContentDescription("설정") | ||
.performClick() | ||
|
||
// then | ||
val actual = navController.currentBackStackEntry?.destination?.route | ||
assertEquals("setting", actual) | ||
} | ||
} | ||
|
||
private class FakeHomeNavigation : HomeNavigation { | ||
@Composable | ||
override fun Content() { | ||
// no-op | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 20 additions & 60 deletions
80
feature/main/src/main/java/com/droidknights/app2023/feature/main/MainNavigator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,43 @@ | ||
package com.droidknights.app2023.feature.main | ||
|
||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.navigation.NavHostController | ||
import androidx.navigation.compose.NavHost | ||
import androidx.navigation.compose.composable | ||
import androidx.navigation.compose.rememberNavController | ||
import com.droidknights.app2023.core.navigation.HomeNavigation | ||
import com.droidknights.app2023.core.navigation.MainDestination | ||
import com.droidknights.app2023.feature.home.navigation.HomeRoute | ||
import com.droidknights.app2023.feature.home.navigation.navigateHome | ||
|
||
internal class MainNavigator( | ||
private val navController: NavHostController, | ||
private val homeNavigation: HomeNavigation, | ||
startTab: MainTab = MainTab.HOME, | ||
val navController: NavHostController, | ||
) { | ||
private val startDestination: MainDestination = MainDestination.Home | ||
private var currentDestination: MainDestination by mutableStateOf(startDestination) | ||
|
||
val currentTab: MainTab | ||
get() = when (currentDestination) { | ||
MainDestination.Home -> MainTab.HOME | ||
MainDestination.Setting -> MainTab.SETTING | ||
MainDestination.Temp -> MainTab.TEMP | ||
} | ||
|
||
@Composable | ||
fun Content() { | ||
NavHost( | ||
navController = navController, | ||
startDestination = startDestination.route, | ||
) { | ||
composable(MainDestination.Home.route) { | ||
with(homeNavigation) { | ||
Content() | ||
} | ||
} | ||
|
||
// TODO: 각 모듈로 이동 | ||
val content: @Composable (String) -> Unit = { | ||
Box( | ||
modifier = Modifier.fillMaxSize(), | ||
contentAlignment = Alignment.Center, | ||
content = { Text(text = it) } | ||
) | ||
} | ||
composable("setting") { content("setting") } | ||
composable("temp") { content("temp") } | ||
} | ||
val startDestination: String = when (startTab) { | ||
MainTab.HOME -> HomeRoute.route | ||
MainTab.SETTING -> "setting" | ||
MainTab.TEMP -> "temp" | ||
} | ||
|
||
var currentTab: MainTab by mutableStateOf(startTab) | ||
private set | ||
|
||
fun navigate(tab: MainTab) { | ||
val destination = when (tab) { | ||
MainTab.SETTING -> MainDestination.Setting | ||
MainTab.HOME -> MainDestination.Home | ||
MainTab.TEMP -> MainDestination.Temp | ||
} | ||
navigate(destination) | ||
} | ||
|
||
private fun navigate(destination: MainDestination) { | ||
if (destination == currentDestination) { | ||
return | ||
if (tab == currentTab) return | ||
when (tab) { | ||
MainTab.SETTING -> navController.navigate("setting") // TODO: navigate settings | ||
MainTab.HOME -> navController.navigateHome() | ||
MainTab.TEMP -> navController.navigate("temp") // TODO: ??? | ||
} | ||
currentDestination = destination | ||
navController.navigate(destination.route) | ||
currentTab = tab | ||
} | ||
} | ||
|
||
@Composable | ||
internal fun rememberMainNavigator( | ||
homeNavigation: HomeNavigation, | ||
starTab: MainTab = MainTab.HOME, | ||
navController: NavHostController = rememberNavController(), | ||
): MainNavigator = remember(navController, homeNavigation) { | ||
MainNavigator(navController, homeNavigation) | ||
): MainNavigator = remember(navController) { | ||
MainNavigator(starTab, navController) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
단순 상수값으로도 충분하다고 생각했는데 HomeRoute가 object여야할 이유가 있을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wisemuji
Route를 만들어주는 역할을 하는 목적으로 보면 object로 관리하는 이점이 있습니다.
ref : https://github.com/DroidKaigi/conference-app-2022/blob/main/feature/sessions/src/main/java/io/github/droidkaigi/confsched2022/feature/sessions/SessionsNavGraph.kt