Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Refactor navigation utils
Browse files Browse the repository at this point in the history
  • Loading branch information
M3DZIK committed Oct 13, 2023
1 parent d664199 commit 3a9402d
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.foundation.isSystemInDarkTheme
import androidx.core.view.WindowCompat
import androidx.fragment.app.FragmentActivity
import androidx.navigation.NavController
import dev.medzik.android.components.navigate
import dev.medzik.librepass.android.data.getRepository
import dev.medzik.librepass.android.ui.LibrePassNavigation
import dev.medzik.librepass.android.ui.Screen
Expand All @@ -17,7 +18,6 @@ import dev.medzik.librepass.android.utils.StoreKey
import dev.medzik.librepass.android.utils.ThemeValues
import dev.medzik.librepass.android.utils.UserSecrets
import dev.medzik.librepass.android.utils.VaultTimeoutValues
import dev.medzik.librepass.android.utils.navigation.navigate

class MainActivity : FragmentActivity() {
lateinit var userSecrets: UserSecrets
Expand Down
89 changes: 41 additions & 48 deletions app/src/main/java/dev/medzik/librepass/android/ui/Screen.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package dev.medzik.librepass.android.ui

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.currentStateAsState
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import dev.medzik.android.components.NavArgument
import dev.medzik.android.components.NavScreen
import dev.medzik.android.components.getString
import dev.medzik.librepass.android.MainActivity
import dev.medzik.librepass.android.data.getRepository
import dev.medzik.librepass.android.ui.screens.PasswordGenerator
import dev.medzik.librepass.android.ui.screens.WelcomeScreen
Expand All @@ -19,15 +28,14 @@ import dev.medzik.librepass.android.ui.screens.dashboard.settings.SettingsAccoun
import dev.medzik.librepass.android.ui.screens.dashboard.settings.SettingsAppearance
import dev.medzik.librepass.android.ui.screens.dashboard.settings.SettingsSecurity
import dev.medzik.librepass.android.utils.SecretStore.getUserSecrets
import dev.medzik.librepass.android.utils.navigation.getString
import dev.medzik.librepass.types.cipher.Cipher
import java.util.UUID

enum class Argument {
enum class Argument : NavArgument {
CipherId
}

enum class Screen(private val argument: Argument? = null) {
enum class Screen(override val args: Array<NavArgument>? = null) : NavScreen {
Welcome,

Register,
Expand All @@ -36,29 +44,14 @@ enum class Screen(private val argument: Argument? = null) {

Unlock,
Dashboard,
CipherView(Argument.CipherId),
CipherView(arrayOf(Argument.CipherId)),
CipherAdd,
CipherEdit(Argument.CipherId),
CipherEdit(arrayOf(Argument.CipherId)),
PasswordGenerator,

SettingsAppearance,
SettingsSecurity,
SettingsAccount;

val route = if (argument != null) "$name/{${argument.name}}" else name

fun fill(argumentPair: Pair<Argument, String>? = null): String {
val arg = argumentPair?.first
val value = argumentPair?.second

if (arg != argument)
throw IllegalArgumentException("Invalid arguments. Expected ${this.argument?.name}, got $arg")

return when (argumentPair) {
null -> route
else -> route.replace("{${arg!!.name}}", value!!)
}
}
SettingsAccount
}

@Composable
Expand All @@ -67,71 +60,71 @@ fun LibrePassNavigation() {

val navController = rememberNavController()

// val lifecycleOwner = LocalLifecycleOwner.current
// val lifecycleState by lifecycleOwner.lifecycle.currentStateAsState()
// LaunchedEffect(lifecycleState) {
// when (lifecycleState) {
// Lifecycle.State.RESUMED -> {
// (context as MainActivity).onResume(navController)
// }
//
// else -> {}
// }
// }
val lifecycleOwner = LocalLifecycleOwner.current
val lifecycleState by lifecycleOwner.lifecycle.currentStateAsState()
LaunchedEffect(lifecycleState) {
when (lifecycleState) {
Lifecycle.State.RESUMED -> {
(context as MainActivity).onResume(navController)
}

else -> {}
}
}

val repository = context.getRepository()
val userSecrets = context.getUserSecrets()

fun getStartRoute(): String {
// if a user is not logged in, show welcome screen
repository.credentials.get()
?: return Screen.Welcome.route
?: return Screen.Welcome.getRoute()

// if user secrets are not set, show unlock screen
userSecrets
?: return Screen.Unlock.route
?: return Screen.Unlock.getRoute()

// else where the user secrets are set, show dashboard screen
return Screen.Dashboard.route
return Screen.Dashboard.getRoute()
}

NavHost(
navController = navController,
startDestination = getStartRoute()
) {
composable(Screen.Welcome.route) {
composable(Screen.Welcome.getRoute()) {
WelcomeScreen(navController)
}

composable(Screen.Register.route) {
composable(Screen.Register.getRoute()) {
RegisterScreen(navController)
}

composable(Screen.Login.route) {
composable(Screen.Login.getRoute()) {
LoginScreen(navController)
}

composable(Screen.AddCustomServer.route) {
composable(Screen.AddCustomServer.getRoute()) {
AddCustomServer(navController)
}

composable(Screen.Unlock.route) {
composable(Screen.Unlock.getRoute()) {
UnlockScreen(navController)
}

composable(Screen.Dashboard.route) {
composable(Screen.Dashboard.getRoute()) {
DashboardNavigation(mainNavController = navController)
}

composable(Screen.CipherView.route) {
composable(Screen.CipherView.getRoute()) {
CipherViewScreen(navController = navController)
}

composable(Screen.CipherAdd.route) {
composable(Screen.CipherAdd.getRoute()) {
CipherAddEditView(navController = navController)
}

composable(Screen.CipherEdit.route) {
composable(Screen.CipherEdit.getRoute()) {
val cipherId = navController.getString(Argument.CipherId)
?: return@composable

Expand All @@ -143,19 +136,19 @@ fun LibrePassNavigation() {
CipherAddEditView(navController = navController, baseCipher = cipher)
}

composable(Screen.PasswordGenerator.route) {
composable(Screen.PasswordGenerator.getRoute()) {
PasswordGenerator(navController)
}

composable(Screen.SettingsAppearance.route) {
composable(Screen.SettingsAppearance.getRoute()) {
SettingsAppearance(navController)
}

composable(Screen.SettingsSecurity.route) {
composable(Screen.SettingsSecurity.getRoute()) {
SettingsSecurity(navController)
}

composable(Screen.SettingsAccount.route) {
composable(Screen.SettingsAccount.getRoute()) {
SettingsAccount(navController)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.google.accompanist.drawablepainter.DrawablePainter
import dev.medzik.android.components.navigate
import dev.medzik.librepass.android.R
import dev.medzik.librepass.android.ui.Screen
import dev.medzik.librepass.android.utils.TopBarTwoColor
import dev.medzik.librepass.android.utils.navigation.navigate

@Composable
fun WelcomeScreen(navController: NavController) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import dev.medzik.android.components.LoadingButton
import dev.medzik.android.components.PickerDialog
import dev.medzik.android.components.navigate
import dev.medzik.android.components.rememberDialogState
import dev.medzik.libcrypto.Hex
import dev.medzik.librepass.android.R
Expand All @@ -35,7 +36,6 @@ import dev.medzik.librepass.android.utils.TopBar
import dev.medzik.librepass.android.utils.TopBarBackIcon
import dev.medzik.librepass.android.utils.UserSecrets
import dev.medzik.librepass.android.utils.exception.handle
import dev.medzik.librepass.android.utils.navigation.navigate
import dev.medzik.librepass.android.utils.rememberLoadingState
import dev.medzik.librepass.android.utils.rememberStringData
import dev.medzik.librepass.android.utils.showToast
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import dev.medzik.android.components.LoadingButton
import dev.medzik.android.components.PickerDialog
import dev.medzik.android.components.navigate
import dev.medzik.android.components.rememberDialogState
import dev.medzik.librepass.android.R
import dev.medzik.librepass.android.ui.Screen
Expand All @@ -30,7 +31,6 @@ import dev.medzik.librepass.android.utils.TextInputField
import dev.medzik.librepass.android.utils.TopBar
import dev.medzik.librepass.android.utils.TopBarBackIcon
import dev.medzik.librepass.android.utils.exception.handle
import dev.medzik.librepass.android.utils.navigation.navigate
import dev.medzik.librepass.android.utils.rememberLoadingState
import dev.medzik.librepass.android.utils.rememberStringData
import dev.medzik.librepass.android.utils.showToast
Expand Down Expand Up @@ -69,10 +69,7 @@ fun RegisterScreen(navController: NavController) {

navController.navigate(
screen = Screen.Login,
options = {
// disable back to this page
popUpTo(Screen.Register.route) { inclusive = true }
}
disableBack = true
)
}
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.ui.unit.dp
import androidx.fragment.app.FragmentActivity
import androidx.navigation.NavController
import dev.medzik.android.components.LoadingButton
import dev.medzik.android.components.navigate
import dev.medzik.android.crypto.KeyStore
import dev.medzik.libcrypto.Argon2
import dev.medzik.libcrypto.Hex
Expand All @@ -34,7 +35,6 @@ import dev.medzik.librepass.android.utils.TextInputField
import dev.medzik.librepass.android.utils.TopBar
import dev.medzik.librepass.android.utils.UserSecrets
import dev.medzik.librepass.android.utils.exception.EncryptException
import dev.medzik.librepass.android.utils.navigation.navigate
import dev.medzik.librepass.android.utils.rememberLoadingState
import dev.medzik.librepass.android.utils.rememberStringData
import dev.medzik.librepass.android.utils.showToast
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.navigation.NavController
import com.google.gson.Gson
import dev.medzik.android.components.LoadingButton
import dev.medzik.android.components.PreferenceGroupTitle
import dev.medzik.android.components.navigate
import dev.medzik.librepass.android.R
import dev.medzik.librepass.android.data.CipherTable
import dev.medzik.librepass.android.data.getRepository
Expand All @@ -37,7 +38,6 @@ import dev.medzik.librepass.android.utils.TextInputFieldBase
import dev.medzik.librepass.android.utils.TopBar
import dev.medzik.librepass.android.utils.TopBarBackIcon
import dev.medzik.librepass.android.utils.exception.handle
import dev.medzik.librepass.android.utils.navigation.navigate
import dev.medzik.librepass.android.utils.rememberLoadingState
import dev.medzik.librepass.android.utils.shorten
import dev.medzik.librepass.client.Server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import dev.medzik.android.components.BaseDialog
import dev.medzik.android.components.PreferenceGroupTitle
import dev.medzik.android.components.getString
import dev.medzik.android.components.navigate
import dev.medzik.android.components.rememberDialogState
import dev.medzik.librepass.android.R
import dev.medzik.librepass.android.data.getRepository
Expand All @@ -42,8 +44,6 @@ import dev.medzik.librepass.android.utils.SHORTEN_NAME_LENGTH
import dev.medzik.librepass.android.utils.SecretStore.getUserSecrets
import dev.medzik.librepass.android.utils.TopBar
import dev.medzik.librepass.android.utils.TopBarBackIcon
import dev.medzik.librepass.android.utils.navigation.getString
import dev.medzik.librepass.android.utils.navigation.navigate
import dev.medzik.librepass.android.utils.shorten
import dev.medzik.librepass.types.cipher.Cipher
import java.text.SimpleDateFormat
Expand Down Expand Up @@ -80,7 +80,7 @@ fun CipherViewScreen(navController: NavController) {
FloatingActionButton(onClick = {
navController.navigate(
screen = Screen.CipherEdit,
argument = Argument.CipherId to cipherId
args = arrayOf(Argument.CipherId to cipherId)
)
}) {
Icon(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import dev.medzik.android.components.ElevationTokens
import dev.medzik.android.components.navigate
import dev.medzik.librepass.android.R
import dev.medzik.librepass.android.ui.Screen
import dev.medzik.librepass.android.ui.screens.dashboard.settings.SettingsScreen
import dev.medzik.librepass.android.utils.TopBar
import dev.medzik.librepass.android.utils.navigation.navigate

enum class DashboardNavigationItem(val route: String, val icon: ImageVector, val titleId: Int) {
Dashboard("dashboard", Icons.Default.Lock, R.string.DashboardBottomNav_Dashboard),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.NavController
import dev.medzik.android.components.navigate
import dev.medzik.librepass.android.data.CipherTable
import dev.medzik.librepass.android.data.getRepository
import dev.medzik.librepass.android.ui.Argument
import dev.medzik.librepass.android.ui.Screen
import dev.medzik.librepass.android.ui.composables.CipherCard
import dev.medzik.librepass.android.utils.SecretStore.getUserSecrets
import dev.medzik.librepass.android.utils.exception.handle
import dev.medzik.librepass.android.utils.navigation.navigate
import dev.medzik.librepass.android.utils.rememberLoadingState
import dev.medzik.librepass.client.Server
import dev.medzik.librepass.client.api.CipherClient
Expand Down Expand Up @@ -159,13 +159,13 @@ fun DashboardScreen(navController: NavController) {
onClick = { cipher ->
navController.navigate(
screen = Screen.CipherView,
argument = Argument.CipherId to cipher.id.toString()
args = arrayOf(Argument.CipherId to cipher.id.toString())
)
},
onEdit = { cipher ->
navController.navigate(
screen = Screen.CipherEdit,
argument = Argument.CipherId to cipher.id.toString()
args = arrayOf(Argument.CipherId to cipher.id.toString())
)
},
onDelete = { cipher ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavController
import dev.medzik.android.components.PreferenceEntry
import dev.medzik.android.components.navigate
import dev.medzik.librepass.android.R
import dev.medzik.librepass.android.ui.Screen
import dev.medzik.librepass.android.utils.navigation.navigate

@Composable
fun SettingsScreen(navController: NavController) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavController
import dev.medzik.android.components.PreferenceEntry
import dev.medzik.android.components.navigate
import dev.medzik.librepass.android.R
import dev.medzik.librepass.android.data.getRepository
import dev.medzik.librepass.android.ui.Screen
import dev.medzik.librepass.android.utils.TopBar
import dev.medzik.librepass.android.utils.TopBarBackIcon
import dev.medzik.librepass.android.utils.navigation.navigate
import kotlinx.coroutines.runBlocking

@Composable
Expand Down
Loading

0 comments on commit 3a9402d

Please sign in to comment.