Skip to content

Commit

Permalink
Merge pull request #270 from DevJethava/main
Browse files Browse the repository at this point in the history
solve Invalid Email Format in Text Field #245
  • Loading branch information
hieuwu authored Oct 11, 2024
2 parents 51c6cb6 + 3cd7ac7 commit 6b29f56
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -50,6 +55,8 @@ fun SignInScreen(
viewModel: SignInViewModel = hiltViewModel()
) {
val snackbarHostState = remember { SnackbarHostState() }
val coroutineScope = rememberCoroutineScope()

LaunchedEffect(Unit) {
viewModel
.showAccountNotExistedError
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6b29f56

Please sign in to comment.