From ec63b515f4de4ac27a2b5ba6979d791bae85ec9c Mon Sep 17 00:00:00 2001 From: Dev Jethava Date: Fri, 11 Oct 2024 13:48:43 +0530 Subject: [PATCH 1/2] add check Email Format in SignInScreen and SignUpScreen --- .../authentication/signin/SignInScreen.kt | 13 ++++++++++++- .../authentication/signin/SignInViewModel.kt | 4 ++++ .../authentication/signup/SignUpScreen.kt | 19 ++++++++++++++++--- .../authentication/signup/SignUpViewModel.kt | 4 ++++ 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signin/SignInScreen.kt b/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signin/SignInScreen.kt index 9ce570d1..19ad6a4e 100644 --- a/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signin/SignInScreen.kt +++ b/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signin/SignInScreen.kt @@ -41,6 +41,9 @@ import androidx.compose.ui.unit.dp import androidx.hilt.navigation.compose.hiltViewModel import com.hieuwu.groceriesstore.R import com.hieuwu.groceriesstore.presentation.authentication.composables.IconTextInput +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch @Composable fun SignInScreen( @@ -127,7 +130,15 @@ fun SignInScreen( ) Button( modifier = Modifier.fillMaxWidth(), - onClick = { viewModel.signIn() }, + onClick = { + if (viewModel.isValidEmail()) + viewModel.signIn() + else { + CoroutineScope(Dispatchers.Main).launch { + snackbarHostState.showSnackbar("Invalid Email address!!") + } + } + }, colors = ButtonDefaults.buttonColors(containerColor = colorResource(id = R.color.colorPrimary)), ) { Text("Sign in") diff --git a/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signin/SignInViewModel.kt b/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signin/SignInViewModel.kt index 418ff7fd..13699ee9 100644 --- a/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signin/SignInViewModel.kt +++ b/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signin/SignInViewModel.kt @@ -40,6 +40,10 @@ class SignInViewModel @Inject constructor( _password.value = newPassword } + fun isValidEmail(): Boolean { + return android.util.Patterns.EMAIL_ADDRESS.matcher(_email.value).matches() + } + fun signIn() { viewModelScope.launch { when (signInUseCase( diff --git a/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signup/SignUpScreen.kt b/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signup/SignUpScreen.kt index f91893be..5384b448 100644 --- a/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signup/SignUpScreen.kt +++ b/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signup/SignUpScreen.kt @@ -40,10 +40,15 @@ import androidx.compose.ui.unit.dp import androidx.hilt.navigation.compose.hiltViewModel import com.hieuwu.groceriesstore.R import com.hieuwu.groceriesstore.presentation.authentication.composables.IconTextInput +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch @Composable -fun SignUpScreen(modifier: Modifier = Modifier, - viewModel: SignUpViewModel = hiltViewModel()) { +fun SignUpScreen( + modifier: Modifier = Modifier, + viewModel: SignUpViewModel = hiltViewModel() +) { val snackbarHostState = remember { SnackbarHostState() } @@ -137,7 +142,15 @@ fun SignUpScreen(modifier: Modifier = Modifier, ) Button( modifier = Modifier.fillMaxWidth(), - onClick = { viewModel.createAccount() }, + onClick = { + if (viewModel.isValidEmail()) { + viewModel.createAccount() + } else { + CoroutineScope(Dispatchers.Main).launch { + snackbarHostState.showSnackbar("Invalid Email address!!") + } + } + }, colors = ButtonDefaults.buttonColors(containerColor = colorResource(id = R.color.colorPrimary)), ) { Text("Sign up") diff --git a/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signup/SignUpViewModel.kt b/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signup/SignUpViewModel.kt index 20fb8372..9887a6d8 100644 --- a/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signup/SignUpViewModel.kt +++ b/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signup/SignUpViewModel.kt @@ -50,6 +50,10 @@ class SignUpViewModel @Inject constructor(private val userRepository: UserReposi _name.value = text } + fun isValidEmail(): Boolean { + return android.util.Patterns.EMAIL_ADDRESS.matcher(_email.value).matches() + } + fun createAccount() { viewModelScope.launch { val result = userRepository.createAccount(_email.value, _password.value, _name.value) From d80f1b495cf9a6fa8b72caba7db353e3f4df97e6 Mon Sep 17 00:00:00 2001 From: Dev Jethava Date: Fri, 11 Oct 2024 18:35:29 +0530 Subject: [PATCH 2/2] fix: Show snackbar on invalid email address - Added a coroutine to display a snackbar when an invalid email address is entered. - Used `withContext(Dispatchers.Main)` to ensure the snackbar is displayed on the main thread. - Displayed "Invalid Email address!!" message in the snackbar. --- .../presentation/authentication/signin/SignInScreen.kt | 10 ++++++++-- .../authentication/signin/SignInViewModel.kt | 3 ++- .../presentation/authentication/signup/SignUpScreen.kt | 9 +++++++-- .../authentication/signup/SignUpViewModel.kt | 3 ++- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signin/SignInScreen.kt b/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signin/SignInScreen.kt index 19ad6a4e..4992a214 100644 --- a/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signin/SignInScreen.kt +++ b/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signin/SignInScreen.kt @@ -28,6 +28,7 @@ import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @@ -44,6 +45,7 @@ import com.hieuwu.groceriesstore.presentation.authentication.composables.IconTex import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext @Composable fun SignInScreen( @@ -53,6 +55,8 @@ fun SignInScreen( viewModel: SignInViewModel = hiltViewModel() ) { val snackbarHostState = remember { SnackbarHostState() } + val coroutineScope = rememberCoroutineScope() + LaunchedEffect(Unit) { viewModel .showAccountNotExistedError @@ -134,8 +138,10 @@ fun SignInScreen( if (viewModel.isValidEmail()) viewModel.signIn() else { - CoroutineScope(Dispatchers.Main).launch { - snackbarHostState.showSnackbar("Invalid Email address!!") + coroutineScope.launch { + withContext(Dispatchers.Main){ + snackbarHostState.showSnackbar("Invalid Email address!") + } } } }, diff --git a/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signin/SignInViewModel.kt b/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signin/SignInViewModel.kt index 13699ee9..c6ebe231 100644 --- a/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signin/SignInViewModel.kt +++ b/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signin/SignInViewModel.kt @@ -1,5 +1,6 @@ package com.hieuwu.groceriesstore.presentation.authentication.signin +import android.util.Patterns.EMAIL_ADDRESS import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.hieuwu.groceriesstore.domain.usecases.SignInUseCase @@ -41,7 +42,7 @@ class SignInViewModel @Inject constructor( } fun isValidEmail(): Boolean { - return android.util.Patterns.EMAIL_ADDRESS.matcher(_email.value).matches() + return EMAIL_ADDRESS.matcher(_email.value).matches() } fun signIn() { diff --git a/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signup/SignUpScreen.kt b/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signup/SignUpScreen.kt index 5384b448..5024bb22 100644 --- a/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signup/SignUpScreen.kt +++ b/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signup/SignUpScreen.kt @@ -28,6 +28,7 @@ import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @@ -43,6 +44,7 @@ import com.hieuwu.groceriesstore.presentation.authentication.composables.IconTex import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext @Composable fun SignUpScreen( @@ -51,6 +53,7 @@ fun SignUpScreen( ) { val snackbarHostState = remember { SnackbarHostState() } + val coroutineScope = rememberCoroutineScope() LaunchedEffect(Unit) { viewModel.showSignUpSuccessMessage.collect { @@ -146,8 +149,10 @@ fun SignUpScreen( if (viewModel.isValidEmail()) { viewModel.createAccount() } else { - CoroutineScope(Dispatchers.Main).launch { - snackbarHostState.showSnackbar("Invalid Email address!!") + coroutineScope.launch { + withContext(Dispatchers.Main) { + snackbarHostState.showSnackbar("Invalid Email address!!") + } } } }, diff --git a/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signup/SignUpViewModel.kt b/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signup/SignUpViewModel.kt index 9887a6d8..02efe9d9 100644 --- a/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signup/SignUpViewModel.kt +++ b/app/src/main/java/com/hieuwu/groceriesstore/presentation/authentication/signup/SignUpViewModel.kt @@ -1,5 +1,6 @@ package com.hieuwu.groceriesstore.presentation.authentication.signup +import android.util.Patterns.EMAIL_ADDRESS import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.hieuwu.groceriesstore.data.repository.UserRepository @@ -51,7 +52,7 @@ class SignUpViewModel @Inject constructor(private val userRepository: UserReposi } fun isValidEmail(): Boolean { - return android.util.Patterns.EMAIL_ADDRESS.matcher(_email.value).matches() + return EMAIL_ADDRESS.matcher(_email.value).matches() } fun createAccount() {