Skip to content

Commit

Permalink
#10882 Slow Media Tab View | Rebase of #11281 #11288
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Coticopol <bcoticopol@gmail.com>
- Workaround to improve gallery performance by eliminating the startDate from the query;
- Fixed a bug when the gallery would reset after playing a movie (searchAndDisplay);
  • Loading branch information
Bogdan Coticopol authored and AlvaroBrey committed Jan 26, 2023
1 parent f007d04 commit 8af820d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public class GallerySearchTask extends AsyncTask<Void, Void, GallerySearchTask.R
private final WeakReference<GalleryFragment> photoFragmentWeakReference;
private final FileDataStorageManager storageManager;
private final int limit;
private final long startDate;
@Deprecated
private final long startDate = 0; // we don't use startDate anymore, only endDate and limit
private final long endDate;

public GallerySearchTask(GalleryFragment photoFragment,
Expand All @@ -64,7 +65,6 @@ public GallerySearchTask(GalleryFragment photoFragment,
this.user = user;
this.photoFragmentWeakReference = new WeakReference<>(photoFragment);
this.storageManager = storageManager;
this.startDate = startDate;
this.endDate = endDate;
this.limit = limit;
}
Expand Down Expand Up @@ -104,9 +104,6 @@ protected GallerySearchTask.Result doInBackground(Void... voids) {
if (result.isSuccess()) {
boolean emptySearch = parseMedia(startDate, endDate, result.getData());
long lastTimeStamp = findLastTimestamp(result.getData());



return new Result(result.isSuccess(), emptySearch, lastTimeStamp);
} else {
return new Result(false, false, -1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ public class GalleryFragment extends OCFileListFragment implements GalleryFragme

private boolean photoSearchQueryRunning = false;
private AsyncTask<Void, Void, GallerySearchTask.Result> photoSearchTask;
private long startDate;
@Deprecated
private long startDate = 0; // we don't use the startDate anymore, we use only endDate and row limit
private long endDate;
@Deprecated
private long daySpan = 30;
private int limit = 300;
private GalleryAdapter mAdapter;
Expand Down Expand Up @@ -218,51 +220,39 @@ private void handleSearchEvent() {
}

private void searchAndDisplay() {
// first: always search from now to -30 days
if (!photoSearchQueryRunning) {
photoSearchQueryRunning = true;

startDate = (System.currentTimeMillis() / 1000) - 30 * 24 * 60 * 60;
endDate = System.currentTimeMillis() / 1000;

runGallerySearchTask();
// fix an issue when the method is called after loading the gallery and pressing play on a movie
// to avoid reloading the gallery, check if endDate has already a value which is not -1 or 0 (which generally means some kind of reset/init)
if (this.endDate <= 0) {
endDate = System.currentTimeMillis() / 1000;
photoSearchQueryRunning = true;
runGallerySearchTask();
return;
}
}
}

@SuppressLint("NotifyDataSetChanged")
public void searchCompleted(boolean emptySearch, long lastTimeStamp) {
photoSearchQueryRunning = false;

if (emptySearch && mAdapter.isEmpty()) {
if (lastTimeStamp > -1) {
endDate = lastTimeStamp;
}

if (mAdapter.isEmpty()) {
setEmptyListMessage(SearchType.GALLERY_SEARCH);
return;
}

if(!emptySearch) {
mAdapter.notifyDataSetChanged();
this.showAllGalleryItems();
return;
}

if (daySpan == 30) {
daySpan = 90;
} else if (daySpan == 90) {
daySpan = 180;
} else if (daySpan == 180) {
daySpan = 999;
} else if (daySpan == 999 && limit > 0) {
limit = -1; // no limit
} else {
Log_OC.d(this, "End gallery search");
return;
}

if (lastTimeStamp > -1) {
endDate = lastTimeStamp;
}

startDate = endDate - (daySpan * 24 * 60 * 60);
Log_OC.d(this, "End gallery search");
return;

runGallerySearchTask();
}

@Override
Expand Down Expand Up @@ -316,6 +306,7 @@ private void searchAndDisplayAfterChangingFolder() {

private void runGallerySearchTask() {
if (mContainerActivity != null) {
photoSearchQueryRunning = true;
photoSearchTask = new GallerySearchTask(this,
accountManager.getUser(),
mContainerActivity.getStorageManager(),
Expand Down Expand Up @@ -356,7 +347,6 @@ private void loadMoreWhenEndReached(@NonNull RecyclerView recyclerView, int dy)

daySpan = 30;
endDate = lastFile.getModificationTimestamp() / 1000;
startDate = endDate - (daySpan * 24 * 60 * 60);

photoSearchQueryRunning = true;
runGallerySearchTask();
Expand Down

0 comments on commit 8af820d

Please sign in to comment.