Skip to content

Commit

Permalink
Fix UI freeze on launch register screen (#3599)
Browse files Browse the repository at this point in the history
* Move tasks to io thread

Signed-off-by: Elly Kitoto <[email protected]>

* Separate count state from the rest of register UI state

Signed-off-by: Elly Kitoto <[email protected]>

* Run spotless

Signed-off-by: Elly Kitoto <[email protected]>

---------

Signed-off-by: Elly Kitoto <[email protected]>
  • Loading branch information
ellykits authored Nov 12, 2024
1 parent 6fc15b0 commit 4217119
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import org.smartregister.fhircore.quest.ui.register.NO_REGISTER_VIEW_COLUMN_TEST
import org.smartregister.fhircore.quest.ui.register.NoRegisterDataView
import org.smartregister.fhircore.quest.ui.register.REGISTER_CARD_TEST_TAG
import org.smartregister.fhircore.quest.ui.register.RegisterScreen
import org.smartregister.fhircore.quest.ui.register.RegisterUiCountState
import org.smartregister.fhircore.quest.ui.register.RegisterUiState
import org.smartregister.fhircore.quest.ui.register.TOP_REGISTER_SCREEN_TEST_TAG
import org.smartregister.fhircore.quest.ui.shared.components.SYNC_PROGRESS_INDICATOR_TEST_TAG
Expand Down Expand Up @@ -126,9 +127,6 @@ class RegisterScreenTest {
registerConfiguration =
configurationRegistry.retrieveConfiguration(ConfigType.Register, "householdRegister"),
registerId = "register101",
totalRecordsCount = 1,
filteredRecordsCount = 0,
pagesCount = 0,
progressPercentage = flowOf(0),
isSyncUpload = flowOf(false),
params = emptyList(),
Expand All @@ -146,6 +144,12 @@ class RegisterScreenTest {
openDrawer = {},
onEvent = {},
registerUiState = registerUiState,
registerUiCountState =
RegisterUiCountState(
totalRecordsCount = 1,
filteredRecordsCount = 0,
pagesCount = 0,
),
onAppMainEvent = {},
searchQuery = searchText,
currentPage = currentPage,
Expand All @@ -168,9 +172,6 @@ class RegisterScreenTest {
registerConfiguration =
configurationRegistry.retrieveConfiguration(ConfigType.Register, "householdRegister"),
registerId = "register101",
totalRecordsCount = 1,
filteredRecordsCount = 0,
pagesCount = 1,
progressPercentage = flowOf(0),
isSyncUpload = flowOf(false),
params = emptyList(),
Expand All @@ -188,6 +189,12 @@ class RegisterScreenTest {
openDrawer = {},
onEvent = {},
registerUiState = registerUiState,
registerUiCountState =
RegisterUiCountState(
totalRecordsCount = 1,
filteredRecordsCount = 0,
pagesCount = 1,
),
onAppMainEvent = {},
searchQuery = searchText,
currentPage = currentPage,
Expand All @@ -213,13 +220,17 @@ class RegisterScreenTest {
registerConfiguration =
configurationRegistry.retrieveConfiguration(ConfigType.Register, "householdRegister"),
registerId = "register101",
totalRecordsCount = 1,
filteredRecordsCount = 0,
pagesCount = 1,
progressPercentage = flowOf(0),
isSyncUpload = flowOf(false),
params = emptyList(),
)

val registerUiCountState =
RegisterUiCountState(
totalRecordsCount = 1,
filteredRecordsCount = 0,
pagesCount = 1,
)
val searchText = mutableStateOf(SearchQuery.emptyText)
val currentPage = mutableStateOf(0)

Expand All @@ -233,6 +244,7 @@ class RegisterScreenTest {
openDrawer = {},
onEvent = {},
registerUiState = registerUiState,
registerUiCountState = registerUiCountState,
onAppMainEvent = {},
searchQuery = searchText,
currentPage = currentPage,
Expand All @@ -258,9 +270,6 @@ class RegisterScreenTest {
registerConfiguration =
configurationRegistry.retrieveConfiguration(ConfigType.Register, "childRegister"),
registerId = "register101",
totalRecordsCount = 0,
filteredRecordsCount = 0,
pagesCount = 1,
progressPercentage = flowOf(0),
isSyncUpload = flowOf(false),
params = emptyList(),
Expand All @@ -283,6 +292,12 @@ class RegisterScreenTest {
openDrawer = {},
onEvent = {},
registerUiState = registerUiState,
registerUiCountState =
RegisterUiCountState(
totalRecordsCount = 0,
filteredRecordsCount = 0,
pagesCount = 1,
),
onAppMainEvent = {},
searchQuery = searchText,
currentPage = currentPage,
Expand All @@ -304,9 +319,6 @@ class RegisterScreenTest {
registerConfiguration =
configurationRegistry.retrieveConfiguration(ConfigType.Register, "householdRegister"),
registerId = "register101",
totalRecordsCount = 1,
filteredRecordsCount = 0,
pagesCount = 0,
progressPercentage = flowOf(0),
isSyncUpload = flowOf(false),
params = emptyList(),
Expand All @@ -324,6 +336,12 @@ class RegisterScreenTest {
openDrawer = {},
onEvent = {},
registerUiState = registerUiState,
registerUiCountState =
RegisterUiCountState(
totalRecordsCount = 1,
filteredRecordsCount = 0,
pagesCount = 0,
),
onAppMainEvent = {},
searchQuery = searchText,
currentPage = currentPage,
Expand Down Expand Up @@ -351,9 +369,6 @@ class RegisterScreenTest {
listOf(ActionConfig(trigger = ActionTrigger.ON_SEARCH_SINGLE_RESULT)),
),
registerId = "register101",
totalRecordsCount = 1,
filteredRecordsCount = 0,
pagesCount = 0,
progressPercentage = flowOf(0),
isSyncUpload = flowOf(false),
params = emptyList(),
Expand All @@ -371,6 +386,12 @@ class RegisterScreenTest {
openDrawer = {},
onEvent = {},
registerUiState = registerUiState,
registerUiCountState =
RegisterUiCountState(
totalRecordsCount = 1,
filteredRecordsCount = 0,
pagesCount = 0,
),
onAppMainEvent = {},
searchQuery = searchText,
currentPage = currentPage,
Expand Down Expand Up @@ -429,9 +450,6 @@ class RegisterScreenTest {
registerConfiguration =
configurationRegistry.retrieveConfiguration(ConfigType.Register, "householdRegister"),
registerId = "register101",
totalRecordsCount = 1,
filteredRecordsCount = 0,
pagesCount = 0,
progressPercentage = flowOf(50),
isSyncUpload = flowOf(true),
currentSyncJobStatus =
Expand All @@ -455,6 +473,12 @@ class RegisterScreenTest {
openDrawer = {},
onEvent = {},
registerUiState = registerUiState,
registerUiCountState =
RegisterUiCountState(
totalRecordsCount = 1,
filteredRecordsCount = 0,
pagesCount = 0,
),
appDrawerUIState =
AppDrawerUIState(
currentSyncJobStatus =
Expand Down Expand Up @@ -489,9 +513,6 @@ class RegisterScreenTest {
registerConfiguration =
configurationRegistry.retrieveConfiguration(ConfigType.Register, "householdRegister"),
registerId = "register101",
totalRecordsCount = 1,
filteredRecordsCount = 0,
pagesCount = 0,
progressPercentage = flowOf(100),
isSyncUpload = flowOf(false),
currentSyncJobStatus = flowOf(CurrentSyncJobStatus.Succeeded(OffsetDateTime.now())),
Expand All @@ -509,6 +530,12 @@ class RegisterScreenTest {
openDrawer = {},
onEvent = {},
registerUiState = registerUiState,
registerUiCountState =
RegisterUiCountState(
totalRecordsCount = 1,
filteredRecordsCount = 0,
pagesCount = 0,
),
appDrawerUIState =
AppDrawerUIState(
currentSyncJobStatus = CurrentSyncJobStatus.Succeeded(OffsetDateTime.now()),
Expand Down Expand Up @@ -541,9 +568,6 @@ class RegisterScreenTest {
registerConfiguration =
configurationRegistry.retrieveConfiguration(ConfigType.Register, "householdRegister"),
registerId = "register101",
totalRecordsCount = 1,
filteredRecordsCount = 0,
pagesCount = 0,
progressPercentage = flowOf(100),
isSyncUpload = flowOf(false),
currentSyncJobStatus = flowOf(CurrentSyncJobStatus.Succeeded(OffsetDateTime.now())),
Expand All @@ -561,6 +585,12 @@ class RegisterScreenTest {
openDrawer = {},
onEvent = {},
registerUiState = registerUiState,
registerUiCountState =
RegisterUiCountState(
totalRecordsCount = 1,
filteredRecordsCount = 0,
pagesCount = 0,
),
appDrawerUIState =
AppDrawerUIState(
currentSyncJobStatus = CurrentSyncJobStatus.Failed(OffsetDateTime.now()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import org.junit.Test
import org.smartregister.fhircore.engine.configuration.register.RegisterCardConfig
import org.smartregister.fhircore.engine.configuration.view.CompoundTextProperties
import org.smartregister.fhircore.engine.domain.model.ResourceData
import org.smartregister.fhircore.quest.ui.register.RegisterUiCountState
import org.smartregister.fhircore.quest.ui.register.RegisterUiState
import org.smartregister.fhircore.quest.ui.register.components.REGISTER_CARD_LIST_TEST_TAG
import org.smartregister.fhircore.quest.ui.register.components.RegisterCardList
Expand All @@ -58,6 +59,7 @@ class RegisterCardListTest {
lazyListState = rememberLazyListState(),
onEvent = {},
registerUiState = RegisterUiState(),
registerUiCountState = RegisterUiCountState(),
currentPage = mutableStateOf(1),
onSearchByQrSingleResultAction = {},
decodeImage = null,
Expand All @@ -84,6 +86,7 @@ class RegisterCardListTest {
lazyListState = rememberLazyListState(),
onEvent = {},
registerUiState = RegisterUiState(),
registerUiCountState = RegisterUiCountState(),
currentPage = mutableStateOf(1),
onSearchByQrSingleResultAction = {},
decodeImage = null,
Expand Down Expand Up @@ -116,6 +119,7 @@ class RegisterCardListTest {
lazyListState = rememberLazyListState(),
onEvent = {},
registerUiState = RegisterUiState(),
registerUiCountState = RegisterUiCountState(),
currentPage = mutableStateOf(1),
showPagination = true,
onSearchByQrSingleResultAction = {},
Expand Down Expand Up @@ -144,6 +148,7 @@ class RegisterCardListTest {
lazyListState = rememberLazyListState(),
onEvent = {},
registerUiState = RegisterUiState(),
registerUiCountState = RegisterUiCountState(),
currentPage = mutableStateOf(1),
onSearchByQrSingleResultAction = {},
decodeImage = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,12 @@ class RegisterFragment : Fragment(), OnSyncListener {
savedInstanceState: Bundle?,
): View {
with(registerFragmentArgs) {
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.CREATED) {
registerViewModel.retrieveRegisterUiState(
registerId = registerId,
screenTitle = screenTitle,
params = params,
clearCache = false,
)
}
}
registerViewModel.retrieveRegisterUiState(
registerId = registerId,
screenTitle = screenTitle,
params = params,
clearCache = false,
)
}
return ComposeView(requireContext()).apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
Expand Down Expand Up @@ -174,6 +170,7 @@ class RegisterFragment : Fragment(), OnSyncListener {
openDrawer = openDrawer,
onEvent = registerViewModel::onEvent,
registerUiState = registerViewModel.registerUiState.value,
registerUiCountState = registerViewModel.registerUiCountState.value,
appDrawerUIState = appMainViewModel.appDrawerUiState.value,
onAppMainEvent = { appMainViewModel.onEvent(it) },
searchQuery = searchViewModel.searchQuery,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ fun RegisterScreen(
openDrawer: (Boolean) -> Unit,
onEvent: (RegisterEvent) -> Unit,
registerUiState: RegisterUiState,
registerUiCountState: RegisterUiCountState,
appDrawerUIState: AppDrawerUIState = AppDrawerUIState(),
onAppMainEvent: (AppMainEvent) -> Unit,
searchQuery: MutableState<SearchQuery>,
Expand All @@ -114,7 +115,7 @@ fun RegisterScreen(
registerUiState.registerConfiguration?.topScreenSection?.title ?: ""
},
searchQuery = searchQuery.value,
filteredRecordsCount = registerUiState.filteredRecordsCount,
filteredRecordsCount = registerUiCountState.filteredRecordsCount,
isSearchBarVisible = registerUiState.registerConfiguration?.searchBar?.visible ?: true,
searchPlaceholder = registerUiState.registerConfiguration?.searchBar?.display,
showSearchByQrCode = registerUiState.registerConfiguration?.showSearchByQrCode ?: false,
Expand Down Expand Up @@ -198,6 +199,7 @@ fun RegisterScreen(
lazyListState = lazyListState,
onEvent = onEvent,
registerUiState = registerUiState,
registerUiCountState = registerUiCountState,
currentPage = currentPage,
showPagination =
!registerUiState.registerConfiguration.infiniteScroll &&
Expand Down Expand Up @@ -292,13 +294,17 @@ fun RegisterScreenWithDataPreview() {
FhirResourceConfig(baseResource = ResourceConfig(resource = ResourceType.Patient)),
),
registerId = "register101",
totalRecordsCount = 1,
filteredRecordsCount = 0,
pagesCount = 1,
progressPercentage = flowOf(0),
isSyncUpload = flowOf(false),
params = emptyList(),
)

val registerUiCountState =
RegisterUiCountState(
totalRecordsCount = 1,
filteredRecordsCount = 0,
pagesCount = 1,
)
val searchText = remember { mutableStateOf(SearchQuery.emptyText) }
val currentPage = remember { mutableIntStateOf(0) }
val data = listOf(ResourceData("1", ResourceType.Patient, emptyMap()))
Expand All @@ -310,6 +316,7 @@ fun RegisterScreenWithDataPreview() {
openDrawer = {},
onEvent = {},
registerUiState = registerUiState,
registerUiCountState = registerUiCountState,
onAppMainEvent = {},
searchQuery = searchText,
currentPage = currentPage,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2021-2024 Ona Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.smartregister.fhircore.quest.ui.register

data class RegisterUiCountState(
val totalRecordsCount: Long = 0,
val filteredRecordsCount: Long? = null,
val pagesCount: Int = 1,
)
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ data class RegisterUiState(
val isFirstTimeSync: Boolean = false,
val registerConfiguration: RegisterConfiguration? = null,
val registerId: String = "",
val totalRecordsCount: Long = 0,
val filteredRecordsCount: Long = 0,
val pagesCount: Int = 1,
val progressPercentage: Flow<Int> = flowOf(0),
val isSyncUpload: Flow<Boolean> = flowOf(false),
val currentSyncJobStatus: Flow<CurrentSyncJobStatus?> = flowOf(null),
Expand Down
Loading

0 comments on commit 4217119

Please sign in to comment.