Skip to content

Commit 76daa39

Browse files
committed
Address review feedback from Manu
1 parent 0d5dd94 commit 76daa39

File tree

7 files changed

+22
-63
lines changed

7 files changed

+22
-63
lines changed

app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ class NavigationTest {
206206

207207
@Test
208208
fun whenSettingsDialogDismissed_previousScreenIsDisplayed() {
209-
210209
composeTestRule.apply {
211210

212211
// Navigate to the saved screen, open the settings dialog, then close it.

app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NiaAppStateTest.kt

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,14 @@ import androidx.navigation.compose.ComposeNavigator
3030
import androidx.navigation.compose.composable
3131
import androidx.navigation.createGraph
3232
import androidx.navigation.testing.TestNavHostController
33-
import com.google.samples.apps.nowinandroid.core.testing.util.MainDispatcherRule
3433
import com.google.samples.apps.nowinandroid.core.testing.util.TestNetworkMonitor
35-
import kotlinx.coroutines.cancel
3634
import kotlinx.coroutines.flow.collect
3735
import kotlinx.coroutines.launch
38-
import kotlinx.coroutines.test.TestScope
3936
import kotlinx.coroutines.test.UnconfinedTestDispatcher
4037
import kotlinx.coroutines.test.runTest
41-
import org.junit.After
4238
import org.junit.Assert.assertEquals
4339
import org.junit.Assert.assertFalse
4440
import org.junit.Assert.assertTrue
45-
import org.junit.Before
4641
import org.junit.Rule
4742
import org.junit.Test
4843

@@ -55,31 +50,15 @@ import org.junit.Test
5550
@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
5651
class NiaAppStateTest {
5752

58-
@get:Rule
59-
val mainDispatcherRule = MainDispatcherRule()
60-
6153
@get:Rule
6254
val composeTestRule = createComposeRule()
6355

6456
// Create the test dependencies.
65-
private lateinit var testScope: TestScope
6657
private val networkMonitor = TestNetworkMonitor()
6758

6859
// Subject under test.
6960
private lateinit var state: NiaAppState
7061

71-
@Before
72-
fun setup() {
73-
// We use the Unconfined dispatcher to ensure that coroutines are executed sequentially in
74-
// tests.
75-
testScope = TestScope(UnconfinedTestDispatcher())
76-
}
77-
78-
@After
79-
fun cleanup() {
80-
testScope.cancel()
81-
}
82-
8362
@Test
8463
fun niaAppState_currentDestination() = runTest {
8564
var currentDestination: String? = null
@@ -91,7 +70,7 @@ class NiaAppStateTest {
9170
windowSizeClass = getCompactWindowClass(),
9271
navController = navController,
9372
networkMonitor = networkMonitor,
94-
coroutineScope = testScope
73+
coroutineScope = backgroundScope
9574
)
9675
}
9776

@@ -122,28 +101,14 @@ class NiaAppStateTest {
122101
assertTrue(state.topLevelDestinations[2].name.contains("interests", true))
123102
}
124103

125-
@Test
126-
fun niaAppState_showTopBarForTopLevelDestinations() {
127-
composeTestRule.setContent {
128-
val navController = rememberTestNavController()
129-
state = rememberNiaAppState(
130-
windowSizeClass = getCompactWindowClass(),
131-
navController = navController,
132-
networkMonitor = networkMonitor
133-
)
134-
135-
// Do nothing - we should already be
136-
}
137-
}
138-
139104
@Test
140105
fun niaAppState_showBottomBar_compact() = runTest {
141106
composeTestRule.setContent {
142107
state = NiaAppState(
143108
windowSizeClass = getCompactWindowClass(),
144109
navController = NavHostController(LocalContext.current),
145110
networkMonitor = networkMonitor,
146-
coroutineScope = testScope
111+
coroutineScope = backgroundScope
147112
)
148113
}
149114

@@ -158,7 +123,7 @@ class NiaAppStateTest {
158123
windowSizeClass = WindowSizeClass.calculateFromSize(DpSize(800.dp, 800.dp)),
159124
navController = NavHostController(LocalContext.current),
160125
networkMonitor = networkMonitor,
161-
coroutineScope = testScope
126+
coroutineScope = backgroundScope
162127
)
163128
}
164129

@@ -174,7 +139,7 @@ class NiaAppStateTest {
174139
windowSizeClass = WindowSizeClass.calculateFromSize(DpSize(900.dp, 1200.dp)),
175140
navController = NavHostController(LocalContext.current),
176141
networkMonitor = networkMonitor,
177-
coroutineScope = testScope
142+
coroutineScope = backgroundScope
178143
)
179144
}
180145

@@ -183,27 +148,23 @@ class NiaAppStateTest {
183148
}
184149

185150
@Test
186-
fun stateIsOfflineWhenNetworkMonitorIsOffline() = runTest {
151+
fun stateIsOfflineWhenNetworkMonitorIsOffline() = runTest(UnconfinedTestDispatcher()) {
187152

188153
composeTestRule.setContent {
189154
state = NiaAppState(
190155
windowSizeClass = WindowSizeClass.calculateFromSize(DpSize(900.dp, 1200.dp)),
191156
navController = NavHostController(LocalContext.current),
192157
networkMonitor = networkMonitor,
193-
coroutineScope = testScope
158+
coroutineScope = backgroundScope
194159
)
195160
}
196161

197-
val collectJob = testScope.launch { state.isOffline.collect() }
198-
162+
backgroundScope.launch { state.isOffline.collect() }
199163
networkMonitor.setConnected(false)
200-
201164
assertEquals(
202165
true,
203166
state.isOffline.value
204167
)
205-
206-
collectJob.cancel()
207168
}
208169

209170
private fun getCompactWindowClass() = WindowSizeClass.calculateFromSize(DpSize(500.dp, 300.dp))

app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaApp.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ import com.google.samples.apps.nowinandroid.navigation.TopLevelDestination
7070
@OptIn(
7171
ExperimentalMaterial3Api::class,
7272
ExperimentalLayoutApi::class,
73-
ExperimentalComposeUiApi::class, ExperimentalLifecycleComposeApi::class
73+
ExperimentalComposeUiApi::class,
74+
ExperimentalLifecycleComposeApi::class
7475
)
7576
@Composable
7677
fun NiaApp(
@@ -101,19 +102,18 @@ fun NiaApp(
101102
snackbarHost = { SnackbarHost(snackbarHostState) },
102103
topBar = {
103104
// Show the top app bar on top level destinations.
104-
val topLevelDestination =
105-
appState.topLevelDestinations[appState.currentDestination?.route]
106-
if (topLevelDestination != null) {
105+
val destination = appState.currentTopLevelDestination
106+
if (destination != null) {
107107
NiaTopAppBar(
108-
titleRes = topLevelDestination.titleTextId,
108+
titleRes = destination.titleTextId,
109109
actionIcon = NiaIcons.Settings,
110110
actionIconContentDescription = stringResource(
111111
id = settingsR.string.top_app_bar_action_icon_description
112112
),
113113
colors = TopAppBarDefaults.centerAlignedTopAppBarColors(
114114
containerColor = Color.Transparent
115115
),
116-
onActionClick = { appState.toggleSettingsDialog(true) }
116+
onActionClick = { appState.setShowSettingsDialog(true) }
117117
)
118118
}
119119
},
@@ -141,7 +141,7 @@ fun NiaApp(
141141

142142
if (appState.shouldShowSettingsDialog) {
143143
SettingsDialog(
144-
onDismiss = { appState.toggleSettingsDialog(false) }
144+
onDismiss = { appState.setShowSettingsDialog(false) }
145145
)
146146
}
147147

app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ class NiaAppState(
7676
@Composable get() = navController
7777
.currentBackStackEntryAsState().value?.destination
7878

79-
private var _shouldShowSettingsDialog by mutableStateOf(false)
80-
val shouldShowSettingsDialog
81-
get() = _shouldShowSettingsDialog
79+
val currentTopLevelDestination: TopLevelDestination?
80+
@Composable get() = topLevelDestinations[currentDestination?.route]
81+
82+
var shouldShowSettingsDialog by mutableStateOf(false)
83+
private set
8284

8385
val shouldShowBottomBar: Boolean
8486
get() = windowSizeClass.widthSizeClass == WindowWidthSizeClass.Compact ||
@@ -136,8 +138,8 @@ class NiaAppState(
136138
navController.popBackStack()
137139
}
138140

139-
fun toggleSettingsDialog(shouldShow: Boolean) {
140-
_shouldShowSettingsDialog = shouldShow
141+
fun setShowSettingsDialog(shouldShow: Boolean) {
142+
shouldShowSettingsDialog = shouldShow
141143
}
142144
}
143145

core/designsystem/src/main/java/com/google/samples/apps/nowinandroid/core/designsystem/component/TopAppBar.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ import androidx.compose.runtime.Composable
3232
import androidx.compose.ui.Modifier
3333
import androidx.compose.ui.graphics.vector.ImageVector
3434
import androidx.compose.ui.res.stringResource
35-
import androidx.compose.ui.semantics.contentDescription
3635
import androidx.compose.ui.tooling.preview.Preview
37-
import com.google.samples.apps.nowinandroid.core.designsystem.R
3836

3937
@OptIn(ExperimentalMaterial3Api::class)
4038
@Composable

core/designsystem/src/main/res/values/strings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@
1818
<string name="follow">Follow</string>
1919
<string name="unfollow">Unfollow</string>
2020
<string name="browse_topic">Browse topic</string>
21-
<string name="screen_title">Screen title</string>
2221
</resources>

feature/settings/src/main/java/com/google/samples/apps/nowinandroid/feature/settings/SettingsViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import kotlinx.coroutines.launch
3333

3434
@HiltViewModel
3535
class SettingsViewModel @Inject constructor(
36-
private val userDataRepository: UserDataRepository
36+
private val userDataRepository: UserDataRepository,
3737
) : ViewModel() {
3838
val settingsUiState: StateFlow<SettingsUiState> =
3939
userDataRepository.userDataStream

0 commit comments

Comments
 (0)