Merge pull request #10705 from osmandapp/upload-photo-progress-bar

Check upload success
This commit is contained in:
Vitaliy 2021-02-01 04:06:50 +02:00 committed by GitHub
commit 3108dfbfbe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 27 deletions

View file

@ -33,6 +33,7 @@ public class UploadPhotoProgressBottomSheet extends MenuBottomSheetDialogFragmen
private int progress;
private int maxProgress;
private boolean uploadingFinished;
@Override
public void createMenuItems(Bundle savedInstanceState) {
@ -44,17 +45,12 @@ public class UploadPhotoProgressBottomSheet extends MenuBottomSheetDialogFragmen
uploadedPhotosCounter = view.findViewById(R.id.description);
progressBar = view.findViewById(R.id.progress_bar);
progressBar.setMax(maxProgress);
String titleProgress = getString(progress == maxProgress? R.string.upload_photo_completed: R.string.upload_photo);
String descriptionProgress;
if (progress == maxProgress) {
descriptionProgress = getString(R.string.uploaded_count, progress, maxProgress);
} else {
descriptionProgress = getString(R.string.uploading_count, progress, maxProgress);
}
int descriptionId = uploadingFinished ? R.string.uploaded_count : R.string.uploading_count;
BaseBottomSheetItem descriptionItem = new BottomSheetItemWithDescription.Builder()
.setDescription(descriptionProgress)
.setTitle(titleProgress)
.setDescription(getString(descriptionId, progress, maxProgress))
.setTitle(getString(uploadingFinished ? R.string.upload_photo_completed : R.string.upload_photo))
.setCustomView(view)
.create();
items.add(descriptionItem);
@ -74,9 +70,10 @@ public class UploadPhotoProgressBottomSheet extends MenuBottomSheetDialogFragmen
}
private void updateProgress(int progress) {
int descriptionId = uploadingFinished ? R.string.uploaded_count : R.string.uploading_count;
progressBar.setProgress(progress);
uploadedPhotosCounter.setText((getString(R.string.uploading_count, progress, maxProgress)));
uploadedPhotosTitle.setText(progress == maxProgress ? R.string.upload_photo_completed : R.string.upload_photo);
uploadedPhotosCounter.setText(getString(descriptionId, progress, maxProgress));
uploadedPhotosTitle.setText(uploadingFinished ? R.string.upload_photo_completed : R.string.upload_photo);
}
@Override
@ -87,13 +84,10 @@ public class UploadPhotoProgressBottomSheet extends MenuBottomSheetDialogFragmen
@Override
public void uploadPhotosFinished() {
updateProgress(maxProgress);
if (progress == maxProgress) {
uploadedPhotosCounter.setText((getString(R.string.uploaded_count, progress, maxProgress)));
setDismissButtonTextId(R.string.shared_string_close);
uploadingFinished = true;
updateProgress(progress);
UiUtilities.setupDialogButton(nightMode, dismissButton, getDismissButtonType(), getDismissButtonTextId());
}
}
@Override
public void onDismiss(@NonNull DialogInterface dialog) {
@ -104,6 +98,11 @@ public class UploadPhotoProgressBottomSheet extends MenuBottomSheetDialogFragmen
}
}
@Override
protected int getDismissButtonTextId() {
return uploadingFinished ? R.string.shared_string_close : R.string.shared_string_cancel;
}
public static UploadPhotosListener showInstance(@NonNull FragmentManager fragmentManager, int maxProgress, OnDismissListener listener) {
UploadPhotoProgressBottomSheet fragment = new UploadPhotoProgressBottomSheet();
fragment.setRetainInstance(true);

View file

@ -34,6 +34,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -45,14 +46,13 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
private final OsmandApplication app;
private final WeakReference<MapActivity> activityRef;
private UploadPhotosListener listener;
private final OpenDBAPI openDBAPI = new OpenDBAPI();
private final LatLon latLon;
private final List<Uri> data;
private final String[] placeId;
private final Map<String, String> params;
private final GetImageCardsListener imageCardListener;
private UploadPhotosListener listener;
public UploadPhotosAsyncTask(MapActivity activity, List<Uri> data, LatLon latLon, String[] placeId,
Map<String, String> params, GetImageCardsListener imageCardListener) {
@ -87,13 +87,16 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
}
protected Void doInBackground(Void... uris) {
List<Uri> uploadedPhotoUris = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
if (isCancelled()) {
break;
}
Uri uri = data.get(i);
handleSelectedImage(uri);
publishProgress(i + 1);
if (handleSelectedImage(uri)) {
uploadedPhotoUris.add(uri);
publishProgress(uploadedPhotoUris.size());
}
}
return null;
}
@ -105,12 +108,13 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
}
}
private void handleSelectedImage(final Uri uri) {
private boolean handleSelectedImage(final Uri uri) {
boolean success = false;
InputStream inputStream = null;
try {
inputStream = app.getContentResolver().openInputStream(uri);
if (inputStream != null) {
uploadImageToPlace(inputStream);
success = uploadImageToPlace(inputStream);
}
} catch (Exception e) {
LOG.error(e);
@ -118,11 +122,13 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
} finally {
Algorithms.closeStream(inputStream);
}
return success;
}
private void uploadImageToPlace(InputStream image) {
private boolean uploadImageToPlace(InputStream image) {
boolean success = false;
InputStream serverData = new ByteArrayInputStream(compressImageToJpeg(image));
final String baseUrl = OPRConstants.getBaseUrl(app);
String baseUrl = OPRConstants.getBaseUrl(app);
// all these should be constant
String url = baseUrl + "api/ipfs/image";
String response = NetworkUtils.sendPostDataRequest(url, "file", "compressed.jpeg", serverData);
@ -140,8 +146,6 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
response, error);
if (res != 200) {
app.showToastMessage(error.toString());
} else {
//ok, continue
}
} catch (FailedVerificationException e) {
LOG.error(e);
@ -151,6 +155,7 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
//image was uploaded but not added to blockchain
checkTokenAndShowScreen();
} else {
success = true;
String str = app.getString(R.string.successfully_uploaded_pattern, 1, 1);
app.showToastMessage(str);
//refresh the image
@ -163,6 +168,7 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
} else {
checkTokenAndShowScreen();
}
return success;
}
//This method runs on non main thread