Issue
Hello i would like to know How to skip the loginScreen and the passwordScreen and move to homeScreen after launching the app because i have Stored the username and password in Datastore, So how can i do that?
Here is my code:
MainActivity.kt
setContent {
EEETheme {
// A surface container using the 'background'
// color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
Nav()
}
}
}
Screen Class
sealed class Screen(val route: String) {
object LoginScreen: Screen(route = "LoginPage_route")
object PasswordScreen: Screen(route = "PasswordPage_route")
}
Navigation.kt
@Composable
fun Nav() {
val context = LocalContext.current
val navController = rememberNavController()
NavHost(
navController = navController,
startDestination = Screen.LoginScreen.route
) {
composable(route = Screen.LoginScreen.route) {
LoginScreen(navController)
}
composable(route = Screen.PasswordScreen.route) {
PasswordScreen(navController)
}
composable(route = Screen.Home.route) {
Screen_A_with_WithTopBar(navController)
}
}
LoginScreen.kt
@Composable
fun LoginScreen(navController: NavController) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = Modifier
.fillMaxSize()
) {
val context = LocalContext.current
var username by remember { mutableStateOf("") }
Text(text = "Login", fontSize = 33.sp)
Spacer(modifier = Modifier.height(20.dp))
OutlinedTextField(
textStyle = TextStyle(fontSize = 24.sp),
value = username,
onValueChange = { username = it },
label = { Text("Enter Your username") },
maxLines = 1
)
Button(onClick = {
}) {
Text(text = "Submit", fontSize = 33.sp)
}
}
}
PasswordScreen.kt
@Composable
fun PasswordLogin(navController: NavController) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = Modifier
.fillMaxSize()
) {
val context = LocalContext.current
var username by remember { mutableStateOf("") }
Text(text = "Login", fontSize = 33.sp)
Spacer(modifier = Modifier.height(20.dp))
OutlinedTextField(
textStyle = TextStyle(fontSize = 24.sp),
value = username,
onValueChange = { username = it },
label = { Text("Enter Your username") },
maxLines = 1
)
Button(onClick = {
}) {
Text(text = "Submit", fontSize = 33.sp)
}
}
}
HomeScreen.kt
@Composable
fun HomeScreen(navController: NavController) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = Modifier
.fillMaxSize()
) {
Text(text = "Home Screen", fontSize = 33.sp)
}
}
DataStore Class
class StoreUserName(private val context: Context) {
// to make sure there's only one instance
companion object {
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore("authentication")
val USERNAME_KEY = stringPreferencesKey("username")
val PASSWORD_KEY = stringPreferencesKey("password")
}
//get the saved CompanyID
val getUsername: Flow<String?> = context.dataStore.data
.map { preferences ->
preferences[USERNAME_KEY] ?: ""
}
//get the saved LoginID
val getPassword: Flow<String?> = context.dataStore.data
.map { preferences ->
preferences[PASSWORD_KEY] ?: ""
}
//save username into datastore
suspend fun saveUsername(username: String) {
context.dataStore.edit { preferences ->
preferences[USERNAME_KEY] = username
}
}
//save Password into datastore
suspend fun savePassword(password: String) {
context.dataStore.edit { preferences ->
preferences[PASSWORD_KEY] = password
}
}
}
Solution
In your fun Nav()
you should check if you have credentials stored and then based on that you should set startDestination
for example:
@Composable
fun Nav() {
val context = LocalContext.current
val navController = rememberNavController()
val isLoginRequired = //you condition
NavHost(
navController = navController,
startDestination = if(isLoginRequired)
Screen.LoginScreen.route
else
Screen.Home.route
) {
composable(route = Screen.LoginScreen.route) {
LoginScreen(navController)
}
composable(route = Screen.PasswordScreen.route) {
PasswordScreen(navController)
}
composable(route = Screen.Home.route) {
Screen_A_with_WithTopBar(navController)
}
}
Answered By - GTID
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.