Skip to content

Commit

Permalink
Removes paging in stock use case
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdyrod committed Dec 22, 2023
1 parent 9c02193 commit b4af35d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.dhis2.android.rtsm.data.models

import androidx.lifecycle.LiveData
import androidx.paging.PagedList

data class SearchResult(
val items: LiveData<PagedList<StockItem>>,
val totalCount: Int,
val items: LiveData<List<StockItem>>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface StockManager {
* and total count of matched items
*/

fun search(query: SearchParametersModel, ou: String?, config: AppConfig): SearchResult
suspend fun search(query: SearchParametersModel, ou: String?, config: AppConfig): SearchResult

fun saveTransaction(
items: List<StockEntry>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.dhis2.android.rtsm.services

import androidx.paging.DataSource
import androidx.paging.LivePagedListBuilder
import androidx.lifecycle.liveData
import io.reactivex.Single
import io.reactivex.disposables.CompositeDisposable
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.apache.commons.lang3.math.NumberUtils
import org.dhis2.android.rtsm.commons.Constants
import org.dhis2.android.rtsm.data.AppConfig
import org.dhis2.android.rtsm.data.models.IdentifiableModel
import org.dhis2.android.rtsm.data.models.SearchParametersModel
Expand Down Expand Up @@ -38,68 +38,59 @@ class StockManagerImpl @Inject constructor(
private val ruleValidationHelper: RuleValidationHelper,
) : StockManager {

override fun search(
override suspend fun search(
query: SearchParametersModel,
ou: String?,
config: AppConfig,
): SearchResult {
var teiRepository = d2.trackedEntityModule().trackedEntityInstanceQuery()
val list = withContext(Dispatchers.IO) {
var teiRepository = d2.trackedEntityModule().trackedEntityInstanceQuery()

if (!ou.isNullOrEmpty()) {
teiRepository.byOrgUnits()
.eq(ou)
.byOrgUnitMode()
.eq(OrganisationUnitMode.SELECTED)
if (!ou.isNullOrEmpty()) {
teiRepository.byOrgUnits()
.eq(ou)
.byOrgUnitMode()
.eq(OrganisationUnitMode.SELECTED)
.also { teiRepository = it }
}

teiRepository.byProgram()
.eq(config.program)
.also { teiRepository = it }
}

teiRepository.byProgram()
.eq(config.program)
.also { teiRepository = it }
if (!query.name.isNullOrEmpty()) {
teiRepository
.byQuery()
.like(query.name).also { teiRepository = it }
}

if (!query.name.isNullOrEmpty()) {
teiRepository
.byQuery()
.like(query.name).also { teiRepository = it }
}
if (!query.code.isNullOrEmpty()) {
teiRepository
.byQuery()
.eq(query.code)
.also { teiRepository = it }
}

if (!query.code.isNullOrEmpty()) {
teiRepository
.byQuery()
.eq(query.code)
teiRepository.orderByAttribute(config.itemName)
.eq(RepositoryScope.OrderByDirection.ASC)
.also { teiRepository = it }
}

teiRepository.orderByAttribute(config.itemName)
.eq(RepositoryScope.OrderByDirection.ASC)
.also { teiRepository = it }
val teiList = teiRepository.blockingGet()
.filter { it.deleted() == null || !it.deleted()!! }
.map { transform(it, config) }

val dataSource: DataSource<TrackedEntityInstance, StockItem> = teiRepository.dataSource
.mapByPage { filterDeleted(it.toMutableList()) }
.mapByPage { transform(it, config) }

val totalCount = teiRepository.blockingCount()
val pagedList = LivePagedListBuilder(
object : DataSource.Factory<TrackedEntityInstance, StockItem>() {
override fun create(): DataSource<TrackedEntityInstance, StockItem> {
return dataSource
}
},
Constants.ITEM_PAGE_SIZE,
)
.build()
teiList
}

return SearchResult(pagedList, totalCount)
return SearchResult(liveData { emit(list) })
}

private fun transform(teis: List<TrackedEntityInstance>, config: AppConfig): List<StockItem> {
return teis.map { tei ->
StockItem(
tei.uid(),
AttributeHelper.teiAttributeValueByAttributeUid(tei, config.itemName) ?: "",
getStockOnHand(tei, config.stockOnHand) ?: "",
)
}
private fun transform(tei: TrackedEntityInstance, config: AppConfig): StockItem {
return StockItem(
tei.uid(),
AttributeHelper.teiAttributeValueByAttributeUid(tei, config.itemName) ?: "",
getStockOnHand(tei, config.stockOnHand) ?: "",
)
}

private fun getStockOnHand(tei: TrackedEntityInstance, stockOnHandUid: String): String? {
Expand All @@ -125,14 +116,6 @@ class StockManagerImpl @Inject constructor(
return null
}

private fun filterDeleted(
list: List<TrackedEntityInstance>,
): List<TrackedEntityInstance> {
return list.filter {
it.deleted() == null || !it.deleted()!!
}
}

private fun createEventProjection(
facility: IdentifiableModel,
programStage: ProgramStage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.compose.ui.graphics.Color
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.asFlow
import androidx.lifecycle.switchMap
import androidx.lifecycle.viewModelScope
import com.jakewharton.rxrelay2.PublishRelay
import dagger.hilt.android.lifecycle.HiltViewModel
Expand Down Expand Up @@ -142,7 +141,17 @@ class ManageStockViewModel @Inject constructor(

fun refreshData() {
viewModelScope.launch {
getStockItems().asFlow().collect { stockItems ->
val result = stockManagerRepository.search(
search.value ?: SearchParametersModel(
null,
null,
transaction.value?.facility?.uid ?: "",
),
transaction.value?.facility?.uid,
config.value!!,
).items

result.asFlow().collect { stockItems ->
_stockItems.value = stockItems
populateTable()
}
Expand Down Expand Up @@ -175,13 +184,6 @@ class ManageStockViewModel @Inject constructor(
}
}

private fun getStockItems() = search.switchMap { q ->
val result =
stockManagerRepository.search(q, transaction.value?.facility?.uid, config.value!!)

result.items
}

private fun configureRelays() {
disposable.add(
searchRelay
Expand Down

0 comments on commit b4af35d

Please sign in to comment.