Skip to content

Commit

Permalink
Migrated from navigating with parameters to type-safe navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
imrannextgeni021 committed Oct 10, 2024
1 parent 7e2fab5 commit 761869e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 80 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ dependencies {
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.7.7"
implementation "androidx.navigation:navigation-compose:2.8.2"
// Jetpack compose.
implementation "androidx.compose.ui:ui"
implementation "androidx.compose.ui:ui-tooling-preview"
Expand Down
49 changes: 14 additions & 35 deletions app/src/main/java/com/starry/greenstash/ui/navigation/NavGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import androidx.navigation.toRoute
import com.starry.greenstash.ui.screens.archive.composables.ArchiveScreen
import com.starry.greenstash.ui.screens.backups.composables.BackupScreen
import com.starry.greenstash.ui.screens.dwscreen.composables.DWScreen
Expand Down Expand Up @@ -80,60 +79,40 @@ fun NavGraph(
}

/** Deposit Withdraw Screen */
composable(
route = Screens.DWScreen.route,
composable<DWScreen>(
enterTransition = { enterTransition() },
exitTransition = { exitTransition() },
popEnterTransition = { popEnterTransition() },
popExitTransition = { popExitTransition() },
arguments = listOf(
navArgument(DW_GOAL_ID_ARG_KEY) {
type = NavType.StringType
},
),
popExitTransition = { popExitTransition() }
) { backStackEntry ->
val goalId = backStackEntry.arguments!!.getString(DW_GOAL_ID_ARG_KEY)!!
val transactionType =
backStackEntry.arguments!!.getString(DW_TRANSACTION_TYPE_ARG_KEY)!!
val args = backStackEntry.toRoute<DWScreen>()
DWScreen(
goalId = goalId,
transactionTypeName = transactionType,
goalId = args.goalId,
transactionTypeName = args.transactionType,
navController = navController
)
}

/** Goal Info Screen */
composable(
route = Screens.GoalInfoScreen.route,
composable<GoalInfoScreen>(
enterTransition = { enterTransition() },
exitTransition = { exitTransition() },
popEnterTransition = { popEnterTransition() },
popExitTransition = { popExitTransition() },
arguments = listOf(
navArgument(GOAL_INFO_ARG_KEY) {
type = NavType.StringType
},
),
popExitTransition = { popExitTransition() }
) { backStackEntry ->
val goalId = backStackEntry.arguments!!.getString(GOAL_INFO_ARG_KEY)!!
GoalInfoScreen(goalId = goalId, navController = navController)
val args = backStackEntry.toRoute<GoalInfoScreen>()
GoalInfoScreen(goalId = args.goalId, navController = navController)
}

/** Input Screen */
composable(
route = Screens.InputScreen.route,
composable<InputScreen>(
enterTransition = { enterTransition() },
exitTransition = { exitTransition() },
popEnterTransition = { popEnterTransition() },
popExitTransition = { popExitTransition() },
arguments = listOf(navArgument(EDIT_GOAL_ARG_KEY) {
nullable = true
defaultValue = null
type = NavType.StringType
})
popExitTransition = { popExitTransition() }
) { backStackEntry ->
val editGoalId = backStackEntry.arguments!!.getString(EDIT_GOAL_ARG_KEY)
InputScreen(editGoalId = editGoalId, navController = navController)
val args = backStackEntry.toRoute<InputScreen>()
InputScreen(editGoalId = args.goalId, navController = navController)
}

/** Goal Achieved Screen */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.starry.greenstash.ui.navigation

import kotlinx.serialization.Serializable




@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)



34 changes: 5 additions & 29 deletions app/src/main/java/com/starry/greenstash/ui/navigation/Screens.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,21 @@

package com.starry.greenstash.ui.navigation

const val DW_GOAL_ID_ARG_KEY = "dwGoal"
const val DW_TRANSACTION_TYPE_ARG_KEY = "dwTransactionType"
const val EDIT_GOAL_ARG_KEY = "editGoal"
const val GOAL_INFO_ARG_KEY = "goalId"

sealed class Screens(val route: String) {
// Settings Screens

// Deposit / Withdraw Screens
data object GoalCardStyle : Screens("goal_card_style")

data object DWScreen :
Screens("deposit_withdraw_screen/{$DW_GOAL_ID_ARG_KEY}/{$DW_TRANSACTION_TYPE_ARG_KEY}") {
fun withGoalId(goalId: String, trasactionType: String): String {
return route.replace("{$DW_GOAL_ID_ARG_KEY}", goalId)
.replace("{$DW_TRANSACTION_TYPE_ARG_KEY}", trasactionType)
}
}

// New Goal / Edit Goal Screen
data object InputScreen : Screens("input_screen?$EDIT_GOAL_ARG_KEY={$EDIT_GOAL_ARG_KEY}") {
fun withGoalToEdit(goalId: String): String {
return route.replace("{$EDIT_GOAL_ARG_KEY}", goalId)
}
}
data object AboutScreen : Screens("about_screen")

// Goal Info Screen
data object GoalInfoScreen : Screens("goal_info_screen/{$GOAL_INFO_ARG_KEY}") {
fun withGoalId(goalId: String): String {
return route.replace("{$GOAL_INFO_ARG_KEY}", goalId)
}
}

// Settings Screens
data object GoalCardStyle : Screens("goal_card_style")
data object AboutScreen : Screens("about_screen")
data object OSLScreen : Screens("osl_screen")

// Goal Achieved Screen

data object CongratsScreen : Screens("goal_achieved_screen")

// Welcome / Onboarding Screen
data object WelcomeScreen : Screens("welcome_screen")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ 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.Screens
import com.starry.greenstash.ui.screens.home.GoalCardStyle
import com.starry.greenstash.ui.screens.home.HomeViewModel
Expand Down Expand Up @@ -107,9 +110,9 @@ fun GoalLazyColumnItem(
}
} else {
navController.navigate(
Screens.DWScreen.withGoalId(
DWScreen(
goalId = item.goal.goalId.toString(),
trasactionType = TransactionType.Deposit.name
transactionType = TransactionType.Deposit.name
)
)
}
Expand All @@ -122,25 +125,25 @@ fun GoalLazyColumnItem(
}
} else {
navController.navigate(
Screens.DWScreen.withGoalId(
DWScreen(
goalId = item.goal.goalId.toString(),
trasactionType = TransactionType.Withdraw.name
transactionType = TransactionType.Withdraw.name
)
)
}
},
onInfoClicked = {
localView.weakHapticFeedback()
navController.navigate(
Screens.GoalInfoScreen.withGoalId(
GoalInfoScreen(
goalId = item.goal.goalId.toString()
)
)
},
onEditClicked = {
localView.weakHapticFeedback()
navController.navigate(
Screens.InputScreen.withGoalToEdit(
InputScreen(
goalId = item.goal.goalId.toString()
)
)
Expand Down Expand Up @@ -191,9 +194,9 @@ fun GoalLazyColumnItem(
}
} else {
navController.navigate(
Screens.DWScreen.withGoalId(
DWScreen(
goalId = item.goal.goalId.toString(),
trasactionType = TransactionType.Deposit.name
transactionType = TransactionType.Deposit.name
)
)
}
Expand All @@ -206,25 +209,25 @@ fun GoalLazyColumnItem(
}
} else {
navController.navigate(
Screens.DWScreen.withGoalId(
DWScreen(
goalId = item.goal.goalId.toString(),
trasactionType = TransactionType.Withdraw.name
transactionType = TransactionType.Withdraw.name
)
)
}
},
onInfoClicked = {
localView.weakHapticFeedback()
navController.navigate(
Screens.GoalInfoScreen.withGoalId(
GoalInfoScreen(
goalId = item.goal.goalId.toString()
)
)
},
onEditClicked = {
localView.weakHapticFeedback()
navController.navigate(
Screens.InputScreen.withGoalToEdit(
InputScreen(
goalId = item.goal.goalId.toString()
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +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.Screens
import com.starry.greenstash.ui.screens.home.FilterField
import com.starry.greenstash.ui.screens.home.FilterSortType
Expand Down Expand Up @@ -418,7 +419,7 @@ private fun HomeExtendedFAB(
modifier = modifier.padding(end = 10.dp, bottom = 12.dp),
onClick = {
view.weakHapticFeedback()
navController.navigate(Screens.InputScreen.route)
navController.navigate(InputScreen())
},
elevation = FloatingActionButtonDefaults.elevation(8.dp)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.starry.greenstash.MainActivity
import com.starry.greenstash.MainViewModel
import com.starry.greenstash.ui.navigation.GoalInfoScreen
import com.starry.greenstash.ui.navigation.InputScreen
import com.starry.greenstash.ui.navigation.NavGraph
import com.starry.greenstash.ui.navigation.Screens
import com.starry.greenstash.ui.screens.other.AppLockedScreen
Expand Down Expand Up @@ -98,11 +100,11 @@ private fun HandleShortcutIntent(intent: Intent, navController: NavController) {
if (data != null && data.scheme == MainViewModel.LAUNCHER_SHORTCUT_SCHEME) {
val goalId = intent.getLongExtra(MainViewModel.LC_SHORTCUT_GOAL_ID, -100)
if (goalId != -100L) {
navController.navigate(Screens.GoalInfoScreen.withGoalId(goalId.toString()))
navController.navigate(GoalInfoScreen(goalId.toString()))
return
}
if (intent.getBooleanExtra(MainViewModel.LC_SHORTCUT_NEW_GOAL, false)) {
navController.navigate(Screens.InputScreen.route)
navController.navigate(InputScreen)
}
}
}

0 comments on commit 761869e

Please sign in to comment.