From bc34a70e4f9f2e4749187d05f8301e8b45f0a798 Mon Sep 17 00:00:00 2001 From: Elly Kitoto Date: Fri, 29 Sep 2023 16:35:45 +0300 Subject: [PATCH] Fix register data filter queries (#2789) * Fix register data filter queries Signed-off-by: Elly Kitoto * Fix badge UI padding Signed-off-by: Elly Kitoto * Solve pagination issue on data filter Signed-off-by: Elly Kitoto * Run spotlessApply Signed-off-by: Elly Kitoto --------- Signed-off-by: Elly Kitoto --- .../ui/main/components/TopScreenSection.kt | 9 +++++--- .../quest/ui/register/RegisterViewModel.kt | 21 ++++++++++--------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/main/components/TopScreenSection.kt b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/main/components/TopScreenSection.kt index 1dd7cb7334..c546308ff3 100644 --- a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/main/components/TopScreenSection.kt +++ b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/main/components/TopScreenSection.kt @@ -104,11 +104,14 @@ fun TopScreenSection( modifier = modifier.weight(1f).testTag(TOP_ROW_TEXT_TEST_TAG), ) if (isFilterIconEnabled) { - IconButton(onClick = { onClick.invoke(ToolbarClickEvent.FilterData) }) { + IconButton( + onClick = { onClick.invoke(ToolbarClickEvent.FilterData) }, + modifier = Modifier.padding(horizontal = 16.dp), + ) { BadgedBox( badge = { if (filteredRecordsCount != null && filteredRecordsCount > -1) { - Badge(modifier = Modifier.size(18.dp)) { + Badge { Text( text = filteredRecordsCount.toString(), overflow = TextOverflow.Clip, @@ -180,7 +183,7 @@ fun TopScreenSectionPreview() { TopScreenSection( title = "All Clients", searchText = "Eddy", - filteredRecordsCount = 8, + filteredRecordsCount = 1890, onSearchTextChanged = {}, toolBarHomeNavigation = ToolBarHomeNavigation.NAVIGATE_BACK, isFilterIconEnabled = true, diff --git a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/register/RegisterViewModel.kt b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/register/RegisterViewModel.kt index b77dd53778..639c87a41b 100644 --- a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/register/RegisterViewModel.kt +++ b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/register/RegisterViewModel.kt @@ -177,7 +177,7 @@ constructor( currentPage.value.let { if (it > 0) currentPage.value = it.minus(1) } paginateRegisterData(registerUiState.value.registerId) } - RegisterEvent.ResetFilterRecordsCount -> _filteredRecordsCount.value = -1 + RegisterEvent.ResetFilterRecordsCount -> _filteredRecordsCount.longValue = -1 } fun filterRegisterData(event: RegisterEvent.SearchRegister) { @@ -223,8 +223,7 @@ constructor( // Get filter queries from the map. NOTE: filterId MUST be unique for all resources val newBaseResourceDataQueries = createQueriesForRegisterFilter( - registerDataFilterFieldsMap?.get(baseResource.filterId)?.dataQueries - ?: baseResource.dataQueries, + registerDataFilterFieldsMap?.get(baseResource.filterId)?.dataQueries, qrItemMap, ) @@ -263,7 +262,7 @@ constructor( relatedResources.map { val newDataQueries = createQueriesForRegisterFilter( - registerDataFilterFieldsMap?.get(it.filterId)?.dataQueries ?: it.dataQueries, + registerDataFilterFieldsMap?.get(it.filterId)?.dataQueries, qrItemMap, ) it.copy( @@ -407,12 +406,12 @@ constructor( viewModelScope.launch(dispatcherProvider.io()) { val currentRegisterConfiguration = retrieveRegisterConfiguration(registerId, paramsMap) - _totalRecordsCount.value = + _totalRecordsCount.longValue = registerRepository.countRegisterData(registerId = registerId, paramsMap = paramsMap) // Only count filtered data when queries are updated if (registerFilterState.value.fhirResourceConfig != null) { - _filteredRecordsCount.value = + _filteredRecordsCount.longValue = registerRepository.countRegisterData( registerId = registerId, paramsMap = paramsMap, @@ -428,14 +427,16 @@ constructor( isFirstTimeSync = sharedPreferencesHelper .read(SharedPreferenceKey.LAST_SYNC_TIMESTAMP.name, null) - .isNullOrEmpty() && _totalRecordsCount.value == 0L, + .isNullOrEmpty() && _totalRecordsCount.longValue == 0L, registerConfiguration = currentRegisterConfiguration, registerId = registerId, - totalRecordsCount = _totalRecordsCount.value, - filteredRecordsCount = _filteredRecordsCount.value, + totalRecordsCount = _totalRecordsCount.longValue, + filteredRecordsCount = _filteredRecordsCount.longValue, pagesCount = ceil( - _totalRecordsCount.value + (if (registerFilterState.value.fhirResourceConfig != null) { + _filteredRecordsCount.longValue + } else _totalRecordsCount.longValue) .toDouble() .div(currentRegisterConfiguration.pageSize.toLong()), )