From 01f3fad9b0b9967569d0745221849c3934e9731d Mon Sep 17 00:00:00 2001 From: Sasikanth Miriyampalli Date: Wed, 28 Feb 2024 13:03:14 +0530 Subject: [PATCH] Open TEI dashboard when a TEI is found after QR code scanning --- .../searchTrackEntity/SearchTEIViewModel.kt | 58 +++++++++++++++++-- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchTEIViewModel.kt b/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchTEIViewModel.kt index 7a85d4bc556..585de95ffcd 100644 --- a/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchTEIViewModel.kt +++ b/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchTEIViewModel.kt @@ -769,11 +769,7 @@ class SearchTEIViewModel( } is FormIntent.OnQrCodeScanned -> { - // TODO: Search based on QR code or update query - updateQuery( - formIntent.uid, - formIntent.value - ) + onQrCodeScanned(formIntent) } is FormIntent.OnFocus -> { @@ -801,6 +797,58 @@ class SearchTEIViewModel( } } + private fun onQrCodeScanned(formIntent: FormIntent.OnQrCodeScanned) { + viewModelScope.launch { + updateQuery( + formIntent.uid, + formIntent.value, + ) + + searching = queryData.isNotEmpty() + uiState = uiState.copy(clearSearchEnabled = queryData.isNotEmpty()) + + val searchParametersModel = SearchParametersModel( + selectedProgram = searchRepository.getProgram(initialProgramUid), + queryData = queryData, + ) + val isOnline = searching && networkUtils.isOnline() + val trackedEntities = withContext(dispatchers.io()) { + searchRepositoryKt.searchTrackedEntitiesImmediate( + searchParametersModel = searchParametersModel, + isOnline = isOnline, + ) + } + + if (trackedEntities.isEmpty() || trackedEntities.size > 1) return@launch + + val tei = trackedEntities.first() + val searchTeiModel = withContext(dispatchers.io()) { + searchRepository.transform( + /* searchItem = */ + tei, + /* selectedProgram = */ + searchParametersModel.selectedProgram, + /* offlineOnly = */ + !(isOnline && FilterManager.getInstance().stateFilters.isEmpty()), + /* sortingItem = */ + FilterManager.getInstance().sortingItem, + ) + } + + // Open TEI dashboard for the found TEI + onTeiClick( + teiUid = searchTeiModel.uid(), + enrollmentUid = searchTeiModel.selectedEnrollment.uid(), + online = searchTeiModel.isOnline, + ) + + searching = false + + clearQueryData() + clearFocus() + } + } + fun clearFocus() { val updatedItems = uiState.items.map { if (it.focused) {