8000 fix: [ANDROAPP-6669] ANR in search paging by Balcan · Pull Request #4096 · dhis2/dhis2-android-capture-app · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

fix: [ANDROAPP-6669] ANR in search paging #4096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ dependencies {
testImplementation(libs.test.truth)
testImplementation(libs.test.kotlinCoroutines)
testImplementation(libs.test.turbine)

testImplementation(libs.test.androidx.paging)
androidTestUtil(libs.test.orchestrator)

androidTestImplementation(libs.test.testRunner)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.ui.test.junit4.ComposeTestRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performImeAction
import androidx.compose.ui.test.performTextReplacement
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
Expand Down Expand Up @@ -108,6 +109,10 @@ class EnrollmentRobot(val composeTestRule: ComposeTestRule) : BaseRobot() {
hasTestTag("INPUT_DATE_TIME_TEXT_FIELD") and hasAnySibling(hasText(label)),
useUnmergedTree = true,
).performTextReplacement(dateValue)
onNode(
hasTestTag("INPUT_DATE_TIME_TEXT_FIELD") and hasAnySibling(hasText(label)),
useUnmergedTree = true,
).performImeAction()
}
}

Expand All @@ -126,6 +131,14 @@ class EnrollmentRobot(val composeTestRule: ComposeTestRule) : BaseRobot() {
),
useUnmergedTree = true,
).performTextReplacement(dateValue)
onNode(
hasTestTag(
"INPUT_DATE_TIME_TEXT_FIELD"
) and hasAnySibling(
hasText(title)
),
useUnmergedTree = true,
).performImeAction()
}
}

Expand Down
8000
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.dhis2.R
Expand Down Expand Up @@ -128,6 +136,26 @@ class SearchTEIViewModel(

private var fetchJob: Job? = null

private val = 1)

val searchPagingData = onNewSearch.onStart { emit(Unit) }
.flatMapLatest {
flow {
CoroutineTracker.increment()
emitAll(
when {
searching -> loadSearchResults()
displayFrontPageList() -> loadDisplayInListResults()
else -> emptyFlow()
},
)
CoroutineTracker.decrement()
}
}
.flowOn(dispatchers.io())
.cachedIn(viewModelScope)
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), PagingData.empty())

init {
viewModelScope.launch(dispatchers.io()) {
createButtonScrollVisibility.postValue(
Expand Down Expand Up @@ -217,8 +245,7 @@ class SearchTEIViewModel(
previousSate = _screenState.value?.screenState ?: SearchScreenState.NONE,
listType = SearchScreenState.LIST,
displayFrontPageList = searchRepository.getProgram(initialProgramUid)
?.displayFrontPageList()
?: false,
?.displayFrontPageList() == true,
canCreateWithoutSearch = searchRepository.canCreateInProgramWithoutSearch(),
isSearching = searching,
searchForm = SearchForm(
Expand Down Expand Up @@ -384,26 +411,6 @@ class SearchTEIViewModel(
uiState = uiState.copy(searchEnabled = queryData.isNotEmpty())
}

fun fetchListResults(onPagedListReady: (Flow<PagingData<SearchTeiModel>>?) -> Unit) {
CoroutineTracker.increment()
viewModelScope.launch(dispatchers.io()) {
val resultPagedList = async {
when {
searching -> loadSearchResults().cachedIn(viewModelScope)
displayFrontPageList() -> loadDisplayInListResults().cachedIn(viewModelScope)
else -> null
}
}
try {
onPagedListReady(resultPagedList.await())
} catch (e: Exception) {
Timber.e(e)
} finally {
CoroutineTracker.decrement()
}
}
}

private suspend fun loadSearchResults() = withContext(dispatchers.io()) {
val searchParametersModel = SearchParametersModel(
selectedProgram = searchRepository.getProgram(initialProgramUid),
Expand Down Expand Up @@ -545,16 +552,8 @@ class SearchTEIViewModel(
when (_screenState.value?.screenState) {
SearchScreenState.LIST -> {
setListScreen()
fetchListResults { flow ->
flow?.let {
fetchListResults { flow ->
flow?.let {
_refreshData.postValue(Unit)
CoroutineTracker.decrement()
}
}
}
}
onNewSearch.emit(Unit)
CoroutineTracker.decrement()
}

SearchScreenState.MAP -> {
Expand All @@ -577,9 +576,10 @@ class SearchTEIViewModel(
uiState.updateMinAttributeWarning(true)
setSearchScreen()
_refreshData.postValue(Unit)
onNewSearch.emit(Unit)
}
} catch (e: Exception) {
Timber.d(e.message)
Timber.d(e)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.lifecycle.viewModelScope
import androidx.paging.LoadState
import androidx.recyclerview.widget.ConcatAdapter
Expand Down Expand Up @@ -286,9 +288,9 @@ class SearchTEList : FragmentGlobalAbstract() {
}

private fun observeNewData() {
initData()
viewModel.refreshData.observe(viewLifecycleOwner) {
restoreAdapters()
initData()
}

viewModel.dataResult.observe(viewLifecycleOwner) {
Expand Down Expand Up @@ -348,14 +350,15 @@ class SearchTEList : FragmentGlobalAbstract() {
private fun initData() {
displayLoadingData()

viewModel.fetchListResults {
lifecycleScope.launch {
it?.takeIf { view != null }?.collectLatest {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.searchPagingData.collect { data ->
liveAdapter.addOnPagesUpdatedListener {
onInitDataLoaded()
CoroutineTracker.decrement()
}
liveAdapter.submitData(lifecycle, it)
} ?: onInitDataLoaded()
liveAdapter.submitData(lifecycle, data)
}
}
}
}
Expand Down
< CE32 /tr>
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import androidx.compose.material.icons.automirrored.filled.List
import androidx.compose.material.icons.automirrored.outlined.List
import androidx.compose.material.icons.filled.Map
import androidx.compose.material.icons.outlined.Map
import androidx.paging.PagingData
import androidx.paging.testing.asSnapshot
import app.cash.turbine.test
import com.mapbox.geojson.BoundingBox
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
Expand Down Expand Up @@ -38,6 +42,7 @@ import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.times
Expand All @@ -53,7 +58,9 @@ class SearchTEIViewModelTest {
private val initialProgram = "programUid"
private val initialQuery = mutableMapOf<String, String>()
private val repository: SearchRepository = mock()
private val repositoryKt: SearchRepositoryKt = mock()
private val repositoryKt: SearchRepositoryKt = mock {
on { searchTrackedEntities(any(), any()) } doReturn flowOf(PagingData.empty())
}
private val pageConfigurator: SearchPageConfigurator = mock()
private val mapDataRepository: MapDataRepository = mock()
private val networkUtils: NetworkUtils = mock()
Expand Down Expand Up @@ -205,48 +212,50 @@ class SearchTEIViewModelTest {

@ExperimentalCoroutinesApi
@Test
fun `Should return local results LiveData if not searching and displayInList is true`() {
val testingProgram = testingProgram()
setCurrentProgram(testingProgram)
viewModel.fetchListResults {}
testingDispatcher.scheduler.advanceUntilIdle()
verify(repositoryKt).searchTrackedEntities(
SearchParametersModel(
selectedProgram = testingProgram,
queryData = mutableMapOf(),
),
false,
)
}
fun `Should return local results LiveData if not searching and displayInList is true`() =
runTest {
val testingProgram = testingProgram()
setCurrentProgram(testingProgram)

viewModel.searchPagingData.take(1).asSnapshot()

verify(repositoryKt).searchTrackedEntities(
SearchParametersModel(
selectedProgram = testingProgram,
queryData = mutableMapOf(),
),
false,
)
}

@Test
fun `Should return null if not searching and displayInList is false`() {
fun `Should return null if not searching and displayInList is false`() = runTest {
val testingProgram = testingProgram(displayFrontPageList = false)
setCurrentProgram(testingProgram)
viewModel.fetchListResults {}

verify(repositoryKt, times(0)).searchTrackedEntities(
SearchParametersModel(
selectedProgram = testingProgram,
queryData = mutableMapOf(),
),
true,
)
viewModel.searchPagingData.test {
awaitItem()
verify(repositoryKt, times(0)).searchTrackedEntities(
SearchParametersModel(
selectedProgram = testingProgram,
queryData = mutableMapOf(),
),
true,
)

verify(repositoryKt, times(0)).searchTrackedEntities(
SearchParametersModel(
selectedProgram = testingProgram,
queryData = mutableMapOf(),
),
false,
)
verify(repositoryKt, times(0)).searchTrackedEntities(
SearchParametersModel(
selectedProgram = testingProgram,
queryData = mutableMapOf(),
),
false,
)
}
}

@Test
fun `Should return null global results if not searching`() {
viewModel.fetchListResults {
assertTrue(it == null)
}
fun `Should return empty global results if not searching`() = runTest {
val result = viewModel.searchPagingData.take(1).asSnapshot()
assertTrue(result.isEmpty())
}

@ExperimentalCoroutinesApi
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ recyclerview = "1.3.1"
compose = "1.5.4"
composePlugin = "1.7.3"
composePaging = "3.3.0"
pagingTesting = "3.3.0"
composeLifecycle = "2.8.1"
composeConstraintLayout = "1.0.1"
activityCompose = "1.8.2"
Expand Down Expand Up @@ -119,6 +120,7 @@ androidx-compose-uitooling = { group = "androidx.compose.ui", name = "ui-tooling
androidx-compose-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview", version.ref = "compose" }
androidx-compose-viewbinding = { group = "androidx.compose.ui", name = "ui-viewbinding", version.ref = "compose" }
androidx-compose-paging = { group = "androidx.paging", name = "paging-compose", version.ref = "composePaging" }
test-androidx-paging = { group = "androidx.paging", name = "paging-testing", version.ref = "pagingTesting" }
androidx-compose-lifecycle = { group = "androidx.lifecycle", name = "lifecycle-runtime-compose", version.ref = "composeLifecycle" }
androidx-coreKtx = { group = "androidx.core", name = "core-ktx", version.ref = "corektx" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
Expand Down
Loading
0