Browse Source

Add item API implemented for bank-accounts form list

Khubaib 1 week ago
parent
commit
6d9b22fdd9

+ 149 - 22
app/src/main/java/com/fastest/pass/home/presentation/ui/components/AddBankAccountFormScreen.kt

@@ -1,5 +1,6 @@
 package com.fastest.pass.home.presentation.ui.components
 
+import android.util.Log
 import androidx.compose.foundation.background
 import androidx.compose.foundation.border
 import androidx.compose.foundation.clickable
@@ -46,11 +47,28 @@ import androidx.compose.ui.text.style.TextAlign
 import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.sp
 import com.fastest.pass.R
+import com.fastest.pass.app.Utils
+import com.fastest.pass.helpers.BasePreferenceHelper
+import com.fastest.pass.home.data.model.NewItemFormField
 
 @Composable
-fun AddBankAccountFormScreen() {
+fun AddBankAccountFormScreen(
+    basePreferenceHelper: BasePreferenceHelper,
+    onBankAccountFormList: (ArrayList<NewItemFormField>, Boolean) -> Unit
+    ) {
     val keyboardController = LocalSoftwareKeyboardController.current
     val focusManager = LocalFocusManager.current
+    var bank_accounts_title by remember { mutableStateOf("") }
+    var bank_accounts_bank_name by remember { mutableStateOf("") }
+    var bank_accounts_account_type by remember { mutableStateOf("") }
+    var bank_accounts_routing_number by remember { mutableStateOf("") }
+    var bank_accounts_account_number by remember { mutableStateOf("") }
+    var bank_accounts_swift_code by remember { mutableStateOf("") }
+    var bank_accounts_iban_number by remember { mutableStateOf("") }
+    var bank_accounts_pin by remember { mutableStateOf("") }
+    var bank_accounts_branch_address by remember { mutableStateOf("") }
+    var bank_accounts_branch_phone by remember { mutableStateOf("") }
+    var bank_accounts_notes by remember { mutableStateOf("") }
 
     Box(
         modifier = Modifier
@@ -60,7 +78,42 @@ fun AddBankAccountFormScreen() {
             .background(Color.Transparent)
             .imePadding()
     ) {
-        SaveButtonABAFS(buttonText = R.string.save)
+        SaveButtonABAFS(
+            buttonText = R.string.save,
+            onSaveButtonCLick = {
+                val bankAccountFormList = ArrayList<NewItemFormField>()
+                val saltKey = basePreferenceHelper.getCustomSaltKey()
+                val secretKey = basePreferenceHelper.getCustomSecretKey()
+
+                val isTitleEmpty = bank_accounts_title.isEmpty()
+
+                bank_accounts_title = encryptIfNotEmpty(bank_accounts_title, secretKey, saltKey)
+                bank_accounts_bank_name = encryptIfNotEmpty(bank_accounts_bank_name, secretKey, saltKey)
+                bank_accounts_account_type = encryptIfNotEmpty(bank_accounts_account_type, secretKey, saltKey)
+                bank_accounts_routing_number = encryptIfNotEmpty(bank_accounts_routing_number, secretKey, saltKey)
+                bank_accounts_account_number = encryptIfNotEmpty(bank_accounts_account_number, secretKey, saltKey)
+                bank_accounts_swift_code = encryptIfNotEmpty(bank_accounts_swift_code, secretKey, saltKey)
+                bank_accounts_iban_number = encryptIfNotEmpty(bank_accounts_iban_number, secretKey, saltKey)
+                bank_accounts_pin = encryptIfNotEmpty(bank_accounts_pin, secretKey, saltKey)
+                bank_accounts_branch_address = encryptIfNotEmpty(bank_accounts_branch_address, secretKey, saltKey)
+                bank_accounts_branch_phone = encryptIfNotEmpty(bank_accounts_branch_phone, secretKey, saltKey)
+                bank_accounts_notes = encryptIfNotEmpty(bank_accounts_notes, secretKey, saltKey)
+
+                bankAccountFormList.add(NewItemFormField("bank-accounts_title", bank_accounts_title))
+                bankAccountFormList.add(NewItemFormField("bank-accounts_bank_name", bank_accounts_bank_name))
+                bankAccountFormList.add(NewItemFormField("bank-accounts_account_type", bank_accounts_account_type))
+                bankAccountFormList.add(NewItemFormField("bank-accounts_routing_number", bank_accounts_routing_number))
+                bankAccountFormList.add(NewItemFormField("bank-accounts_account_number", bank_accounts_account_number))
+                bankAccountFormList.add(NewItemFormField("bank-accounts_swift_code", bank_accounts_swift_code))
+                bankAccountFormList.add(NewItemFormField("bank-accounts_iban_number", bank_accounts_iban_number))
+                bankAccountFormList.add(NewItemFormField("bank-accounts_pin", bank_accounts_pin))
+                bankAccountFormList.add(NewItemFormField("bank-accounts_branch_address", bank_accounts_branch_address))
+                bankAccountFormList.add(NewItemFormField("bank-accounts_branch_phone", bank_accounts_branch_phone))
+                bankAccountFormList.add(NewItemFormField("bank-accounts_notes", bank_accounts_notes))
+
+                onBankAccountFormList.invoke(bankAccountFormList, isTitleEmpty)
+            }
+        )
 
         Column(
             modifier = Modifier
@@ -71,27 +124,86 @@ fun AddBankAccountFormScreen() {
                 .verticalScroll(rememberScrollState())
         ) {
             Spacer(modifier = Modifier.height(20.dp))
-            NameTextFieldABAFS(keyboardController = keyboardController, focusManager = focusManager, R.string.title)
+            NameTextFieldABAFS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                R.string.title,
+                onText = { bank_accounts_title = it }
+            )
             Spacer(modifier = Modifier.height(20.dp))
-            NameTextFieldABAFS(keyboardController = keyboardController, focusManager = focusManager, R.string.bank_name)
+            NameTextFieldABAFS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                R.string.bank_name,
+                onText = { bank_accounts_bank_name = it }
+            )
             Spacer(modifier = Modifier.height(20.dp))
-            NameTextFieldABAFS(keyboardController = keyboardController, focusManager = focusManager, R.string.account_type)
+            NameTextFieldABAFS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                R.string.account_type,
+                onText = { bank_accounts_account_type = it }
+            )
             Spacer(modifier = Modifier.height(20.dp))
-            NumberTextFieldAPS(keyboardController = keyboardController, focusManager = focusManager, R.string.routing_number, "123456789")
+            NumberTextFieldAPS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                R.string.routing_number,
+                "123456789",
+                onText = { bank_accounts_routing_number = it }
+            )
             Spacer(modifier = Modifier.height(20.dp))
-            TextNumberTextFieldAPS(keyboardController = keyboardController, focusManager = focusManager, R.string.account_number, "12345678912")
+            TextNumberTextFieldAPS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                R.string.account_number,
+                "12345678912",
+                onText = { bank_accounts_account_number = it }
+            )
             Spacer(modifier = Modifier.height(20.dp))
-            TextNumberTextFieldAPS(keyboardController = keyboardController, focusManager = focusManager, R.string.swift_code, "1234567890")
+            TextNumberTextFieldAPS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                R.string.swift_code,
+                "1234567890",
+                onText = { bank_accounts_swift_code = it }
+            )
             Spacer(modifier = Modifier.height(20.dp))
-            TextNumberTextFieldAPS(keyboardController = keyboardController, focusManager = focusManager, R.string.iban_number, "12 12 123456 12345678")
+            TextNumberTextFieldAPS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                R.string.iban_number,
+                "12 12 123456 12345678",
+                onText = { bank_accounts_iban_number = it }
+            )
             Spacer(modifier = Modifier.height(20.dp))
-            TextNumberTextFieldAPS(keyboardController = keyboardController, focusManager = focusManager, R.string.pin, "1234")
+            TextNumberTextFieldAPS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                R.string.pin,
+                "1234",
+                onText = { bank_accounts_pin = it }
+            )
             Spacer(modifier = Modifier.height(20.dp))
-            NameTextFieldABAFS(keyboardController = keyboardController, focusManager = focusManager, R.string.branch_address)
+            NameTextFieldABAFS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                R.string.branch_address,
+                onText = { bank_accounts_branch_address = it }
+            )
             Spacer(modifier = Modifier.height(20.dp))
-            PhoneNumberTextFieldAPS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.branch_phone)
+            PhoneNumberTextFieldAPS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                labelText = R.string.branch_phone,
+                onText = { bank_accounts_branch_phone = it }
+            )
             Spacer(modifier = Modifier.height(20.dp))
-            NotesTextFieldABAFS(keyboardController = keyboardController, focusManager = focusManager)
+            NotesTextFieldABAFS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                onText = { bank_accounts_notes = it }
+            )
 //            Spacer(modifier = Modifier.height(25.dp))
 //            SaveButtonABAFS(buttonText = R.string.save)
         }
@@ -102,7 +214,8 @@ fun AddBankAccountFormScreen() {
 fun ColumnScope.NameTextFieldABAFS(
     keyboardController: SoftwareKeyboardController?,
     focusManager: FocusManager,
-    labelText: Int
+    labelText: Int,
+    onText: (String) -> Unit
 ) {
     var text by remember { mutableStateOf("") }
 
@@ -110,6 +223,7 @@ fun ColumnScope.NameTextFieldABAFS(
         value = text,
         onValueChange = {
             text = it
+            onText.invoke(text)
         },
         textStyle = MaterialTheme.typography.displayMedium,
         modifier = Modifier
@@ -173,7 +287,8 @@ fun ColumnScope.NumberTextFieldAPS(
     keyboardController: SoftwareKeyboardController?,
     focusManager: FocusManager,
     labelText: Int,
-    placeholder: String
+    placeholder: String,
+    onText: (String) -> Unit
 ) {
     var text by remember { mutableStateOf("") }
 
@@ -181,6 +296,7 @@ fun ColumnScope.NumberTextFieldAPS(
         value = text,
         onValueChange = {
             text = it
+            onText.invoke(text)
         },
         textStyle = MaterialTheme.typography.displayMedium,
         modifier = Modifier
@@ -245,7 +361,8 @@ fun ColumnScope.TextNumberTextFieldAPS(
     keyboardController: SoftwareKeyboardController?,
     focusManager: FocusManager,
     labelText: Int,
-    placeholder: String
+    placeholder: String,
+    onText: (String) -> Unit
 ) {
     var text by remember { mutableStateOf("") }
 
@@ -253,6 +370,7 @@ fun ColumnScope.TextNumberTextFieldAPS(
         value = text,
         onValueChange = {
             text = it
+            onText.invoke(text)
         },
         textStyle = MaterialTheme.typography.displayMedium,
         modifier = Modifier
@@ -316,7 +434,8 @@ fun ColumnScope.TextNumberTextFieldAPS(
 fun ColumnScope.PhoneNumberTextFieldAPS(
     keyboardController: SoftwareKeyboardController?,
     focusManager: FocusManager,
-    labelText: Int
+    labelText: Int,
+    onText: (String) -> Unit
 ) {
     var text by remember { mutableStateOf("") }
 
@@ -324,6 +443,7 @@ fun ColumnScope.PhoneNumberTextFieldAPS(
         value = text,
         onValueChange = {
             text = it
+            onText.invoke(text)
         },
         textStyle = MaterialTheme.typography.displayMedium,
         modifier = Modifier
@@ -384,15 +504,20 @@ fun ColumnScope.PhoneNumberTextFieldAPS(
 }
 
 @Composable
-fun BoxScope.SaveButtonABAFS(buttonText: Int) {
+fun BoxScope.SaveButtonABAFS(
+    buttonText: Int,
+    onSaveButtonCLick: () -> Unit
+    ) {
     Button(
         modifier = Modifier
             .background(colorResource(id = R.color.transparent))
             .fillMaxWidth()
             .height(60.dp)
             .align(Alignment.BottomCenter)
-            .clickable() { },
-        onClick = {},
+            .clickable() {},
+        onClick = {
+            onSaveButtonCLick.invoke()
+        },
         shape = RoundedCornerShape(15.dp),
 //            border = BorderStroke(25.dp, colorResource(id = R.color.black)),
         colors = ButtonDefaults.buttonColors(
@@ -415,7 +540,8 @@ fun BoxScope.SaveButtonABAFS(buttonText: Int) {
 @Composable
 fun ColumnScope.NotesTextFieldABAFS(
     keyboardController: SoftwareKeyboardController?,
-    focusManager: FocusManager
+    focusManager: FocusManager,
+    onText: (String) -> Unit
 ) {
     var notesText by remember { mutableStateOf("") }
 
@@ -423,6 +549,7 @@ fun ColumnScope.NotesTextFieldABAFS(
         value = notesText,
         onValueChange = {
             notesText = it
+            onText.invoke(notesText)
         },
         textStyle = MaterialTheme.typography.displayMedium,
         modifier = Modifier
@@ -480,4 +607,4 @@ fun ColumnScope.NotesTextFieldABAFS(
             }
         ),
     )
-}
+}

+ 16 - 20
app/src/main/java/com/fastest/pass/home/presentation/ui/components/AddPasswordFormScreen.kt

@@ -70,7 +70,7 @@ import javax.inject.Inject
 
 @Composable
 fun AddPasswordFormScreen(
-    onPasswordsFormList: (ArrayList<NewItemFormField>, Boolean) -> Unit,
+    onPasswordsFormList: (ArrayList<NewItemFormField>, Boolean, Boolean) -> Unit,
     basePreferenceHelper: BasePreferenceHelper
 ) {
     val keyboardController = LocalSoftwareKeyboardController.current
@@ -97,25 +97,13 @@ fun AddPasswordFormScreen(
                 val secretKey = basePreferenceHelper.getCustomSecretKey()
 
                 val isTitleEmpty = passwords_title.isEmpty()
+                val isPasswordEmpty = passwords_password.isEmpty()
 
-                if (passwords_title.isNotEmpty()) {
-                    passwords_title = Utils.encryptData(passwords_title, secretKey ?: "", saltKey ?: "")
-                }
-                if (passwords_url.isNotEmpty()) {
-                    passwords_url = Utils.encryptData(passwords_url, secretKey ?: "", saltKey ?: "")
-                }
-                if (passwords_username.isNotEmpty()) {
-                    passwords_username =
-                        Utils.encryptData(passwords_username, secretKey ?: "", saltKey ?: "")
-                }
-                if (passwords_password.isNotEmpty()) {
-                    passwords_password =
-                        Utils.encryptData(passwords_password, secretKey ?: "", saltKey ?: "")
-                }
-                if (passwords_notes.isNotEmpty()) {
-                    passwords_notes =
-                        Utils.encryptData(passwords_notes, secretKey ?: "", saltKey ?: "")
-                }
+                passwords_title = encryptIfNotEmpty(passwords_title, secretKey, saltKey)
+                passwords_url = encryptIfNotEmpty(passwords_url, secretKey, saltKey)
+                passwords_username = encryptIfNotEmpty(passwords_username, secretKey, saltKey)
+                passwords_password = encryptIfNotEmpty(passwords_password, secretKey, saltKey)
+                passwords_notes = encryptIfNotEmpty(passwords_notes, secretKey, saltKey)
 
                 passwordsFormList.add(NewItemFormField("passwords_title", passwords_title))
                 passwordsFormList.add(NewItemFormField("passwords_url", passwords_url))
@@ -123,7 +111,7 @@ fun AddPasswordFormScreen(
                 passwordsFormList.add(NewItemFormField("passwords_password", passwords_password))
                 passwordsFormList.add(NewItemFormField("passwords_notes", passwords_notes))
 
-                onPasswordsFormList.invoke(passwordsFormList, isTitleEmpty)
+                onPasswordsFormList.invoke(passwordsFormList, isTitleEmpty, isPasswordEmpty)
             }
         )
 
@@ -691,4 +679,12 @@ fun BoxScope.SaveButtonAPWFS(
             textAlign = TextAlign.Center
         )
     }
+}
+
+fun encryptIfNotEmpty(value: String, secretKey: String?, saltKey: String?): String {
+    return if (value.isNotEmpty()) {
+        Utils.encryptData(value, secretKey ?: "", saltKey ?: "")
+    } else {
+        value
+    }
 }

+ 11 - 5
app/src/main/java/com/fastest/pass/home/presentation/ui/components/NewItemFormScreen.kt

@@ -50,8 +50,9 @@ fun NewItemFormScreen(
     onSearchTextChanged: (String) -> Unit,
     onCountryList: (List<CountryInfo>, String) -> Unit,
     countryListDisplayed: List<CountryInfo>,
-    onPasswordsFormList: (ArrayList<NewItemFormField>, Boolean) -> Unit,
-    basePreferenceHelper: BasePreferenceHelper
+    basePreferenceHelper: BasePreferenceHelper,
+    onPasswordsFormList: (ArrayList<NewItemFormField>, Boolean, Boolean) -> Unit,
+    onBankAccountFormList: (ArrayList<NewItemFormField>, Boolean) -> Unit
 ) {
     val keyboardController = LocalSoftwareKeyboardController.current
     val focusManager = LocalFocusManager.current
@@ -87,14 +88,19 @@ fun NewItemFormScreen(
         when (screenNameType) {
             ClickTypeAddNewItem.Password -> {
                 AddPasswordFormScreen(
-                    onPasswordsFormList = { data, boolean ->
-                        onPasswordsFormList.invoke(data, boolean)
+                    onPasswordsFormList = { data, boolean, boolean2 ->
+                        onPasswordsFormList.invoke(data, boolean, boolean2)
                     },
                     basePreferenceHelper = basePreferenceHelper
                 )
             }
             ClickTypeAddNewItem.Bank -> {
-                AddBankAccountFormScreen()
+                AddBankAccountFormScreen(
+                    basePreferenceHelper = basePreferenceHelper,
+                    onBankAccountFormList = { data, boolean ->
+                        onBankAccountFormList.invoke(data, boolean)
+                    }
+                )
             }
             ClickTypeAddNewItem.Payment -> {
                 AddPaymentCardFormScreen()

+ 23 - 5
app/src/main/java/com/fastest/pass/home/presentation/ui/fragment/NewItemFormFragment.kt

@@ -120,20 +120,38 @@ class NewItemFormFragment : BaseFragment() {
                                     viewmodel.getCountries(text =  text, countryList = list)
                                 },
                                 countryListDisplayed = countryList.value,
-                                onPasswordsFormList = { passwordFormData, isTitleEmpty ->
-                                    Log.d("passwordFormData", "${passwordFormData.size} , $isTitleEmpty")
+                                basePreferenceHelper = basePreferenceHelper,
+                                onPasswordsFormList = { passwordFormData, isTitleEmpty, isPasswordEmpty ->
                                     passwordFormData.forEachIndexed { i, data ->
-                                        Log.d("passwordFormData", "${data.key}, ${data.value}")
+                                        Log.d("passwordFormData", "data = ${data.key} , ${data.value}")
                                     }
                                     if (isTitleEmpty) {
                                         coroutineScope.launch {
                                             snackBarStateRed.showSnackbar("Title field is empty.")
                                         }
-                                    } else {
+                                    }
+                                    else if (isPasswordEmpty) {
+                                        coroutineScope.launch {
+                                            snackBarStateRed.showSnackbar("Password field is empty.")
+                                        }
+                                    }
+                                    else {
                                         viewmodel.addItems("passwords", passwordFormData)
                                     }
                                 },
-                                basePreferenceHelper = basePreferenceHelper
+                                onBankAccountFormList = { bankAccountFormData, isTitleEmpty ->
+                                    bankAccountFormData.forEachIndexed { i, data ->
+                                        Log.d("bankform", "data = ${data.key} , ${data.value}")
+                                    }
+                                    if (isTitleEmpty) {
+                                        coroutineScope.launch {
+                                            snackBarStateRed.showSnackbar("Title field is empty.")
+                                        }
+                                    }
+                                    else {
+                                        viewmodel.addItems("bank-accounts", bankAccountFormData)
+                                    }
+                                }
                             )
 
                             ShowCustomSnackBar(snackBarStateRed, R.color.red_login_button, R.color.white)