Skip to content
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
6 changes: 4 additions & 2 deletions app/src/org/commcare/entity/AndroidAsyncNodeEntityFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class AndroidAsyncNodeEntityFactory(
getCurrentCommandId(),
detail,
entityDatum,
entities
entities,
this
)
schedulePrimeEntityCacheWorker()
} else {
Expand All @@ -70,7 +71,8 @@ class AndroidAsyncNodeEntityFactory(
getCurrentCommandId(),
detail,
entityDatum,
entities
entities,
this
)
}
}
Expand Down
43 changes: 23 additions & 20 deletions app/src/org/commcare/tasks/EntityLoaderHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,29 @@ class EntityLoaderHelper(
detail: Detail,
sessionDatum: EntityDatum?,
evalCtx: EvaluationContext,
inBackground: Boolean
inBackground: Boolean,
var factory: NodeEntityFactory? = null
) : Cancellable {

var focusTargetIndex: Int = -1
private var stopLoading: Boolean = false
var factory: NodeEntityFactory

init {
evalCtx.addFunctionHandler(EntitySelectActivity.getHereFunctionHandler())
if (detail.shouldOptimize()) {
val entityStorageCache: EntityStorageCache = CommCareEntityStorageCache("case")
factory = AndroidAsyncNodeEntityFactory(detail, sessionDatum, evalCtx, entityStorageCache, inBackground)
} else if (detail.useAsyncStrategy()) {
// legacy cache and index
val entityStorageCache: EntityStorageCache = CommCareEntityStorageCache("case")
factory = AsyncNodeEntityFactory(detail, evalCtx, entityStorageCache, inBackground)
} else {
factory = NodeEntityFactory(detail, evalCtx)
if (DeveloperPreferences.collectAndDisplayEntityTraces()) {
factory.activateDebugTraceOutput()
if (factory == null) {
if (detail.shouldOptimize()) {
val entityStorageCache: EntityStorageCache = CommCareEntityStorageCache("case")
factory =
AndroidAsyncNodeEntityFactory(detail, sessionDatum, evalCtx, entityStorageCache, inBackground)
} else if (detail.useAsyncStrategy()) {
// legacy cache and index
val entityStorageCache: EntityStorageCache = CommCareEntityStorageCache("case")
factory = AsyncNodeEntityFactory(detail, evalCtx, entityStorageCache, inBackground)
} else {
factory = NodeEntityFactory(detail, evalCtx)
if (DeveloperPreferences.collectAndDisplayEntityTraces()) {
factory!!.activateDebugTraceOutput()
}
}
}
}
Expand All @@ -60,11 +63,11 @@ class EntityLoaderHelper(
CommCareApplication.instance().currentApp.primeEntityCacheHelper.cancelWork()
}
try {
val references = factory.expandReferenceList(nodeset)
val references = factory!!.expandReferenceList(nodeset)
val entities = loadEntitiesWithReferences(references, progressListener)
entities?.let {
factory.prepareEntities(entities)
factory.printAndClearTraces("build")
factory!!.prepareEntities(entities)
factory!!.printAndClearTraces("build")
return Pair<List<Entity<TreeReference>>, List<TreeReference>>(entities, references)
}
} finally {
Expand All @@ -80,14 +83,14 @@ class EntityLoaderHelper(
* Primes the entity cache
*/
fun cacheEntities(nodeset: TreeReference): Pair<List<Entity<TreeReference>>, List<TreeReference>> {
val references = factory.expandReferenceList(nodeset)
val references = factory!!.expandReferenceList(nodeset)
val entities = loadEntitiesWithReferences(references, null)
cacheEntities(entities)
return Pair<List<Entity<TreeReference>>, List<TreeReference>>(entities, references)
}

fun cacheEntities(entities: MutableList<Entity<TreeReference>>?) {
factory.cacheEntities(entities)
factory!!.cacheEntities(entities)
}

/**
Expand All @@ -109,7 +112,7 @@ class EntityLoaderHelper(
if (stopLoading) {
return null
}
val e = factory.getEntity(ref)
val e = factory!!.getEntity(ref)
if (e != null) {
entities.add(e)
if (e.shouldReceiveFocus()) {
Expand All @@ -123,6 +126,6 @@ class EntityLoaderHelper(

override fun cancel() {
stopLoading = true
factory.markAsCancelled()
factory!!.markAsCancelled()
}
}
2 changes: 1 addition & 1 deletion app/src/org/commcare/tasks/EntityLoaderTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class EntityLoaderTask
* @param evalCtx evaluation context
*/
public EntityLoaderTask(Detail detail, @Nullable EntityDatum entityDatum, EvaluationContext evalCtx) {
entityLoaderHelper = new EntityLoaderHelper(detail, entityDatum, evalCtx, false);
entityLoaderHelper = new EntityLoaderHelper(detail, entityDatum, evalCtx, false, null);
// we only want to provide progress updates for the new caching config
provideDetailProgressUpdates = detail.shouldOptimize();
}
Expand Down
11 changes: 7 additions & 4 deletions app/src/org/commcare/tasks/PrimeEntityCacheHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.runBlocking
import org.commcare.AppUtils.getCurrentAppId
import org.commcare.CommCareApplication
import org.commcare.cases.entity.AsyncNodeEntityFactory
import org.commcare.cases.entity.Entity
import org.commcare.cases.entity.EntityLoadingProgressListener
import org.commcare.suite.model.Detail
Expand Down Expand Up @@ -131,11 +132,12 @@ class PrimeEntityCacheHelper() : Cancellable, EntityLoadingProgressListener {
commandId: String,
detail: Detail,
entityDatum: EntityDatum,
entities: MutableList<Entity<TreeReference>>
entities: MutableList<Entity<TreeReference>>,
factory: AsyncNodeEntityFactory? = null
) {
checkPreConditions()
try {
primeCacheForDetail(commandId, detail, entityDatum, entities)
primeCacheForDetail(commandId, detail, entityDatum, entities, false, factory)
} finally {
clearState()
runBlocking {
Expand Down Expand Up @@ -184,12 +186,13 @@ class PrimeEntityCacheHelper() : Cancellable, EntityLoadingProgressListener {
entityDatum: EntityDatum,
entities: MutableList<Entity<TreeReference>>? = null,
inBackground: Boolean = false,
factory: AsyncNodeEntityFactory? = null
) {
if (!detail.isCacheEnabled() || cancelled) return
currentDatumInProgress = entityDatum.dataId
currentDetailInProgress = detail.id
entityLoaderHelper = EntityLoaderHelper(detail, entityDatum, evalCtx(commandId), inBackground).also {
it.factory.setEntityProgressListener(this)
entityLoaderHelper = EntityLoaderHelper(detail, entityDatum, evalCtx(commandId), inBackground, factory).also {
it.factory!!.setEntityProgressListener(this)
}
// Handle the cache operation based on the available input
val cachedEntities = when {
Expand Down