Browse Source

Add item API implemented for Driver License form list

Khubaib 1 week atrás
parent
commit
91806cc039

+ 1 - 1
app/src/main/java/com/fastest/pass/home/data/model/NewItemFormField.kt

@@ -2,5 +2,5 @@ package com.fastest.pass.home.data.model
 
 data class NewItemFormField(
     val key: String,
-    val value: String
+    var value: String
 )

+ 174 - 24
app/src/main/java/com/fastest/pass/home/presentation/ui/components/AddDriverLicenseFormScreen.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
@@ -14,6 +15,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
 import androidx.compose.foundation.layout.height
 import androidx.compose.foundation.layout.imePadding
 import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.size
 import androidx.compose.foundation.layout.wrapContentHeight
 import androidx.compose.foundation.rememberScrollState
 import androidx.compose.foundation.shape.RoundedCornerShape
@@ -40,6 +42,7 @@ import androidx.compose.material3.TextFieldDefaults
 import androidx.compose.material3.rememberDatePickerState
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateListOf
 import androidx.compose.runtime.mutableStateOf
 import androidx.compose.runtime.remember
 import androidx.compose.runtime.setValue
@@ -51,6 +54,7 @@ import androidx.compose.ui.platform.LocalFocusManager
 import androidx.compose.ui.platform.LocalSoftwareKeyboardController
 import androidx.compose.ui.platform.SoftwareKeyboardController
 import androidx.compose.ui.res.colorResource
+import androidx.compose.ui.res.painterResource
 import androidx.compose.ui.res.stringResource
 import androidx.compose.ui.text.input.ImeAction
 import androidx.compose.ui.text.input.KeyboardType
@@ -58,17 +62,53 @@ 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.helpers.BasePreferenceHelper
+import com.fastest.pass.home.data.model.NewItemFormField
 import com.fastest.pass.home.domain.model.CountryInfo
 
 @Composable
 fun AddDriverLicenseFormScreen(
     onCountryList: (List<CountryInfo>, String) -> Unit,
-    countryListDisplayed: List<CountryInfo>
+    countryListDisplayed: List<CountryInfo>,
+    basePreferenceHelper: BasePreferenceHelper,
+    onDriverLicenseFormList: (List<NewItemFormField>, Boolean) -> Unit
 ) {
     val keyboardController = LocalSoftwareKeyboardController.current
     val focusManager = LocalFocusManager.current
     val gender = listOf("Please Select", "Male", "Female", "Other")
 
+    var drivers_license_title by remember { mutableStateOf("") }
+    var drivers_license_number by remember { mutableStateOf("") }
+    var drivers_license_expiration_date by remember { mutableStateOf("") }
+    var drivers_license_license_class by remember { mutableStateOf("") }
+    var drivers_license_name by remember { mutableStateOf("") }
+    var drivers_license_address by remember { mutableStateOf("") }
+    var drivers_license_city_town by remember { mutableStateOf("") }
+    var drivers_license_state by remember { mutableStateOf("") }
+    var drivers_license_zip_postalcode by remember { mutableStateOf("") }
+    var drivers_license_country by remember { mutableStateOf("") }
+    var drivers_license_date_of_birth by remember { mutableStateOf("") }
+    var drivers_license_gender by remember { mutableStateOf("") }
+    var drivers_license_height by remember { mutableStateOf("") }
+    var drivers_license_notes by remember { mutableStateOf("") }
+
+    val driverLicenseFields = mutableListOf(
+        NewItemFormField("drivers-license_title", drivers_license_title),
+        NewItemFormField("drivers-license_number", drivers_license_number),
+        NewItemFormField("drivers-license_expiration_date", drivers_license_expiration_date),
+        NewItemFormField("drivers-license_license_class", drivers_license_license_class),
+        NewItemFormField("drivers-license_name", drivers_license_name),
+        NewItemFormField("drivers-license_address", drivers_license_address),
+        NewItemFormField("drivers-license_city_town", drivers_license_city_town),
+        NewItemFormField("drivers-license_state", drivers_license_state),
+        NewItemFormField("drivers-license_zip_postalcode", drivers_license_zip_postalcode),
+        NewItemFormField("drivers-license_country", drivers_license_country),
+        NewItemFormField("drivers-license_date_of_birth", drivers_license_date_of_birth),
+        NewItemFormField("drivers-license_gender", drivers_license_gender),
+        NewItemFormField("drivers-license_height", drivers_license_height),
+        NewItemFormField("drivers-license_notes", drivers_license_notes),
+    )
+
     Box(
         modifier = Modifier
             .fillMaxSize()
@@ -77,7 +117,24 @@ fun AddDriverLicenseFormScreen(
             .background(Color.Transparent)
             .imePadding()
     ) {
-        SaveButtonADLFS(buttonText = R.string.save)
+        SaveButtonADLFS(
+            buttonText = R.string.save,
+            onSaveButtonClick = {
+                val saltKey = basePreferenceHelper.getCustomSaltKey()
+                val secretKey = basePreferenceHelper.getCustomSecretKey()
+                val isTitleEmpty = drivers_license_title.isEmpty()
+
+                driverLicenseFields.forEachIndexed { index, newItemFormField ->
+                    driverLicenseFields[index].value = encryptIfNotEmpty(newItemFormField.value, secretKey, saltKey)
+                }
+
+                val driverLicenseFormData = driverLicenseFields.map { field ->
+                    NewItemFormField(field.key, field.value)
+                }
+
+                onDriverLicenseFormList.invoke(driverLicenseFormData, isTitleEmpty)
+            }
+        )
 
         Column(
             modifier = Modifier
@@ -88,25 +145,69 @@ fun AddDriverLicenseFormScreen(
                 .verticalScroll(rememberScrollState())
         ) {
             Spacer(modifier = Modifier.height(20.dp))
-            NameTextFieldADLFS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.title)
+            NameTextFieldADLFS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                labelText = R.string.title,
+                onText = { drivers_license_title = it }
+            )
             Spacer(modifier = Modifier.height(20.dp))
-            TextNumberTextFieldADLFS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.number, "L123456789")
+            TextNumberTextFieldADLFS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                labelText = R.string.number,
+                "L123456789",
+                onText = { drivers_license_number = it }
+            )
 
             Spacer(modifier = Modifier.height(20.dp))
-            DatePickerADLFS(labelText = R.string.expiration_date) {}
+            DatePickerADLFS(
+                labelText = R.string.expiration_date,
+                onDateSelected = { drivers_license_expiration_date = it }
+                )
 
             Spacer(modifier = Modifier.height(20.dp))
-            NameTextFieldADLFS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.license_class)
+            NameTextFieldADLFS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                labelText = R.string.license_class,
+                onText = { drivers_license_license_class = it }
+            )
             Spacer(modifier = Modifier.height(20.dp))
-            NameTextFieldADLFS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.name)
+            NameTextFieldADLFS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                labelText = R.string.name,
+                onText = { drivers_license_name = it }
+            )
             Spacer(modifier = Modifier.height(20.dp))
-            NameTextFieldADLFS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.address)
+            NameTextFieldADLFS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                labelText = R.string.address,
+                onText = { drivers_license_address = it }
+            )
             Spacer(modifier = Modifier.height(20.dp))
-            NameTextFieldADLFS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.city_town)
+            NameTextFieldADLFS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                labelText = R.string.city_town,
+                onText = { drivers_license_city_town = it }
+            )
             Spacer(modifier = Modifier.height(20.dp))
-            NameTextFieldADLFS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.state)
+            NameTextFieldADLFS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                labelText = R.string.state,
+                onText = { drivers_license_state = it }
+            )
             Spacer(modifier = Modifier.height(20.dp))
-            NameTextFieldADLFS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.zip_postal_code)
+            NameTextFieldADLFS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                labelText = R.string.zip_postal_code,
+                onText = { drivers_license_zip_postalcode = it }
+            )
             Spacer(modifier = Modifier.height(20.dp))
             SelectCountryInfoDS(
                 labelText = R.string.country,
@@ -115,30 +216,51 @@ fun AddDriverLicenseFormScreen(
                 onCountryList = { list, text ->
                     onCountryList.invoke(list, text)
                 },
-                countryListDisplayed = countryListDisplayed
+                countryListDisplayed = countryListDisplayed,
+                onText = { drivers_license_country = it }
             )
 //            NameTextFieldADLFS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.country)
 
             Spacer(modifier = Modifier.height(20.dp))
-            DatePickerADLFS(labelText = R.string.date_of_birth) {}
+            DatePickerADLFS(
+                labelText = R.string.date_of_birth,
+                onDateSelected = { drivers_license_date_of_birth = it }
+                )
 
             Spacer(modifier = Modifier.height(20.dp))
-            DropDownFieldADLFS(keyboardController = keyboardController, focusManager = focusManager,
-                labelText = R.string.gender, titles = gender)
+            DropDownFieldADLFS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                labelText = R.string.gender,
+                titles = gender,
+                onText = { drivers_license_gender = it }
+            )
 //            NameTextFieldADLFS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.sex)
 
 
             Spacer(modifier = Modifier.height(20.dp))
-            NameTextFieldADLFS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.height)
+            NameTextFieldADLFS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                labelText = R.string.height,
+                onText = { drivers_license_height = it }
+            )
 
             Spacer(modifier = Modifier.height(20.dp))
-            NotesTextFieldADLFS(keyboardController = keyboardController, focusManager = focusManager)
+            NotesTextFieldADLFS(
+                keyboardController = keyboardController,
+                focusManager = focusManager,
+                onText = { drivers_license_notes = it }
+            )
         }
     }
 }
 
 @Composable
-fun BoxScope.SaveButtonADLFS(buttonText: Int) {
+fun BoxScope.SaveButtonADLFS(
+    buttonText: Int,
+    onSaveButtonClick: () -> Unit
+    ) {
     Button(
         modifier = Modifier
             .background(colorResource(id = R.color.transparent))
@@ -146,7 +268,9 @@ fun BoxScope.SaveButtonADLFS(buttonText: Int) {
             .height(60.dp)
             .align(Alignment.BottomCenter)
             .clickable() { },
-        onClick = {},
+        onClick = {
+            onSaveButtonClick.invoke()
+        },
         shape = RoundedCornerShape(15.dp),
 //            border = BorderStroke(25.dp, colorResource(id = R.color.black)),
         colors = ButtonDefaults.buttonColors(
@@ -170,7 +294,8 @@ fun BoxScope.SaveButtonADLFS(buttonText: Int) {
 fun ColumnScope.NameTextFieldADLFS(
     keyboardController: SoftwareKeyboardController?,
     focusManager: FocusManager,
-    labelText: Int
+    labelText: Int,
+    onText: (String) -> Unit
 ) {
     var text by remember { mutableStateOf("") }
 
@@ -178,6 +303,7 @@ fun ColumnScope.NameTextFieldADLFS(
         value = text,
         onValueChange = {
             text = it
+            onText.invoke(text)
         },
         textStyle = MaterialTheme.typography.displayMedium,
         modifier = Modifier
@@ -241,7 +367,8 @@ fun ColumnScope.TextNumberTextFieldADLFS(
     keyboardController: SoftwareKeyboardController?,
     focusManager: FocusManager,
     labelText: Int,
-    placeholder: String
+    placeholder: String,
+    onText: (String) -> Unit
 ) {
     var text by remember { mutableStateOf("") }
 
@@ -249,6 +376,7 @@ fun ColumnScope.TextNumberTextFieldADLFS(
         value = text,
         onValueChange = {
             text = it
+            onText.invoke(text)
         },
         textStyle = MaterialTheme.typography.displayMedium,
         modifier = Modifier
@@ -343,6 +471,14 @@ fun ColumnScope.DatePickerADLFS(
                     tint = colorResource(id = R.color.gray_text)
                 )
             },
+            trailingIcon = {
+                Icon(
+                    painter = painterResource(id = R.drawable.arrow_right),
+                    contentDescription = "description",
+                    modifier = Modifier.size(24.dp),
+                    tint = colorResource(id = R.color.gray_text)
+                )
+            },
             modifier = Modifier
                 .align(Alignment.Start)
                 .fillMaxWidth()
@@ -417,7 +553,8 @@ fun ColumnScope.DatePickerADLFS(
 @Composable
 fun ColumnScope.NotesTextFieldADLFS(
     keyboardController: SoftwareKeyboardController?,
-    focusManager: FocusManager
+    focusManager: FocusManager,
+    onText: (String) -> Unit
 ) {
     var notesText by remember { mutableStateOf("") }
 
@@ -425,6 +562,7 @@ fun ColumnScope.NotesTextFieldADLFS(
         value = notesText,
         onValueChange = {
             notesText = it
+            onText.invoke(notesText)
         },
         textStyle = MaterialTheme.typography.displayMedium,
         modifier = Modifier
@@ -491,7 +629,8 @@ fun ColumnScope.DropDownFieldADLFS(
     focusManager: FocusManager,
     labelText: Int,
     titles: List<String>,
-    topPadding: Int = 0
+    topPadding: Int = 0,
+    onText: (String) -> Unit
 ) {
     var isExpanded by remember { mutableStateOf(false) }
     var selectedText by remember { mutableStateOf(titles[0]) }
@@ -574,6 +713,7 @@ fun ColumnScope.DropDownFieldADLFS(
                             onClick = {
                                 selectedText = s
                                 isExpanded = false
+                                onText.invoke(selectedText)
                             },
                             modifier = Modifier
                                 .background(colorResource(id = R.color.white))
@@ -592,7 +732,8 @@ fun ColumnScope.SelectCountryInfoDS(
     keyboardController: SoftwareKeyboardController?,
     focusManager: FocusManager,
     onCountryList: (List<CountryInfo>, String) -> Unit,
-    countryListDisplayed: List<CountryInfo>
+    countryListDisplayed: List<CountryInfo>,
+    onText: (String) -> Unit
 ) {
     var selectedCountry by remember { mutableStateOf("Please Select") }
     var isSheetOpened by remember { mutableStateOf(false) }
@@ -612,6 +753,14 @@ fun ColumnScope.SelectCountryInfoDS(
                     color = colorResource(id = R.color.gray_text)
                 )
             ) },
+        trailingIcon = {
+            Icon(
+                painter = painterResource(id = R.drawable.arrow_right),
+                contentDescription = "description",
+                modifier = Modifier.size(24.dp),
+                tint = colorResource(id = R.color.gray_text)
+            )
+        },
         modifier = Modifier
             .align(Alignment.Start)
             .fillMaxWidth()
@@ -650,6 +799,7 @@ fun ColumnScope.SelectCountryInfoDS(
             isSheetOpened = {isSheetOpened = false},
             onSelectedCountry = { countryName ->
                 selectedCountry = countryName
+                onText.invoke(selectedCountry)
             },
             onSearchTextChanged = {},
             onCountryList = { list , text ->

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

@@ -57,6 +57,7 @@ fun NewItemFormScreen(
     onWifiPasswordFormList: (ArrayList<NewItemFormField>, Boolean) -> Unit,
     onSecureNoteFormList: (ArrayList<NewItemFormField>, Boolean) -> Unit,
     onContactInfoFormList: (ArrayList<NewItemFormField>, Boolean) -> Unit,
+    onDriverLicenseFormList: (List<NewItemFormField>, Boolean) -> Unit,
 ) {
     val keyboardController = LocalSoftwareKeyboardController.current
     val focusManager = LocalFocusManager.current
@@ -150,7 +151,11 @@ fun NewItemFormScreen(
                     onCountryList = { list, text ->
                         onCountryList.invoke(list, text)
                     },
-                    countryListDisplayed = countryListDisplayed
+                    countryListDisplayed = countryListDisplayed,
+                    basePreferenceHelper = basePreferenceHelper,
+                    onDriverLicenseFormList = { data, boolean ->
+                        onDriverLicenseFormList.invoke(data, boolean)
+                    }
                 )
             }
             ClickTypeAddNewItem.Passport -> {

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

@@ -191,6 +191,16 @@ class NewItemFormFragment : BaseFragment() {
                                     else {
                                         viewmodel.addItems("address", contactInfoFormData)
                                     }
+                                },
+                                onDriverLicenseFormList = { driverLicenseFormData, isTitleEmpty ->
+                                    if (isTitleEmpty) {
+                                        coroutineScope.launch {
+                                            snackBarStateRed.showSnackbar("Title field is empty.")
+                                        }
+                                    }
+                                    else {
+                                        viewmodel.addItems("drivers-license", driverLicenseFormData)
+                                    }
                                 }
                             )