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

View file

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