Skip to content
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

Fixes #5743 Reduce delay between peer reviews #5897

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
13 changes: 13 additions & 0 deletions app/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions app/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions app/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 80 additions & 28 deletions app/src/main/java/fr/free/nrw/commons/review/ReviewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
Expand All @@ -22,19 +23,25 @@
import fr.free.nrw.commons.theme.BaseActivity;
import fr.free.nrw.commons.utils.DialogUtil;
import fr.free.nrw.commons.utils.ViewUtil;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;

public class ReviewActivity extends BaseActivity {


private ActivityReviewBinding binding;

MediaDetailFragment mediaDetailFragment;
public ReviewPagerAdapter reviewPagerAdapter;
public ReviewController reviewController;




@Inject
ReviewHelper reviewHelper;
@Inject
Expand All @@ -53,6 +60,13 @@ public class ReviewActivity extends BaseActivity {
final String SAVED_MEDIA = "saved_media";
private Media media;

private List<Media> cachedMedia = new ArrayList<>();

private static final String PREF_NAME = "ReviewActivityPrefs";
private static final String LAST_CACHE_TIME_KEY = "lastCacheTime";
private static final int CACHE_SIZE = 5;
u7805486 marked this conversation as resolved.
Show resolved Hide resolved
private static final long CACHE_EXPIRY_TIME = 24 * 60 * 60 * 1000;

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Expand Down Expand Up @@ -100,10 +114,10 @@ protected void onCreate(Bundle savedInstanceState) {
d[2].setColorFilter(getApplicationContext().getResources().getColor(R.color.button_blue), PorterDuff.Mode.SRC_IN);

if (savedInstanceState != null && savedInstanceState.getParcelable(SAVED_MEDIA) != null) {
updateImage(savedInstanceState.getParcelable(SAVED_MEDIA)); // Use existing media if we have one
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any way to improve these comments rather than just remove them?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I have added java doc to explain why I changed this part, but my teammates pushed a old version, sorry about that. the new version included java doc and kdoc for all changed part.

updateImage(savedInstanceState.getParcelable(SAVED_MEDIA));
setUpMediaDetailOnOrientation();
} else {
runRandomizer(); //Run randomizer whenever everything is ready so that a first random image will be added
runRandomizer();
}

binding.skipImage.setOnClickListener(view -> {
Expand Down Expand Up @@ -136,31 +150,66 @@ public boolean runRandomizer() {
hasNonHiddenCategories = false;
binding.pbReviewImage.setVisibility(View.VISIBLE);
binding.viewPagerReview.setCurrentItem(0);
// Finds non-hidden categories from Media instance
compositeDisposable.add(reviewHelper.getRandomMedia()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::checkWhetherFileIsUsedInWikis));

if (cachedMedia.isEmpty() || isCacheExpired()) {
fetchAndCacheMedia();
} else {
processNextCachedMedia();
}
return true;
}

private void fetchAndCacheMedia() {
u7805486 marked this conversation as resolved.
Show resolved Hide resolved
compositeDisposable.add(Observable.range(0, CACHE_SIZE)
.flatMap(i -> reviewHelper.getRandomMedia().toObservable())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.toList()
.subscribe(mediaList -> {
cachedMedia.clear();
cachedMedia.addAll(mediaList);
updateLastCacheTime();
processNextCachedMedia();
}, this::handleError));
}

private void processNextCachedMedia() {
if (!cachedMedia.isEmpty()) {
Media media = cachedMedia.remove(0);
checkWhetherFileIsUsedInWikis(media);
} else {
fetchAndCacheMedia();
}
}

private boolean isCacheExpired() {
SharedPreferences prefs = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
long lastCacheTime = prefs.getLong(LAST_CACHE_TIME_KEY, 0);
long currentTime = System.currentTimeMillis();
return (currentTime - lastCacheTime) > CACHE_EXPIRY_TIME;
}

private void updateLastCacheTime() {
SharedPreferences prefs = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putLong(LAST_CACHE_TIME_KEY, System.currentTimeMillis());
editor.apply();
}

/**
* Check whether media is used or not in any Wiki Page
*/
@SuppressLint("CheckResult")
private void checkWhetherFileIsUsedInWikis(final Media media) {
compositeDisposable.add(reviewHelper.checkFileUsage(media.getFilename())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> {
// result false indicates media is not used in any wiki
if (!result) {
// Finds non-hidden categories from Media instance
findNonHiddenCategories(media);
} else {
runRandomizer();
processNextCachedMedia();
}
}));
}, this::handleError));
}

/**
Expand All @@ -181,7 +230,6 @@ private void findNonHiddenCategories(Media media) {
updateImage(media);
}

@SuppressLint("CheckResult")
private void updateImage(Media media) {
reviewHelper.addViewedImagesToDB(media.getPageId());
this.media = media;
Expand All @@ -191,27 +239,26 @@ private void updateImage(Media media) {
return;
}

//If The Media User and Current Session Username is same then Skip the Image
if (media.getUser() != null && media.getUser().equals(AccountUtil.getUserName(getApplicationContext()))) {
runRandomizer();
processNextCachedMedia();
return;
}

binding.reviewImageView.setImageURI(media.getImageUrl());

reviewController.onImageRefreshed(media); //file name is updated
reviewController.onImageRefreshed(media);
compositeDisposable.add(reviewHelper.getFirstRevisionOfFile(fileName)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(revision -> {
reviewController.firstRevision = revision;
reviewPagerAdapter.updateFileInformation();
@SuppressLint({"StringFormatInvalid", "LocalSuppress"}) String caption = String.format(getString(R.string.review_is_uploaded_by), fileName, revision.getUser());
binding.tvImageCaption.setText(caption);
binding.pbReviewImage.setVisibility(View.GONE);
reviewImageFragment = getInstanceOfReviewImageFragment();
reviewImageFragment.enableButtons();
}));
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(revision -> {
reviewController.firstRevision = revision;
reviewPagerAdapter.updateFileInformation();
String caption = String.format(getString(R.string.review_is_uploaded_by), fileName, revision.getUser());
binding.tvImageCaption.setText(caption);
binding.pbReviewImage.setVisibility(View.GONE);
reviewImageFragment = getInstanceOfReviewImageFragment();
reviewImageFragment.enableButtons();
}, this::handleError));
binding.viewPagerReview.setCurrentItem(0);
}

Expand Down Expand Up @@ -258,6 +305,11 @@ public void showReviewImageInfo() {
null,
null);
}
private void handleError(Throwable error) {
binding.pbReviewImage.setVisibility(View.GONE);
ViewUtil.showShortSnackbar(binding.drawerLayout, R.string.error_review);

}


@Override
Expand Down
Loading
Loading