Skip to content

Commit

Permalink
Removed Any from NavGraph and implemented type safety and static type…
Browse files Browse the repository at this point in the history
… checking at compile time.

Removed Gradle migration to type-safe navigation comment line.

Added back Header Comment in NormalScreens file.
  • Loading branch information
imrannextgeni021 committed Oct 14, 2024
1 parent 25d5a26 commit 1538546
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 85 deletions.
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ dependencies {
implementation 'androidx.activity:activity-compose:1.9.2'
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.8.6"
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.8.6"
// TODO: Needs migration to type-safe navigation in v2.8.x
implementation "androidx.navigation:navigation-compose:2.8.2"
// Jetpack compose.
implementation "androidx.compose.ui:ui"
Expand Down
11 changes: 6 additions & 5 deletions app/src/main/java/com/starry/greenstash/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ import androidx.lifecycle.viewModelScope
import com.starry.greenstash.database.goal.GoalDao
import com.starry.greenstash.other.WelcomeDataStore
import com.starry.greenstash.reminder.ReminderManager
import com.starry.greenstash.ui.navigation.NormalScreens
import com.starry.greenstash.ui.navigation.DrawerScreens
import com.starry.greenstash.ui.navigation.WelcomeScreen
import com.starry.greenstash.ui.navigation.Screen
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
Expand All @@ -66,9 +67,9 @@ class MainViewModel @Inject constructor(
private val _isLoading: MutableState<Boolean> = mutableStateOf(true)
val isLoading: State<Boolean> = _isLoading

private val _startDestination: MutableState<Any> =
mutableStateOf(WelcomeScreen)
val startDestination: State<Any> = _startDestination
private val _startDestination: MutableState<Screen> =
mutableStateOf(NormalScreens.WelcomeScreen)
val startDestination: State<Screen> = _startDestination

companion object {
// Must be same as the one in AndroidManifest.xml
Expand All @@ -87,7 +88,7 @@ class MainViewModel @Inject constructor(
if (completed) {
_startDestination.value = DrawerScreens.Home
} else {
_startDestination.value = WelcomeScreen
_startDestination.value = NormalScreens.WelcomeScreen
}

delay(120)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ import com.starry.greenstash.R
import kotlinx.serialization.Serializable

@Serializable
sealed class DrawerScreens( val nameResId: Int, val iconResId: Int) {
sealed class DrawerScreens(val nameResId: Int, val iconResId: Int) : Screen() {

companion object {
fun getAllItems() = listOf(Home, Archive, Backups, Settings)
}

@Serializable
data object Home : DrawerScreens( R.string.drawer_home, R.drawable.ic_nav_home)
data object Home : DrawerScreens(R.string.drawer_home, R.drawable.ic_nav_home)

@Serializable
data object Archive :
DrawerScreens( R.string.drawer_archive, R.drawable.ic_nav_archive)
DrawerScreens(R.string.drawer_archive, R.drawable.ic_nav_archive)

@Serializable
data object Backups :
DrawerScreens( R.string.drawer_backup, R.drawable.ic_nav_backups)
DrawerScreens(R.string.drawer_backup, R.drawable.ic_nav_backups)

@Serializable
data object Settings :
DrawerScreens( R.string.drawer_settings, R.drawable.ic_nav_settings)
DrawerScreens(R.string.drawer_settings, R.drawable.ic_nav_settings)
}


24 changes: 12 additions & 12 deletions app/src/main/java/com/starry/greenstash/ui/navigation/NavGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import com.starry.greenstash.ui.screens.welcome.composables.WelcomeScreen
@Composable
fun NavGraph(
navController: NavHostController,
startDestination: Any
startDestination: Screen
) {

NavHost(
Expand All @@ -60,7 +60,7 @@ fun NavGraph(

) {
/** Welcome Screen */
composable<WelcomeScreen>(
composable<NormalScreens.WelcomeScreen>(
exitTransition = { exitTransition() },
popEnterTransition = { popEnterTransition() },
) {
Expand All @@ -78,13 +78,13 @@ fun NavGraph(
}

/** Deposit Withdraw Screen */
composable<DWScreen>(
composable<NormalScreens.DWScreen>(
enterTransition = { enterTransition() },
exitTransition = { exitTransition() },
popEnterTransition = { popEnterTransition() },
popExitTransition = { popExitTransition() }
) { backStackEntry ->
val args = backStackEntry.toRoute<DWScreen>()
val args = backStackEntry.toRoute<NormalScreens.DWScreen>()
DWScreen(
goalId = args.goalId,
transactionTypeName = args.transactionType,
Expand All @@ -93,29 +93,29 @@ fun NavGraph(
}

/** Goal Info Screen */
composable<GoalInfoScreen>(
composable<NormalScreens.GoalInfoScreen>(
enterTransition = { enterTransition() },
exitTransition = { exitTransition() },
popEnterTransition = { popEnterTransition() },
popExitTransition = { popExitTransition() }
) { backStackEntry ->
val args = backStackEntry.toRoute<GoalInfoScreen>()
val args = backStackEntry.toRoute<NormalScreens.GoalInfoScreen>()
GoalInfoScreen(goalId = args.goalId, navController = navController)
}

/** Input Screen */
composable<InputScreen>(
composable<NormalScreens.InputScreen>(
enterTransition = { enterTransition() },
exitTransition = { exitTransition() },
popEnterTransition = { popEnterTransition() },
popExitTransition = { popExitTransition() }
) { backStackEntry ->
val args = backStackEntry.toRoute<InputScreen>()
val args = backStackEntry.toRoute<NormalScreens.InputScreen>()
InputScreen(editGoalId = args.goalId, navController = navController)
}

/** Goal Achieved Screen */
composable<CongratsScreen>(
composable<NormalScreens.CongratsScreen>(
enterTransition = { enterTransition() },
exitTransition = { exitTransition() },
popEnterTransition = { popEnterTransition() },
Expand Down Expand Up @@ -155,23 +155,23 @@ fun NavGraph(
}

/** Goal Ui Settings Screen */
composable<GoalCardStyleScreen>(
composable<NormalScreens.GoalCardStyleScreen>(
enterTransition = { enterTransition() },
popExitTransition = { popExitTransition() },
) {
GoalCardStyle(navController = navController)
}

/** Open Source Licenses Screen */
composable<OSLScreen>(
composable<NormalScreens.OSLScreen>(
enterTransition = { enterTransition() },
popExitTransition = { popExitTransition() },
) {
OSLScreen(navController = navController)
}

/** About Screen */
composable<AboutScreen>(
composable<NormalScreens.AboutScreen>(
enterTransition = { enterTransition() },
popExitTransition = { popExitTransition() },
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* MIT License
*
* Copyright (c) [2022 - Present] Stɑrry Shivɑm
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/



package com.starry.greenstash.ui.navigation

import kotlinx.serialization.Serializable

sealed class NormalScreens : Screen() {

@Serializable
data class DWScreen(val goalId: String, val transactionType: String)

@Serializable
data class InputScreen(val goalId: String? = null)

@Serializable
data class GoalInfoScreen(val goalId: String)

@Serializable
data object AboutScreen

@Serializable
data object OSLScreen

@Serializable
data object GoalCardStyleScreen


// Goal Achieved Screen
@Serializable
data object CongratsScreen

// Welcome / Onboarding Screen
@Serializable
data object WelcomeScreen : NormalScreens()
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.starry.greenstash.ui.navigation


open class Screen
36 changes: 0 additions & 36 deletions app/src/main/java/com/starry/greenstash/ui/navigation/Screens.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ import com.maxkeppeler.sheets.date_time.models.DateTimeSelection
import com.starry.greenstash.R
import com.starry.greenstash.database.transaction.TransactionType
import com.starry.greenstash.ui.common.DateTimeCard
import com.starry.greenstash.ui.navigation.CongratsScreen
import com.starry.greenstash.ui.navigation.NormalScreens
import com.starry.greenstash.ui.navigation.DrawerScreens
import com.starry.greenstash.ui.screens.dwscreen.DWViewModel
import com.starry.greenstash.ui.theme.greenstashFont
Expand Down Expand Up @@ -208,7 +208,7 @@ fun DWScreen(goalId: String, transactionTypeName: String, navController: NavCont
showTransactionAddedAnim.value = true
delay(1100)
withContext(Dispatchers.Main) {
navController.navigate(CongratsScreen)
navController.navigate(NormalScreens.CongratsScreen)
}
}
}, onComplete = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ import com.starry.greenstash.MainActivity
import com.starry.greenstash.R
import com.starry.greenstash.database.core.GoalWithTransactions
import com.starry.greenstash.database.transaction.TransactionType
import com.starry.greenstash.ui.navigation.DWScreen
import com.starry.greenstash.ui.navigation.GoalInfoScreen
import com.starry.greenstash.ui.navigation.InputScreen
import com.starry.greenstash.ui.navigation.NormalScreens
import com.starry.greenstash.ui.screens.home.GoalCardStyle
import com.starry.greenstash.ui.screens.home.HomeViewModel
import com.starry.greenstash.utils.Constants
Expand Down Expand Up @@ -109,7 +107,7 @@ fun GoalLazyColumnItem(
}
} else {
navController.navigate(
DWScreen(
NormalScreens.DWScreen(
goalId = item.goal.goalId.toString(),
transactionType = TransactionType.Deposit.name
)
Expand All @@ -124,7 +122,7 @@ fun GoalLazyColumnItem(
}
} else {
navController.navigate(
DWScreen(
NormalScreens.DWScreen(
goalId = item.goal.goalId.toString(),
transactionType = TransactionType.Withdraw.name
)
Expand All @@ -134,15 +132,15 @@ fun GoalLazyColumnItem(
onInfoClicked = {
localView.weakHapticFeedback()
navController.navigate(
GoalInfoScreen(
NormalScreens.GoalInfoScreen(
goalId = item.goal.goalId.toString()
)
)
},
onEditClicked = {
localView.weakHapticFeedback()
navController.navigate(
InputScreen(
NormalScreens.InputScreen(
goalId = item.goal.goalId.toString()
)
)
Expand Down Expand Up @@ -193,7 +191,7 @@ fun GoalLazyColumnItem(
}
} else {
navController.navigate(
DWScreen(
NormalScreens.DWScreen(
goalId = item.goal.goalId.toString(),
transactionType = TransactionType.Deposit.name
)
Expand All @@ -208,7 +206,7 @@ fun GoalLazyColumnItem(
}
} else {
navController.navigate(
DWScreen(
NormalScreens.DWScreen(
goalId = item.goal.goalId.toString(),
transactionType = TransactionType.Withdraw.name
)
Expand All @@ -218,15 +216,15 @@ fun GoalLazyColumnItem(
onInfoClicked = {
localView.weakHapticFeedback()
navController.navigate(
GoalInfoScreen(
NormalScreens.GoalInfoScreen(
goalId = item.goal.goalId.toString()
)
)
},
onEditClicked = {
localView.weakHapticFeedback()
navController.navigate(
InputScreen(
NormalScreens.InputScreen(
goalId = item.goal.goalId.toString()
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ import com.psoffritti.taptargetcompose.TextDefinition
import com.starry.greenstash.MainActivity
import com.starry.greenstash.R
import com.starry.greenstash.database.core.GoalWithTransactions
import com.starry.greenstash.ui.navigation.InputScreen
import com.starry.greenstash.ui.navigation.NormalScreens
import com.starry.greenstash.ui.screens.home.FilterField
import com.starry.greenstash.ui.screens.home.FilterSortType
import com.starry.greenstash.ui.screens.home.HomeViewModel
Expand Down Expand Up @@ -418,7 +418,7 @@ private fun HomeExtendedFAB(
modifier = modifier.padding(end = 10.dp, bottom = 12.dp),
onClick = {
view.weakHapticFeedback()
navController.navigate(InputScreen())
navController.navigate(NormalScreens.InputScreen())
},
elevation = FloatingActionButtonDefaults.elevation(8.dp)
) {
Expand Down
Loading

0 comments on commit 1538546

Please sign in to comment.