Browse Source

Integrated forgot password API and completed UI and API response, showing snackbar

Khubaib 1 week ago
parent
commit
964b718d15

+ 38 - 3
app/src/main/java/com/fastest/pass/forgotpassword/presentation/ui/ForgotPasswordFragment.kt

@@ -5,8 +5,15 @@ import android.util.Log
 import android.view.LayoutInflater
 import android.view.LayoutInflater
 import android.view.View
 import android.view.View
 import android.view.ViewGroup
 import android.view.ViewGroup
+import androidx.compose.material3.SnackbarHostState
+import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.rememberCoroutineScope
 import androidx.compose.ui.platform.ComposeView
 import androidx.compose.ui.platform.ComposeView
+import androidx.compose.ui.platform.LocalContext
+import androidx.compose.ui.unit.dp
 import androidx.fragment.app.viewModels
 import androidx.fragment.app.viewModels
+import com.fastest.pass.R
 import com.fastest.pass.app.BaseFragment
 import com.fastest.pass.app.BaseFragment
 import com.fastest.pass.app.GenericLoader
 import com.fastest.pass.app.GenericLoader
 import com.fastest.pass.forgotpassword.presentation.ui.components.ClickType
 import com.fastest.pass.forgotpassword.presentation.ui.components.ClickType
@@ -15,7 +22,9 @@ import com.fastest.pass.forgotpassword.presentation.viewmodels.ForgotPasswordVie
 import com.fastest.pass.forgotpassword.utils.ForgotPasswordNavigation
 import com.fastest.pass.forgotpassword.utils.ForgotPasswordNavigation
 import com.fastest.pass.forgotpassword.utils.ForgotPasswordRoute
 import com.fastest.pass.forgotpassword.utils.ForgotPasswordRoute
 import com.fastest.pass.ui.theme.FastestPassTheme
 import com.fastest.pass.ui.theme.FastestPassTheme
+import com.fastest.pass.views.ShowCustomSnackBar
 import dagger.hilt.android.AndroidEntryPoint
 import dagger.hilt.android.AndroidEntryPoint
+import kotlinx.coroutines.launch
 import javax.inject.Inject
 import javax.inject.Inject
 
 
 @AndroidEntryPoint
 @AndroidEntryPoint
@@ -39,8 +48,30 @@ class ForgotPasswordFragment : BaseFragment() {
         return ComposeView(requireActivity()).apply {
         return ComposeView(requireActivity()).apply {
             setContent {
             setContent {
                 FastestPassTheme {
                 FastestPassTheme {
+                    val coroutineScope = rememberCoroutineScope()
+                    val snackBarState = remember { SnackbarHostState() }
+                    val snackBarStateRed = remember { SnackbarHostState() }
                     val forgotPasswordResponse = viewmodel.forgotPasswordResponse.value
                     val forgotPasswordResponse = viewmodel.forgotPasswordResponse.value
-                    Log.d("forgotPasswordResponse", "forgotPasswordResponse = ${forgotPasswordResponse.response?.message}")
+                    val message = forgotPasswordResponse.response?.message
+
+                    Log.d(
+                        "forgotPasswordResponse",
+                        "forgotPasswordResponse = ${forgotPasswordResponse.response?.message}"
+                    )
+
+                    LaunchedEffect(key1 = forgotPasswordResponse.response?.status) {
+                        coroutineScope.launch {
+                            message?.let {
+                                if (forgotPasswordResponse.response?.status == true) {
+                                    snackBarState.showSnackbar(it)
+                                    viewmodel.navigateTo(ForgotPasswordRoute.GoBackToLogin)
+                                } else if (forgotPasswordResponse.response?.status == false) {
+                                    snackBarStateRed.showSnackbar(it)
+                                }
+                            }
+                        }
+                    }
+
 
 
                     ForgotPasswordScreen(
                     ForgotPasswordScreen(
                         clickType = { clickType ->
                         clickType = { clickType ->
@@ -48,9 +79,11 @@ class ForgotPasswordFragment : BaseFragment() {
                                 ClickType.GO_BACK -> {
                                 ClickType.GO_BACK -> {
                                     viewmodel.navigateTo(ForgotPasswordRoute.GoBackToLogin)
                                     viewmodel.navigateTo(ForgotPasswordRoute.GoBackToLogin)
                                 }
                                 }
+
                                 ClickType.OPEN_FORGOT_VERIFY -> {
                                 ClickType.OPEN_FORGOT_VERIFY -> {
                                     viewmodel.navigateTo(ForgotPasswordRoute.OpenForgotVerifyScreen)
                                     viewmodel.navigateTo(ForgotPasswordRoute.OpenForgotVerifyScreen)
                                 }
                                 }
+
                                 ClickType.FORGOT_API -> {}
                                 ClickType.FORGOT_API -> {}
                             }
                             }
                         },
                         },
@@ -59,10 +92,12 @@ class ForgotPasswordFragment : BaseFragment() {
                             viewmodel.forgotPassword(email)
                             viewmodel.forgotPassword(email)
                         }
                         }
                     )
                     )
-
+                    ShowCustomSnackBar(snackBarState, R.color.green_text, R.color.white)
+                    ShowCustomSnackBar(snackBarStateRed, R.color.red_login_button, R.color.white)
                     GenericLoader(loader = forgotPasswordResponse.isLoading)
                     GenericLoader(loader = forgotPasswordResponse.isLoading)
                 }
                 }
             }
             }
         }
         }
     }
     }
-}
+}
+

+ 5 - 1
app/src/main/java/com/fastest/pass/forgotpassword/presentation/ui/components/ForgotPasswordScreen.kt

@@ -107,7 +107,11 @@ fun ForgotPasswordScreen(
                     buttonText = R.string.send_email,
                     buttonText = R.string.send_email,
                     clickType = { clickType -> clickType(clickType) },
                     clickType = { clickType -> clickType(clickType) },
                     onSendButtonClicked = {
                     onSendButtonClicked = {
-                        if (email.isNotEmpty()) { onSendButtonClicked.invoke(email) }
+                        if (email.isNotEmpty()) {
+                            focusManager.clearFocus()
+                            keyboardController?.hide()
+                            onSendButtonClicked.invoke(email)
+                        }
                     }
                     }
                 )
                 )
             }
             }

+ 7 - 4
app/src/main/java/com/fastest/pass/views/CustomSnackBar.kt

@@ -4,6 +4,7 @@ import androidx.compose.foundation.layout.Box
 import androidx.compose.foundation.layout.Row
 import androidx.compose.foundation.layout.Row
 import androidx.compose.foundation.layout.padding
 import androidx.compose.foundation.layout.padding
 import androidx.compose.foundation.layout.size
 import androidx.compose.foundation.layout.size
+import androidx.compose.foundation.layout.statusBarsPadding
 import androidx.compose.material.Snackbar
 import androidx.compose.material.Snackbar
 import androidx.compose.material3.Icon
 import androidx.compose.material3.Icon
 import androidx.compose.material3.SnackbarHost
 import androidx.compose.material3.SnackbarHost
@@ -17,23 +18,25 @@ import androidx.compose.ui.res.colorResource
 import androidx.compose.ui.res.painterResource
 import androidx.compose.ui.res.painterResource
 import androidx.compose.ui.text.TextStyle
 import androidx.compose.ui.text.TextStyle
 import androidx.compose.ui.text.font.FontWeight
 import androidx.compose.ui.text.font.FontWeight
+import androidx.compose.ui.unit.Dp
 import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.sp
 import androidx.compose.ui.unit.sp
 import com.fastest.pass.R
 import com.fastest.pass.R
 import com.fastest.pass.ui.theme.outfitFontFamily
 import com.fastest.pass.ui.theme.outfitFontFamily
 
 
 @Composable
 @Composable
-fun ShowCustomSnackBar(snackBarState: SnackbarHostState, color: Int, textColor: Int) {
+fun ShowCustomSnackBar(snackBarState: SnackbarHostState, color: Int, textColor: Int, topPadding: Dp = 0.dp) {
     SnackbarHost(
     SnackbarHost(
         hostState = snackBarState,
         hostState = snackBarState,
-        modifier = Modifier.padding(10.dp),
-    ) {data ->
+        modifier = Modifier.padding(10.dp)
+            .statusBarsPadding(),
+    ) { data ->
         Snackbar(
         Snackbar(
             elevation = 0.dp,
             elevation = 0.dp,
             backgroundColor = colorResource(id = color),
             backgroundColor = colorResource(id = color),
         ) {
         ) {
             Box(
             Box(
-                modifier = Modifier,
+                modifier = Modifier
             ) {
             ) {
                 Row(
                 Row(
                     verticalAlignment = Alignment.CenterVertically,
                     verticalAlignment = Alignment.CenterVertically,