-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate loading of entities from references into a method
- Loading branch information
1 parent
17fc613
commit adeee34
Showing
3 changed files
with
114 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.commcare.tasks | ||
|
||
import android.content.Context | ||
import androidx.work.Worker | ||
import androidx.work.WorkerParameters | ||
import org.commcare.CommCareApplication | ||
import org.commcare.utils.SessionUnavailableException | ||
|
||
class PrimeEntityCache(appContext: Context, workerParams: WorkerParameters) | ||
: Worker(appContext, workerParams) { | ||
|
||
override fun doWork(): Result { | ||
try { | ||
PrimeEntityCacheHelper.getInstance().primeEntityCache() | ||
return Result.success(); | ||
} catch (_: SessionUnavailableException) { | ||
} | ||
return Result.failure() | ||
} | ||
|
||
override fun onStopped() { | ||
PrimeEntityCacheHelper.getInstance().cancel() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.commcare.tasks | ||
|
||
import io.reactivex.functions.Cancellable | ||
import okhttp3.internal.notifyAll | ||
import org.commcare.CommCareApplication | ||
import org.commcare.suite.model.Detail | ||
import org.commcare.suite.model.EntityDatum | ||
import org.commcare.utils.AndroidCommCarePlatform | ||
import org.javarosa.core.model.condition.EvaluationContext | ||
|
||
class PrimeEntityCacheHelper private constructor() : Cancellable { | ||
|
||
private var entityLoaderHelper: EntityLoaderHelper? = null | ||
private var inProgress = false | ||
|
||
companion object { | ||
@Volatile | ||
private var instance: PrimeEntityCacheHelper? = null | ||
|
||
fun getInstance() = | ||
instance ?: synchronized(this) { | ||
instance ?: PrimeEntityCacheHelper().also { instance = it } | ||
} | ||
} | ||
|
||
fun primeEntityCache() { | ||
checkPreConditions() | ||
primeEntityCacheForApp(CommCareApplication.instance().commCarePlatform) | ||
clearState() | ||
} | ||
|
||
private fun primeEntityCacheForApp(commCarePlatform: AndroidCommCarePlatform) { | ||
inProgress = true | ||
val commandMap = commCarePlatform.commandToEntryMap | ||
for (command in commandMap.keys()) { | ||
val entry = commandMap[command]!! | ||
val sessionDatums = entry.sessionDataReqs | ||
for (sessionDatum in sessionDatums) { | ||
if (sessionDatum is EntityDatum) { | ||
val shortDetailId = sessionDatum.shortDetail | ||
if (shortDetailId != null) { | ||
val detail = commCarePlatform.getDetail(shortDetailId) | ||
primeCacheForDetail(detail, sessionDatum) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun primeCacheForDetail(detail: Detail, sessionDatum: EntityDatum) { | ||
if (detail.shouldCache()) { | ||
entityLoaderHelper = EntityLoaderHelper(detail, evalCtx()) | ||
entityLoaderHelper!!.loadEntities(sessionDatum.nodeset) | ||
} | ||
} | ||
|
||
private fun evalCtx(): EvaluationContext { | ||
return CommCareApplication.instance().currentSessionWrapper.evaluationContext | ||
} | ||
|
||
private fun clearState() { | ||
entityLoaderHelper = null | ||
inProgress = false | ||
} | ||
|
||
private fun checkPreConditions() { | ||
require(CommCareApplication.instance().session.isActive) { "User session must be active to prime entity cache" } | ||
require(!inProgress) { "We are already priming the cache" } | ||
} | ||
|
||
override fun cancel() { | ||
entityLoaderHelper?.cancel() | ||
} | ||
} |