Explorar o código

Showing user's email and first char on account screen

Khubaib hai 2 semanas
pai
achega
511ef2594e

+ 7 - 3
app/src/main/java/com/fastest/pass/account/presentation/ui/components/AccountScreen.kt

@@ -100,7 +100,11 @@ enum class ClickType {
 
 @OptIn(ExperimentalMaterial3Api::class)
 @Composable
-fun AccountScreen(clickType: (ClickType) -> Unit) {
+fun AccountScreen(
+    userEmail: String,
+    userChar: String,
+    clickType: (ClickType) -> Unit
+) {
     val keyboardController = LocalSoftwareKeyboardController.current
     val focusManager = LocalFocusManager.current
     var isLoggedOut by remember { mutableStateOf(false) }
@@ -139,7 +143,7 @@ fun AccountScreen(clickType: (ClickType) -> Unit) {
                     horizontalAlignment = Alignment.CenterHorizontally
                 ) {
                     Spacer(modifier = Modifier.height(90.dp))
-                    ShowUserAccountText(text = "kp@yopmail.com", MaterialTheme.typography.headlineLarge)
+                    ShowUserAccountText(text = userEmail, MaterialTheme.typography.headlineLarge)
                     Spacer(modifier = Modifier.height(3.dp))
                     ShowUserAccountText(text = "Free Plan", MaterialTheme.typography.headlineSmall)
                     Spacer(modifier = Modifier.height(50.dp))
@@ -165,7 +169,7 @@ fun AccountScreen(clickType: (ClickType) -> Unit) {
                 .align(Alignment.TopCenter),
             contentAlignment = Alignment.Center
         ) {
-            Text(text = "K",
+            Text(text = userChar,
                 style = MaterialTheme.typography.titleLarge.copy(
                     color = colorResource(id = R.color.blue_login)
                 )

+ 18 - 8
app/src/main/java/com/fastest/pass/account/presentation/ui/fragment/AccountFragment.kt

@@ -1,6 +1,7 @@
 package com.fastest.pass.account.presentation.ui.fragment
 
 import android.os.Bundle
+import android.util.Log
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
@@ -58,6 +59,10 @@ class AccountFragment : BaseFragment() {
                                 .padding(paddingValues.calculateBottomPadding())
                         ) {
                             val logoutResponse = viewmodel.logoutResponse.value
+                            val userInfo = basePreferenceHelper.getUserInfo()
+                            val userEmail = userInfo?.email ?: ""
+                            val userChar = userInfo?.email?.first()?.uppercase() ?: ""
+                            Log.d("test_str", "email = $userEmail , char = $userChar")
 
                             if (logoutResponse.response?.status == true) {
                                 basePreferenceHelper.setLoggedInState(false)
@@ -65,16 +70,21 @@ class AccountFragment : BaseFragment() {
                                 viewmodel.navigateTo(AccountRoute.OpenLoginScreen)
                             }
 
-                            AccountScreen { clickType ->
-                                when (clickType) {
-                                    ClickType.OPEN_LOGIN_SCREEN -> {
-                                        viewmodel.navigateTo(AccountRoute.OpenLoginScreen)
-                                    }
-                                    ClickType.LOGOUT_API -> {
-                                        viewmodel.logout()
+                            AccountScreen(
+                                userEmail = userEmail,
+                                userChar = userChar,
+                                clickType = { clickType ->
+                                    when (clickType) {
+                                        ClickType.OPEN_LOGIN_SCREEN -> {
+                                            viewmodel.navigateTo(AccountRoute.OpenLoginScreen)
+                                        }
+
+                                        ClickType.LOGOUT_API -> {
+                                            viewmodel.logout()
+                                        }
                                     }
                                 }
-                            }
+                            )
 
                             GenericLoader(loader = logoutResponse.isLoading)
                         }

+ 21 - 4
app/src/main/java/com/fastest/pass/helpers/BasePreferenceHelper.kt

@@ -1,6 +1,9 @@
 package com.fastest.pass.helpers
 
 import android.content.Context
+import com.fastest.pass.login.data.model.UserInfo
+import com.google.gson.Gson
+import com.google.gson.reflect.TypeToken
 
 class BasePreferenceHelper(private val context: Context) : PreferencesHelper() {
 
@@ -9,14 +12,14 @@ class BasePreferenceHelper(private val context: Context) : PreferencesHelper() {
         removePreference(context, KEY_FILENAME, KEY_BEARER_TOKEN)
     }
 
-    fun getLoggedInState(): Boolean {
-        return getBooleanPreference(context, KEY_FILENAME, KEY_ISLOGGEDIN)
-    }
-
     fun setLoggedInState(state: Boolean) {
         putBooleanPreference(context, KEY_FILENAME, KEY_ISLOGGEDIN, state)
     }
 
+    fun getLoggedInState(): Boolean {
+        return getBooleanPreference(context, KEY_FILENAME, KEY_ISLOGGEDIN)
+    }
+
     fun saveToken(token: String) {
         putStringPreference(context, KEY_FILENAME, KEY_BEARER_TOKEN, token)
     }
@@ -25,9 +28,23 @@ class BasePreferenceHelper(private val context: Context) : PreferencesHelper() {
         return getStringPreference(context, KEY_FILENAME, KEY_BEARER_TOKEN)
     }
 
+    fun saveUserInfo(userInfo: UserInfo?) {
+        putStringPreference(context, KEY_FILENAME, KEY_USERINFO, Gson().toJson(userInfo))
+    }
+
+    fun getUserInfo() : UserInfo? {
+        val type = object : TypeToken<UserInfo?>() {}.type
+        return Gson().fromJson<UserInfo?>(
+            getStringPreference(
+                context, KEY_FILENAME, KEY_USERINFO
+            ), type
+        )
+    }
+
     companion object {
         private const val KEY_FILENAME = "file_fastestpass"
         private const val KEY_ISLOGGEDIN = "key_isloggedin"
         private const val KEY_BEARER_TOKEN = "key_bearer_token"
+        private const val KEY_USERINFO = "key_userinfo"
     }
 }

+ 1 - 1
app/src/main/java/com/fastest/pass/login/data/model/LoginResponse.kt

@@ -1,7 +1,7 @@
 package com.fastest.pass.login.data.model
 
 data class LoginResponse(
-    val userInfo: UserInfo,
+    val userinfo: UserInfo,
     val token: String
 )
 

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

@@ -63,9 +63,12 @@ class LoginFragment : BaseFragment() {
                             val loginResponse = viewmodel.loginResponse.value
 
                             loginResponse.response?.data?.let {
-                                basePreferenceHelper.setLoggedInState(true)
-                                basePreferenceHelper.saveToken(it.token)
-                                viewmodel.navigateTo(LoginRoute.OpenDashBoardScreen)
+                                if (loginResponse.response?.status == true) {
+                                    basePreferenceHelper.setLoggedInState(true)
+                                    basePreferenceHelper.saveToken(it.token)
+                                    basePreferenceHelper.saveUserInfo(it.userinfo)
+                                    viewmodel.navigateTo(LoginRoute.OpenDashBoardScreen)
+                                }
                             }
 
                             LoginScreen(clickType = { clickType ->

+ 12 - 4
app/src/main/java/com/fastest/pass/signup/presentation/ui/SignUpFragment.kt

@@ -10,6 +10,7 @@ import androidx.fragment.app.activityViewModels
 import androidx.fragment.app.viewModels
 import com.fastest.pass.app.BaseFragment
 import com.fastest.pass.app.GenericLoader
+import com.fastest.pass.helpers.BasePreferenceHelper
 import com.fastest.pass.login.presentation.viewmodels.LoginViewModel
 import com.fastest.pass.signup.presentation.ui.components.ClickType
 import com.fastest.pass.signup.presentation.ui.components.SignUpScreen
@@ -30,6 +31,8 @@ class SignUpFragment : BaseFragment() {
 
     @Inject
     lateinit var navigation: SignUpNavigation
+    @Inject
+    lateinit var basePreferenceHelper: BasePreferenceHelper
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -51,14 +54,19 @@ class SignUpFragment : BaseFragment() {
                     Log.d("test_signup_api", "loginResponse = ${loginResponse.response?.message}")
 
                     if (signupResponse.response?.status == true) {
-                        Log.d("test_signup_api", "viewmodelLogin.login")
                         viewmodelLogin.login(emailSU, passwordSU)
                     }
-                    if (loginResponse.response?.status == true) {
-                        Log.d("test_signup_api", "navigateTo(OpenDashBoardScreen)")
-                        viewmodel.navigateTo(SignUpRoute.OpenDashBoardScreen)
+
+                    loginResponse.response?.let { it ->
+                        if (it.status) {
+                            basePreferenceHelper.setLoggedInState(true)
+                            basePreferenceHelper.saveToken(it.data?.token ?: "")
+                            it.data?.userinfo?.let { it1 -> basePreferenceHelper.saveUserInfo(it1) }
+                            viewmodel.navigateTo(SignUpRoute.OpenDashBoardScreen)
+                        }
                     }
 
+
                     SignUpScreen(
                         clickType = { clickType ->
                             when (clickType) {