Skip to content

Commit

Permalink
Fix navigation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
JackEblan committed Aug 11, 2024
1 parent 85fbf62 commit b0ae884
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 64 deletions.
3 changes: 0 additions & 3 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
Expand All @@ -48,7 +47,6 @@ import kotlinx.coroutines.launch
class MainActivity : ComponentActivity() {
private val viewModel: MainActivityViewModel by viewModels()

@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
val splashScreen = installSplashScreen()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.android.socialworkreviewer.feature.announcement.navigation.navigateToAnnouncementScreen
import com.android.socialworkreviewer.feature.category.navigation.navigateToCategoryScreen
import com.android.socialworkreviewer.feature.home.navigation.HomeDestination
import com.android.socialworkreviewer.feature.home.navigation.HomeRouteData
import com.android.socialworkreviewer.feature.home.navigation.homeScreen
import com.android.socialworkreviewer.feature.question.navigation.navigateToQuestionScreen
Expand All @@ -44,18 +44,29 @@ fun SwrNavHost(modifier: Modifier = Modifier) {

val topAppBarScrollBehavior = enterAlwaysScrollBehavior()

val currentDestination =
homeNavHostController.currentBackStackEntryAsState().value?.destination

val topLevelDestinations = listOf(
CategoryDestination(),
AnnouncementDestination(),
SettingsDestination(),
)

NavHost(
modifier = modifier,
navController = swrNavHostController,
startDestination = HomeRouteData::class,
) {
homeScreen(
currentDestination = currentDestination,
topLevelDestinations = topLevelDestinations,
topAppBarScrollBehavior = topAppBarScrollBehavior,
onItemClick = { homeDestination ->
when (homeDestination) {
HomeDestination.CATEGORY -> homeNavHostController.navigateToCategoryScreen()
HomeDestination.ANNOUNCEMENT -> homeNavHostController.navigateToAnnouncementScreen()
HomeDestination.SETTINGS -> homeNavHostController.navigateToSettings()
is CategoryDestination -> homeNavHostController.navigateToCategoryScreen()
is AnnouncementDestination -> homeNavHostController.navigateToAnnouncementScreen()
is SettingsDestination -> homeNavHostController.navigateToSettings()
}
},
content = { paddingValues ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
*
* Copyright 2023 Einstein Blanco
*
* Licensed under the GNU General Public License v3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.gnu.org/licenses/gpl-3.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.android.socialworkreviewer.navigation

import androidx.compose.ui.graphics.vector.ImageVector
import com.android.socialworkreviewer.R
import com.android.socialworkreviewer.core.designsystem.icon.Swr
import com.android.socialworkreviewer.feature.announcement.navigation.AnnouncementRouteData
import com.android.socialworkreviewer.feature.category.navigation.CategoryRouteData
import com.android.socialworkreviewer.feature.home.navigation.HomeDestination
import com.android.socialworkreviewer.feature.settings.navigation.SettingsRouteData
import kotlin.reflect.KClass

class CategoryDestination : HomeDestination {
override val label: Int
get() = R.string.category
override val icon: ImageVector
get() = Swr.Category
override val contentDescription: Int
get() = R.string.category
override val route: KClass<*>
get() = CategoryRouteData::class
}

class AnnouncementDestination : HomeDestination {
override val label: Int
get() = R.string.announcement
override val icon: ImageVector
get() = Swr.Campaign
override val contentDescription: Int
get() = R.string.announcement
override val route: KClass<*>
get() = AnnouncementRouteData::class
}

class SettingsDestination : HomeDestination {
override val label: Int
get() = R.string.settings
override val icon: ImageVector
get() = Swr.Settings
override val contentDescription: Int
get() = R.string.settings
override val route: KClass<*>
get() = SettingsRouteData::class
}
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@
-->
<resources>
<string name="app_name">Social Work Reviewer</string>
<string name="category">Category</string>
<string name="announcement">Announcement</string>
<string name="settings">Settings</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ class FakeCountDownTimer : CountDownTimerWrapper {
get() = _countDownTimeFlow.asSharedFlow()

override fun setCountDownTimer(millisInFuture: Long, countDownInterval: Long) {
_countDownTimeFlow.tryEmit(
CountDownTime(
minutes = "",
isFinished = false,
),
)
}

override fun start() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,29 @@ class FakeChoiceRepository : ChoiceRepository {
override suspend fun addChoice(choice: Choice) {
_selectedChoices.add(choice)

_questionsWithSelectedChoicesFlow.emit(getQuestionsWithSelectedChoices())
_currentQuestionData.emit(
QuestionData(
selectedChoices = getQuestionsWithSelectedChoices().getOrDefault(
key = choice.question,
defaultValue = emptyList(),
),
questionsWithSelectedChoicesSize = getQuestionsWithSelectedChoices().size,
),
)
}

override suspend fun deleteChoice(choice: Choice) {
_selectedChoices.remove(choice)

_questionsWithSelectedChoicesFlow.emit(getQuestionsWithSelectedChoices())
_currentQuestionData.emit(
QuestionData(
selectedChoices = getQuestionsWithSelectedChoices().getOrDefault(
key = choice.question,
defaultValue = emptyList(),
),
questionsWithSelectedChoicesSize = getQuestionsWithSelectedChoices().size,
),
)
}

override fun clearCache() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ class AnnouncementViewModelTest {

@Test
fun announcementUiState_isSuccess() = runTest {
val collectJob =
launch(UnconfinedTestDispatcher()) { viewModel.announcementUiState.collect() }

val announcements = List(10) { index ->
Announcement(id = "$index", title = "", message = "")
}

val collectJob =
launch(UnconfinedTestDispatcher()) { viewModel.announcementUiState.collect() }

announcementRepository.setAnnouncements(announcements)

assertIs<AnnouncementUiState.Success>(viewModel.announcementUiState.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,32 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteScaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hasRoute
import androidx.navigation.NavDestination.Companion.hierarchy
import com.android.socialworkreviewer.feature.home.navigation.HomeDestination

@OptIn(ExperimentalMaterial3Api::class)
@Composable
internal fun HomeRoute(
currentDestination: NavDestination?,
topLevelDestinations: List<HomeDestination>,
topAppBarScrollBehavior: TopAppBarScrollBehavior,
onItemClick: (HomeDestination) -> Unit,
content: @Composable (PaddingValues) -> Unit,
) {
var currentDestination by rememberSaveable { mutableStateOf(HomeDestination.CATEGORY) }
val topBarTitleStringResource = topLevelDestinations.find { destination ->
currentDestination.isTopLevelDestinationInHierarchy(destination)
}?.label ?: topLevelDestinations.first().label

NavigationSuiteScaffold(
navigationSuiteItems = {
HomeDestination.entries.forEach { destination ->
topLevelDestinations.forEach { destination ->
item(
icon = {
Icon(
Expand All @@ -58,10 +61,8 @@ internal fun HomeRoute(
)
},
label = { Text(stringResource(id = destination.label)) },
selected = destination == currentDestination,
selected = currentDestination.isTopLevelDestinationInHierarchy(destination),
onClick = {
currentDestination = destination

onItemClick(destination)
},
)
Expand All @@ -71,7 +72,7 @@ internal fun HomeRoute(
Scaffold(
topBar = {
HomeLargeTopAppBar(
title = stringResource(id = currentDestination.label),
title = stringResource(id = topBarTitleStringResource),
topAppBarScrollBehavior = topAppBarScrollBehavior,
)
},
Expand Down Expand Up @@ -105,3 +106,8 @@ private fun HomeLargeTopAppBar(
scrollBehavior = topAppBarScrollBehavior,
)
}

private fun NavDestination?.isTopLevelDestinationInHierarchy(destination: HomeDestination) =
this?.hierarchy?.any {
it.hasRoute(destination.route)
} ?: false
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,16 @@ package com.android.socialworkreviewer.feature.home.navigation

import androidx.annotation.StringRes
import androidx.compose.ui.graphics.vector.ImageVector
import com.android.socialworkreviewer.core.designsystem.icon.Swr
import com.android.socialworkreviewer.feature.home.R
import kotlin.reflect.KClass

enum class HomeDestination(
@StringRes val label: Int,
val icon: ImageVector,
@StringRes val contentDescription: Int,
) {
CATEGORY(
R.string.category,
Swr.Category,
R.string.category,
),
ANNOUNCEMENT(
R.string.announcement,
Swr.Campaign,
R.string.announcement,
),
SETTINGS(R.string.settings, Swr.Settings, R.string.category),
interface HomeDestination {
@get:StringRes
val label: Int

val icon: ImageVector

@get:StringRes
val contentDescription: Int

val route: KClass<*>
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,23 @@ import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.runtime.Composable
import androidx.navigation.NavDestination
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import com.android.socialworkreviewer.feature.home.HomeRoute

@OptIn(ExperimentalMaterial3Api::class)
fun NavGraphBuilder.homeScreen(
currentDestination: NavDestination?,
topLevelDestinations: List<HomeDestination>,
topAppBarScrollBehavior: TopAppBarScrollBehavior,
onItemClick: (HomeDestination) -> Unit,
content: @Composable (PaddingValues) -> Unit,
) {
composable<HomeRouteData> {
HomeRoute(
currentDestination = currentDestination,
topLevelDestinations = topLevelDestinations,
topAppBarScrollBehavior = topAppBarScrollBehavior,
onItemClick = onItemClick,
content = content,
Expand Down
22 changes: 0 additions & 22 deletions feature/home/src/main/res/values/strings.xml

This file was deleted.

Loading

0 comments on commit b0ae884

Please sign in to comment.