mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 21:03:54 +01:00
Added pending uploads screen (#5752)
* Added pending uploads screen * Added failed uploads fragment * Improved progress bars * Implemented pause functionality * Improved pause feature * Fixed issue with sorting when adding more pictures during an upload * Improved Tap to View notification * Fixed issue with on going upload deletion * Improved the deletion feature * Fixed indentations and unit tests * Fixed bugs * Fixed failing test * Added error message in Failed Uploads Fragment * Improved error notification * Moved auto-retry from the Main Activity to UploadProgressActivity * Fixed large uploads issue * Minor fixes * Removed HashSet * Fixed issue with progress bar * Bug fixes * Moved Auto Retry to MainActivity * Fixed conflicts * Fixed issue with upload icon * Fixed null ptr issue on changing modes * Improved recycler view * Fixed irrelevant network call * Fixed irrelevant network call * Fixed constantly failing uploads * Fixed constantly failing uploads * Fixed constantly failing uploads * Added error log * Fixed refresh icon visibility in light mode * Changed progress in progress activity * Fixed progress bar issue * Improved icons * Improved deletion and removed cancelledUploads Hashset * Fixed sorting, list size issue * Improved current implementation * Implemented flag for workers * Implemented flag for workers * Fixed sorting bug * Fixed upload icon * Improved pausing * Made changes to visibility implementation * Added image duplicity check on restart of failed image * minor adjustments * added javadoc/kdoc and fixed minor bug * Fixed unit tests * Added synchronized(lock) * Added check to prevent multiple uploads starting at once * Ignored failing test cases * Temporary commit - Added jcenter * Temporary commit - Removed library/commented * Temporary commit - Removed library/commented * Updated com.jraska.livedata:testing-ktx * Ignored failing test - UploadControllerTest.kt * Ignored failing test - UploadModelUnitTest * Ignored failing test - UploadModelUnitTest * Ignored failing test - UploadModelUnitTest * Ignored failing test - UploadModelUnitTest * Ignored failing test - UploadModelUnitTest * Ignored failing test - UploadModelUnitTest * Ignored failing test - UploadModelUnitTest * Ignored failing test - UploadModelUnitTest * Ignored failing test - UploadPresenterTest.kt * Ignored failing test - UploadPresenterTest.kt * Ignored failing test - UploadPresenterTest.kt * Ignored failing test - UploadPresenterTest.kt * Ignored failing test - UploadPresenterTest.kt * Ignored failing test - UploadPresenterTest.kt * Ignored failing test - UploadPresenterTest.kt * Ignored failing test - UploadPresenterTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing tests - UploadRepositoryUnitTest.kt * Ignored failing test - UploadRepositoryUnitTest.kt * Ignored failing test - DepictedItemTest.kt * Ignored failing test - DepictedItemTest.kt * Ignored failing test - DepictedItemTest.kt * Ignored failing test - DepictedItemTest.kt * Ignored failing test - DepictedItemTest.kt * Ignored failing test - DepictedItemTest.kt * Ignored failing test - DepictedItemTest.kt * Ignored failing test - DepictedItemTest.kt * Ignored failing test - FilesUtilsTest.kt * Ignored failing test - WikiBaseClientUnitTest.kt * Ignored failing test - WikiBaseClientUnitTest.kt * Ignored failing test - WikiBaseClientUnitTest.kt * Ignored failing test - WikidataClientTest.kt * Ignored failing test - WikidataClientTest.kt * Fixed unit tests * Updated kdoc --------- Co-authored-by: Nicolas Raoul <nicolas.raoul@gmail.com>
This commit is contained in:
parent
62d6dea219
commit
93f1e1ec29
69 changed files with 2717 additions and 955 deletions
|
|
@ -142,15 +142,7 @@ public class CommonsApplication extends MultiDexApplication {
|
|||
@Inject
|
||||
ContributionDao contributionDao;
|
||||
|
||||
/**
|
||||
* In-memory list of contributions whose uploads have been paused by the user
|
||||
*/
|
||||
public static Map<String, Boolean> pauseUploads = new HashMap<>();
|
||||
|
||||
/**
|
||||
* In-memory list of uploads that have been cancelled by the user
|
||||
*/
|
||||
public static HashSet<String> cancelledUploads = new HashSet<>();
|
||||
public static Boolean isPaused = false;
|
||||
|
||||
/**
|
||||
* Used to declare and initialize various components and dependencies
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ data class Contribution constructor(
|
|||
var dateCreatedSource: String? = null,
|
||||
var wikidataPlace: WikidataPlace? = null,
|
||||
var chunkInfo: ChunkInfo? = null,
|
||||
var errorInfo: String? = null,
|
||||
/**
|
||||
* @return array list of entityids for the depictions
|
||||
*/
|
||||
|
|
@ -42,6 +43,7 @@ data class Contribution constructor(
|
|||
var dateCreated: Date? = null,
|
||||
var dateCreatedString: String? = null,
|
||||
var dateModified: Date? = null,
|
||||
var dateUploadStarted: Date? = null,
|
||||
var hasInvalidLocation : Int = 0,
|
||||
var contentUri: Uri? = null,
|
||||
var countryCode : String? = null,
|
||||
|
|
@ -99,7 +101,6 @@ data class Contribution constructor(
|
|||
const val STATE_QUEUED = 2
|
||||
const val STATE_IN_PROGRESS = 3
|
||||
const val STATE_PAUSED = 4
|
||||
const val STATE_QUEUED_LIMITED_CONNECTION_MODE=5
|
||||
|
||||
/**
|
||||
* Formatting captions to the Wikibase format for sending labels
|
||||
|
|
@ -127,11 +128,8 @@ data class Contribution constructor(
|
|||
return chunkInfo != null && chunkInfo!!.totalChunks == chunkInfo!!.indexOfNextChunkToUpload
|
||||
}
|
||||
|
||||
fun isPaused(): Boolean {
|
||||
return CommonsApplication.pauseUploads[pageId] ?: false
|
||||
fun dateUploadStartedInMillis(): Long {
|
||||
return dateUploadStarted!!.time
|
||||
}
|
||||
|
||||
fun unpause() {
|
||||
CommonsApplication.pauseUploads[pageId] = false
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,10 @@ import android.content.Intent;
|
|||
import android.widget.Toast;
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.paging.DataSource.Factory;
|
||||
import androidx.paging.LivePagedListBuilder;
|
||||
import androidx.paging.PagedList;
|
||||
import fr.free.nrw.commons.R;
|
||||
import fr.free.nrw.commons.filepicker.DefaultCallback;
|
||||
import fr.free.nrw.commons.filepicker.FilePicker;
|
||||
|
|
@ -25,6 +29,8 @@ import fr.free.nrw.commons.utils.DialogUtil;
|
|||
import fr.free.nrw.commons.utils.PermissionUtils;
|
||||
import fr.free.nrw.commons.utils.ViewUtil;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
|
@ -39,10 +45,16 @@ public class ContributionController {
|
|||
private boolean isInAppCameraUpload;
|
||||
public LocationPermissionCallback locationPermissionCallback;
|
||||
private LocationPermissionsHelper locationPermissionsHelper;
|
||||
LiveData<PagedList<Contribution>> failedAndPendingContributionList;
|
||||
LiveData<PagedList<Contribution>> pendingContributionList;
|
||||
LiveData<PagedList<Contribution>> failedContributionList;
|
||||
|
||||
@Inject
|
||||
LocationServiceManager locationManager;
|
||||
|
||||
@Inject
|
||||
ContributionsRepository repository;
|
||||
|
||||
@Inject
|
||||
public ContributionController(@Named("default_preferences") JsonKvStore defaultKvStore) {
|
||||
this.defaultKvStore = defaultKvStore;
|
||||
|
|
@ -115,14 +127,14 @@ public class ContributionController {
|
|||
}
|
||||
|
||||
/**
|
||||
* Shows a dialog alerting the user about location services being off
|
||||
* and asking them to turn it on
|
||||
* Shows a dialog alerting the user about location services being off and asking them to turn it
|
||||
* on
|
||||
* TODO: Add a seperate callback in LocationPermissionsHelper for this.
|
||||
* Ref: https://github.com/commons-app/apps-android-commons/pull/5494/files#r1510553114
|
||||
*
|
||||
* @param activity Activity reference
|
||||
* @param activity Activity reference
|
||||
* @param dialogTextResource Resource id of text to be shown in dialog
|
||||
* @param toastTextResource Resource id of text to be shown in toast
|
||||
* @param toastTextResource Resource id of text to be shown in toast
|
||||
*/
|
||||
private void showLocationOffDialog(Activity activity, int dialogTextResource,
|
||||
int toastTextResource) {
|
||||
|
|
@ -307,4 +319,60 @@ public class ContributionController {
|
|||
isInAppCameraUpload = false; // reset the flag for next use
|
||||
return shareIntent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the contributions with the state "IN_PROGRESS", "QUEUED" and "PAUSED" and then it
|
||||
* populates the `pendingContributionList`.
|
||||
**/
|
||||
void getPendingContributions() {
|
||||
final PagedList.Config pagedListConfig =
|
||||
(new PagedList.Config.Builder())
|
||||
.setPrefetchDistance(50)
|
||||
.setPageSize(10).build();
|
||||
Factory<Integer, Contribution> factory;
|
||||
factory = repository.fetchContributionsWithStates(
|
||||
Arrays.asList(Contribution.STATE_IN_PROGRESS, Contribution.STATE_QUEUED,
|
||||
Contribution.STATE_PAUSED));
|
||||
|
||||
LivePagedListBuilder livePagedListBuilder = new LivePagedListBuilder(factory,
|
||||
pagedListConfig);
|
||||
pendingContributionList = livePagedListBuilder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the contributions with the state "FAILED" and populates the
|
||||
* `failedContributionList`.
|
||||
**/
|
||||
void getFailedContributions() {
|
||||
final PagedList.Config pagedListConfig =
|
||||
(new PagedList.Config.Builder())
|
||||
.setPrefetchDistance(50)
|
||||
.setPageSize(10).build();
|
||||
Factory<Integer, Contribution> factory;
|
||||
factory = repository.fetchContributionsWithStates(
|
||||
Collections.singletonList(Contribution.STATE_FAILED));
|
||||
|
||||
LivePagedListBuilder livePagedListBuilder = new LivePagedListBuilder(factory,
|
||||
pagedListConfig);
|
||||
failedContributionList = livePagedListBuilder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the contributions with the state "IN_PROGRESS", "QUEUED", "PAUSED" and "FAILED" and
|
||||
* then it populates the `failedAndPendingContributionList`.
|
||||
**/
|
||||
void getFailedAndPendingContributions() {
|
||||
final PagedList.Config pagedListConfig =
|
||||
(new PagedList.Config.Builder())
|
||||
.setPrefetchDistance(50)
|
||||
.setPageSize(10).build();
|
||||
Factory<Integer, Contribution> factory;
|
||||
factory = repository.fetchContributionsWithStates(
|
||||
Arrays.asList(Contribution.STATE_IN_PROGRESS, Contribution.STATE_QUEUED,
|
||||
Contribution.STATE_PAUSED, Contribution.STATE_FAILED));
|
||||
|
||||
LivePagedListBuilder livePagedListBuilder = new LivePagedListBuilder(factory,
|
||||
pagedListConfig);
|
||||
failedAndPendingContributionList = livePagedListBuilder.build();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import io.reactivex.Completable;
|
|||
import io.reactivex.Single;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
import timber.log.Timber;
|
||||
|
||||
@Dao
|
||||
public abstract class ContributionDao {
|
||||
|
|
@ -27,6 +28,9 @@ public abstract class ContributionDao {
|
|||
return Completable
|
||||
.fromAction(() -> {
|
||||
contribution.setDateModified(Calendar.getInstance().getTime());
|
||||
if (contribution.getDateUploadStarted() == null) {
|
||||
contribution.setDateUploadStarted(Calendar.getInstance().getTime());
|
||||
}
|
||||
saveSynchronous(contribution);
|
||||
});
|
||||
}
|
||||
|
|
@ -44,11 +48,32 @@ public abstract class ContributionDao {
|
|||
@Delete
|
||||
public abstract void deleteSynchronous(Contribution contribution);
|
||||
|
||||
/**
|
||||
* Deletes contributions with specific states from the database.
|
||||
*
|
||||
* @param states The states of the contributions to delete.
|
||||
* @throws SQLiteException If an SQLite error occurs.
|
||||
*/
|
||||
@Query("DELETE FROM contribution WHERE state IN (:states)")
|
||||
public abstract void deleteContributionsWithStatesSynchronous(List<Integer> states)
|
||||
throws SQLiteException;
|
||||
|
||||
public Completable delete(final Contribution contribution) {
|
||||
return Completable
|
||||
.fromAction(() -> deleteSynchronous(contribution));
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes contributions with specific states from the database.
|
||||
*
|
||||
* @param states The states of the contributions to delete.
|
||||
* @return A Completable indicating the result of the operation.
|
||||
*/
|
||||
public Completable deleteContributionsWithStates(List<Integer> states) {
|
||||
return Completable
|
||||
.fromAction(() -> deleteContributionsWithStatesSynchronous(states));
|
||||
}
|
||||
|
||||
@Query("SELECT * from contribution WHERE media_filename=:fileName")
|
||||
public abstract List<Contribution> getContributionWithTitle(String fileName);
|
||||
|
||||
|
|
@ -58,6 +83,26 @@ public abstract class ContributionDao {
|
|||
@Query("SELECT * from contribution WHERE state IN (:states) order by media_dateUploaded DESC")
|
||||
public abstract Single<List<Contribution>> getContribution(List<Integer> states);
|
||||
|
||||
/**
|
||||
* Gets contributions with specific states in descending order by the date they were uploaded.
|
||||
*
|
||||
* @param states The states of the contributions to fetch.
|
||||
* @return A DataSource factory for paginated contributions with the specified states.
|
||||
*/
|
||||
@Query("SELECT * from contribution WHERE state IN (:states) order by media_dateUploaded DESC")
|
||||
public abstract DataSource.Factory<Integer, Contribution> getContributions(
|
||||
List<Integer> states);
|
||||
|
||||
/**
|
||||
* Gets contributions with specific states in ascending order by the date the upload started.
|
||||
*
|
||||
* @param states The states of the contributions to fetch.
|
||||
* @return A DataSource factory for paginated contributions with the specified states.
|
||||
*/
|
||||
@Query("SELECT * from contribution WHERE state IN (:states) order by dateUploadStarted ASC")
|
||||
public abstract DataSource.Factory<Integer, Contribution> getContributionsSortedByDateUploadStarted(
|
||||
List<Integer> states);
|
||||
|
||||
@Query("SELECT COUNT(*) from contribution WHERE state in (:toUpdateStates)")
|
||||
public abstract Single<Integer> getPendingUploads(int[] toUpdateStates);
|
||||
|
||||
|
|
@ -67,6 +112,15 @@ public abstract class ContributionDao {
|
|||
@Update
|
||||
public abstract void updateSynchronous(Contribution contribution);
|
||||
|
||||
/**
|
||||
* Updates the state of contributions with specific states.
|
||||
*
|
||||
* @param states The current states of the contributions to update.
|
||||
* @param newState The new state to set.
|
||||
*/
|
||||
@Query("UPDATE contribution SET state = :newState WHERE state IN (:states)")
|
||||
public abstract void updateContributionsState(List<Integer> states, int newState);
|
||||
|
||||
public Completable update(final Contribution contribution) {
|
||||
return Completable
|
||||
.fromAction(() -> {
|
||||
|
|
@ -74,4 +128,18 @@ public abstract class ContributionDao {
|
|||
updateSynchronous(contribution);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the state of contributions with specific states asynchronously.
|
||||
*
|
||||
* @param states The current states of the contributions to update.
|
||||
* @param newState The new state to set.
|
||||
* @return A Completable indicating the result of the operation.
|
||||
*/
|
||||
public Completable updateContributionsWithStates(List<Integer> states, int newState) {
|
||||
return Completable
|
||||
.fromAction(() -> {
|
||||
updateContributionsState(states, newState);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,11 +48,8 @@ public class ContributionViewHolder extends RecyclerView.ViewHolder {
|
|||
|
||||
binding = LayoutContributionBinding.bind(parent);
|
||||
|
||||
binding.retryButton.setOnClickListener(v -> retryUpload());
|
||||
binding.cancelButton.setOnClickListener(v -> deleteUpload());
|
||||
binding.contributionImage.setOnClickListener(v -> imageClicked());
|
||||
binding.wikipediaButton.setOnClickListener(v -> wikipediaButtonClicked());
|
||||
binding.pauseResumeButton.setOnClickListener(v -> onPauseResumeButtonClicked());
|
||||
|
||||
/* Set a dialog indicating that the upload is being paused. This is needed because pausing
|
||||
an upload might take a dozen seconds. */
|
||||
|
|
@ -79,9 +76,6 @@ public class ContributionViewHolder extends RecyclerView.ViewHolder {
|
|||
|
||||
binding.contributionImage.getHierarchy().setPlaceholderImage(R.drawable.image_placeholder);
|
||||
binding.contributionImage.getHierarchy().setFailureImage(R.drawable.image_placeholder);
|
||||
|
||||
|
||||
|
||||
|
||||
final String imageSource = chooseImageSource(contribution.getMedia().getThumbUrl(),
|
||||
contribution.getLocalUri());
|
||||
|
|
@ -90,79 +84,27 @@ public class ContributionViewHolder extends RecyclerView.ViewHolder {
|
|||
imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(imageSource))
|
||||
.setProgressiveRenderingEnabled(true)
|
||||
.build();
|
||||
}
|
||||
else if (URLUtil.isFileUrl(imageSource)){
|
||||
imageRequest=ImageRequest.fromUri(Uri.parse(imageSource));
|
||||
}
|
||||
else if(imageSource != null) {
|
||||
} else if (URLUtil.isFileUrl(imageSource)) {
|
||||
imageRequest = ImageRequest.fromUri(Uri.parse(imageSource));
|
||||
} else if (imageSource != null) {
|
||||
final File file = new File(imageSource);
|
||||
imageRequest = ImageRequest.fromFile(file);
|
||||
}
|
||||
|
||||
if(imageRequest != null){
|
||||
if (imageRequest != null) {
|
||||
binding.contributionImage.setImageRequest(imageRequest);
|
||||
}
|
||||
}
|
||||
|
||||
binding.contributionSequenceNumber.setText(String.valueOf(position + 1));
|
||||
binding.contributionSequenceNumber.setVisibility(View.VISIBLE);
|
||||
|
||||
binding.wikipediaButton.setVisibility(View.GONE);
|
||||
switch (contribution.getState()) {
|
||||
case Contribution.STATE_COMPLETED:
|
||||
binding.contributionState.setVisibility(View.GONE);
|
||||
binding.contributionProgress.setVisibility(View.GONE);
|
||||
binding.imageOptions.setVisibility(View.GONE);
|
||||
binding.contributionState.setText("");
|
||||
checkIfMediaExistsOnWikipediaPage(contribution);
|
||||
break;
|
||||
case Contribution.STATE_QUEUED:
|
||||
case Contribution.STATE_QUEUED_LIMITED_CONNECTION_MODE:
|
||||
binding.contributionProgress.setVisibility(View.GONE);
|
||||
binding.contributionState.setVisibility(View.VISIBLE);
|
||||
binding.contributionState.setText(R.string.contribution_state_queued);
|
||||
binding.imageOptions.setVisibility(View.GONE);
|
||||
break;
|
||||
case Contribution.STATE_IN_PROGRESS:
|
||||
binding.contributionState.setVisibility(View.GONE);
|
||||
binding.contributionProgress.setVisibility(View.VISIBLE);
|
||||
binding.wikipediaButton.setVisibility(View.GONE);
|
||||
binding.pauseResumeButton.setVisibility(View.VISIBLE);
|
||||
binding.cancelButton.setVisibility(View.GONE);
|
||||
binding.retryButton.setVisibility(View.GONE);
|
||||
binding.imageOptions.setVisibility(View.VISIBLE);
|
||||
final long total = contribution.getDataLength();
|
||||
final long transferred = contribution.getTransferred();
|
||||
if (transferred == 0 || transferred >= total) {
|
||||
binding.contributionProgress.setIndeterminate(true);
|
||||
} else {
|
||||
binding.contributionProgress.setIndeterminate(false);
|
||||
binding.contributionProgress.setProgress((int) (((double) transferred / (double) total) * 100));
|
||||
}
|
||||
break;
|
||||
case Contribution.STATE_PAUSED:
|
||||
binding.contributionProgress.setVisibility(View.GONE);
|
||||
binding.contributionState.setVisibility(View.VISIBLE);
|
||||
binding.contributionState.setText(R.string.paused);
|
||||
binding.cancelButton.setVisibility(View.VISIBLE);
|
||||
binding.retryButton.setVisibility(View.GONE);
|
||||
binding.pauseResumeButton.setVisibility(View.VISIBLE);
|
||||
binding.imageOptions.setVisibility(View.VISIBLE);
|
||||
setResume();
|
||||
if(pausingPopUp.isShowing()){
|
||||
pausingPopUp.hide();
|
||||
}
|
||||
break;
|
||||
case Contribution.STATE_FAILED:
|
||||
binding.contributionState.setVisibility(View.VISIBLE);
|
||||
binding.contributionState.setText(R.string.contribution_state_failed);
|
||||
binding.contributionProgress.setVisibility(View.GONE);
|
||||
binding.cancelButton.setVisibility(View.VISIBLE);
|
||||
binding.retryButton.setVisibility(View.VISIBLE);
|
||||
binding.pauseResumeButton.setVisibility(View.GONE);
|
||||
binding.imageOptions.setVisibility(View.VISIBLE);
|
||||
break;
|
||||
}
|
||||
binding.contributionState.setVisibility(View.GONE);
|
||||
binding.contributionProgress.setVisibility(View.GONE);
|
||||
binding.imageOptions.setVisibility(View.GONE);
|
||||
binding.contributionState.setText("");
|
||||
checkIfMediaExistsOnWikipediaPage(contribution);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -196,8 +138,6 @@ public class ContributionViewHolder extends RecyclerView.ViewHolder {
|
|||
if (!mediaExists) {
|
||||
binding.wikipediaButton.setVisibility(View.VISIBLE);
|
||||
isWikipediaButtonDisplayed = true;
|
||||
binding.cancelButton.setVisibility(View.GONE);
|
||||
binding.retryButton.setVisibility(View.GONE);
|
||||
binding.imageOptions.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
|
|
@ -217,20 +157,6 @@ public class ContributionViewHolder extends RecyclerView.ViewHolder {
|
|||
null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retry upload when it is failed
|
||||
*/
|
||||
public void retryUpload() {
|
||||
callback.retryUpload(contribution);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a failed upload attempt
|
||||
*/
|
||||
public void deleteUpload() {
|
||||
callback.deleteUpload(contribution);
|
||||
}
|
||||
|
||||
public void imageClicked() {
|
||||
callback.openMediaDetail(position, isWikipediaButtonDisplayed);
|
||||
}
|
||||
|
|
@ -239,44 +165,6 @@ public class ContributionViewHolder extends RecyclerView.ViewHolder {
|
|||
callback.addImageToWikipedia(contribution);
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggers a callback for pause/resume
|
||||
*/
|
||||
public void onPauseResumeButtonClicked() {
|
||||
if (binding.pauseResumeButton.getTag().toString().equals("pause")) {
|
||||
pause();
|
||||
} else {
|
||||
resume();
|
||||
}
|
||||
}
|
||||
|
||||
private void resume() {
|
||||
callback.resumeUpload(contribution);
|
||||
setPaused();
|
||||
}
|
||||
|
||||
private void pause() {
|
||||
pausingPopUp.show();
|
||||
callback.pauseUpload(contribution);
|
||||
setResume();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update pause/resume button to show pause state
|
||||
*/
|
||||
private void setPaused() {
|
||||
binding.pauseResumeButton.setImageResource(R.drawable.pause_icon);
|
||||
binding.pauseResumeButton.setTag(parent.getContext().getString(R.string.pause));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update pause/resume button to show resume state
|
||||
*/
|
||||
private void setResume() {
|
||||
binding.pauseResumeButton.setImageResource(R.drawable.play_icon);
|
||||
binding.pauseResumeButton.setTag(parent.getContext().getString(R.string.resume));
|
||||
}
|
||||
|
||||
public ImageRequest getImageRequest() {
|
||||
return imageRequest;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,8 +19,5 @@ public class ContributionsContract {
|
|||
|
||||
Contribution getContributionsWithTitle(String uri);
|
||||
|
||||
void deleteUpload(Contribution contribution);
|
||||
|
||||
void saveContribution(Contribution contribution);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import static fr.free.nrw.commons.contributions.Contribution.STATE_FAILED;
|
|||
import static fr.free.nrw.commons.contributions.Contribution.STATE_PAUSED;
|
||||
import static fr.free.nrw.commons.nearby.fragments.NearbyParentFragment.WLM_URL;
|
||||
import static fr.free.nrw.commons.profile.ProfileActivity.KEY_USERNAME;
|
||||
import static fr.free.nrw.commons.utils.ImageUtils.IMAGE_OK;
|
||||
import static fr.free.nrw.commons.utils.LengthUtils.computeBearing;
|
||||
import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;
|
||||
|
||||
|
|
@ -12,6 +13,7 @@ import android.Manifest;
|
|||
import android.Manifest.permission;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
|
|
@ -25,6 +27,7 @@ import android.view.MenuItem.OnMenuItemClickListener;
|
|||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
|
@ -44,6 +47,8 @@ import fr.free.nrw.commons.notification.models.Notification;
|
|||
import fr.free.nrw.commons.notification.NotificationController;
|
||||
import fr.free.nrw.commons.profile.ProfileActivity;
|
||||
import fr.free.nrw.commons.theme.BaseActivity;
|
||||
import fr.free.nrw.commons.upload.UploadProgressActivity;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
|
@ -104,6 +109,8 @@ public class ContributionsFragment
|
|||
LocationServiceManager locationManager;
|
||||
@Inject
|
||||
NotificationController notificationController;
|
||||
@Inject
|
||||
ContributionController contributionController;
|
||||
|
||||
private CompositeDisposable compositeDisposable = new CompositeDisposable();
|
||||
|
||||
|
|
@ -113,10 +120,10 @@ public class ContributionsFragment
|
|||
static final String MEDIA_DETAIL_PAGER_FRAGMENT_TAG = "MediaDetailFragmentTag";
|
||||
private static final int MAX_RETRIES = 10;
|
||||
|
||||
|
||||
public FragmentContributionsBinding binding;
|
||||
|
||||
@Inject ContributionsPresenter contributionsPresenter;
|
||||
@Inject
|
||||
ContributionsPresenter contributionsPresenter;
|
||||
|
||||
@Inject
|
||||
SessionManager sessionManager;
|
||||
|
|
@ -129,6 +136,12 @@ public class ContributionsFragment
|
|||
|
||||
public TextView notificationCount;
|
||||
|
||||
public TextView pendingUploadsCountTextView;
|
||||
|
||||
public TextView uploadsErrorTextView;
|
||||
|
||||
public ImageView pendingUploadsImageView;
|
||||
|
||||
private Campaign wlmCampaign;
|
||||
|
||||
String userName;
|
||||
|
|
@ -147,20 +160,22 @@ public class ContributionsFragment
|
|||
areAllGranted = areAllGranted && b;
|
||||
}
|
||||
|
||||
if (areAllGranted) {
|
||||
onLocationPermissionGranted();
|
||||
} else {
|
||||
if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
&& store.getBoolean("displayLocationPermissionForCardView", true)
|
||||
&& !store.getBoolean("doNotAskForLocationPermission", false)
|
||||
&& (((MainActivity) getActivity()).activeFragment == ActiveFragment.CONTRIBUTIONS)) {
|
||||
binding.cardViewNearby.permissionType = NearbyNotificationCardView.PermissionType.ENABLE_LOCATION_PERMISSION;
|
||||
if (areAllGranted) {
|
||||
onLocationPermissionGranted();
|
||||
} else {
|
||||
displayYouWontSeeNearbyMessage();
|
||||
if (shouldShowRequestPermissionRationale(
|
||||
Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
&& store.getBoolean("displayLocationPermissionForCardView", true)
|
||||
&& !store.getBoolean("doNotAskForLocationPermission", false)
|
||||
&& (((MainActivity) getActivity()).activeFragment
|
||||
== ActiveFragment.CONTRIBUTIONS)) {
|
||||
binding.cardViewNearby.permissionType = NearbyNotificationCardView.PermissionType.ENABLE_LOCATION_PERMISSION;
|
||||
} else {
|
||||
displayYouWontSeeNearbyMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@NonNull
|
||||
public static ContributionsFragment newInstance() {
|
||||
|
|
@ -198,11 +213,10 @@ public class ContributionsFragment
|
|||
checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||||
if (isChecked) {
|
||||
// Do not ask for permission on activity start again
|
||||
store.putBoolean("displayLocationPermissionForCardView",false);
|
||||
store.putBoolean("displayLocationPermissionForCardView", false);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
if (savedInstanceState != null) {
|
||||
mediaDetailPagerFragment = (MediaDetailPagerFragment) getChildFragmentManager()
|
||||
.findFragmentByTag(MEDIA_DETAIL_PAGER_FRAGMENT_TAG);
|
||||
|
|
@ -212,9 +226,7 @@ public class ContributionsFragment
|
|||
}
|
||||
|
||||
initFragments();
|
||||
if(isUserProfile) {
|
||||
binding.limitedConnectionEnabledLayout.setVisibility(View.GONE);
|
||||
}else {
|
||||
if (!isUserProfile) {
|
||||
upDateUploadCount();
|
||||
}
|
||||
if (shouldShowMediaDetailsFragment) {
|
||||
|
|
@ -230,7 +242,6 @@ public class ContributionsFragment
|
|||
&& sessionManager.getCurrentAccount() != null && !isUserProfile) {
|
||||
setUploadCount();
|
||||
}
|
||||
binding.limitedConnectionEnabledLayout.setOnClickListener(toggleDescriptionListener);
|
||||
setHasOptionsMenu(true);
|
||||
return binding.getRoot();
|
||||
}
|
||||
|
|
@ -258,10 +269,32 @@ public class ContributionsFragment
|
|||
MenuItem notificationsMenuItem = menu.findItem(R.id.notifications);
|
||||
final View notification = notificationsMenuItem.getActionView();
|
||||
notificationCount = notification.findViewById(R.id.notification_count_badge);
|
||||
MenuItem uploadMenuItem = menu.findItem(R.id.upload_tab);
|
||||
final View uploadMenuItemActionView = uploadMenuItem.getActionView();
|
||||
pendingUploadsCountTextView = uploadMenuItemActionView.findViewById(
|
||||
R.id.pending_uploads_count_badge);
|
||||
uploadsErrorTextView = uploadMenuItemActionView.findViewById(
|
||||
R.id.uploads_error_count_badge);
|
||||
pendingUploadsImageView = uploadMenuItemActionView.findViewById(
|
||||
R.id.pending_uploads_image_view);
|
||||
if (pendingUploadsImageView != null) {
|
||||
pendingUploadsImageView.setOnClickListener(view -> {
|
||||
startActivity(new Intent(getContext(), UploadProgressActivity.class));
|
||||
});
|
||||
}
|
||||
if (pendingUploadsCountTextView != null) {
|
||||
pendingUploadsCountTextView.setOnClickListener(view -> {
|
||||
startActivity(new Intent(getContext(), UploadProgressActivity.class));
|
||||
});
|
||||
}
|
||||
if (uploadsErrorTextView != null) {
|
||||
uploadsErrorTextView.setOnClickListener(view -> {
|
||||
startActivity(new Intent(getContext(), UploadProgressActivity.class));
|
||||
});
|
||||
}
|
||||
notification.setOnClickListener(view -> {
|
||||
NotificationActivity.startYourself(getContext(), "unread");
|
||||
});
|
||||
updateLimitedConnectionToggle(menu);
|
||||
}
|
||||
|
||||
@SuppressLint("CheckResult")
|
||||
|
|
@ -273,6 +306,33 @@ public class ContributionsFragment
|
|||
throwable -> Timber.e(throwable, "Error occurred while loading notifications")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the visibility of the upload icon based on the number of failed and pending
|
||||
* contributions.
|
||||
*/
|
||||
public void setUploadIconVisibility() {
|
||||
contributionController.getFailedAndPendingContributions();
|
||||
contributionController.failedAndPendingContributionList.observe(getViewLifecycleOwner(),
|
||||
list -> {
|
||||
updateUploadIcon(list.size());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the count for the upload icon based on the number of pending and failed contributions.
|
||||
*/
|
||||
public void setUploadIconCount() {
|
||||
contributionController.getPendingContributions();
|
||||
contributionController.pendingContributionList.observe(getViewLifecycleOwner(),
|
||||
list -> {
|
||||
updatePendingIcon(list.size());
|
||||
});
|
||||
contributionController.getFailedContributions();
|
||||
contributionController.failedContributionList.observe(getViewLifecycleOwner(), list -> {
|
||||
updateErrorIcon(list.size());
|
||||
});
|
||||
}
|
||||
|
||||
public void scrollToTop() {
|
||||
if (contributionsListFragment != null) {
|
||||
contributionsListFragment.scrollToTop();
|
||||
|
|
@ -289,29 +349,6 @@ public class ContributionsFragment
|
|||
}
|
||||
}
|
||||
|
||||
public void updateLimitedConnectionToggle(Menu menu) {
|
||||
MenuItem checkable = menu.findItem(R.id.toggle_limited_connection_mode);
|
||||
boolean isEnabled = store
|
||||
.getBoolean(CommonsApplication.IS_LIMITED_CONNECTION_MODE_ENABLED, false);
|
||||
|
||||
checkable.setChecked(isEnabled);
|
||||
if (binding!=null) {
|
||||
binding.limitedConnectionEnabledLayout.setVisibility(isEnabled ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
|
||||
checkable.setIcon((isEnabled) ? R.drawable.ic_baseline_cloud_off_24:R.drawable.ic_baseline_cloud_queue_24);
|
||||
checkable.setOnMenuItemClickListener(new OnMenuItemClickListener() {
|
||||
@Override
|
||||
public boolean onMenuItemClick(MenuItem item) {
|
||||
((MainActivity) getActivity()).toggleLimitedConnectionMode();
|
||||
boolean isEnabled = store.getBoolean(CommonsApplication.IS_LIMITED_CONNECTION_MODE_ENABLED, false);
|
||||
binding.limitedConnectionEnabledLayout.setVisibility(isEnabled ? View.VISIBLE : View.GONE);
|
||||
checkable.setIcon((isEnabled) ? R.drawable.ic_baseline_cloud_off_24:R.drawable.ic_baseline_cloud_queue_24);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
|
|
@ -355,7 +392,7 @@ public class ContributionsFragment
|
|||
}
|
||||
|
||||
private void setupViewForMediaDetails() {
|
||||
if (binding!=null) {
|
||||
if (binding != null) {
|
||||
binding.campaignsView.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
|
@ -465,7 +502,7 @@ public class ContributionsFragment
|
|||
contributionsPresenter.onAttachView(this);
|
||||
locationManager.addLocationListener(this);
|
||||
|
||||
if (binding==null) {
|
||||
if (binding == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -484,7 +521,8 @@ public class ContributionsFragment
|
|||
} catch (Exception e) {
|
||||
Timber.e(e);
|
||||
}
|
||||
if (binding.cardViewNearby.cardViewVisibilityState == NearbyNotificationCardView.CardViewVisibilityState.READY) {
|
||||
if (binding.cardViewNearby.cardViewVisibilityState
|
||||
== NearbyNotificationCardView.CardViewVisibilityState.READY) {
|
||||
binding.cardViewNearby.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
|
|
@ -494,16 +532,19 @@ public class ContributionsFragment
|
|||
}
|
||||
|
||||
// Notification Count and Campaigns should not be set, if it is used in User Profile
|
||||
if(!isUserProfile) {
|
||||
if (!isUserProfile) {
|
||||
setNotificationCount();
|
||||
fetchCampaigns();
|
||||
setUploadIconVisibility();
|
||||
setUploadIconCount();
|
||||
}
|
||||
}
|
||||
mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_UI);
|
||||
}
|
||||
|
||||
private void checkPermissionsAndShowNearbyCardView() {
|
||||
if (PermissionUtils.hasPermission(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION})) {
|
||||
if (PermissionUtils.hasPermission(getActivity(),
|
||||
new String[]{Manifest.permission.ACCESS_FINE_LOCATION})) {
|
||||
onLocationPermissionGranted();
|
||||
} else if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
&& store.getBoolean("displayLocationPermissionForCardView", true)
|
||||
|
|
@ -636,14 +677,14 @@ public class ContributionsFragment
|
|||
*/
|
||||
private void fetchCampaigns() {
|
||||
if (Utils.isMonumentsEnabled(new Date())) {
|
||||
if (binding!=null) {
|
||||
if (binding != null) {
|
||||
binding.campaignsView.setCampaign(wlmCampaign);
|
||||
binding.campaignsView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
} else if (store.getBoolean(CampaignView.CAMPAIGNS_DEFAULT_PREFERENCE, true)) {
|
||||
presenter.getCampaigns();
|
||||
} else {
|
||||
if (binding!=null) {
|
||||
if (binding != null) {
|
||||
binding.campaignsView.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
|
@ -657,7 +698,7 @@ public class ContributionsFragment
|
|||
@Override
|
||||
public void showCampaigns(Campaign campaign) {
|
||||
if (campaign != null && !isUserProfile) {
|
||||
if (binding!=null) {
|
||||
if (binding != null) {
|
||||
binding.campaignsView.setCampaign(campaign);
|
||||
}
|
||||
}
|
||||
|
|
@ -676,67 +717,6 @@ public class ContributionsFragment
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restarts the upload process for a contribution
|
||||
*
|
||||
* @param contribution
|
||||
*/
|
||||
public void restartUpload(Contribution contribution) {
|
||||
contribution.setState(Contribution.STATE_QUEUED);
|
||||
contributionsPresenter.saveContribution(contribution);
|
||||
Timber.d("Restarting for %s", contribution.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Retry upload when it is failed
|
||||
*
|
||||
* @param contribution contribution to be retried
|
||||
*/
|
||||
@Override
|
||||
public void retryUpload(Contribution contribution) {
|
||||
if (NetworkUtils.isInternetConnectionEstablished(getContext())) {
|
||||
if (contribution.getState() == STATE_PAUSED
|
||||
|| contribution.getState() == Contribution.STATE_QUEUED_LIMITED_CONNECTION_MODE) {
|
||||
restartUpload(contribution);
|
||||
} else if (contribution.getState() == STATE_FAILED) {
|
||||
int retries = contribution.getRetries();
|
||||
// TODO: Improve UX. Additional details: https://github.com/commons-app/apps-android-commons/pull/5257#discussion_r1304662562
|
||||
/* Limit the number of retries for a failed upload
|
||||
to handle cases like invalid filename as such uploads
|
||||
will never be successful */
|
||||
if (retries < MAX_RETRIES) {
|
||||
contribution.setRetries(retries + 1);
|
||||
Timber.d("Retried uploading %s %d times", contribution.getMedia().getFilename(),
|
||||
retries + 1);
|
||||
restartUpload(contribution);
|
||||
} else {
|
||||
// TODO: Show the exact reason for failure
|
||||
Toast.makeText(getContext(),
|
||||
R.string.retry_limit_reached, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
} else {
|
||||
Timber.d("Skipping re-upload for non-failed %s", contribution.toString());
|
||||
}
|
||||
} else {
|
||||
ViewUtil.showLongToast(getContext(), R.string.this_function_needs_network_connection);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Pauses the upload
|
||||
*
|
||||
* @param contribution
|
||||
*/
|
||||
@Override
|
||||
public void pauseUpload(Contribution contribution) {
|
||||
//Pause the upload in the global singleton
|
||||
CommonsApplication.pauseUploads.put(contribution.getPageId(), true);
|
||||
//Retain the paused state in DB
|
||||
contribution.setState(STATE_PAUSED);
|
||||
contributionsPresenter.saveContribution(contribution);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify the viewpager that number of items have changed.
|
||||
*/
|
||||
|
|
@ -747,6 +727,54 @@ public class ContributionsFragment
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the visibility and text of the pending uploads count TextView based on the given
|
||||
* count.
|
||||
*
|
||||
* @param pendingCount The number of pending uploads.
|
||||
*/
|
||||
public void updatePendingIcon(int pendingCount) {
|
||||
if (pendingUploadsCountTextView != null) {
|
||||
if (pendingCount != 0) {
|
||||
pendingUploadsCountTextView.setVisibility(View.VISIBLE);
|
||||
pendingUploadsCountTextView.setText(String.valueOf(pendingCount));
|
||||
} else {
|
||||
pendingUploadsCountTextView.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the visibility and text of the error uploads TextView based on the given count.
|
||||
*
|
||||
* @param errorCount The number of error uploads.
|
||||
*/
|
||||
public void updateErrorIcon(int errorCount) {
|
||||
if (uploadsErrorTextView != null) {
|
||||
if (errorCount != 0) {
|
||||
uploadsErrorTextView.setVisibility(View.VISIBLE);
|
||||
uploadsErrorTextView.setText(String.valueOf(errorCount));
|
||||
} else {
|
||||
uploadsErrorTextView.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the visibility of the pending uploads ImageView based on the given count.
|
||||
*
|
||||
* @param count The number of pending uploads.
|
||||
*/
|
||||
public void updateUploadIcon(int count) {
|
||||
if (pendingUploadsImageView != null) {
|
||||
if (count != 0) {
|
||||
pendingUploadsImageView.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
pendingUploadsImageView.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace whatever is in the current contributionsFragmentContainer view with
|
||||
* mediaDetailPagerFragment, and preserve previous state in back stack. Called when user selects
|
||||
|
|
@ -782,7 +810,8 @@ public class ContributionsFragment
|
|||
public boolean backButtonClicked() {
|
||||
if (mediaDetailPagerFragment != null && mediaDetailPagerFragment.isVisible()) {
|
||||
if (store.getBoolean("displayNearbyCardView", true) && !isUserProfile) {
|
||||
if (binding.cardViewNearby.cardViewVisibilityState == NearbyNotificationCardView.CardViewVisibilityState.READY) {
|
||||
if (binding.cardViewNearby.cardViewVisibilityState
|
||||
== NearbyNotificationCardView.CardViewVisibilityState.READY) {
|
||||
binding.cardViewNearby.setVisibility(View.VISIBLE);
|
||||
}
|
||||
} else {
|
||||
|
|
@ -829,6 +858,60 @@ public class ContributionsFragment
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Restarts the upload process for a contribution
|
||||
*
|
||||
* @param contribution
|
||||
*/
|
||||
public void restartUpload(Contribution contribution) {
|
||||
contribution.setDateUploadStarted(Calendar.getInstance().getTime());
|
||||
if (contribution.getState() == Contribution.STATE_FAILED) {
|
||||
if (contribution.getErrorInfo() == null) {
|
||||
contribution.setChunkInfo(null);
|
||||
contribution.setTransferred(0);
|
||||
}
|
||||
contributionsPresenter.checkDuplicateImageAndRestartContribution(contribution);
|
||||
} else {
|
||||
contribution.setState(Contribution.STATE_QUEUED);
|
||||
contributionsPresenter.saveContribution(contribution);
|
||||
Timber.d("Restarting for %s", contribution.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retry upload when it is failed
|
||||
*
|
||||
* @param contribution contribution to be retried
|
||||
*/
|
||||
public void retryUpload(Contribution contribution) {
|
||||
if (NetworkUtils.isInternetConnectionEstablished(getContext())) {
|
||||
if (contribution.getState() == STATE_PAUSED) {
|
||||
restartUpload(contribution);
|
||||
} else if (contribution.getState() == STATE_FAILED) {
|
||||
int retries = contribution.getRetries();
|
||||
// TODO: Improve UX. Additional details: https://github.com/commons-app/apps-android-commons/pull/5257#discussion_r1304662562
|
||||
/* Limit the number of retries for a failed upload
|
||||
to handle cases like invalid filename as such uploads
|
||||
will never be successful */
|
||||
if (retries < MAX_RETRIES) {
|
||||
contribution.setRetries(retries + 1);
|
||||
Timber.d("Retried uploading %s %d times", contribution.getMedia().getFilename(),
|
||||
retries + 1);
|
||||
restartUpload(contribution);
|
||||
} else {
|
||||
// TODO: Show the exact reason for failure
|
||||
Toast.makeText(getContext(),
|
||||
R.string.retry_limit_reached, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
} else {
|
||||
Timber.d("Skipping re-upload for non-failed %s", contribution.toString());
|
||||
}
|
||||
} else {
|
||||
ViewUtil.showLongToast(getContext(), R.string.this_function_needs_network_connection);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Reload media detail fragment once media is nominated
|
||||
*
|
||||
|
|
@ -844,21 +927,6 @@ public class ContributionsFragment
|
|||
}
|
||||
}
|
||||
|
||||
// click listener to toggle description that means uses can press the limited connection
|
||||
// banner and description will hide. Tap again to show description.
|
||||
private View.OnClickListener toggleDescriptionListener = new View.OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
View view2 = binding.limitedConnectionDescriptionTextView;
|
||||
if (view2.getVisibility() == View.GONE) {
|
||||
view2.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
view2.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* When the device rotates, rotate the Nearby banner's compass arrow in tandem.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -70,16 +70,8 @@ public class ContributionsListAdapter extends
|
|||
|
||||
public interface Callback {
|
||||
|
||||
void retryUpload(Contribution contribution);
|
||||
|
||||
void deleteUpload(Contribution contribution);
|
||||
|
||||
void openMediaDetail(int contribution, boolean isWikipediaPageExists);
|
||||
|
||||
void addImageToWikipedia(Contribution contribution);
|
||||
|
||||
void pauseUpload(Contribution contribution);
|
||||
|
||||
void resumeUpload(Contribution contribution);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,5 @@ public class ContributionsListContract {
|
|||
}
|
||||
|
||||
public interface UserActionListener extends BasePresenter<View> {
|
||||
|
||||
void deleteUpload(Contribution contribution);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ import android.view.animation.AnimationUtils;
|
|||
import android.widget.LinearLayout;
|
||||
import androidx.activity.result.ActivityResultCallback;
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.activity.result.contract.ActivityResultContracts;
|
||||
import androidx.activity.result.contract.ActivityResultContracts.RequestMultiplePermissions;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
|
@ -30,11 +30,11 @@ import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver;
|
|||
import androidx.recyclerview.widget.RecyclerView.ItemAnimator;
|
||||
import androidx.recyclerview.widget.RecyclerView.OnItemTouchListener;
|
||||
import androidx.recyclerview.widget.SimpleItemAnimator;
|
||||
import fr.free.nrw.commons.CommonsApplication;
|
||||
import fr.free.nrw.commons.Media;
|
||||
import fr.free.nrw.commons.R;
|
||||
import fr.free.nrw.commons.Utils;
|
||||
import fr.free.nrw.commons.auth.SessionManager;
|
||||
import fr.free.nrw.commons.contributions.ContributionsListAdapter.Callback;
|
||||
import fr.free.nrw.commons.databinding.FragmentContributionsListBinding;
|
||||
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
|
||||
import fr.free.nrw.commons.media.MediaClient;
|
||||
|
|
@ -42,7 +42,6 @@ import fr.free.nrw.commons.profile.ProfileActivity;
|
|||
import fr.free.nrw.commons.utils.DialogUtil;
|
||||
import fr.free.nrw.commons.utils.SystemThemeUtils;
|
||||
import fr.free.nrw.commons.utils.ViewUtil;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import javax.inject.Inject;
|
||||
|
|
@ -56,7 +55,7 @@ import fr.free.nrw.commons.wikidata.model.WikiSite;
|
|||
*/
|
||||
|
||||
public class ContributionsListFragment extends CommonsDaggerSupportFragment implements
|
||||
ContributionsListContract.View, ContributionsListAdapter.Callback,
|
||||
ContributionsListContract.View, Callback,
|
||||
WikipediaInstructionsDialogFragment.Callback {
|
||||
|
||||
private static final String RV_STATE = "rv_scroll_state";
|
||||
|
|
@ -81,7 +80,6 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
|
|||
private Animation rotate_forward;
|
||||
private Animation rotate_backward;
|
||||
private boolean isFabOpen;
|
||||
|
||||
@VisibleForTesting
|
||||
protected RecyclerView rvContributionsList;
|
||||
|
||||
|
|
@ -99,7 +97,7 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
|
|||
private String userName;
|
||||
|
||||
private ActivityResultLauncher<String[]> inAppCameraLocationPermissionLauncher = registerForActivityResult(
|
||||
new ActivityResultContracts.RequestMultiplePermissions(),
|
||||
new RequestMultiplePermissions(),
|
||||
new ActivityResultCallback<Map<String, Boolean>>() {
|
||||
@Override
|
||||
public void onActivityResult(Map<String, Boolean> result) {
|
||||
|
|
@ -151,7 +149,7 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
|
|||
contributionsListPresenter.onAttachView(this);
|
||||
binding.fabCustomGallery.setOnClickListener(v -> launchCustomSelector());
|
||||
binding.fabCustomGallery.setOnLongClickListener(view -> {
|
||||
ViewUtil.showShortToast(getContext(),R.string.custom_selector_title);
|
||||
ViewUtil.showShortToast(getContext(), R.string.custom_selector_title);
|
||||
return true;
|
||||
});
|
||||
|
||||
|
|
@ -160,7 +158,8 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
|
|||
binding.fabLayout.setVisibility(VISIBLE);
|
||||
} else {
|
||||
binding.tvContributionsOfUser.setVisibility(VISIBLE);
|
||||
binding.tvContributionsOfUser.setText(getString(R.string.contributions_of_user, userName));
|
||||
binding.tvContributionsOfUser.setText(
|
||||
getString(R.string.contributions_of_user, userName));
|
||||
binding.fabLayout.setVisibility(GONE);
|
||||
}
|
||||
|
||||
|
|
@ -305,8 +304,9 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
|
|||
public void onConfigurationChanged(final Configuration newConfig) {
|
||||
super.onConfigurationChanged(newConfig);
|
||||
// check orientation
|
||||
binding.fabLayout.setOrientation(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ?
|
||||
LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
|
||||
binding.fabLayout.setOrientation(
|
||||
newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ?
|
||||
LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
|
||||
rvContributionsList
|
||||
.setLayoutManager(
|
||||
new GridLayoutManager(getContext(), getSpanCount(newConfig.orientation)));
|
||||
|
|
@ -326,7 +326,7 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
|
|||
animateFAB(isFabOpen);
|
||||
});
|
||||
binding.fabCamera.setOnLongClickListener(view -> {
|
||||
ViewUtil.showShortToast(getContext(),R.string.add_contribution_from_camera);
|
||||
ViewUtil.showShortToast(getContext(), R.string.add_contribution_from_camera);
|
||||
return true;
|
||||
});
|
||||
binding.fabGallery.setOnClickListener(view -> {
|
||||
|
|
@ -334,7 +334,7 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
|
|||
animateFAB(isFabOpen);
|
||||
});
|
||||
binding.fabGallery.setOnLongClickListener(view -> {
|
||||
ViewUtil.showShortToast(getContext(),R.string.menu_from_gallery);
|
||||
ViewUtil.showShortToast(getContext(), R.string.menu_from_gallery);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
|
@ -415,30 +415,6 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void retryUpload(final Contribution contribution) {
|
||||
if (null != callback) {//Just being safe, ideally they won't be called when detached
|
||||
callback.retryUpload(contribution);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteUpload(final Contribution contribution) {
|
||||
DialogUtil.showAlertDialog(getActivity(),
|
||||
String.format(Locale.getDefault(),
|
||||
getString(R.string.cancelling_upload)),
|
||||
String.format(Locale.getDefault(),
|
||||
getString(R.string.cancel_upload_dialog)),
|
||||
String.format(Locale.getDefault(), getString(R.string.yes)), String.format(Locale.getDefault(), getString(R.string.no)),
|
||||
() -> {
|
||||
ViewUtil.showShortToast(getContext(), R.string.cancelling_upload);
|
||||
contributionsListPresenter.deleteUpload(contribution);
|
||||
CommonsApplication.cancelledUploads.add(contribution.getPageId());
|
||||
}, () -> {
|
||||
// Do nothing
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openMediaDetail(final int position, boolean isWikipediaButtonDisplayed) {
|
||||
if (null != callback) {//Just being safe, ideally they won't be called when detached
|
||||
|
|
@ -463,28 +439,6 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Pauses the current upload
|
||||
*
|
||||
* @param contribution
|
||||
*/
|
||||
@Override
|
||||
public void pauseUpload(Contribution contribution) {
|
||||
ViewUtil.showShortToast(getContext(), R.string.pausing_upload);
|
||||
callback.pauseUpload(contribution);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resumes the current upload
|
||||
*
|
||||
* @param contribution
|
||||
*/
|
||||
@Override
|
||||
public void resumeUpload(Contribution contribution) {
|
||||
ViewUtil.showShortToast(getContext(), R.string.resuming_upload);
|
||||
callback.retryUpload(contribution);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display confirmation dialog with instructions when the user tries to add image to wikipedia
|
||||
*
|
||||
|
|
@ -536,13 +490,10 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
|
|||
|
||||
void notifyDataSetChanged();
|
||||
|
||||
void retryUpload(Contribution contribution);
|
||||
|
||||
void showDetail(int position, boolean isWikipediaButtonDisplayed);
|
||||
|
||||
void pauseUpload(Contribution contribution);
|
||||
|
||||
// Notify the viewpager that number of items have changed.
|
||||
void viewPagerNotifyDataSetChanged();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ import fr.free.nrw.commons.contributions.ContributionsListContract.UserActionLis
|
|||
import fr.free.nrw.commons.di.CommonsApplicationModule;
|
||||
import io.reactivex.Scheduler;
|
||||
import io.reactivex.disposables.CompositeDisposable;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
|
|
@ -36,7 +38,7 @@ public class ContributionsListPresenter implements UserActionListener {
|
|||
this.contributionBoundaryCallback = contributionBoundaryCallback;
|
||||
this.repository = repository;
|
||||
this.ioThreadScheduler = ioThreadScheduler;
|
||||
this.contributionsRemoteDataSource=contributionsRemoteDataSource;
|
||||
this.contributionsRemoteDataSource = contributionsRemoteDataSource;
|
||||
compositeDisposable = new CompositeDisposable();
|
||||
}
|
||||
|
||||
|
|
@ -71,10 +73,12 @@ public class ContributionsListPresenter implements UserActionListener {
|
|||
} else {
|
||||
contributionBoundaryCallback.setUserName(userName);
|
||||
shouldSetBoundaryCallback = true;
|
||||
factory = repository.fetchContributions();
|
||||
factory = repository.fetchContributionsWithStates(
|
||||
Collections.singletonList(Contribution.STATE_COMPLETED));
|
||||
}
|
||||
|
||||
LivePagedListBuilder livePagedListBuilder = new LivePagedListBuilder(factory, pagedListConfig);
|
||||
LivePagedListBuilder livePagedListBuilder = new LivePagedListBuilder(factory,
|
||||
pagedListConfig);
|
||||
if (shouldSetBoundaryCallback) {
|
||||
livePagedListBuilder.setBoundaryCallback(contributionBoundaryCallback);
|
||||
}
|
||||
|
|
@ -89,15 +93,4 @@ public class ContributionsListPresenter implements UserActionListener {
|
|||
contributionBoundaryCallback.dispose();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a failed contribution from the local db
|
||||
*/
|
||||
@Override
|
||||
public void deleteUpload(final Contribution contribution) {
|
||||
compositeDisposable.add(repository
|
||||
.deleteContributionFromDB(contribution)
|
||||
.subscribeOn(ioThreadScheduler)
|
||||
.subscribe());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,14 @@
|
|||
package fr.free.nrw.commons.contributions;
|
||||
|
||||
import androidx.paging.DataSource.Factory;
|
||||
import fr.free.nrw.commons.kvstore.JsonKvStore;
|
||||
import io.reactivex.Completable;
|
||||
import io.reactivex.Single;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
import fr.free.nrw.commons.kvstore.JsonKvStore;
|
||||
import io.reactivex.Single;
|
||||
|
||||
/**
|
||||
* The LocalDataSource class for Contributions
|
||||
*/
|
||||
|
|
@ -21,8 +19,8 @@ class ContributionsLocalDataSource {
|
|||
|
||||
@Inject
|
||||
public ContributionsLocalDataSource(
|
||||
@Named("default_preferences") final JsonKvStore defaultKVStore,
|
||||
final ContributionDao contributionDao) {
|
||||
@Named("default_preferences") final JsonKvStore defaultKVStore,
|
||||
final ContributionDao contributionDao) {
|
||||
this.defaultKVStore = defaultKVStore;
|
||||
this.contributionDao = contributionDao;
|
||||
}
|
||||
|
|
@ -38,17 +36,19 @@ class ContributionsLocalDataSource {
|
|||
* Fetch default number of contributions to be show, based on user preferences
|
||||
*/
|
||||
public long getLong(final String key) {
|
||||
return defaultKVStore.getLong(key);
|
||||
return defaultKVStore.getLong(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get contribution object from cursor
|
||||
*
|
||||
* @param uri
|
||||
* @return
|
||||
*/
|
||||
public Contribution getContributionWithFileName(final String uri) {
|
||||
final List<Contribution> contributionWithUri = contributionDao.getContributionWithTitle(uri);
|
||||
if(!contributionWithUri.isEmpty()){
|
||||
final List<Contribution> contributionWithUri = contributionDao.getContributionWithTitle(
|
||||
uri);
|
||||
if (!contributionWithUri.isEmpty()) {
|
||||
return contributionWithUri.get(0);
|
||||
}
|
||||
return null;
|
||||
|
|
@ -56,6 +56,7 @@ class ContributionsLocalDataSource {
|
|||
|
||||
/**
|
||||
* Remove a contribution from the contributions table
|
||||
*
|
||||
* @param contribution
|
||||
* @return
|
||||
*/
|
||||
|
|
@ -63,15 +64,48 @@ class ContributionsLocalDataSource {
|
|||
return contributionDao.delete(contribution);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes contributions with specific states.
|
||||
*
|
||||
* @param states The states of the contributions to delete.
|
||||
* @return A Completable indicating the result of the operation.
|
||||
*/
|
||||
public Completable deleteContributionsWithStates(List<Integer> states) {
|
||||
return contributionDao.deleteContributionsWithStates(states);
|
||||
}
|
||||
|
||||
public Factory<Integer, Contribution> getContributions() {
|
||||
return contributionDao.fetchContributions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches contributions with specific states.
|
||||
*
|
||||
* @param states The states of the contributions to fetch.
|
||||
* @return A DataSource factory for paginated contributions with the specified states.
|
||||
*/
|
||||
public Factory<Integer, Contribution> getContributionsWithStates(List<Integer> states) {
|
||||
return contributionDao.getContributions(states);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches contributions with specific states sorted by the date the upload started.
|
||||
*
|
||||
* @param states The states of the contributions to fetch.
|
||||
* @return A DataSource factory for paginated contributions with the specified states sorted by
|
||||
* date upload started.
|
||||
*/
|
||||
public Factory<Integer, Contribution> getContributionsWithStatesSortedByDateUploadStarted(
|
||||
List<Integer> states) {
|
||||
return contributionDao.getContributionsSortedByDateUploadStarted(states);
|
||||
}
|
||||
|
||||
public Single<List<Long>> saveContributions(final List<Contribution> contributions) {
|
||||
final List<Contribution> contributionList = new ArrayList<>();
|
||||
for(final Contribution contribution: contributions) {
|
||||
final Contribution oldContribution = contributionDao.getContribution(contribution.getPageId());
|
||||
if(oldContribution != null) {
|
||||
for (final Contribution contribution : contributions) {
|
||||
final Contribution oldContribution = contributionDao.getContribution(
|
||||
contribution.getPageId());
|
||||
if (oldContribution != null) {
|
||||
contribution.setWikidataPlace(oldContribution.getWikidataPlace());
|
||||
}
|
||||
contributionList.add(contribution);
|
||||
|
|
@ -84,10 +118,14 @@ class ContributionsLocalDataSource {
|
|||
}
|
||||
|
||||
public void set(final String key, final long value) {
|
||||
defaultKVStore.putLong(key,value);
|
||||
defaultKVStore.putLong(key, value);
|
||||
}
|
||||
|
||||
public Completable updateContribution(final Contribution contribution) {
|
||||
return contributionDao.update(contribution);
|
||||
}
|
||||
|
||||
public Completable updateContributionsWithStates(List<Integer> states, int newState) {
|
||||
return contributionDao.updateContributionsWithStates(states, newState);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,26 @@
|
|||
package fr.free.nrw.commons.contributions;
|
||||
|
||||
import static fr.free.nrw.commons.utils.ImageUtils.IMAGE_OK;
|
||||
|
||||
import androidx.work.ExistingWorkPolicy;
|
||||
import fr.free.nrw.commons.MediaDataExtractor;
|
||||
import fr.free.nrw.commons.contributions.ContributionsContract.UserActionListener;
|
||||
import fr.free.nrw.commons.di.CommonsApplicationModule;
|
||||
import fr.free.nrw.commons.repository.UploadRepository;
|
||||
import fr.free.nrw.commons.upload.worker.WorkRequestHelper;
|
||||
import io.reactivex.Scheduler;
|
||||
import io.reactivex.disposables.CompositeDisposable;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import timber.log.Timber;
|
||||
|
||||
/**
|
||||
* The presenter class for Contributions
|
||||
*/
|
||||
public class ContributionsPresenter implements UserActionListener {
|
||||
|
||||
private final ContributionsRepository repository;
|
||||
private final ContributionsRepository contributionsRepository;
|
||||
private final UploadRepository uploadRepository;
|
||||
private final Scheduler ioThreadScheduler;
|
||||
private CompositeDisposable compositeDisposable;
|
||||
private ContributionsContract.View view;
|
||||
|
|
@ -25,15 +30,17 @@ public class ContributionsPresenter implements UserActionListener {
|
|||
|
||||
@Inject
|
||||
ContributionsPresenter(ContributionsRepository repository,
|
||||
UploadRepository uploadRepository,
|
||||
@Named(CommonsApplicationModule.IO_THREAD) Scheduler ioThreadScheduler) {
|
||||
this.repository = repository;
|
||||
this.ioThreadScheduler=ioThreadScheduler;
|
||||
this.contributionsRepository = repository;
|
||||
this.uploadRepository = uploadRepository;
|
||||
this.ioThreadScheduler = ioThreadScheduler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttachView(ContributionsContract.View view) {
|
||||
this.view = view;
|
||||
compositeDisposable=new CompositeDisposable();
|
||||
compositeDisposable = new CompositeDisposable();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -44,19 +51,30 @@ public class ContributionsPresenter implements UserActionListener {
|
|||
|
||||
@Override
|
||||
public Contribution getContributionsWithTitle(String title) {
|
||||
return repository.getContributionWithFileName(title);
|
||||
return contributionsRepository.getContributionWithFileName(title);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a failed contribution from the local db
|
||||
* @param contribution
|
||||
* Checks if a contribution is a duplicate and restarts the contribution process if it is not.
|
||||
*
|
||||
* @param contribution The contribution to check and potentially restart.
|
||||
*/
|
||||
@Override
|
||||
public void deleteUpload(Contribution contribution) {
|
||||
compositeDisposable.add(repository
|
||||
.deleteContributionFromDB(contribution)
|
||||
public void checkDuplicateImageAndRestartContribution(Contribution contribution) {
|
||||
compositeDisposable.add(uploadRepository
|
||||
.checkDuplicateImage(contribution.getLocalUriPath().getPath())
|
||||
.subscribeOn(ioThreadScheduler)
|
||||
.subscribe());
|
||||
.subscribe(imageCheckResult -> {
|
||||
if (imageCheckResult == IMAGE_OK) {
|
||||
contribution.setState(Contribution.STATE_QUEUED);
|
||||
saveContribution(contribution);
|
||||
} else {
|
||||
Timber.e("Contribution already exists");
|
||||
compositeDisposable.add(contributionsRepository
|
||||
.deleteContributionFromDB(contribution)
|
||||
.subscribeOn(ioThreadScheduler)
|
||||
.subscribe());
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -65,9 +83,8 @@ public class ContributionsPresenter implements UserActionListener {
|
|||
*
|
||||
* @param contribution
|
||||
*/
|
||||
@Override
|
||||
public void saveContribution(Contribution contribution) {
|
||||
compositeDisposable.add(repository
|
||||
compositeDisposable.add(contributionsRepository
|
||||
.save(contribution)
|
||||
.subscribeOn(ioThreadScheduler)
|
||||
.subscribe(() -> WorkRequestHelper.Companion.makeOneTimeWorkRequest(
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ public class ContributionsRepository {
|
|||
|
||||
/**
|
||||
* Deletes a failed upload from DB
|
||||
*
|
||||
* @param contribution
|
||||
* @return
|
||||
*/
|
||||
|
|
@ -36,8 +37,19 @@ public class ContributionsRepository {
|
|||
return localDataSource.deleteContribution(contribution);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes contributions from the database with specific states.
|
||||
*
|
||||
* @param states The states of the contributions to delete.
|
||||
* @return A Completable indicating the result of the operation.
|
||||
*/
|
||||
public Completable deleteContributionsFromDBWithStates(List<Integer> states) {
|
||||
return localDataSource.deleteContributionsWithStates(states);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get contribution object with title
|
||||
*
|
||||
* @param fileName
|
||||
* @return
|
||||
*/
|
||||
|
|
@ -49,19 +61,52 @@ public class ContributionsRepository {
|
|||
return localDataSource.getContributions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches contributions with specific states.
|
||||
*
|
||||
* @param states The states of the contributions to fetch.
|
||||
* @return A DataSource factory for paginated contributions with the specified states.
|
||||
*/
|
||||
public Factory<Integer, Contribution> fetchContributionsWithStates(List<Integer> states) {
|
||||
return localDataSource.getContributionsWithStates(states);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches contributions with specific states sorted by the date the upload started.
|
||||
*
|
||||
* @param states The states of the contributions to fetch.
|
||||
* @return A DataSource factory for paginated contributions with the specified states sorted by
|
||||
* date upload started.
|
||||
*/
|
||||
public Factory<Integer, Contribution> fetchContributionsWithStatesSortedByDateUploadStarted(
|
||||
List<Integer> states) {
|
||||
return localDataSource.getContributionsWithStatesSortedByDateUploadStarted(states);
|
||||
}
|
||||
|
||||
public Single<List<Long>> save(List<Contribution> contributions) {
|
||||
return localDataSource.saveContributions(contributions);
|
||||
}
|
||||
|
||||
public Completable save(Contribution contributions){
|
||||
public Completable save(Contribution contributions) {
|
||||
return localDataSource.saveContributions(contributions);
|
||||
}
|
||||
|
||||
public void set(String key, long value) {
|
||||
localDataSource.set(key,value);
|
||||
localDataSource.set(key, value);
|
||||
}
|
||||
|
||||
public Completable updateContribution(Contribution contribution) {
|
||||
return localDataSource.updateContribution(contribution);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the state of contributions with specific states.
|
||||
*
|
||||
* @param states The current states of the contributions to update.
|
||||
* @param newState The new state to set.
|
||||
* @return A Completable indicating the result of the operation.
|
||||
*/
|
||||
public Completable updateContributionsWithStates(List<Integer> states, int newState) {
|
||||
return localDataSource.updateContributionsWithStates(states, newState);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,18 +41,21 @@ import fr.free.nrw.commons.notification.NotificationController;
|
|||
import fr.free.nrw.commons.quiz.QuizChecker;
|
||||
import fr.free.nrw.commons.settings.SettingsFragment;
|
||||
import fr.free.nrw.commons.theme.BaseActivity;
|
||||
import fr.free.nrw.commons.upload.UploadActivity;
|
||||
import fr.free.nrw.commons.upload.UploadProgressActivity;
|
||||
import fr.free.nrw.commons.upload.worker.WorkRequestHelper;
|
||||
import fr.free.nrw.commons.utils.PermissionUtils;
|
||||
import fr.free.nrw.commons.utils.ViewUtilWrapper;
|
||||
import io.reactivex.Completable;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import timber.log.Timber;
|
||||
|
||||
public class MainActivity extends BaseActivity
|
||||
public class MainActivity extends BaseActivity
|
||||
implements FragmentManager.OnBackStackChangedListener {
|
||||
|
||||
@Inject
|
||||
|
|
@ -144,16 +147,16 @@ public class MainActivity extends BaseActivity
|
|||
applicationKvStore.putBoolean("hasAlreadyLaunchedBigMultiupload", false);
|
||||
applicationKvStore.putBoolean("hasAlreadyLaunchedCategoriesDialog", false);
|
||||
}
|
||||
if(savedInstanceState == null){
|
||||
if (savedInstanceState == null) {
|
||||
//starting a fresh fragment.
|
||||
// Open Last opened screen if it is Contributions or Nearby, otherwise Contributions
|
||||
if(applicationKvStore.getBoolean("last_opened_nearby")){
|
||||
if (applicationKvStore.getBoolean("last_opened_nearby")) {
|
||||
setTitle(getString(R.string.nearby_fragment));
|
||||
showNearby();
|
||||
loadFragment(NearbyParentFragment.newInstance(),false);
|
||||
}else{
|
||||
loadFragment(NearbyParentFragment.newInstance(), false);
|
||||
} else {
|
||||
setTitle(getString(R.string.contributions_fragment));
|
||||
loadFragment(ContributionsFragment.newInstance(),false);
|
||||
loadFragment(ContributionsFragment.newInstance(), false);
|
||||
}
|
||||
}
|
||||
setUpPager();
|
||||
|
|
@ -165,7 +168,8 @@ public class MainActivity extends BaseActivity
|
|||
if (VERSION.SDK_INT >= VERSION_CODES.Q) {
|
||||
PermissionUtils.checkPermissionsAndPerformAction(
|
||||
this,
|
||||
() -> {},
|
||||
() -> {
|
||||
},
|
||||
R.string.media_location_permission_denied,
|
||||
R.string.add_location_manually,
|
||||
permission.ACCESS_MEDIA_LOCATION);
|
||||
|
|
@ -179,32 +183,33 @@ public class MainActivity extends BaseActivity
|
|||
}
|
||||
|
||||
private void setUpPager() {
|
||||
binding.fragmentMainNavTabLayout.setOnNavigationItemSelectedListener(navListener = (item) -> {
|
||||
if (!item.getTitle().equals(getString(R.string.more))) {
|
||||
// do not change title for more fragment
|
||||
setTitle(item.getTitle());
|
||||
}
|
||||
// set last_opened_nearby true if item is nearby screen else set false
|
||||
applicationKvStore.putBoolean("last_opened_nearby",
|
||||
item.getTitle().equals(getString(R.string.nearby_fragment)));
|
||||
final Fragment fragment = NavTab.of(item.getOrder()).newInstance();
|
||||
return loadFragment(fragment, true);
|
||||
});
|
||||
binding.fragmentMainNavTabLayout.setOnNavigationItemSelectedListener(
|
||||
navListener = (item) -> {
|
||||
if (!item.getTitle().equals(getString(R.string.more))) {
|
||||
// do not change title for more fragment
|
||||
setTitle(item.getTitle());
|
||||
}
|
||||
// set last_opened_nearby true if item is nearby screen else set false
|
||||
applicationKvStore.putBoolean("last_opened_nearby",
|
||||
item.getTitle().equals(getString(R.string.nearby_fragment)));
|
||||
final Fragment fragment = NavTab.of(item.getOrder()).newInstance();
|
||||
return loadFragment(fragment, true);
|
||||
});
|
||||
}
|
||||
|
||||
private void setUpLoggedOutPager() {
|
||||
loadFragment(ExploreFragment.newInstance(),false);
|
||||
loadFragment(ExploreFragment.newInstance(), false);
|
||||
binding.fragmentMainNavTabLayout.setOnNavigationItemSelectedListener(item -> {
|
||||
if (!item.getTitle().equals(getString(R.string.more))) {
|
||||
// do not change title for more fragment
|
||||
setTitle(item.getTitle());
|
||||
}
|
||||
Fragment fragment = NavTabLoggedOut.of(item.getOrder()).newInstance();
|
||||
return loadFragment(fragment,true);
|
||||
return loadFragment(fragment, true);
|
||||
});
|
||||
}
|
||||
|
||||
private boolean loadFragment(Fragment fragment,boolean showBottom ) {
|
||||
private boolean loadFragment(Fragment fragment, boolean showBottom) {
|
||||
//showBottom so that we do not show the bottom tray again when constructing
|
||||
//from the saved instance state.
|
||||
if (fragment instanceof ContributionsFragment) {
|
||||
|
|
@ -234,7 +239,8 @@ public class MainActivity extends BaseActivity
|
|||
bookmarkFragment = (BookmarkFragment) fragment;
|
||||
activeFragment = ActiveFragment.BOOKMARK;
|
||||
} else if (fragment == null && showBottom) {
|
||||
if (applicationKvStore.getBoolean("login_skipped") == true) { // If logged out, more sheet is different
|
||||
if (applicationKvStore.getBoolean("login_skipped")
|
||||
== true) { // If logged out, more sheet is different
|
||||
MoreBottomSheetLoggedOutFragment bottomSheet = new MoreBottomSheetLoggedOutFragment();
|
||||
bottomSheet.show(getSupportFragmentManager(),
|
||||
"MoreBottomSheetLoggedOut");
|
||||
|
|
@ -264,28 +270,30 @@ public class MainActivity extends BaseActivity
|
|||
}
|
||||
|
||||
/**
|
||||
* Adds number of uploads next to tab text "Contributions" then it will look like
|
||||
* "Contributions (NUMBER)"
|
||||
* Adds number of uploads next to tab text "Contributions" then it will look like "Contributions
|
||||
* (NUMBER)"
|
||||
*
|
||||
* @param uploadCount
|
||||
*/
|
||||
public void setNumOfUploads(int uploadCount) {
|
||||
if (activeFragment == ActiveFragment.CONTRIBUTIONS) {
|
||||
setTitle(getResources().getString(R.string.contributions_fragment) +" "+ (
|
||||
setTitle(getResources().getString(R.string.contributions_fragment) + " " + (
|
||||
!(uploadCount == 0) ?
|
||||
getResources()
|
||||
.getQuantityString(R.plurals.contributions_subtitle,
|
||||
uploadCount, uploadCount):getString(R.string.contributions_subtitle_zero)));
|
||||
getResources()
|
||||
.getQuantityString(R.plurals.contributions_subtitle,
|
||||
uploadCount, uploadCount)
|
||||
: getString(R.string.contributions_subtitle_zero)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resume the uploads that got stuck because of the app being killed
|
||||
* or the device being rebooted.
|
||||
*
|
||||
* Resume the uploads that got stuck because of the app being killed or the device being
|
||||
* rebooted.
|
||||
* <p>
|
||||
* When the app is terminated or the device is restarted, contributions remain in the
|
||||
* 'STATE_IN_PROGRESS' state. This status persists and doesn't change during these events.
|
||||
* So, retrieving contributions labeled as 'STATE_IN_PROGRESS'
|
||||
* from the database will provide the list of uploads that appear as stuck on opening the app again
|
||||
* 'STATE_IN_PROGRESS' state. This status persists and doesn't change during these events. So,
|
||||
* retrieving contributions labeled as 'STATE_IN_PROGRESS' from the database will provide the
|
||||
* list of uploads that appear as stuck on opening the app again
|
||||
*/
|
||||
@SuppressLint("CheckResult")
|
||||
private void checkAndResumeStuckUploads() {
|
||||
|
|
@ -294,9 +302,10 @@ public class MainActivity extends BaseActivity
|
|||
.subscribeOn(Schedulers.io())
|
||||
.blockingGet();
|
||||
Timber.d("Resuming " + stuckUploads.size() + " uploads...");
|
||||
if(!stuckUploads.isEmpty()) {
|
||||
for(Contribution contribution: stuckUploads) {
|
||||
if (!stuckUploads.isEmpty()) {
|
||||
for (Contribution contribution : stuckUploads) {
|
||||
contribution.setState(Contribution.STATE_QUEUED);
|
||||
contribution.setDateUploadStarted(Calendar.getInstance().getTime());
|
||||
Completable.fromAction(() -> contributionDao.saveSynchronous(contribution))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe();
|
||||
|
|
@ -323,24 +332,24 @@ public class MainActivity extends BaseActivity
|
|||
protected void onRestoreInstanceState(Bundle savedInstanceState) {
|
||||
super.onRestoreInstanceState(savedInstanceState);
|
||||
String activeFragmentName = savedInstanceState.getString("activeFragment");
|
||||
if(activeFragmentName != null) {
|
||||
if (activeFragmentName != null) {
|
||||
restoreActiveFragment(activeFragmentName);
|
||||
}
|
||||
}
|
||||
|
||||
private void restoreActiveFragment(@NonNull String fragmentName) {
|
||||
if(fragmentName.equals(ActiveFragment.CONTRIBUTIONS.name())) {
|
||||
if (fragmentName.equals(ActiveFragment.CONTRIBUTIONS.name())) {
|
||||
setTitle(getString(R.string.contributions_fragment));
|
||||
loadFragment(ContributionsFragment.newInstance(),false);
|
||||
}else if(fragmentName.equals(ActiveFragment.NEARBY.name())) {
|
||||
loadFragment(ContributionsFragment.newInstance(), false);
|
||||
} else if (fragmentName.equals(ActiveFragment.NEARBY.name())) {
|
||||
setTitle(getString(R.string.nearby_fragment));
|
||||
loadFragment(NearbyParentFragment.newInstance(),false);
|
||||
}else if(fragmentName.equals(ActiveFragment.EXPLORE.name())) {
|
||||
loadFragment(NearbyParentFragment.newInstance(), false);
|
||||
} else if (fragmentName.equals(ActiveFragment.EXPLORE.name())) {
|
||||
setTitle(getString(R.string.navigation_item_explore));
|
||||
loadFragment(ExploreFragment.newInstance(),false);
|
||||
}else if(fragmentName.equals(ActiveFragment.BOOKMARK.name())) {
|
||||
loadFragment(ExploreFragment.newInstance(), false);
|
||||
} else if (fragmentName.equals(ActiveFragment.BOOKMARK.name())) {
|
||||
setTitle(getString(R.string.bookmarks));
|
||||
loadFragment(BookmarkFragment.newInstance(),false);
|
||||
loadFragment(BookmarkFragment.newInstance(), false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -356,8 +365,9 @@ public class MainActivity extends BaseActivity
|
|||
// Means that nearby fragment is visible
|
||||
/* If function nearbyParentFragment.backButtonClick() returns false, it means that the bottomsheet is
|
||||
not expanded. So if the back button is pressed, then go back to the Contributions tab */
|
||||
if(!nearbyParentFragment.backButtonClicked()){
|
||||
getSupportFragmentManager().beginTransaction().remove(nearbyParentFragment).commit();
|
||||
if (!nearbyParentFragment.backButtonClicked()) {
|
||||
getSupportFragmentManager().beginTransaction().remove(nearbyParentFragment)
|
||||
.commit();
|
||||
setSelectedItemId(NavTab.CONTRIBUTIONS.code());
|
||||
}
|
||||
} else if (exploreFragment != null && activeFragment == ActiveFragment.EXPLORE) {
|
||||
|
|
@ -382,18 +392,6 @@ public class MainActivity extends BaseActivity
|
|||
//initBackButton();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.notifications:
|
||||
// Starts notification activity on click to notification icon
|
||||
NotificationActivity.startYourself(this, "unread");
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retry all failed uploads as soon as the user returns to the app
|
||||
*/
|
||||
|
|
@ -403,41 +401,45 @@ public class MainActivity extends BaseActivity
|
|||
getContribution(Collections.singletonList(Contribution.STATE_FAILED))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe(failedUploads -> {
|
||||
for (Contribution contribution: failedUploads) {
|
||||
for (Contribution contribution : failedUploads) {
|
||||
contributionsFragment.retryUpload(contribution);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void toggleLimitedConnectionMode() {
|
||||
defaultKvStore.putBoolean(CommonsApplication.IS_LIMITED_CONNECTION_MODE_ENABLED,
|
||||
!defaultKvStore
|
||||
.getBoolean(CommonsApplication.IS_LIMITED_CONNECTION_MODE_ENABLED, false));
|
||||
if (defaultKvStore
|
||||
.getBoolean(CommonsApplication.IS_LIMITED_CONNECTION_MODE_ENABLED, false)) {
|
||||
viewUtilWrapper
|
||||
.showShortToast(getBaseContext(), getString(R.string.limited_connection_enabled));
|
||||
} else {
|
||||
WorkRequestHelper.Companion.makeOneTimeWorkRequest(getApplicationContext(),
|
||||
ExistingWorkPolicy.APPEND_OR_REPLACE);
|
||||
viewUtilWrapper
|
||||
.showShortToast(getBaseContext(), getString(R.string.limited_connection_disabled));
|
||||
/**
|
||||
* Handles item selection in the options menu. This method is called when a user interacts with
|
||||
* the options menu in the Top Bar.
|
||||
*/
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.upload_tab:
|
||||
startActivity(new Intent(this, UploadProgressActivity.class));
|
||||
return true;
|
||||
case R.id.notifications:
|
||||
// Starts notification activity on click to notification icon
|
||||
NotificationActivity.startYourself(this, "unread");
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
||||
public void centerMapToPlace(Place place) {
|
||||
setSelectedItemId(NavTab.NEARBY.code());
|
||||
nearbyParentFragment.setNearbyParentFragmentInstanceReadyCallback(new NearbyParentFragmentInstanceReadyCallback() {
|
||||
@Override
|
||||
public void onReady() {
|
||||
nearbyParentFragment.centerMapToPlace(place);
|
||||
}
|
||||
});
|
||||
nearbyParentFragment.setNearbyParentFragmentInstanceReadyCallback(
|
||||
new NearbyParentFragmentInstanceReadyCallback() {
|
||||
@Override
|
||||
public void onReady() {
|
||||
nearbyParentFragment.centerMapToPlace(place);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
Timber.d(data!=null?data.toString():"onActivityResult data is null");
|
||||
Timber.d(data != null ? data.toString() : "onActivityResult data is null");
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
controller.handleActivityResult(this, requestCode, resultCode, data);
|
||||
}
|
||||
|
|
@ -482,14 +484,15 @@ public class MainActivity extends BaseActivity
|
|||
/**
|
||||
* Load default language in onCreate from SharedPreferences
|
||||
*/
|
||||
private void loadLocale(){
|
||||
final SharedPreferences preferences = getSharedPreferences("Settings", Activity.MODE_PRIVATE);
|
||||
private void loadLocale() {
|
||||
final SharedPreferences preferences = getSharedPreferences("Settings",
|
||||
Activity.MODE_PRIVATE);
|
||||
final String language = preferences.getString("language", "");
|
||||
final SettingsFragment settingsFragment = new SettingsFragment();
|
||||
settingsFragment.setLocale(this, language);
|
||||
}
|
||||
|
||||
public NavTabLayout.OnNavigationItemSelectedListener getNavListener(){
|
||||
public NavTabLayout.OnNavigationItemSelectedListener getNavListener() {
|
||||
return navListener;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import fr.free.nrw.commons.profile.ProfileActivity;
|
|||
import fr.free.nrw.commons.review.ReviewActivity;
|
||||
import fr.free.nrw.commons.settings.SettingsActivity;
|
||||
import fr.free.nrw.commons.upload.UploadActivity;
|
||||
import fr.free.nrw.commons.upload.UploadProgressActivity;
|
||||
|
||||
/**
|
||||
* This Class handles the dependency injection (using dagger)
|
||||
|
|
@ -81,6 +82,9 @@ public abstract class ActivityBuilderModule {
|
|||
@ContributesAndroidInjector
|
||||
abstract ZoomableActivity bindZoomableActivity();
|
||||
|
||||
@ContributesAndroidInjector
|
||||
abstract UploadProgressActivity bindUploadProgressActivity();
|
||||
|
||||
@ContributesAndroidInjector
|
||||
abstract WikidataFeedback bindWikiFeedback();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,8 @@ import fr.free.nrw.commons.profile.achievements.AchievementsFragment;
|
|||
import fr.free.nrw.commons.profile.leaderboard.LeaderboardFragment;
|
||||
import fr.free.nrw.commons.review.ReviewImageFragment;
|
||||
import fr.free.nrw.commons.settings.SettingsFragment;
|
||||
import fr.free.nrw.commons.upload.FailedUploadsFragment;
|
||||
import fr.free.nrw.commons.upload.PendingUploadsFragment;
|
||||
import fr.free.nrw.commons.upload.categories.UploadCategoriesFragment;
|
||||
import fr.free.nrw.commons.upload.depicts.DepictsFragment;
|
||||
import fr.free.nrw.commons.upload.license.MediaLicenseFragment;
|
||||
|
|
@ -155,4 +157,10 @@ public abstract class FragmentBuilderModule {
|
|||
|
||||
@ContributesAndroidInjector
|
||||
abstract LeaderboardFragment bindLeaderboardFragment();
|
||||
|
||||
@ContributesAndroidInjector
|
||||
abstract PendingUploadsFragment bindPendingUploadsFragment();
|
||||
|
||||
@ContributesAndroidInjector
|
||||
abstract FailedUploadsFragment bindFailedUploadsFragment();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -306,7 +306,6 @@ public class AchievementsFragment extends CommonsDaggerSupportFragment {
|
|||
if (uploadCount==0){
|
||||
setZeroAchievements();
|
||||
}else {
|
||||
|
||||
binding.imagesUploadedProgressbar.setVisibility(View.VISIBLE);
|
||||
binding.imagesUploadedProgressbar.setProgress
|
||||
(100*uploadCount/levelInfo.getMaxUploadCount());
|
||||
|
|
@ -326,9 +325,9 @@ public class AchievementsFragment extends CommonsDaggerSupportFragment {
|
|||
getString(R.string.ok),
|
||||
() -> {},
|
||||
true);
|
||||
binding.imagesUploadedProgressbar.setVisibility(View.INVISIBLE);
|
||||
binding.imageRevertsProgressbar.setVisibility(View.INVISIBLE);
|
||||
binding.imagesUsedByWikiProgressBar.setVisibility(View.INVISIBLE);
|
||||
// binding.imagesUploadedProgressbar.setVisibility(View.INVISIBLE);
|
||||
// binding.imageRevertsProgressbar.setVisibility(View.INVISIBLE);
|
||||
// binding.imagesUsedByWikiProgressBar.setVisibility(View.INVISIBLE);
|
||||
binding.achievementBadgeImage.setVisibility(View.INVISIBLE);
|
||||
binding.imagesUsedByWikiText.setText(R.string.no_image);
|
||||
binding.imagesRevertedText.setText(R.string.no_image_reverted);
|
||||
|
|
@ -354,7 +353,7 @@ public class AchievementsFragment extends CommonsDaggerSupportFragment {
|
|||
* @param achievements
|
||||
*/
|
||||
private void inflateAchievements(Achievements achievements) {
|
||||
binding.imagesUsedByWikiProgressBar.setVisibility(View.VISIBLE);
|
||||
// binding.imagesUsedByWikiProgressBar.setVisibility(View.VISIBLE);
|
||||
binding.thanksReceived.setText(String.valueOf(achievements.getThanksReceived()));
|
||||
binding.imagesUsedByWikiProgressBar.setProgress
|
||||
(100 * achievements.getUniqueUsedImages() / levelInfo.getMaxUniqueImages());
|
||||
|
|
|
|||
|
|
@ -203,6 +203,16 @@ public class UploadRepository {
|
|||
return uploadModel.getImageQuality(uploadItem, location);
|
||||
}
|
||||
|
||||
/**
|
||||
* query the RemoteDataSource for image duplicity check
|
||||
*
|
||||
* @param filePath file to be checked
|
||||
* @return IMAGE_DUPLICATE or IMAGE_OK
|
||||
*/
|
||||
public Single<Integer> checkDuplicateImage(String filePath) {
|
||||
return uploadModel.checkDuplicateImage(filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* query the RemoteDataSource for caption quality
|
||||
*
|
||||
|
|
|
|||
|
|
@ -0,0 +1,139 @@
|
|||
package fr.free.nrw.commons.upload
|
||||
|
||||
import android.net.Uri
|
||||
import android.text.TextUtils
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.webkit.URLUtil
|
||||
import android.widget.ImageView
|
||||
import android.widget.ProgressBar
|
||||
import android.widget.TextView
|
||||
import androidx.paging.PagedListAdapter
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.facebook.imagepipeline.request.ImageRequest
|
||||
import fr.free.nrw.commons.R
|
||||
import fr.free.nrw.commons.contributions.Contribution
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* Adapter for displaying failed uploads in a paginated list in FailedUploadsFragment. This adapter
|
||||
* binds the data from [Contribution] objects to the item views in the RecyclerView, allowing users to view
|
||||
* details of failed uploads, retry them, or delete them.
|
||||
*
|
||||
* @param callback The callback to handle user actions such as Delete Uploads and Restart Uploads
|
||||
* on failed uploads.
|
||||
*/
|
||||
class FailedUploadsAdapter(callback: Callback) :
|
||||
PagedListAdapter<Contribution, FailedUploadsAdapter.ViewHolder>(ContributionDiffCallback()) {
|
||||
private var callback: Callback = callback
|
||||
|
||||
/**
|
||||
* Creates a new ViewHolder instance. Inflates the layout for each item in the list.
|
||||
*/
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val view: View =
|
||||
LayoutInflater.from(parent.context).inflate(R.layout.item_failed_upload, parent, false)
|
||||
return ViewHolder(view)
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds data to the provided ViewHolder. Sets up the item view with data from the
|
||||
* contribution at the specified position.
|
||||
*/
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
val item: Contribution? = getItem(position)
|
||||
if (item != null) {
|
||||
holder.titleTextView.setText(item.media.displayTitle)
|
||||
}
|
||||
var imageRequest: ImageRequest? = null
|
||||
val imageSource: String = item?.localUri.toString()
|
||||
|
||||
if (!TextUtils.isEmpty(imageSource)) {
|
||||
if (URLUtil.isFileUrl(imageSource)) {
|
||||
imageRequest = ImageRequest.fromUri(Uri.parse(imageSource))!!
|
||||
} else if (imageSource != null) {
|
||||
val file = File(imageSource)
|
||||
imageRequest = ImageRequest.fromFile(file)!!
|
||||
}
|
||||
|
||||
if (imageRequest != null) {
|
||||
holder.itemImage.setImageRequest(imageRequest)
|
||||
}
|
||||
}
|
||||
|
||||
if (item != null) {
|
||||
if (item.state == Contribution.STATE_FAILED) {
|
||||
if (item.errorInfo != null) {
|
||||
holder.errorTextView.setText(item.errorInfo)
|
||||
} else {
|
||||
holder.errorTextView.setText("Failed")
|
||||
}
|
||||
holder.errorTextView.visibility = View.VISIBLE
|
||||
holder.itemProgress.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
holder.deleteButton.setOnClickListener {
|
||||
callback.deleteUpload(item)
|
||||
}
|
||||
holder.retryButton.setOnClickListener {
|
||||
callback.restartUpload(position)
|
||||
}
|
||||
holder.itemImage.setImageRequest(imageRequest)
|
||||
}
|
||||
|
||||
/**
|
||||
* ViewHolder for the failed upload item. Holds references to the views for each item.
|
||||
*/
|
||||
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
var itemImage: com.facebook.drawee.view.SimpleDraweeView =
|
||||
itemView.findViewById(R.id.itemImage)
|
||||
var titleTextView: TextView = itemView.findViewById<TextView>(R.id.titleTextView)
|
||||
var itemProgress: ProgressBar = itemView.findViewById<ProgressBar>(R.id.itemProgress)
|
||||
var errorTextView: TextView = itemView.findViewById<TextView>(R.id.errorTextView)
|
||||
var deleteButton: ImageView = itemView.findViewById(R.id.deleteButton)
|
||||
var retryButton: ImageView = itemView.findViewById(R.id.retryButton)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ID of the item at the specified position. Uses the pageId of the contribution
|
||||
* for unique identification.
|
||||
*/
|
||||
override fun getItemId(position: Int): Long {
|
||||
return getItem(position)?.pageId?.hashCode()?.toLong() ?: position.toLong()
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses DiffUtil to calculate the changes in the list
|
||||
* It has methods that check pageId and the content of the items to determine if its a new item
|
||||
*/
|
||||
class ContributionDiffCallback : DiffUtil.ItemCallback<Contribution>() {
|
||||
override fun areItemsTheSame(oldItem: Contribution, newItem: Contribution): Boolean {
|
||||
return oldItem.pageId.hashCode() == newItem.pageId.hashCode()
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(oldItem: Contribution, newItem: Contribution): Boolean {
|
||||
return oldItem.transferred == newItem.transferred
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback interface for handling actions related to failed uploads.
|
||||
*/
|
||||
interface Callback {
|
||||
/**
|
||||
* Deletes the failed upload item.
|
||||
*
|
||||
* @param contribution to be deleted.
|
||||
*/
|
||||
fun deleteUpload(contribution: Contribution?)
|
||||
|
||||
/**
|
||||
* Restarts the upload for the item at the specified index.
|
||||
*
|
||||
* @param index The position of the item in the list.
|
||||
*/
|
||||
fun restartUpload(index: Int)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,201 @@
|
|||
package fr.free.nrw.commons.upload
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.paging.PagedList
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import fr.free.nrw.commons.CommonsApplication
|
||||
import fr.free.nrw.commons.R
|
||||
import fr.free.nrw.commons.auth.SessionManager
|
||||
import fr.free.nrw.commons.contributions.Contribution
|
||||
import fr.free.nrw.commons.databinding.FragmentFailedUploadsBinding
|
||||
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment
|
||||
import fr.free.nrw.commons.media.MediaClient
|
||||
import fr.free.nrw.commons.profile.ProfileActivity
|
||||
import fr.free.nrw.commons.utils.DialogUtil
|
||||
import fr.free.nrw.commons.utils.ViewUtil
|
||||
import org.apache.commons.lang3.StringUtils
|
||||
import java.util.Locale
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* Fragment for displaying a list of failed uploads in Upload Progress Activity. This fragment provides
|
||||
* functionality for the user to retry or cancel failed uploads.
|
||||
*/
|
||||
class FailedUploadsFragment : CommonsDaggerSupportFragment(), PendingUploadsContract.View,
|
||||
FailedUploadsAdapter.Callback {
|
||||
|
||||
@Inject
|
||||
lateinit var pendingUploadsPresenter: PendingUploadsPresenter
|
||||
|
||||
@Inject
|
||||
lateinit var mediaClient: MediaClient
|
||||
|
||||
@Inject
|
||||
lateinit var sessionManager: SessionManager
|
||||
|
||||
private var userName: String? = null
|
||||
|
||||
lateinit var binding: FragmentFailedUploadsBinding
|
||||
|
||||
private lateinit var adapter: FailedUploadsAdapter
|
||||
|
||||
var contributionsList = ArrayList<Contribution>()
|
||||
|
||||
private lateinit var uploadProgressActivity: UploadProgressActivity
|
||||
|
||||
override fun onAttach(context: Context) {
|
||||
super.onAttach(context)
|
||||
if (context is UploadProgressActivity) {
|
||||
uploadProgressActivity = context
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
//Now that we are allowing this fragment to be started for
|
||||
// any userName- we expect it to be passed as an argument
|
||||
if (arguments != null) {
|
||||
userName = requireArguments().getString(ProfileActivity.KEY_USERNAME)
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(userName)) {
|
||||
userName = sessionManager!!.getUserName()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater, container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
binding = FragmentFailedUploadsBinding.inflate(layoutInflater)
|
||||
pendingUploadsPresenter.onAttachView(this)
|
||||
initAdapter()
|
||||
return binding.root
|
||||
}
|
||||
|
||||
fun initAdapter() {
|
||||
adapter = FailedUploadsAdapter(this)
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
initRecyclerView()
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the recycler view.
|
||||
*/
|
||||
fun initRecyclerView() {
|
||||
binding.failedUploadsRecyclerView.setLayoutManager(LinearLayoutManager(this.context))
|
||||
binding.failedUploadsRecyclerView.adapter = adapter
|
||||
pendingUploadsPresenter!!.getFailedContributions()
|
||||
pendingUploadsPresenter!!.failedContributionList.observe(
|
||||
viewLifecycleOwner
|
||||
) { list: PagedList<Contribution?> ->
|
||||
adapter.submitList(list)
|
||||
contributionsList = ArrayList()
|
||||
list.forEach {
|
||||
if (it != null) {
|
||||
contributionsList.add(it)
|
||||
}
|
||||
}
|
||||
if (list.size == 0) {
|
||||
uploadProgressActivity.setErrorIconsVisibility(false)
|
||||
binding.nofailedTextView.visibility = View.VISIBLE
|
||||
binding.failedUplaodsLl.visibility = View.GONE
|
||||
} else {
|
||||
uploadProgressActivity.setErrorIconsVisibility(true)
|
||||
binding.nofailedTextView.visibility = View.GONE
|
||||
binding.failedUplaodsLl.visibility = View.VISIBLE
|
||||
binding.failedUploadsRecyclerView.setAdapter(adapter)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restarts all the failed uploads.
|
||||
*/
|
||||
fun restartUploads() {
|
||||
if (contributionsList != null) {
|
||||
pendingUploadsPresenter.restartUploads(
|
||||
contributionsList,
|
||||
0,
|
||||
this.requireContext().applicationContext
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restarts a specific upload.
|
||||
*/
|
||||
override fun restartUpload(index: Int) {
|
||||
if (contributionsList != null) {
|
||||
pendingUploadsPresenter.restartUpload(
|
||||
contributionsList,
|
||||
index,
|
||||
this.requireContext().applicationContext
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a specific upload after getting a confirmation from the user using Dialog.
|
||||
*/
|
||||
override fun deleteUpload(contribution: Contribution?) {
|
||||
DialogUtil.showAlertDialog(
|
||||
requireActivity(),
|
||||
String.format(
|
||||
Locale.getDefault(),
|
||||
requireActivity().getString(R.string.cancelling_upload)
|
||||
),
|
||||
String.format(
|
||||
Locale.getDefault(),
|
||||
requireActivity().getString(R.string.cancel_upload_dialog)
|
||||
),
|
||||
String.format(Locale.getDefault(), requireActivity().getString(R.string.yes)),
|
||||
String.format(Locale.getDefault(), requireActivity().getString(R.string.no)),
|
||||
{
|
||||
ViewUtil.showShortToast(context, R.string.cancelling_upload)
|
||||
pendingUploadsPresenter.deleteUpload(
|
||||
contribution,
|
||||
this.requireContext().applicationContext
|
||||
)
|
||||
},
|
||||
{}
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all the uploads after getting a confirmation from the user using Dialog.
|
||||
*/
|
||||
fun deleteUploads() {
|
||||
if (contributionsList != null) {
|
||||
DialogUtil.showAlertDialog(
|
||||
requireActivity(),
|
||||
String.format(
|
||||
Locale.getDefault(),
|
||||
requireActivity().getString(R.string.cancelling_all_the_uploads)
|
||||
),
|
||||
String.format(
|
||||
Locale.getDefault(),
|
||||
requireActivity().getString(R.string.are_you_sure_that_you_want_cancel_all_the_uploads)
|
||||
),
|
||||
String.format(Locale.getDefault(), requireActivity().getString(R.string.yes)),
|
||||
String.format(Locale.getDefault(), requireActivity().getString(R.string.no)),
|
||||
{
|
||||
ViewUtil.showShortToast(context, R.string.cancelling_upload)
|
||||
uploadProgressActivity.hidePendingIcons()
|
||||
pendingUploadsPresenter.deleteUploads(
|
||||
listOf(Contribution.STATE_FAILED)
|
||||
)
|
||||
},
|
||||
{}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -140,7 +140,7 @@ public class ImageProcessingService {
|
|||
* @param filePath file to be checked
|
||||
* @return IMAGE_DUPLICATE or IMAGE_OK
|
||||
*/
|
||||
private Single<Integer> checkDuplicateImage(String filePath) {
|
||||
Single<Integer> checkDuplicateImage(String filePath) {
|
||||
Timber.d("Checking for duplicate image %s", filePath);
|
||||
return Single.fromCallable(() -> fileUtilsWrapper.getFileInputStream(filePath))
|
||||
.map(fileUtilsWrapper::getSHA1)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,229 @@
|
|||
package fr.free.nrw.commons.upload
|
||||
|
||||
import android.net.Uri
|
||||
import android.text.TextUtils
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.webkit.URLUtil
|
||||
import android.widget.ImageView
|
||||
import android.widget.ProgressBar
|
||||
import android.widget.TextView
|
||||
import androidx.paging.PagedListAdapter
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.facebook.imagepipeline.request.ImageRequest
|
||||
import fr.free.nrw.commons.R
|
||||
import fr.free.nrw.commons.contributions.Contribution
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* Adapter for displaying pending uploads in a paginated list in PendingUploadsFragment. This adapter
|
||||
* binds data from [Contribution] objects to the item views in the RecyclerView, allowing users to
|
||||
* view details of pending uploads and perform actions such as deleting them.
|
||||
*
|
||||
* @param callback The callback to handle user actions such as Delete Uploads on pending uploads.
|
||||
*/
|
||||
class PendingUploadsAdapter(private val callback: Callback) :
|
||||
PagedListAdapter<Contribution, PendingUploadsAdapter.ViewHolder>(ContributionDiffCallback()) {
|
||||
|
||||
/**
|
||||
* Creates a new ViewHolder instance. Inflates the layout for each item in the list.
|
||||
*/
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val view: View = LayoutInflater.from(parent.context)
|
||||
.inflate(R.layout.item_pending_upload, parent, false)
|
||||
return ViewHolder(view)
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds data to the provided ViewHolder. Sets up the item view with data from the
|
||||
* contribution at the specified position utilizing payloads.
|
||||
*/
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: MutableList<Any>) {
|
||||
if (payloads.isNotEmpty()) {
|
||||
when (val latestPayload = payloads.lastOrNull()) {
|
||||
is ContributionChangePayload.Progress -> holder.bindProgress(
|
||||
latestPayload.transferred,
|
||||
latestPayload.total,
|
||||
getItem(position)!!.state
|
||||
)
|
||||
|
||||
is ContributionChangePayload.State -> holder.bindState(latestPayload.state)
|
||||
else -> onBindViewHolder(holder, position)
|
||||
}
|
||||
} else {
|
||||
onBindViewHolder(holder, position)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds data to the provided ViewHolder. Sets up the item view with data from the
|
||||
* contribution at the specified position.
|
||||
*/
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
val contribution = getItem(position)
|
||||
contribution?.let {
|
||||
holder.bind(it)
|
||||
holder.deleteButton.setOnClickListener {
|
||||
callback.deleteUpload(contribution)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ViewHolder class for holding and binding item views.
|
||||
*/
|
||||
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
var itemImage: com.facebook.drawee.view.SimpleDraweeView =
|
||||
itemView.findViewById(R.id.itemImage)
|
||||
var titleTextView: TextView = itemView.findViewById(R.id.titleTextView)
|
||||
var itemProgress: ProgressBar = itemView.findViewById(R.id.itemProgress)
|
||||
var errorTextView: TextView = itemView.findViewById(R.id.errorTextView)
|
||||
var deleteButton: ImageView = itemView.findViewById(R.id.deleteButton)
|
||||
|
||||
fun bind(contribution: Contribution) {
|
||||
titleTextView.text = contribution.media.displayTitle
|
||||
|
||||
val imageSource: String = contribution.localUri.toString()
|
||||
var imageRequest: ImageRequest? = null
|
||||
|
||||
if (!TextUtils.isEmpty(imageSource)) {
|
||||
if (URLUtil.isFileUrl(imageSource)) {
|
||||
imageRequest = ImageRequest.fromUri(Uri.parse(imageSource))
|
||||
} else {
|
||||
val file = File(imageSource)
|
||||
imageRequest = ImageRequest.fromFile(file)
|
||||
}
|
||||
}
|
||||
|
||||
if (imageRequest != null) {
|
||||
itemImage.setImageRequest(imageRequest)
|
||||
}
|
||||
|
||||
bindState(contribution.state)
|
||||
bindProgress(contribution.transferred, contribution.dataLength, contribution.state)
|
||||
}
|
||||
|
||||
fun bindState(state: Int) {
|
||||
if (state == Contribution.STATE_QUEUED || state == Contribution.STATE_PAUSED) {
|
||||
errorTextView.text = "Queued"
|
||||
errorTextView.visibility = View.VISIBLE
|
||||
itemProgress.visibility = View.GONE
|
||||
} else {
|
||||
errorTextView.visibility = View.GONE
|
||||
itemProgress.visibility = View.VISIBLE
|
||||
}
|
||||
}
|
||||
|
||||
fun bindProgress(transferred: Long, total: Long, state: Int) {
|
||||
if (transferred == 0L) {
|
||||
errorTextView.text = "Queued"
|
||||
errorTextView.visibility = View.VISIBLE
|
||||
itemProgress.visibility = View.GONE
|
||||
} else {
|
||||
if (state == Contribution.STATE_QUEUED || state == Contribution.STATE_PAUSED) {
|
||||
errorTextView.text = "Queued"
|
||||
errorTextView.visibility = View.VISIBLE
|
||||
itemProgress.visibility = View.GONE
|
||||
} else {
|
||||
errorTextView.visibility = View.GONE
|
||||
itemProgress.visibility = View.VISIBLE
|
||||
if (transferred >= total) {
|
||||
itemProgress.isIndeterminate = true
|
||||
} else {
|
||||
itemProgress.isIndeterminate = false
|
||||
itemProgress.progress =
|
||||
((transferred.toDouble() / total.toDouble()) * 100).toInt()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback interface for handling actions related to failed uploads.
|
||||
*/
|
||||
interface Callback {
|
||||
/**
|
||||
* Deletes the failed upload item.
|
||||
*
|
||||
* @param contribution to be deleted.
|
||||
*/
|
||||
fun deleteUpload(contribution: Contribution?)
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses DiffUtil and payloads to calculate the changes in the list
|
||||
* It has methods that check pageId and the content of the items to determine if its a new item
|
||||
*/
|
||||
class ContributionDiffCallback : DiffUtil.ItemCallback<Contribution>() {
|
||||
/**
|
||||
* Checks if two items represent the same contribution.
|
||||
* @param oldItem The old contribution item.
|
||||
* @param newItem The new contribution item.
|
||||
* @return True if the items are the same, false otherwise.
|
||||
*/
|
||||
override fun areItemsTheSame(oldItem: Contribution, newItem: Contribution): Boolean {
|
||||
return oldItem.pageId.hashCode() == newItem.pageId.hashCode()
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the content of two items is the same.
|
||||
* @param oldItem The old contribution item.
|
||||
* @param newItem The new contribution item.
|
||||
* @return True if the contents are the same, false otherwise.
|
||||
*/
|
||||
override fun areContentsTheSame(oldItem: Contribution, newItem: Contribution): Boolean {
|
||||
return oldItem.transferred == newItem.transferred
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a payload representing the change between the old and new items.
|
||||
* @param oldItem The old contribution item.
|
||||
* @param newItem The new contribution item.
|
||||
* @return An object representing the change, or null if there are no changes.
|
||||
*/
|
||||
override fun getChangePayload(oldItem: Contribution, newItem: Contribution): Any? {
|
||||
return when {
|
||||
oldItem.transferred != newItem.transferred -> {
|
||||
ContributionChangePayload.Progress(newItem.transferred, newItem.dataLength)
|
||||
}
|
||||
|
||||
oldItem.state != newItem.state -> {
|
||||
ContributionChangePayload.State(newItem.state)
|
||||
}
|
||||
|
||||
else -> super.getChangePayload(oldItem, newItem)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the unique item ID for the contribution at the specified position.
|
||||
* @param position The position of the item.
|
||||
* @return The unique item ID.
|
||||
*/
|
||||
override fun getItemId(position: Int): Long {
|
||||
return getItem(position)?.pageId?.hashCode()?.toLong() ?: position.toLong()
|
||||
}
|
||||
|
||||
/**
|
||||
* Sealed interface representing different types of changes to a contribution.
|
||||
*/
|
||||
private sealed interface ContributionChangePayload {
|
||||
/**
|
||||
* Represents a change in the progress of a contribution.
|
||||
* @param transferred The amount of data transferred.
|
||||
* @param total The total amount of data.
|
||||
*/
|
||||
data class Progress(val transferred: Long, val total: Long) : ContributionChangePayload
|
||||
|
||||
/**
|
||||
* Represents a change in the state of a contribution.
|
||||
* @param state The state of the contribution.
|
||||
*/
|
||||
data class State(val state: Int) : ContributionChangePayload
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
package fr.free.nrw.commons.upload;
|
||||
|
||||
import android.content.Context;
|
||||
import fr.free.nrw.commons.BasePresenter;
|
||||
import fr.free.nrw.commons.contributions.Contribution;
|
||||
import fr.free.nrw.commons.nearby.contract.NearbyParentFragmentContract;
|
||||
import fr.free.nrw.commons.nearby.contract.NearbyParentFragmentContract.View;
|
||||
|
||||
/**
|
||||
* The contract using which the PendingUploadsFragment or FailedUploadsFragment would communicate
|
||||
* with its PendingUploadsPresenter
|
||||
*/
|
||||
public class PendingUploadsContract {
|
||||
|
||||
/**
|
||||
* Interface representing the view for uploads.
|
||||
*/
|
||||
public interface View { }
|
||||
|
||||
/**
|
||||
* Interface representing the user actions related to uploads.
|
||||
*/
|
||||
public interface UserActionListener extends
|
||||
BasePresenter<fr.free.nrw.commons.upload.PendingUploadsContract.View> {
|
||||
|
||||
/**
|
||||
* Deletes a upload.
|
||||
*/
|
||||
void deleteUpload(Contribution contribution, Context context);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,200 @@
|
|||
package fr.free.nrw.commons.upload
|
||||
|
||||
import android.content.Context
|
||||
import android.os.AsyncTask
|
||||
import android.os.Build.VERSION
|
||||
import android.os.Build.VERSION_CODES
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.paging.PagedList
|
||||
import androidx.paging.PositionalDataSource
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver
|
||||
import fr.free.nrw.commons.CommonsApplication
|
||||
import fr.free.nrw.commons.R
|
||||
import fr.free.nrw.commons.auth.SessionManager
|
||||
import fr.free.nrw.commons.contributions.Contribution
|
||||
import fr.free.nrw.commons.databinding.FragmentPendingUploadsBinding
|
||||
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment
|
||||
import fr.free.nrw.commons.media.MediaClient
|
||||
import fr.free.nrw.commons.profile.ProfileActivity
|
||||
import fr.free.nrw.commons.utils.DialogUtil.showAlertDialog
|
||||
import fr.free.nrw.commons.utils.ViewUtil
|
||||
import org.apache.commons.lang3.StringUtils
|
||||
import timber.log.Timber
|
||||
import java.util.Locale
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* Fragment for showing pending uploads in Upload Progress Activity. This fragment provides
|
||||
* functionality for the user to pause uploads.
|
||||
*/
|
||||
class PendingUploadsFragment : CommonsDaggerSupportFragment(), PendingUploadsContract.View,
|
||||
PendingUploadsAdapter.Callback {
|
||||
|
||||
@Inject
|
||||
lateinit var pendingUploadsPresenter: PendingUploadsPresenter
|
||||
|
||||
private lateinit var binding: FragmentPendingUploadsBinding
|
||||
|
||||
private lateinit var uploadProgressActivity: UploadProgressActivity
|
||||
|
||||
private lateinit var adapter: PendingUploadsAdapter
|
||||
|
||||
private var contributionsSize = 0
|
||||
var contributionsList = ArrayList<Contribution>()
|
||||
|
||||
override fun onAttach(context: Context) {
|
||||
super.onAttach(context)
|
||||
if (context is UploadProgressActivity) {
|
||||
uploadProgressActivity = context
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater, container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = FragmentPendingUploadsBinding.inflate(inflater, container, false)
|
||||
pendingUploadsPresenter.onAttachView(this)
|
||||
initAdapter()
|
||||
return binding.root
|
||||
}
|
||||
|
||||
fun initAdapter() {
|
||||
adapter = PendingUploadsAdapter(this)
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
initRecyclerView()
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the recycler view.
|
||||
*/
|
||||
fun initRecyclerView() {
|
||||
binding.pendingUploadsRecyclerView.setLayoutManager(LinearLayoutManager(this.context))
|
||||
binding.pendingUploadsRecyclerView.adapter = adapter
|
||||
pendingUploadsPresenter!!.setup()
|
||||
pendingUploadsPresenter!!.totalContributionList.observe(
|
||||
viewLifecycleOwner
|
||||
) { list: PagedList<Contribution?> ->
|
||||
contributionsSize = list.size
|
||||
contributionsList = ArrayList()
|
||||
var pausedOrQueuedUploads = 0
|
||||
list.forEach {
|
||||
if (it != null) {
|
||||
if (it.state == Contribution.STATE_PAUSED
|
||||
|| it.state == Contribution.STATE_QUEUED
|
||||
|| it.state == Contribution.STATE_IN_PROGRESS
|
||||
) {
|
||||
contributionsList.add(it)
|
||||
}
|
||||
if (it.state == Contribution.STATE_PAUSED
|
||||
|| it.state == Contribution.STATE_QUEUED
|
||||
) {
|
||||
pausedOrQueuedUploads++
|
||||
}
|
||||
}
|
||||
}
|
||||
if (contributionsSize == 0) {
|
||||
binding.nopendingTextView.visibility = View.VISIBLE
|
||||
binding.pendingUplaodsLl.visibility = View.GONE
|
||||
uploadProgressActivity.hidePendingIcons()
|
||||
} else {
|
||||
binding.nopendingTextView.visibility = View.GONE
|
||||
binding.pendingUplaodsLl.visibility = View.VISIBLE
|
||||
adapter.submitList(list)
|
||||
binding.progressTextView.setText(contributionsSize.toString() + " uploads left")
|
||||
if ((pausedOrQueuedUploads == contributionsSize) || CommonsApplication.isPaused) {
|
||||
uploadProgressActivity.setPausedIcon(true)
|
||||
} else {
|
||||
uploadProgressActivity.setPausedIcon(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels a specific upload after getting a confirmation from the user using Dialog.
|
||||
*/
|
||||
override fun deleteUpload(contribution: Contribution?) {
|
||||
showAlertDialog(
|
||||
requireActivity(),
|
||||
String.format(
|
||||
Locale.getDefault(),
|
||||
requireActivity().getString(R.string.cancelling_upload)
|
||||
),
|
||||
String.format(
|
||||
Locale.getDefault(),
|
||||
requireActivity().getString(R.string.cancel_upload_dialog)
|
||||
),
|
||||
String.format(Locale.getDefault(), requireActivity().getString(R.string.yes)),
|
||||
String.format(Locale.getDefault(), requireActivity().getString(R.string.no)),
|
||||
{
|
||||
ViewUtil.showShortToast(context, R.string.cancelling_upload)
|
||||
pendingUploadsPresenter.deleteUpload(
|
||||
contribution,
|
||||
this.requireContext().applicationContext
|
||||
)
|
||||
},
|
||||
{}
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Restarts all the paused uploads.
|
||||
*/
|
||||
fun restartUploads() {
|
||||
if (contributionsList != null) {
|
||||
pendingUploadsPresenter.restartUploads(
|
||||
contributionsList,
|
||||
0,
|
||||
this.requireContext().applicationContext
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pauses all the ongoing uploads.
|
||||
*/
|
||||
fun pauseUploads() {
|
||||
pendingUploadsPresenter.pauseUploads()
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels all the uploads after getting a confirmation from the user using Dialog.
|
||||
*/
|
||||
fun deleteUploads() {
|
||||
showAlertDialog(
|
||||
requireActivity(),
|
||||
String.format(
|
||||
Locale.getDefault(),
|
||||
requireActivity().getString(R.string.cancelling_all_the_uploads)
|
||||
),
|
||||
String.format(
|
||||
Locale.getDefault(),
|
||||
requireActivity().getString(R.string.are_you_sure_that_you_want_cancel_all_the_uploads)
|
||||
),
|
||||
String.format(Locale.getDefault(), requireActivity().getString(R.string.yes)),
|
||||
String.format(Locale.getDefault(), requireActivity().getString(R.string.no)),
|
||||
{
|
||||
ViewUtil.showShortToast(context, R.string.cancelling_upload)
|
||||
uploadProgressActivity.hidePendingIcons()
|
||||
pendingUploadsPresenter.deleteUploads(
|
||||
listOf(
|
||||
Contribution.STATE_QUEUED,
|
||||
Contribution.STATE_IN_PROGRESS,
|
||||
Contribution.STATE_PAUSED
|
||||
)
|
||||
)
|
||||
},
|
||||
{}
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,262 @@
|
|||
package fr.free.nrw.commons.upload;
|
||||
|
||||
|
||||
import static fr.free.nrw.commons.utils.ImageUtils.IMAGE_OK;
|
||||
|
||||
import android.content.Context;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.paging.DataSource.Factory;
|
||||
import androidx.paging.LivePagedListBuilder;
|
||||
import androidx.paging.PagedList;
|
||||
import androidx.work.ExistingWorkPolicy;
|
||||
import fr.free.nrw.commons.CommonsApplication;
|
||||
import fr.free.nrw.commons.contributions.Contribution;
|
||||
import fr.free.nrw.commons.contributions.ContributionBoundaryCallback;
|
||||
import fr.free.nrw.commons.contributions.ContributionsRemoteDataSource;
|
||||
import fr.free.nrw.commons.contributions.ContributionsRepository;
|
||||
import fr.free.nrw.commons.di.CommonsApplicationModule;
|
||||
import fr.free.nrw.commons.repository.UploadRepository;
|
||||
import fr.free.nrw.commons.upload.PendingUploadsContract.UserActionListener;
|
||||
import fr.free.nrw.commons.upload.PendingUploadsContract.View;
|
||||
import fr.free.nrw.commons.upload.worker.WorkRequestHelper;
|
||||
import io.reactivex.Scheduler;
|
||||
import io.reactivex.disposables.CompositeDisposable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import timber.log.Timber;
|
||||
|
||||
/**
|
||||
* The presenter class for PendingUploadsFragment and FailedUploadsFragment
|
||||
*/
|
||||
public class PendingUploadsPresenter implements UserActionListener {
|
||||
|
||||
private final ContributionBoundaryCallback contributionBoundaryCallback;
|
||||
private final ContributionsRepository contributionsRepository;
|
||||
private final UploadRepository uploadRepository;
|
||||
private final Scheduler ioThreadScheduler;
|
||||
|
||||
private final CompositeDisposable compositeDisposable;
|
||||
private final ContributionsRemoteDataSource contributionsRemoteDataSource;
|
||||
|
||||
LiveData<PagedList<Contribution>> totalContributionList;
|
||||
LiveData<PagedList<Contribution>> failedContributionList;
|
||||
|
||||
@Inject
|
||||
PendingUploadsPresenter(
|
||||
final ContributionBoundaryCallback contributionBoundaryCallback,
|
||||
final ContributionsRemoteDataSource contributionsRemoteDataSource,
|
||||
final ContributionsRepository contributionsRepository,
|
||||
final UploadRepository uploadRepository,
|
||||
@Named(CommonsApplicationModule.IO_THREAD) final Scheduler ioThreadScheduler) {
|
||||
this.contributionBoundaryCallback = contributionBoundaryCallback;
|
||||
this.contributionsRepository = contributionsRepository;
|
||||
this.uploadRepository = uploadRepository;
|
||||
this.ioThreadScheduler = ioThreadScheduler;
|
||||
this.contributionsRemoteDataSource = contributionsRemoteDataSource;
|
||||
compositeDisposable = new CompositeDisposable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Setups the paged list of Pending Uploads. This method sets the configuration for paged list
|
||||
* and ties it up with the live data object. This method can be tweaked to update the lazy
|
||||
* loading behavior of the contributions list
|
||||
*/
|
||||
void setup() {
|
||||
final PagedList.Config pagedListConfig =
|
||||
(new PagedList.Config.Builder())
|
||||
.setPrefetchDistance(50)
|
||||
.setPageSize(10).build();
|
||||
Factory<Integer, Contribution> factory;
|
||||
|
||||
factory = contributionsRepository.fetchContributionsWithStatesSortedByDateUploadStarted(
|
||||
Arrays.asList(Contribution.STATE_QUEUED, Contribution.STATE_IN_PROGRESS,
|
||||
Contribution.STATE_PAUSED));
|
||||
LivePagedListBuilder livePagedListBuilder = new LivePagedListBuilder(factory,
|
||||
pagedListConfig);
|
||||
totalContributionList = livePagedListBuilder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Setups the paged list of Failed Uploads. This method sets the configuration for paged list
|
||||
* and ties it up with the live data object. This method can be tweaked to update the lazy
|
||||
* loading behavior of the contributions list
|
||||
*/
|
||||
void getFailedContributions() {
|
||||
final PagedList.Config pagedListConfig =
|
||||
(new PagedList.Config.Builder())
|
||||
.setPrefetchDistance(50)
|
||||
.setPageSize(10).build();
|
||||
Factory<Integer, Contribution> factory;
|
||||
factory = contributionsRepository.fetchContributionsWithStatesSortedByDateUploadStarted(
|
||||
Collections.singletonList(Contribution.STATE_FAILED));
|
||||
LivePagedListBuilder livePagedListBuilder = new LivePagedListBuilder(factory,
|
||||
pagedListConfig);
|
||||
failedContributionList = livePagedListBuilder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttachView(@NonNull View view) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDetachView() {
|
||||
compositeDisposable.clear();
|
||||
contributionsRemoteDataSource.dispose();
|
||||
contributionBoundaryCallback.dispose();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the specified upload (contribution) from the database.
|
||||
*
|
||||
* @param contribution The contribution object representing the upload to be deleted.
|
||||
* @param context The context in which the operation is being performed.
|
||||
*/
|
||||
@Override
|
||||
public void deleteUpload(final Contribution contribution, Context context) {
|
||||
compositeDisposable.add(contributionsRepository
|
||||
.deleteContributionFromDB(contribution)
|
||||
.subscribeOn(ioThreadScheduler)
|
||||
.subscribe());
|
||||
}
|
||||
|
||||
/**
|
||||
* Pauses all the uploads by changing the state of contributions from STATE_QUEUED and
|
||||
* STATE_IN_PROGRESS to STATE_PAUSED in the database.
|
||||
*/
|
||||
public void pauseUploads() {
|
||||
CommonsApplication.isPaused = true;
|
||||
compositeDisposable.add(contributionsRepository
|
||||
.updateContributionsWithStates(
|
||||
List.of(Contribution.STATE_QUEUED, Contribution.STATE_IN_PROGRESS),
|
||||
Contribution.STATE_PAUSED)
|
||||
.subscribeOn(ioThreadScheduler)
|
||||
.subscribe());
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes contributions from the database that match the specified states.
|
||||
*
|
||||
* @param states A list of integers representing the states of the contributions to be deleted.
|
||||
*/
|
||||
public void deleteUploads(List<Integer> states) {
|
||||
compositeDisposable.add(contributionsRepository
|
||||
.deleteContributionsFromDBWithStates(states)
|
||||
.subscribeOn(ioThreadScheduler)
|
||||
.subscribe());
|
||||
}
|
||||
|
||||
/**
|
||||
* Restarts the uploads for the specified list of contributions starting from the given index.
|
||||
*
|
||||
* @param contributionList The list of contributions to be restarted.
|
||||
* @param index The starting index in the list from which to restart uploads.
|
||||
* @param context The context in which the operation is being performed.
|
||||
*/
|
||||
public void restartUploads(List<Contribution> contributionList, int index, Context context) {
|
||||
CommonsApplication.isPaused = false;
|
||||
if (index >= contributionList.size()) {
|
||||
return;
|
||||
}
|
||||
Contribution it = contributionList.get(index);
|
||||
if (it.getState() == Contribution.STATE_FAILED) {
|
||||
it.setDateUploadStarted(Calendar.getInstance().getTime());
|
||||
if (it.getErrorInfo() == null) {
|
||||
it.setChunkInfo(null);
|
||||
it.setTransferred(0);
|
||||
}
|
||||
compositeDisposable.add(uploadRepository
|
||||
.checkDuplicateImage(it.getLocalUriPath().getPath())
|
||||
.subscribeOn(ioThreadScheduler)
|
||||
.subscribe(imageCheckResult -> {
|
||||
if (imageCheckResult == IMAGE_OK) {
|
||||
it.setState(Contribution.STATE_QUEUED);
|
||||
compositeDisposable.add(contributionsRepository
|
||||
.save(it)
|
||||
.subscribeOn(ioThreadScheduler)
|
||||
.doOnComplete(() -> {
|
||||
restartUploads(contributionList, index + 1, context);
|
||||
})
|
||||
.subscribe(() -> WorkRequestHelper.Companion.makeOneTimeWorkRequest(
|
||||
context, ExistingWorkPolicy.KEEP)));
|
||||
} else {
|
||||
Timber.e("Contribution already exists");
|
||||
compositeDisposable.add(contributionsRepository
|
||||
.deleteContributionFromDB(it)
|
||||
.subscribeOn(ioThreadScheduler).doOnComplete(() -> {
|
||||
restartUploads(contributionList, index + 1, context);
|
||||
})
|
||||
.subscribe());
|
||||
}
|
||||
}, throwable -> {
|
||||
Timber.e(throwable);
|
||||
restartUploads(contributionList, index + 1, context);
|
||||
}));
|
||||
} else {
|
||||
it.setState(Contribution.STATE_QUEUED);
|
||||
compositeDisposable.add(contributionsRepository
|
||||
.save(it)
|
||||
.subscribeOn(ioThreadScheduler)
|
||||
.doOnComplete(() -> {
|
||||
restartUploads(contributionList, index + 1, context);
|
||||
})
|
||||
.subscribe(() -> WorkRequestHelper.Companion.makeOneTimeWorkRequest(
|
||||
context, ExistingWorkPolicy.KEEP)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restarts the upload for the specified list of contributions for the given index.
|
||||
*
|
||||
* @param contributionList The list of contributions.
|
||||
* @param index The index in the list which to be restarted.
|
||||
* @param context The context in which the operation is being performed.
|
||||
*/
|
||||
public void restartUpload(List<Contribution> contributionList, int index, Context context) {
|
||||
CommonsApplication.isPaused = false;
|
||||
if (index >= contributionList.size()) {
|
||||
return;
|
||||
}
|
||||
Contribution it = contributionList.get(index);
|
||||
if (it.getState() == Contribution.STATE_FAILED) {
|
||||
it.setDateUploadStarted(Calendar.getInstance().getTime());
|
||||
if (it.getErrorInfo() == null) {
|
||||
it.setChunkInfo(null);
|
||||
it.setTransferred(0);
|
||||
}
|
||||
compositeDisposable.add(uploadRepository
|
||||
.checkDuplicateImage(it.getLocalUriPath().getPath())
|
||||
.subscribeOn(ioThreadScheduler)
|
||||
.subscribe(imageCheckResult -> {
|
||||
if (imageCheckResult == IMAGE_OK) {
|
||||
it.setState(Contribution.STATE_QUEUED);
|
||||
compositeDisposable.add(contributionsRepository
|
||||
.save(it)
|
||||
.subscribeOn(ioThreadScheduler)
|
||||
.subscribe(() -> WorkRequestHelper.Companion.makeOneTimeWorkRequest(
|
||||
context, ExistingWorkPolicy.KEEP)));
|
||||
} else {
|
||||
Timber.e("Contribution already exists");
|
||||
compositeDisposable.add(contributionsRepository
|
||||
.deleteContributionFromDB(it)
|
||||
.subscribeOn(ioThreadScheduler)
|
||||
.subscribe());
|
||||
}
|
||||
}));
|
||||
} else {
|
||||
it.setState(Contribution.STATE_QUEUED);
|
||||
compositeDisposable.add(contributionsRepository
|
||||
.save(it)
|
||||
.subscribeOn(ioThreadScheduler)
|
||||
.subscribe(() -> WorkRequestHelper.Companion.makeOneTimeWorkRequest(
|
||||
context, ExistingWorkPolicy.KEEP)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -9,5 +9,6 @@ data class StashUploadResult(
|
|||
enum class StashUploadState {
|
||||
SUCCESS,
|
||||
PAUSED,
|
||||
FAILED
|
||||
FAILED,
|
||||
CANCELLED
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import fr.free.nrw.commons.CommonsApplication
|
|||
import fr.free.nrw.commons.auth.csrf.CsrfTokenClient
|
||||
import fr.free.nrw.commons.contributions.ChunkInfo
|
||||
import fr.free.nrw.commons.contributions.Contribution
|
||||
import fr.free.nrw.commons.contributions.ContributionDao
|
||||
import fr.free.nrw.commons.upload.worker.UploadWorker.NotificationUpdateProgressListener
|
||||
import fr.free.nrw.commons.wikidata.mwapi.MwException
|
||||
import io.reactivex.Observable
|
||||
|
|
@ -33,7 +34,8 @@ class UploadClient @Inject constructor(
|
|||
private val csrfTokenClient: CsrfTokenClient,
|
||||
private val pageContentsCreator: PageContentsCreator,
|
||||
private val fileUtilsWrapper: FileUtilsWrapper,
|
||||
private val gson: Gson, private val timeProvider: TimeProvider
|
||||
private val gson: Gson, private val timeProvider: TimeProvider,
|
||||
private val contributionDao: ContributionDao
|
||||
) {
|
||||
private val CHUNK_SIZE = 512 * 1024 // 512 KB
|
||||
|
||||
|
|
@ -58,8 +60,6 @@ class UploadClient @Inject constructor(
|
|||
)
|
||||
}
|
||||
|
||||
contribution.unpause()
|
||||
|
||||
val file = contribution.localUriPath
|
||||
val fileChunks = fileUtilsWrapper.getFileChunks(file, CHUNK_SIZE)
|
||||
val mediaType = fileUtilsWrapper.getMimeType(file).toMediaTypeOrNull()
|
||||
|
|
@ -79,17 +79,35 @@ class UploadClient @Inject constructor(
|
|||
val errorMessage = AtomicReference<String>()
|
||||
compositeDisposable.add(
|
||||
Observable.fromIterable(fileChunks).forEach { chunkFile: File ->
|
||||
if (canProcess(contribution, failures)) {
|
||||
processChunk(
|
||||
filename, contribution, notificationUpdater, chunkFile,
|
||||
failures, chunkInfo, index, errorMessage, mediaType!!, file!!, fileChunks.size
|
||||
)
|
||||
if (canProcess(contributionDao, contribution, failures)) {
|
||||
if (contributionDao.getContribution(contribution.pageId) == null) {
|
||||
compositeDisposable.clear()
|
||||
return@forEach
|
||||
} else {
|
||||
processChunk(
|
||||
filename,
|
||||
contribution,
|
||||
notificationUpdater,
|
||||
chunkFile,
|
||||
failures,
|
||||
chunkInfo,
|
||||
index,
|
||||
errorMessage,
|
||||
mediaType!!,
|
||||
file!!,
|
||||
fileChunks.size
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
return when {
|
||||
contribution.isPaused() -> {
|
||||
contributionDao.getContribution(contribution.pageId) == null -> {
|
||||
return Observable.just(StashUploadResult(StashUploadState.CANCELLED, null, "Upload cancelled"))
|
||||
}
|
||||
contributionDao.getContribution(contribution.pageId).state == Contribution.STATE_PAUSED
|
||||
|| CommonsApplication.isPaused -> {
|
||||
Timber.d("Upload stash paused %s", contribution.pageId)
|
||||
Observable.just(StashUploadResult(StashUploadState.PAUSED, null, null))
|
||||
}
|
||||
|
|
@ -248,10 +266,15 @@ class UploadClient @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
private fun canProcess(contribution: Contribution, failures: AtomicBoolean): Boolean {
|
||||
private fun canProcess(
|
||||
contributionDao: ContributionDao,
|
||||
contribution: Contribution,
|
||||
failures: AtomicBoolean
|
||||
): Boolean {
|
||||
// As long as the contribution hasn't been paused and there are no errors,
|
||||
// we can process the current chunk.
|
||||
return !(contribution.isPaused() || failures.get())
|
||||
return !(contributionDao.getContribution(contribution.pageId).state == Contribution.STATE_PAUSED
|
||||
|| failures.get() || CommonsApplication.isPaused)
|
||||
}
|
||||
|
||||
private fun shouldSkip(
|
||||
|
|
|
|||
|
|
@ -103,6 +103,16 @@ public class UploadModel {
|
|||
return imageProcessingService.validateImage(uploadItem, inAppPictureLocation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls checkDuplicateImage() of ImageProcessingService to check if image is duplicate
|
||||
*
|
||||
* @param filePath file to be checked
|
||||
* @return IMAGE_DUPLICATE or IMAGE_OK
|
||||
*/
|
||||
public Single<Integer> checkDuplicateImage(String filePath){
|
||||
return imageProcessingService.checkDuplicateImage(filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls validateCaption() of ImageProcessingService to check caption of image
|
||||
*
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import dagger.Binds;
|
|||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import fr.free.nrw.commons.auth.csrf.CsrfTokenClient;
|
||||
import fr.free.nrw.commons.contributions.ContributionDao;
|
||||
import fr.free.nrw.commons.di.NetworkingModule;
|
||||
import fr.free.nrw.commons.upload.categories.CategoriesContract;
|
||||
import fr.free.nrw.commons.upload.categories.CategoriesPresenter;
|
||||
|
|
@ -50,8 +51,8 @@ public abstract class UploadModule {
|
|||
public static UploadClient provideUploadClient(final UploadInterface uploadInterface,
|
||||
@Named(NetworkingModule.NAMED_COMMONS_CSRF) final CsrfTokenClient csrfTokenClient,
|
||||
final PageContentsCreator pageContentsCreator, final FileUtilsWrapper fileUtilsWrapper,
|
||||
final Gson gson) {
|
||||
final Gson gson, final ContributionDao contributionDao) {
|
||||
return new UploadClient(uploadInterface, csrfTokenClient, pageContentsCreator,
|
||||
fileUtilsWrapper, gson, System::currentTimeMillis);
|
||||
fileUtilsWrapper, gson, System::currentTimeMillis, contributionDao);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,223 @@
|
|||
package fr.free.nrw.commons.upload
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.os.Bundle
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.viewpager.widget.ViewPager
|
||||
import fr.free.nrw.commons.R
|
||||
import fr.free.nrw.commons.ViewPagerAdapter
|
||||
import fr.free.nrw.commons.contributions.Contribution
|
||||
import fr.free.nrw.commons.contributions.ContributionDao
|
||||
import fr.free.nrw.commons.databinding.ActivityUploadProgressBinding
|
||||
import fr.free.nrw.commons.theme.BaseActivity
|
||||
import io.reactivex.functions.Consumer
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* Activity to manage the progress of uploads. It includes tabs to show pending and failed uploads,
|
||||
* and provides menu options to pause, resume, cancel, and retry uploads. Also, it contains ViewPager
|
||||
* which holds Pending Uploads Fragment and Failed Uploads Fragment to show list of pending and
|
||||
* failed uploads respectively.
|
||||
*/
|
||||
class UploadProgressActivity : BaseActivity() {
|
||||
|
||||
private lateinit var binding: ActivityUploadProgressBinding
|
||||
private var pendingUploadsFragment: PendingUploadsFragment? = null
|
||||
private var failedUploadsFragment: FailedUploadsFragment? = null
|
||||
var viewPagerAdapter: ViewPagerAdapter? = null
|
||||
var menu: Menu? = null
|
||||
|
||||
@Inject
|
||||
lateinit var contributionDao: ContributionDao
|
||||
|
||||
val fragmentList: MutableList<Fragment> = ArrayList()
|
||||
val titleList: MutableList<String> = ArrayList()
|
||||
var isPaused = true
|
||||
var isPendingIconsVisible = true
|
||||
var isErrorIconsVisisble = false
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = ActivityUploadProgressBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
viewPagerAdapter = ViewPagerAdapter(supportFragmentManager)
|
||||
binding.uploadProgressViewPager.setAdapter(viewPagerAdapter)
|
||||
binding.uploadProgressViewPager.setId(R.id.upload_progress_view_pager)
|
||||
binding.uploadProgressTabLayout.setupWithViewPager(binding.uploadProgressViewPager)
|
||||
binding.toolbarBinding.toolbar.title = getString(R.string.uploads)
|
||||
setSupportActionBar(binding.toolbarBinding.toolbar)
|
||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||
|
||||
binding.uploadProgressViewPager.addOnPageChangeListener(object :
|
||||
ViewPager.OnPageChangeListener {
|
||||
override fun onPageScrolled(
|
||||
position: Int, positionOffset: Float,
|
||||
positionOffsetPixels: Int
|
||||
) {
|
||||
}
|
||||
|
||||
override fun onPageSelected(position: Int) {
|
||||
updateMenuItems(position)
|
||||
if (position == 2) {
|
||||
binding.uploadProgressViewPager.setCanScroll(false)
|
||||
} else {
|
||||
binding.uploadProgressViewPager.setCanScroll(true)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPageScrollStateChanged(state: Int) {
|
||||
}
|
||||
})
|
||||
setTabs()
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes and sets up the tabs data by creating instances of `PendingUploadsFragment`
|
||||
* and `FailedUploadsFragment`, adds them to the `fragmentList`, and assigns corresponding
|
||||
* titles from resources to the `titleList`.
|
||||
*/
|
||||
fun setTabs() {
|
||||
pendingUploadsFragment = PendingUploadsFragment()
|
||||
failedUploadsFragment = FailedUploadsFragment()
|
||||
|
||||
fragmentList.add(pendingUploadsFragment!!)
|
||||
titleList.add(getString(R.string.pending))
|
||||
fragmentList.add(failedUploadsFragment!!)
|
||||
titleList.add(getString(R.string.failed))
|
||||
viewPagerAdapter!!.setTabData(fragmentList, titleList)
|
||||
viewPagerAdapter!!.notifyDataSetChanged()
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
menuInflater.inflate(R.menu.menu_uploads, menu)
|
||||
this.menu = menu
|
||||
updateMenuItems(0)
|
||||
return super.onCreateOptionsMenu(menu)
|
||||
}
|
||||
|
||||
override fun onSupportNavigateUp(): Boolean {
|
||||
onBackPressed()
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the menu items based on the current position in the view pager and the visibility
|
||||
* of icons related to pending or failed uploads. This function dynamically modifies the menu
|
||||
* to display pause, resume, retry, and cancel options depending on the state of the uploads.
|
||||
*
|
||||
* @param currentPosition The current position in the view pager. A value of `0` indicates
|
||||
* pending uploads, while `1` indicates failed uploads.
|
||||
*/
|
||||
fun updateMenuItems(currentPosition: Int) {
|
||||
if (menu != null) {
|
||||
menu!!.clear()
|
||||
if (currentPosition == 0) {
|
||||
if (isPendingIconsVisible) {
|
||||
if (!isPaused) {
|
||||
if (menu!!.findItem(R.id.pause_icon) == null) {
|
||||
menu!!.add(
|
||||
Menu.NONE,
|
||||
R.id.pause_icon,
|
||||
Menu.NONE,
|
||||
getString(R.string.pause)
|
||||
)
|
||||
.setIcon(R.drawable.pause_icon)
|
||||
.setOnMenuItemClickListener {
|
||||
pendingUploadsFragment!!.pauseUploads()
|
||||
setPausedIcon(true)
|
||||
true
|
||||
}
|
||||
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)
|
||||
}
|
||||
if (menu!!.findItem(R.id.cancel_icon) == null) {
|
||||
menu!!.add(
|
||||
Menu.NONE,
|
||||
R.id.cancel_icon,
|
||||
Menu.NONE,
|
||||
getString(R.string.cancel)
|
||||
)
|
||||
.setIcon(R.drawable.ic_cancel_upload)
|
||||
.setOnMenuItemClickListener {
|
||||
pendingUploadsFragment!!.deleteUploads()
|
||||
true
|
||||
}
|
||||
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)
|
||||
}
|
||||
} else {
|
||||
if (menu!!.findItem(R.id.resume_icon) == null) {
|
||||
menu!!.add(
|
||||
Menu.NONE,
|
||||
R.id.resume_icon,
|
||||
Menu.NONE,
|
||||
getString(R.string.resume)
|
||||
)
|
||||
.setIcon(R.drawable.play_icon)
|
||||
.setOnMenuItemClickListener {
|
||||
pendingUploadsFragment!!.restartUploads()
|
||||
setPausedIcon(false)
|
||||
true
|
||||
}
|
||||
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (currentPosition == 1) {
|
||||
if (isErrorIconsVisisble) {
|
||||
if (menu!!.findItem(R.id.retry_icon) == null) {
|
||||
menu!!.add(Menu.NONE, R.id.retry_icon, Menu.NONE, getString(R.string.retry))
|
||||
.setIcon(R.drawable.ic_refresh_24dp).setOnMenuItemClickListener {
|
||||
failedUploadsFragment!!.restartUploads()
|
||||
true
|
||||
}
|
||||
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)
|
||||
}
|
||||
if (menu!!.findItem(R.id.cancel_icon) == null) {
|
||||
menu!!.add(
|
||||
Menu.NONE,
|
||||
R.id.cancel_icon,
|
||||
Menu.NONE,
|
||||
getString(R.string.cancel)
|
||||
)
|
||||
.setIcon(R.drawable.ic_cancel_upload)
|
||||
.setOnMenuItemClickListener {
|
||||
failedUploadsFragment!!.deleteUploads()
|
||||
true
|
||||
}
|
||||
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hides the menu icons related to pending uploads.
|
||||
*/
|
||||
fun hidePendingIcons() {
|
||||
isPendingIconsVisible = false
|
||||
updateMenuItems(binding.uploadProgressViewPager.currentItem)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the paused state and updates the menu items accordingly.
|
||||
* @param paused A boolean indicating whether all the uploads are paused.
|
||||
*/
|
||||
fun setPausedIcon(paused: Boolean) {
|
||||
isPaused = paused
|
||||
updateMenuItems(binding.uploadProgressViewPager.currentItem)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the visibility of the menu icons related to failed uploads.
|
||||
* @param visible A boolean indicating whether the error icons should be visible.
|
||||
*/
|
||||
fun setErrorIconsVisibility(visible: Boolean) {
|
||||
isErrorIconsVisisble = visible
|
||||
updateMenuItems(binding.uploadProgressViewPager.currentItem)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -34,13 +34,11 @@ import fr.free.nrw.commons.upload.FileUtilsWrapper
|
|||
import fr.free.nrw.commons.upload.StashUploadResult
|
||||
import fr.free.nrw.commons.upload.StashUploadState
|
||||
import fr.free.nrw.commons.upload.UploadClient
|
||||
import fr.free.nrw.commons.upload.UploadProgressActivity
|
||||
import fr.free.nrw.commons.upload.UploadResult
|
||||
import fr.free.nrw.commons.wikidata.WikidataEditService
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.MainScope
|
||||
import kotlinx.coroutines.flow.asFlow
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import timber.log.Timber
|
||||
|
|
@ -106,7 +104,6 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
|
|||
getNotificationBuilder(CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL)!!
|
||||
|
||||
statesToProcess.add(Contribution.STATE_QUEUED)
|
||||
statesToProcess.add(Contribution.STATE_QUEUED_LIMITED_CONNECTION_MODE)
|
||||
}
|
||||
|
||||
@dagger.Module
|
||||
|
|
@ -166,105 +163,85 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
|
|||
}
|
||||
|
||||
override suspend fun doWork(): Result {
|
||||
var countUpload = 0
|
||||
// Start a foreground service
|
||||
setForeground(createForegroundInfo())
|
||||
notificationManager = NotificationManagerCompat.from(appContext)
|
||||
val processingUploads = getNotificationBuilder(
|
||||
CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL
|
||||
)!!
|
||||
withContext(Dispatchers.IO) {
|
||||
/*
|
||||
queuedContributions receives the results from a one-shot query.
|
||||
This means that once the list has been fetched from the database,
|
||||
it does not get updated even if some changes (insertions, deletions, etc.)
|
||||
are made to the contribution table afterwards.
|
||||
try {
|
||||
var totalUploadsStarted = 0
|
||||
// Start a foreground service
|
||||
setForeground(createForegroundInfo())
|
||||
notificationManager = NotificationManagerCompat.from(appContext)
|
||||
val processingUploads = getNotificationBuilder(
|
||||
CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL
|
||||
)!!
|
||||
withContext(Dispatchers.IO) {
|
||||
while (contributionDao.getContribution(statesToProcess)
|
||||
.blockingGet().size > 0 && contributionDao.getContribution(
|
||||
arrayListOf(
|
||||
Contribution.STATE_IN_PROGRESS
|
||||
)
|
||||
).blockingGet().size == 0
|
||||
) {
|
||||
/*
|
||||
queuedContributions receives the results from a one-shot query.
|
||||
This means that once the list has been fetched from the database,
|
||||
it does not get updated even if some changes (insertions, deletions, etc.)
|
||||
are made to the contribution table afterwards.
|
||||
|
||||
Related issues (fixed):
|
||||
https://github.com/commons-app/apps-android-commons/issues/5136
|
||||
https://github.com/commons-app/apps-android-commons/issues/5346
|
||||
*/
|
||||
val queuedContributions = contributionDao.getContribution(statesToProcess)
|
||||
.blockingGet()
|
||||
//Showing initial notification for the number of uploads being processed
|
||||
Related issues (fixed):
|
||||
https://github.com/commons-app/apps-android-commons/issues/5136
|
||||
https://github.com/commons-app/apps-android-commons/issues/5346
|
||||
*/
|
||||
val queuedContributions = contributionDao.getContribution(statesToProcess)
|
||||
.blockingGet()
|
||||
//Showing initial notification for the number of uploads being processed
|
||||
|
||||
Timber.e("Queued Contributions: " + queuedContributions.size)
|
||||
|
||||
processingUploads.setContentTitle(appContext.getString(R.string.starting_uploads))
|
||||
processingUploads.setContentText(
|
||||
appContext.resources.getQuantityString(
|
||||
R.plurals.starting_multiple_uploads,
|
||||
queuedContributions.size,
|
||||
queuedContributions.size
|
||||
processingUploads.setContentTitle(appContext.getString(R.string.starting_uploads))
|
||||
processingUploads.setContentText(
|
||||
appContext.resources.getQuantityString(
|
||||
R.plurals.starting_multiple_uploads,
|
||||
queuedContributions.size,
|
||||
queuedContributions.size
|
||||
)
|
||||
)
|
||||
notificationManager?.notify(
|
||||
PROCESSING_UPLOADS_NOTIFICATION_TAG,
|
||||
PROCESSING_UPLOADS_NOTIFICATION_ID,
|
||||
processingUploads.build()
|
||||
)
|
||||
)
|
||||
notificationManager?.notify(
|
||||
PROCESSING_UPLOADS_NOTIFICATION_TAG,
|
||||
PROCESSING_UPLOADS_NOTIFICATION_ID,
|
||||
processingUploads.build()
|
||||
)
|
||||
|
||||
/**
|
||||
* To avoid race condition when multiple of these workers are working, assign this state
|
||||
so that the next one does not process these contribution again
|
||||
*/
|
||||
queuedContributions.forEach {
|
||||
it.state = Contribution.STATE_IN_PROGRESS
|
||||
contributionDao.saveSynchronous(it)
|
||||
}
|
||||
val sortedQueuedContributionsList: List<Contribution> =
|
||||
queuedContributions.sortedBy { it.dateUploadStartedInMillis() }
|
||||
|
||||
queuedContributions.asFlow().map { contribution ->
|
||||
// Upload the contribution if it has not been cancelled by the user
|
||||
if (!CommonsApplication.cancelledUploads.contains(contribution.pageId)) {
|
||||
/**
|
||||
* If the limited connection mode is on, lets iterate through the queued
|
||||
* contributions
|
||||
* and set the state as STATE_QUEUED_LIMITED_CONNECTION_MODE ,
|
||||
* otherwise proceed with the upload
|
||||
*/
|
||||
if (isLimitedConnectionModeEnabled()) {
|
||||
if (contribution.state == Contribution.STATE_QUEUED) {
|
||||
contribution.state = Contribution.STATE_QUEUED_LIMITED_CONNECTION_MODE
|
||||
contributionDao.saveSynchronous(contribution)
|
||||
}
|
||||
} else {
|
||||
var contribution = sortedQueuedContributionsList.first()
|
||||
|
||||
if (contributionDao.getContribution(contribution.pageId) != null) {
|
||||
contribution.transferred = 0
|
||||
contribution.state = Contribution.STATE_IN_PROGRESS
|
||||
contributionDao.saveSynchronous(contribution)
|
||||
setProgressAsync(Data.Builder().putInt("progress", countUpload).build())
|
||||
countUpload++
|
||||
setProgressAsync(Data.Builder().putInt("progress", totalUploadsStarted).build())
|
||||
totalUploadsStarted++
|
||||
uploadContribution(contribution = contribution)
|
||||
}
|
||||
} else {
|
||||
/* We can remove the cancelled upload from the hashset
|
||||
as this contribution will not be processed again
|
||||
*/
|
||||
removeUploadFromInMemoryHashSet(contribution)
|
||||
}
|
||||
}.collect()
|
||||
//Dismiss the global notification
|
||||
notificationManager?.cancel(
|
||||
PROCESSING_UPLOADS_NOTIFICATION_TAG,
|
||||
PROCESSING_UPLOADS_NOTIFICATION_ID
|
||||
)
|
||||
}
|
||||
// Trigger WorkManager to process any new contributions that may have been added to the queue
|
||||
val updatedContributionQueue = withContext(Dispatchers.IO) {
|
||||
contributionDao.getContribution(statesToProcess).blockingGet()
|
||||
}
|
||||
if (updatedContributionQueue.isNotEmpty()) {
|
||||
return Result.retry()
|
||||
}
|
||||
|
||||
//Dismiss the global notification
|
||||
notificationManager?.cancel(
|
||||
PROCESSING_UPLOADS_NOTIFICATION_TAG,
|
||||
PROCESSING_UPLOADS_NOTIFICATION_ID
|
||||
)
|
||||
return Result.success()
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "UploadWorker encountered an error.")
|
||||
return Result.failure()
|
||||
} finally {
|
||||
WorkRequestHelper.markUploadWorkerAsStopped()
|
||||
}
|
||||
// Trigger WorkManager to process any new contributions that may have been added to the queue
|
||||
val updatedContributionQueue = withContext(Dispatchers.IO) {
|
||||
contributionDao.getContribution(statesToProcess).blockingGet()
|
||||
}
|
||||
if (updatedContributionQueue.isNotEmpty()) {
|
||||
return Result.retry()
|
||||
}
|
||||
|
||||
return Result.success()
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the processed contribution from the cancelledUploads in-memory hashset
|
||||
*/
|
||||
private fun removeUploadFromInMemoryHashSet(contribution: Contribution) {
|
||||
CommonsApplication.cancelledUploads.remove(contribution.pageId)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -287,12 +264,6 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
|
|||
.setContentTitle(appContext.getString(R.string.upload_in_progress))
|
||||
.build()
|
||||
}
|
||||
/**
|
||||
* Returns true is the limited connection mode is enabled
|
||||
*/
|
||||
private fun isLimitedConnectionModeEnabled(): Boolean {
|
||||
return sessionManager.getPreference(CommonsApplication.IS_LIMITED_CONNECTION_MODE_ENABLED)
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload the contribution
|
||||
|
|
@ -343,7 +314,6 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
|
|||
).onErrorReturn{
|
||||
return@onErrorReturn StashUploadResult(StashUploadState.FAILED,fileKey = null,errorMessage = it.message)
|
||||
}.blockingSingle()
|
||||
|
||||
when (stashUploadResult.state) {
|
||||
StashUploadState.SUCCESS -> {
|
||||
//If the stash upload succeeds
|
||||
|
|
@ -403,14 +373,19 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
|
|||
contribution.state = Contribution.STATE_PAUSED
|
||||
contributionDao.saveSynchronous(contribution)
|
||||
}
|
||||
StashUploadState.CANCELLED -> {
|
||||
showCancelledNotification(contribution)
|
||||
}
|
||||
else -> {
|
||||
Timber.e("""upload file to stash failed with status: ${stashUploadResult.state}""")
|
||||
showInvalidLoginNotification(contribution)
|
||||
contribution.state = Contribution.STATE_FAILED
|
||||
contribution.chunkInfo = null
|
||||
contribution.errorInfo = stashUploadResult.errorMessage
|
||||
showErrorNotification(contribution)
|
||||
contributionDao.saveSynchronous(contribution)
|
||||
if (stashUploadResult.errorMessage.equals(CsrfTokenClient.INVALID_TOKEN_ERROR_MESSAGE)) {
|
||||
Timber.e("Invalid Login, logging out")
|
||||
showInvalidLoginNotification(contribution)
|
||||
val username = sessionManager.userName
|
||||
var logoutListener = CommonsApplication.BaseLogoutListener(
|
||||
appContext,
|
||||
|
|
@ -426,6 +401,7 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
|
|||
Timber.e(exception)
|
||||
Timber.e("Stash upload failed for contribution: $filename")
|
||||
showFailedNotification(contribution)
|
||||
contribution.errorInfo=exception.message
|
||||
contribution.state=Contribution.STATE_FAILED
|
||||
clearChunks(contribution)
|
||||
}
|
||||
|
|
@ -543,6 +519,7 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
|
|||
private fun showSuccessNotification(contribution: Contribution) {
|
||||
val displayTitle = contribution.media.displayTitle
|
||||
contribution.state=Contribution.STATE_COMPLETED
|
||||
curentNotification.setContentIntent(getPendingIntent(MainActivity::class.java))
|
||||
curentNotification.setContentTitle(
|
||||
appContext.getString(
|
||||
R.string.upload_completed_notification_title,
|
||||
|
|
@ -565,7 +542,7 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
|
|||
@SuppressLint("StringFormatInvalid")
|
||||
private fun showFailedNotification(contribution: Contribution) {
|
||||
val displayTitle = contribution.media.displayTitle
|
||||
curentNotification.setContentIntent(getPendingIntent(MainActivity::class.java))
|
||||
curentNotification.setContentIntent(getPendingIntent(UploadProgressActivity::class.java))
|
||||
curentNotification.setContentTitle(
|
||||
appContext.getString(
|
||||
R.string.upload_failed_notification_title,
|
||||
|
|
@ -598,12 +575,34 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
|
|||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows a notification for a failed contribution upload.
|
||||
*/
|
||||
@SuppressLint("StringFormatInvalid")
|
||||
private fun showErrorNotification(contribution: Contribution) {
|
||||
val displayTitle = contribution.media.displayTitle
|
||||
curentNotification.setContentTitle(
|
||||
appContext.getString(
|
||||
R.string.upload_failed_notification_title,
|
||||
displayTitle
|
||||
)
|
||||
)
|
||||
.setContentText(contribution.errorInfo)
|
||||
.setProgress(0, 0, false)
|
||||
.setOngoing(false)
|
||||
notificationManager?.notify(
|
||||
currentNotificationTag, currentNotificationID,
|
||||
curentNotification.build()
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify that the current upload is paused
|
||||
* @param contribution
|
||||
*/
|
||||
private fun showPausedNotification(contribution: Contribution) {
|
||||
val displayTitle = contribution.media.displayTitle
|
||||
curentNotification.setContentIntent(getPendingIntent(UploadProgressActivity::class.java))
|
||||
curentNotification.setContentTitle(
|
||||
appContext.getString(
|
||||
R.string.upload_paused_notification_title,
|
||||
|
|
@ -619,6 +618,25 @@ class UploadWorker(var appContext: Context, workerParams: WorkerParameters) :
|
|||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify that the current upload is cancelled
|
||||
* @param contribution
|
||||
*/
|
||||
private fun showCancelledNotification(contribution: Contribution) {
|
||||
val displayTitle = contribution.media.displayTitle
|
||||
curentNotification.setContentIntent(getPendingIntent(UploadProgressActivity::class.java))
|
||||
curentNotification.setContentTitle(
|
||||
displayTitle
|
||||
)
|
||||
.setContentText("Upload has been cancelled!")
|
||||
.setProgress(0, 0, false)
|
||||
.setOngoing(false)
|
||||
notificationManager!!.notify(
|
||||
currentNotificationTag, currentNotificationID,
|
||||
curentNotification.build()
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method used to get Pending intent for opening different screen after clicking on notification
|
||||
* @param toClass
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package fr.free.nrw.commons.upload.worker
|
|||
import android.content.Context
|
||||
import androidx.work.*
|
||||
import androidx.work.WorkRequest.Companion.MIN_BACKOFF_MILLIS
|
||||
import timber.log.Timber
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
/**
|
||||
|
|
@ -11,7 +12,22 @@ import java.util.concurrent.TimeUnit
|
|||
class WorkRequestHelper {
|
||||
|
||||
companion object {
|
||||
|
||||
private var isUploadWorkerRunning = false
|
||||
private val lock = Object()
|
||||
|
||||
fun makeOneTimeWorkRequest(context: Context, existingWorkPolicy: ExistingWorkPolicy) {
|
||||
|
||||
synchronized(lock) {
|
||||
if (isUploadWorkerRunning) {
|
||||
Timber.e("UploadWorker is already running. Cannot start another instance.")
|
||||
return
|
||||
} else {
|
||||
Timber.e("Setting isUploadWorkerRunning to true")
|
||||
isUploadWorkerRunning = true
|
||||
}
|
||||
}
|
||||
|
||||
/* Set backoff criteria for the work request
|
||||
The default backoff policy is EXPONENTIAL, but while testing we found that it
|
||||
too long for the uploads to finish. So, set the backoff policy as LINEAR with the
|
||||
|
|
@ -35,7 +51,17 @@ class WorkRequestHelper {
|
|||
WorkManager.getInstance(context).enqueueUniqueWork(
|
||||
UploadWorker::class.java.simpleName, existingWorkPolicy, uploadRequest
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the flag isUploadWorkerRunning to`false` allowing new worker to be started.
|
||||
*/
|
||||
fun markUploadWorkerAsStopped() {
|
||||
synchronized(lock) {
|
||||
isUploadWorkerRunning = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue