瀏覽代碼

Worked on searching of countries on contact info screen and filter search countries on viewmodel

Khubaib 4 月之前
父節點
當前提交
80c468aba7

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

@@ -56,6 +56,7 @@ import androidx.compose.material3.rememberDatePickerState
 import androidx.compose.material3.rememberModalBottomSheetState
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.CompositionLocalProvider
+import androidx.compose.runtime.LaunchedEffect
 import androidx.compose.runtime.getValue
 import androidx.compose.runtime.mutableStateOf
 import androidx.compose.runtime.remember
@@ -85,17 +86,22 @@ import com.fastest.pass.home.domain.model.CountryInfoCode
 import com.fastest.pass.home.domain.model.countryInfoList
 import com.fastest.pass.home.domain.model.countryInfoListCode
 import com.fastest.pass.home.utils.GetDrawable
+import kotlinx.coroutines.delay
 import java.text.SimpleDateFormat
 import java.util.Date
 import java.util.Locale
 import java.util.TimeZone
 
 @Composable
-fun AddContactInfoScreen() {
+fun AddContactInfoScreen(
+    onSearchTextChanged: (String) -> Unit,
+    onCountryList: (List<CountryInfo>, String) -> Unit,
+    countryListDisplayed: List<CountryInfo>
+) {
     val keyboardController = LocalSoftwareKeyboardController.current
     val focusManager = LocalFocusManager.current
 
-    val titles = listOf("Please Select", "Mr", "Mrs", "Ms", "Dr")
+    val honorific = listOf("Please Select", "Mr", "Mrs", "Ms", "Dr")
     val list = getAllCountriesList()
     val countriesList = ArrayList<String>()
     countriesList.addAll(list)
@@ -124,7 +130,7 @@ fun AddContactInfoScreen() {
             NameTextFieldACIFS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.title)
 
             Spacer(modifier = Modifier.height(20.dp))
-            DropDownFieldACIFS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.honorific, titles)
+            DropDownFieldACIFS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.honorific, honorific)
 
             Spacer(modifier = Modifier.height(20.dp))
             NameTextFieldACIFS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.first_name)
@@ -150,7 +156,19 @@ fun AddContactInfoScreen() {
             NameTextFieldACIFS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.zip_postal_code)
 
             Spacer(modifier = Modifier.height(20.dp))
-            SelectCountryInfo(labelText = R.string.country, keyboardController, focusManager)
+            SelectCountryInfo(
+                labelText = R.string.country,
+                keyboardController,
+                focusManager,
+                onSearchTextChanged = { searchText ->
+                    onSearchTextChanged(searchText)
+                },
+                onCountryList = { list, text ->
+                    onCountryList.invoke(list, text)
+                },
+                countryListDisplayed = countryListDisplayed
+            )
+
 //        DropDownFieldACIFS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.country, countriesList)
 
 //        Spacer(modifier = Modifier.height(20.dp))
@@ -170,7 +188,16 @@ fun AddContactInfoScreen() {
             EmailFieldACIFS(keyboardController = keyboardController, focusManager = focusManager, R.string.enter_email_address)
 
             Spacer(modifier = Modifier.height(20.dp))
-            SelectCountryInfoPhoneIcon(keyboardController, focusManager, R.string.phone, R.string.ext)
+            SelectCountryInfoPhoneIcon(
+                keyboardController,
+                focusManager,
+                R.string.phone,
+                R.string.ext,
+                onCountryList = { list, text ->
+                    onCountryList.invoke(list, text)
+                },
+                countryListDisplayed = countryListDisplayed
+            )
             Spacer(modifier = Modifier.height(20.dp))
             NumberTextFieldACIFS(keyboardController = keyboardController, focusManager = focusManager, labelText = R.string.mobile, placeholder = R.string.mobile, ImeAction.Next)
             Spacer(modifier = Modifier.height(20.dp))
@@ -740,7 +767,10 @@ fun ColumnScope.OpenModalBottomSheet(
     focusManager: FocusManager,
     isSheetOpened: () -> Unit,
     onSelectedCountry: (String) -> Unit,
-    isCountryList: Boolean = true
+    isCountryList: Boolean = true,
+    onSearchTextChanged: (String) -> Unit,
+    onCountryList: (List<CountryInfo>, String) -> Unit,
+    countryListDisplayed: List<CountryInfo> = emptyList()
 ) {
     val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
     var isSheetOpen by remember { mutableStateOf(true) }
@@ -748,6 +778,12 @@ fun ColumnScope.OpenModalBottomSheet(
     var searchText by remember { mutableStateOf("") }
     val countryList = if (isCountryList) countryInfoList else countryInfoListCode
 
+    LaunchedEffect(key1 = searchText) {
+        val delay = if (searchText.isEmpty()) 0L else 300L
+        delay(delay)
+        onCountryList.invoke(countryList, searchText)
+    }
+
    if (isSheetOpen) {
        ModalBottomSheet(
            onDismissRequest = {
@@ -784,6 +820,8 @@ fun ColumnScope.OpenModalBottomSheet(
                            query = searchText,
                            onQueryChange = {
                                searchText = it
+                               Log.d("searchText", "ACIS => OMBS , searchText = $searchText")
+                               onSearchTextChanged.invoke(searchText)
 //                                scope.launch {
 //                                    searchListViewModelSplash.searchTextChange(it)
 //                                }
@@ -835,7 +873,7 @@ fun ColumnScope.OpenModalBottomSheet(
                                .padding(top = 20.dp, bottom = 40.dp)
                        ) {
                            Log.d("countryInfoList", "countryInfoList => ${countryInfoList.size}")
-                           items(countryList, itemContent = { item ->
+                           items(countryListDisplayed, itemContent = { item ->
                                ShowCountryListItem(
                                    item = item,
                                    isSheetOpened = {
@@ -859,7 +897,10 @@ fun ColumnScope.OpenModalBottomSheet(
 fun ColumnScope.SelectCountryInfo(
     labelText: Int,
     keyboardController: SoftwareKeyboardController?,
-    focusManager: FocusManager
+    focusManager: FocusManager,
+    onSearchTextChanged: (String) -> Unit,
+    onCountryList: (List<CountryInfo>, String) -> Unit,
+    countryListDisplayed: List<CountryInfo>
 ) {
     var selectedCountry by remember { mutableStateOf("Please Select") }
     var isSheetOpened by remember { mutableStateOf(false) }
@@ -917,7 +958,14 @@ fun ColumnScope.SelectCountryInfo(
             isSheetOpened = {isSheetOpened = false},
             onSelectedCountry = { countryName ->
                 selectedCountry = countryName
-            }
+            },
+            onSearchTextChanged = { searchText ->
+                onSearchTextChanged.invoke(searchText)
+            },
+            onCountryList = { list , text ->
+                onCountryList.invoke(list, text)
+            },
+            countryListDisplayed = countryListDisplayed
         )
     }
 }
@@ -927,7 +975,9 @@ fun ColumnScope.SelectCountryInfoPhoneIcon(
     keyboardController: SoftwareKeyboardController?,
     focusManager: FocusManager,
     labelText: Int,
-    extText: Int
+    extText: Int,
+    onCountryList: (List<CountryInfo>, String) -> Unit,
+    countryListDisplayed: List<CountryInfo>
 ) {
     var number by remember { mutableStateOf("") }
     var extNumber by remember { mutableStateOf("") }
@@ -1049,7 +1099,12 @@ fun ColumnScope.SelectCountryInfoPhoneIcon(
             onSelectedCountry = { countryCode ->
                 selectedCountryCode = countryCode
             },
-            isCountryList = false
+            isCountryList = false,
+            onSearchTextChanged = {},
+            onCountryList = { list , text ->
+                onCountryList.invoke(list, text)
+            },
+            countryListDisplayed = countryListDisplayed
         )
     }
 }

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

@@ -636,7 +636,9 @@ fun ColumnScope.SelectCountryInfoDS(
             isSheetOpened = {isSheetOpened = false},
             onSelectedCountry = { countryName ->
                 selectedCountry = countryName
-            }
+            },
+            onSearchTextChanged = {},
+            onCountryList = { t , c -> }
         )
     }
 }

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

@@ -534,7 +534,9 @@ fun ColumnScope.SelectCountryInfoPassport(
             isSheetOpened = {isSheetOpened = false},
             onSelectedCountry = { countryName ->
                 selectedCountry = countryName
-            }
+            },
+            onSearchTextChanged = {},
+            onCountryList = { t , c -> }
         )
     }
 }

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

@@ -1,5 +1,6 @@
 package com.fastest.pass.home.presentation.ui.components
 
+import android.util.Log
 import androidx.compose.foundation.Image
 import androidx.compose.foundation.background
 import androidx.compose.foundation.gestures.detectTapGestures
@@ -32,13 +33,21 @@ import androidx.compose.ui.res.stringResource
 import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.sp
 import com.fastest.pass.R
+import com.fastest.pass.home.domain.model.CountryInfo
 
 enum class ClickTypeNewItemForm {
     GO_BACK_ADD_NEW_ITEM
 }
 
 @Composable
-fun NewItemFormScreen(screenName: String, screenNameType: ClickTypeAddNewItem, clickType: (ClickTypeNewItemForm) -> Unit) {
+fun NewItemFormScreen(
+    screenName: String,
+    screenNameType: ClickTypeAddNewItem,
+    clickType: (ClickTypeNewItemForm) -> Unit,
+    onSearchTextChanged: (String) -> Unit,
+    onCountryList: (List<CountryInfo>, String) -> Unit,
+    countryListDisplayed: List<CountryInfo>
+) {
     val keyboardController = LocalSoftwareKeyboardController.current
     val focusManager = LocalFocusManager.current
 
@@ -87,7 +96,17 @@ fun NewItemFormScreen(screenName: String, screenNameType: ClickTypeAddNewItem, c
                 AddSecureNoteFormScreen()
             }
             ClickTypeAddNewItem.Contact -> {
-                AddContactInfoScreen()
+                AddContactInfoScreen(
+                    onSearchTextChanged = { searchText ->
+                        Log.d("searchText", "NIFS => searchText = $searchText")
+                        onSearchTextChanged.invoke(searchText)
+                    },
+                    onCountryList = { list, text ->
+                        Log.d("searchText", "NIFS => list = ${list.size}")
+                        onCountryList.invoke(list, text)
+                    },
+                    countryListDisplayed = countryListDisplayed
+                )
             }
             ClickTypeAddNewItem.Driver -> {
                 AddDriverLicenseFormScreen()

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

@@ -66,6 +66,9 @@ class NewItemFormFragment : BaseFragment() {
                             var headerName: String = ""
                             Log.d("test_screen_name", "NIFF::screenName = ${screenName.value}")
 
+                            val countryList = viewmodel.countriesList.collectAsState()
+                            Log.d("searchText", "NIFS => countryList = ${countryList.value.size}")
+
                             when (screenName.value) {
                                 ClickTypeAddNewItem.Password -> {
                                    headerName = "Add Password"
@@ -94,13 +97,22 @@ class NewItemFormFragment : BaseFragment() {
                                 ClickTypeAddNewItem.GOTO_HOME -> {}
                             }
 
-                            NewItemFormScreen(headerName, screenName.value) { clickTypeNewItemForm ->
-                                when (clickTypeNewItemForm) {
-                                    ClickTypeNewItemForm.GO_BACK_ADD_NEW_ITEM -> {
-                                        viewmodel.navigateTo(NewItemFormRoute.GoBackAddNewItemsScreen)
+                            NewItemFormScreen(
+                                headerName,
+                                screenName.value,
+                                clickType = { clickTypeNewItemForm ->
+                                    when (clickTypeNewItemForm) {
+                                        ClickTypeNewItemForm.GO_BACK_ADD_NEW_ITEM -> {
+                                            viewmodel.navigateTo(NewItemFormRoute.GoBackAddNewItemsScreen)
+                                        }
                                     }
-                                }
-                            }
+                                },
+                                onSearchTextChanged = { },
+                                onCountryList = { list, text ->
+                                    viewmodel.getCountries(text =  text, countryList = list)
+                                },
+                                countryListDisplayed = countryList.value
+                            )
                         }
                     }
                 }

+ 43 - 0
app/src/main/java/com/fastest/pass/home/presentation/viewmodels/NewItemFormViewModel.kt

@@ -1,6 +1,8 @@
 package com.fastest.pass.home.presentation.viewmodels
 
+import android.util.Log
 import androidx.lifecycle.ViewModel
+import com.fastest.pass.home.domain.model.CountryInfo
 import com.fastest.pass.home.utils.NewItemFormRoute
 import kotlinx.coroutines.flow.MutableStateFlow
 
@@ -8,7 +10,48 @@ class NewItemFormViewModel : ViewModel() {
     private val _router = MutableStateFlow<NewItemFormRoute>(NewItemFormRoute.OpenNoneScreen)
     val router: MutableStateFlow<NewItemFormRoute> = _router
 
+    private val _searchText = MutableStateFlow<String>("")
+    val searchText: MutableStateFlow<String> = _searchText
+
+    private val _countriesList = MutableStateFlow<List<CountryInfo>>(emptyList())
+    val countriesList: MutableStateFlow<List<CountryInfo>> = _countriesList
+
     fun navigateTo(newItemFormRoute: NewItemFormRoute) {
         _router.value = newItemFormRoute
     }
+
+    fun updateSearchText(searchText: String) {
+        _searchText.value = searchText
+    }
+
+    fun getCountries(text: String, countryList: List<CountryInfo>) : List<CountryInfo> {
+        var searchFilterCountries = listOf<CountryInfo>()
+        var filterCountries = ArrayList<CountryInfo>()
+
+        Log.d("searchText", "NI VM => countryList = ${countryList.size}")
+
+        countryList.forEachIndexed { index, countryInfo ->
+            if (countryInfo.name.lowercase().contains(text.lowercase()) ||
+                countryInfo.flag.lowercase().contains(text.lowercase()) ||
+                countryInfo.timezone.lowercase().contains(text.lowercase())) {
+                Log.d("searchText", "NI VM => contains = $text")
+                filterCountries.add(countryInfo)
+            }
+        }
+
+        searchFilterCountries = if (text.isEmpty()) {
+            countryList
+        } else {
+            if (filterCountries.size > 0) {
+                filterCountries
+            } else {
+                emptyList()
+            }
+        }
+
+        Log.d("searchText", "NIFS => searchFilterCountries = ${searchFilterCountries.size}")
+
+        _countriesList.value = searchFilterCountries
+        return searchFilterCountries
+    }
 }