Explorar o código

Showing snackbar from login fragment and also response message from API

Khubaib hai 1 semana
pai
achega
619be1f28a

+ 33 - 3
app/src/main/java/com/fastest/pass/login/presentation/ui/LoginFragment.kt

@@ -10,9 +10,15 @@ import androidx.compose.foundation.layout.Box
 import androidx.compose.foundation.layout.fillMaxSize
 import androidx.compose.foundation.layout.padding
 import androidx.compose.material.Scaffold
+import androidx.compose.material3.SnackbarHostState
+import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.rememberCoroutineScope
 import androidx.compose.ui.Modifier
 import androidx.compose.ui.platform.ComposeView
+import androidx.compose.ui.platform.LocalContext
 import androidx.compose.ui.res.colorResource
+import androidx.compose.ui.res.stringResource
 import androidx.fragment.app.activityViewModels
 import androidx.fragment.app.viewModels
 import com.fastest.pass.app.BaseFragment
@@ -27,7 +33,9 @@ import com.fastest.pass.login.presentation.viewmodels.LoginViewModel
 import com.fastest.pass.login.utils.LoginNavigation
 import com.fastest.pass.login.utils.LoginRoute
 import com.fastest.pass.ui.theme.FastestPassTheme
+import com.fastest.pass.views.ShowCustomSnackBar
 import dagger.hilt.android.AndroidEntryPoint
+import kotlinx.coroutines.launch
 import javax.inject.Inject
 
 @AndroidEntryPoint
@@ -61,7 +69,11 @@ class LoginFragment : BaseFragment() {
                                 .background(colorResource(id = R.color.white))
                                 .padding(paddingValues.calculateBottomPadding())
                         ) {
+                            val snackBarStateRed = remember { SnackbarHostState() }
                             val loginResponse = viewmodel.loginResponse.value
+                            val coroutineScope = rememberCoroutineScope()
+                            val context = LocalContext.current
+                            val message = loginResponse.response?.message
 
                             loginResponse.response?.data?.let {
                                 if (loginResponse.response?.status == true) {
@@ -74,7 +86,17 @@ class LoginFragment : BaseFragment() {
                                 }
                             }
 
-                            LoginScreen(clickType = { clickType ->
+                            LaunchedEffect(key1 = message) {
+                                message?.let {
+                                    coroutineScope.launch {
+                                        snackBarStateRed.showSnackbar(it)
+                                    }
+                                }
+
+                            }
+
+                            LoginScreen(
+                                clickType = { clickType ->
                                 when (clickType) {
                                     ClickType.SIGNUP_CLICK -> {
                                         viewmodel.navigateTo(LoginRoute.OpenSignUp)
@@ -95,11 +117,19 @@ class LoginFragment : BaseFragment() {
 //                                        viewmodel.login("eng.asix@gmail.com", "password")
                                     }
                                 }
-                            }, onLoginClickCredentials = { email , password ->
+                            },
+                                onLoginClickCredentials = { email , password ->
                                 Log.d("test_api_login", "onLoginClickCredentials => LoginFragment : $email , $password")
                                 viewmodel.login(email, password)
-                            })
+                            },
+                                onShowSnackBar = { text ->
+                                    coroutineScope.launch {
+                                        snackBarStateRed.showSnackbar(context.getString(text))
+                                    }
+                                }
+                            )
 
+                            ShowCustomSnackBar(snackBarStateRed, R.color.red_login_button, R.color.white)
                             GenericLoader(loginResponse.isLoading)
 
                         }

+ 10 - 18
app/src/main/java/com/fastest/pass/login/presentation/ui/components/LoginScreen.kt

@@ -83,13 +83,13 @@ enum class ClickType {
 @Composable
 fun LoginScreen(
     clickType: (ClickType) -> Unit,
-    onLoginClickCredentials: (String, String) -> Unit
+    onLoginClickCredentials: (String, String) -> Unit,
+    onShowSnackBar: (Int) -> Unit
 ) {
     val keyboardController = LocalSoftwareKeyboardController.current
     val focusManager = LocalFocusManager.current
     var emailLogin by remember { mutableStateOf("") }
     var passwordLogin by remember { mutableStateOf("") }
-    val snackBarStateRed = remember { SnackbarHostState() }
 
     Box(
         modifier = Modifier
@@ -104,15 +104,12 @@ fun LoginScreen(
                 }
             }
     ) {
-
         Column(
             modifier = Modifier
                 .fillMaxSize()
                 .padding(top = 0.dp)
                 .statusBarsPadding()
         ) {
-            ShowCustomSnackBar(snackBarStateRed, R.color.red_login_button, R.color.white)
-
             ShowHeaderLogin(text = stringResource(R.string.login)) { clickType ->
                 clickType(clickType)
             }
@@ -146,9 +143,11 @@ fun LoginScreen(
                     onLoginClickCredentials = { email, password ->
                     onLoginClickCredentials.invoke(email, password)
                 },
-                    snackBarStateRed = snackBarStateRed,
                     keyboardController,
-                    focusManager
+                    focusManager,
+                    onShowSnackBar = {
+                        onShowSnackBar.invoke(it)
+                    }
                 )
                 ForgotPasswordText() { clickType ->
                     clickType(clickType)
@@ -411,13 +410,10 @@ fun ColumnScope.LoginButton(
     email: String,
     password: String,
     onLoginClickCredentials: (String, String) -> Unit,
-    snackBarStateRed: SnackbarHostState,
     keyboardController: SoftwareKeyboardController?,
-    focusManager: FocusManager
+    focusManager: FocusManager,
+    onShowSnackBar: (Int) -> Unit
 ) {
-    val coroutineScope = rememberCoroutineScope()
-    val context = LocalContext.current
-
     Button(
         modifier = Modifier
             .padding(start = 30.dp, end = 30.dp)
@@ -434,13 +430,9 @@ fun ColumnScope.LoginButton(
                 val hashPassword = Utils.hashing_sha256(password)
                 onLoginClickCredentials.invoke(email, hashPassword)
             } else if (email.isEmpty()) {
-                coroutineScope.launch {
-                    snackBarStateRed.showSnackbar(context.getString(R.string.email_field_req))
-                }
+                onShowSnackBar.invoke(R.string.email_field_req)
             } else if (password.isEmpty()) {
-                coroutineScope.launch {
-                    snackBarStateRed.showSnackbar(context.getString(R.string.password_field_req))
-                }
+                onShowSnackBar.invoke(R.string.password_field_req)
             }
         },
         shape = RoundedCornerShape(15.dp),