-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
Cache and Lazy-Load Modifications #2928
Draft
shubham1g5
wants to merge
19
commits into
master
Choose a base branch
from
primeCacheAheadOfTime
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+613
−47
Draft
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2518b23
Adds a worker task to prime entity cache
shubham1g5 878c09c
Schedule prime cache task after 412 and on initializing user session
shubham1g5 c1e6a35
Set up progress indicator on entity list screen
shubham1g5 9aa1220
Creates a Android specific async factory to bind to any existing prim…
shubham1g5 c4a68a6
Move methods to schedule/cancel cache work to cache helper
shubham1g5 d78960d
Re-schedule cancelled work when expediting a detail sepcific cache
shubham1g5 dd05545
Improve progress tracking by providing information on individual enti…
shubham1g5 acd3aa6
Handle for new cache config while initiating entity factory
shubham1g5 7122c92
Cancellation handling
shubham1g5 63743aa
Always clear state after prime cache worker run
shubham1g5 ede6caf
correct eval context by including command id
shubham1g5 f145dd6
Fixes progress tracking plus tracking the live worker in async task
shubham1g5 8b53cbf
Uses datum id to track progress instead of detail as a detail can pot…
shubham1g5 1e8dfd3
Thread safety
shubham1g5 32d554e
lint
shubham1g5 90b0c96
Make clear distinction in between old and new cache and index switch
shubham1g5 ab5d325
Priming cache for normal and sort values for the detail fields marked…
shubham1g5 6c9be03
Tweaks
shubham1g5 dbbcfd4
fixes test dependent on work manager init
shubham1g5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -22,7 +22,6 @@ | |
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.fragment.app.FragmentManager; | ||
|
||
import com.jakewharton.rxbinding2.widget.AdapterViewItemClickEvent; | ||
|
@@ -58,6 +57,7 @@ | |
import org.commcare.utils.EntityDetailUtils; | ||
import org.commcare.utils.EntitySelectRefreshTimer; | ||
import org.commcare.utils.SerializationUtil; | ||
import org.commcare.utils.StringUtils; | ||
import org.commcare.views.EntityView; | ||
import org.commcare.views.TabbedDetailView; | ||
import org.commcare.views.UserfacingErrorHandling; | ||
|
@@ -166,6 +166,8 @@ public class EntitySelectActivity extends SaveSessionCommCareActivity | |
// Handler for displaying alert dialog when no location providers are found | ||
private final LocationNotificationHandler locationNotificationHandler = | ||
new LocationNotificationHandler(this); | ||
private AdapterView visibleView; | ||
private TextView progressTv; | ||
|
||
@Override | ||
public void onCreateSessionSafe(Bundle savedInstanceState) { | ||
|
@@ -254,7 +256,6 @@ private void setupUI(boolean isOrientationChange) { | |
setContentView(R.layout.entity_select_layout); | ||
} | ||
|
||
AdapterView visibleView; | ||
GridView gridView = this.findViewById(R.id.screen_entity_select_grid); | ||
ListView listView = this.findViewById(R.id.screen_entity_select_list); | ||
if (shortSelect.shouldBeLaidOutInGrid()) { | ||
|
@@ -268,6 +269,7 @@ private void setupUI(boolean isOrientationChange) { | |
gridView.setVisibility(View.GONE); | ||
EntitySelectViewSetup.setupDivider(this, listView, shortSelect.usesEntityTileView()); | ||
} | ||
progressTv = findViewById(R.id.progress_text); | ||
RxAdapterView.itemClickEvents(visibleView) | ||
.subscribeOn(AndroidSchedulers.mainThread()) | ||
.throttleFirst(CLICK_DEBOUNCE_TIME, TimeUnit.MILLISECONDS) | ||
|
@@ -356,7 +358,7 @@ private void setupUIFromAdapter(AdapterView view) { | |
if (view instanceof ListView) { | ||
EntitySelectViewSetup.setupDivider(this, (ListView)view, shortSelect.usesEntityTileView()); | ||
} | ||
findViewById(R.id.entity_select_loading).setVisibility(View.GONE); | ||
findViewById(R.id.progress_container).setVisibility(View.GONE); | ||
entitySelectSearchUI.setSearchBannerState(); | ||
} | ||
|
||
|
@@ -476,6 +478,7 @@ public boolean loadEntities() { | |
} | ||
|
||
if (loader == null && !EntityLoaderTask.attachToActivity(this)) { | ||
setProgressText(StringUtils.getStringRobust(this, R.string.entity_list_initializing)); | ||
EntityLoaderTask entityLoader = new EntityLoaderTask(shortSelect, evalContext()); | ||
entityLoader.attachListener(this); | ||
entityLoader.executeParallel(selectDatum.getNodeset()); | ||
|
@@ -852,16 +855,7 @@ public void deliverLoadResult(List<Entity<TreeReference>> entities, | |
List<TreeReference> references, | ||
NodeEntityFactory factory, int focusTargetIndex) { | ||
loader = null; | ||
|
||
AdapterView visibleView; | ||
if (shortSelect.shouldBeLaidOutInGrid()) { | ||
visibleView = ((GridView)this.findViewById(R.id.screen_entity_select_grid)); | ||
} else { | ||
ListView listView = this.findViewById(R.id.screen_entity_select_list); | ||
EntitySelectViewSetup.setupDivider(this, listView, shortSelect.usesEntityTileView()); | ||
visibleView = listView; | ||
} | ||
|
||
setProgressText(StringUtils.getStringRobust(this, R.string.entity_list_finalizing)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This message doesn't seem like the right one to me. Isn't this just the start of the loading process? |
||
adapter = new EntityListAdapter(this, shortSelect, references, entities, factory, | ||
hideActionsFromEntityList, shortSelect.getCustomActions(evalContext()), inAwesomeMode); | ||
visibleView.setAdapter(adapter); | ||
|
@@ -883,7 +877,7 @@ public void deliverLoadResult(List<Entity<TreeReference>> entities, | |
} | ||
} | ||
|
||
findViewById(R.id.entity_select_loading).setVisibility(View.GONE); | ||
findViewById(R.id.progress_container).setVisibility(View.GONE); | ||
|
||
if (adapter != null) { | ||
// filter by additional session data (search string, callout result data) | ||
|
@@ -907,6 +901,10 @@ public void deliverLoadResult(List<Entity<TreeReference>> entities, | |
} | ||
} | ||
|
||
private void setProgressText(String message) { | ||
progressTv.setText(message); | ||
} | ||
|
||
private void restoreAdapterStateFromSession() { | ||
entitySelectSearchUI.restoreSearchString(); | ||
|
||
|
@@ -933,7 +931,7 @@ private void updateSelectedItem(TreeReference selected, boolean forceMove) { | |
|
||
@Override | ||
public void attachLoader(EntityLoaderTask task) { | ||
findViewById(R.id.entity_select_loading).setVisibility(View.VISIBLE); | ||
findViewById(R.id.progress_container).setVisibility(View.VISIBLE); | ||
this.loader = task; | ||
} | ||
|
||
|
@@ -995,6 +993,15 @@ public void deliverLoadError(Exception e) { | |
displayCaseListLoadException(e); | ||
} | ||
|
||
@Override | ||
public void deliverProgress(Integer[] values) { | ||
// throttle to not update text too frequently | ||
if (values[0] % 100 == 0) { | ||
setProgressText(StringUtils.getStringRobust(this, R.string.entity_list_processing, | ||
new String[]{String.valueOf(values[0]), String.valueOf(values[1])})); | ||
} | ||
} | ||
|
||
@Override | ||
protected boolean onForwardSwipe() { | ||
// If user has picked an entity, move along to form entry | ||
|
90 changes: 90 additions & 0 deletions
90
app/src/org/commcare/entity/AndroidAsyncNodeEntityFactory.kt
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,90 @@ | ||
package org.commcare.entity | ||
|
||
import android.util.Pair | ||
import org.commcare.cases.entity.AsyncNodeEntityFactory | ||
import org.commcare.cases.entity.Entity | ||
import org.commcare.cases.entity.EntityStorageCache | ||
import org.commcare.suite.model.Detail | ||
import org.commcare.tasks.PrimeEntityCacheHelper | ||
import org.commcare.util.LogTypes | ||
import org.javarosa.core.model.condition.EvaluationContext | ||
import org.javarosa.core.model.instance.TreeReference | ||
import org.javarosa.core.services.Logger | ||
|
||
/** | ||
* Android Specific Implementation of AsyncNodeEntityFactory | ||
* Uses [PrimeEntityCacheHelper] to prime entity cache blocking the user when required | ||
*/ | ||
class AndroidAsyncNodeEntityFactory(d: Detail, ec: EvaluationContext?, entityStorageCache: EntityStorageCache?) : | ||
AsyncNodeEntityFactory(d, ec, entityStorageCache), PrimeEntityCacheListener { | ||
|
||
companion object { | ||
const val TWO_MINUTES = 2 * 60 * 1000 | ||
} | ||
|
||
private var cachedEntities: List<Entity<TreeReference>>? = null | ||
private var completedCachePrime = false | ||
|
||
override fun prepareEntitiesInternal(entities: MutableList<Entity<TreeReference>>) { | ||
if (detail.shouldCache()) { | ||
// we only want to block if lazy load is not enabled | ||
if (!detail.shouldLazyLoad()) { | ||
val primeEntityCacheHelper = PrimeEntityCacheHelper.getInstance() | ||
if (primeEntityCacheHelper.isInProgress()) { | ||
// if we are priming something else at the moment, expedite the current detail | ||
if (!primeEntityCacheHelper.isDetailInProgress(detail.id)) { | ||
primeEntityCacheHelper.expediteDetailWithId(detail, entities) | ||
} else { | ||
// otherwise wait for existing prime process to complete | ||
primeEntityCacheHelper.setListener(this) | ||
waitForCachePrimeWork(entities, primeEntityCacheHelper) | ||
if (cachedEntities != null) { | ||
entities.clear() | ||
entities.addAll(cachedEntities!!) | ||
} | ||
} | ||
} else { | ||
// we either have not started priming or already completed. In both cases | ||
// we want to re-prime to make sure we calculate any uncalculated data first | ||
primeEntityCacheHelper.primeEntityCacheForDetail(detail, entities) | ||
} | ||
} | ||
} else { | ||
super.prepareEntitiesInternal(entities) | ||
} | ||
} | ||
|
||
private fun waitForCachePrimeWork( | ||
entities: MutableList<Entity<TreeReference>>, | ||
primeEntityCacheHelper: PrimeEntityCacheHelper | ||
) { | ||
val startTime = System.currentTimeMillis() | ||
while (!completedCachePrime && (System.currentTimeMillis() - startTime) < TWO_MINUTES) { | ||
// wait for it to be completed | ||
try { | ||
Thread.sleep(100) | ||
} catch (_: InterruptedException) { | ||
} | ||
} | ||
if (!completedCachePrime) { | ||
Logger.log(LogTypes.TYPE_MAINTENANCE, "Still Waiting for cache priming work to complete") | ||
// confirm if we are still priming in the worker. If yes, wait more | ||
// otherwise recall prepareEntitiesInternal to re-evaluate the best thing to do | ||
if (primeEntityCacheHelper.isInProgress() && primeEntityCacheHelper.isDetailInProgress(detail.id)) { | ||
waitForCachePrimeWork(entities, primeEntityCacheHelper) | ||
} else { | ||
prepareEntitiesInternal(entities) | ||
} | ||
} | ||
} | ||
shubham1g5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
override fun onPrimeEntityCacheComplete( | ||
currentDetailInProgress: String, | ||
cachedEntitiesWithRefs: Pair<List<Entity<TreeReference>>, List<TreeReference>> | ||
) { | ||
if (detail.id!!.contentEquals(currentDetailInProgress)) { | ||
cachedEntities = cachedEntitiesWithRefs.first | ||
completedCachePrime = true | ||
} | ||
} | ||
} |
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,13 @@ | ||
package org.commcare.entity | ||
|
||
import android.util.Pair | ||
import org.commcare.cases.entity.Entity | ||
import org.javarosa.core.model.instance.TreeReference | ||
|
||
interface PrimeEntityCacheListener { | ||
|
||
fun onPrimeEntityCacheComplete( | ||
currentDetailInProgress: String, | ||
cachedEntitiesWithRefs: Pair<List<Entity<TreeReference>>, List<TreeReference>> | ||
) | ||
} |
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this removing the gridview modality of entity selects entirely? I think it is mostly deprecated, but probably important to call out specifically if so to confirm that it's safe to do so.