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..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 @@ -41,6 +42,10 @@ 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 +import kotlinx.coroutines.withContext @Composable fun SignInScreen( @@ -50,6 +55,8 @@ fun SignInScreen( viewModel: SignInViewModel = hiltViewModel() ) { val snackbarHostState = remember { SnackbarHostState() } + val coroutineScope = rememberCoroutineScope() + LaunchedEffect(Unit) { viewModel .showAccountNotExistedError @@ -127,7 +134,17 @@ fun SignInScreen( ) Button( modifier = Modifier.fillMaxWidth(), - onClick = { viewModel.signIn() }, + onClick = { + if (viewModel.isValidEmail()) + viewModel.signIn() + else { + coroutineScope.launch { + withContext(Dispatchers.Main){ + 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..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 @@ -40,6 +41,10 @@ class SignInViewModel @Inject constructor( _password.value = newPassword } + fun isValidEmail(): Boolean { + return 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..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 @@ -40,12 +41,19 @@ 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 +import kotlinx.coroutines.withContext @Composable -fun SignUpScreen(modifier: Modifier = Modifier, - viewModel: SignUpViewModel = hiltViewModel()) { +fun SignUpScreen( + modifier: Modifier = Modifier, + viewModel: SignUpViewModel = hiltViewModel() +) { val snackbarHostState = remember { SnackbarHostState() } + val coroutineScope = rememberCoroutineScope() LaunchedEffect(Unit) { viewModel.showSignUpSuccessMessage.collect { @@ -137,7 +145,17 @@ fun SignUpScreen(modifier: Modifier = Modifier, ) Button( modifier = Modifier.fillMaxWidth(), - onClick = { viewModel.createAccount() }, + onClick = { + if (viewModel.isValidEmail()) { + viewModel.createAccount() + } else { + coroutineScope.launch { + withContext(Dispatchers.Main) { + 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..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 @@ -50,6 +51,10 @@ class SignUpViewModel @Inject constructor(private val userRepository: UserReposi _name.value = text } + fun isValidEmail(): Boolean { + return EMAIL_ADDRESS.matcher(_email.value).matches() + } + fun createAccount() { viewModelScope.launch { val result = userRepository.createAccount(_email.value, _password.value, _name.value)