Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solve Invalid Email Format in Text Field #245 #270

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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