From d63e21668f10e02a82e0799be220ce5680b3de73 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Wed, 3 Feb 2021 14:04:36 +0200 Subject: [PATCH] Show uploaded images in the context menu immediately after upload --- .../UploadPhotoProgressBottomSheet.java | 6 +-- .../plus/mapcontextmenu/MenuBuilder.java | 30 ++++++++++++- .../mapcontextmenu/UploadPhotosAsyncTask.java | 44 ++++++++----------- 3 files changed, 49 insertions(+), 31 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/dialogs/UploadPhotoProgressBottomSheet.java b/OsmAnd/src/net/osmand/plus/dialogs/UploadPhotoProgressBottomSheet.java index 05e4bcc468..cd06804f5e 100644 --- a/OsmAnd/src/net/osmand/plus/dialogs/UploadPhotoProgressBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/dialogs/UploadPhotoProgressBottomSheet.java @@ -19,9 +19,9 @@ import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription; import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerSpaceItem; -import net.osmand.plus.mapcontextmenu.UploadPhotosAsyncTask.UploadPhotosListener; +import net.osmand.plus.mapcontextmenu.UploadPhotosAsyncTask.UploadPhotosProgressListener; -public class UploadPhotoProgressBottomSheet extends MenuBottomSheetDialogFragment implements UploadPhotosListener { +public class UploadPhotoProgressBottomSheet extends MenuBottomSheetDialogFragment implements UploadPhotosProgressListener { public static final String TAG = UploadPhotoProgressBottomSheet.class.getSimpleName(); @@ -103,7 +103,7 @@ public class UploadPhotoProgressBottomSheet extends MenuBottomSheetDialogFragmen 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 UploadPhotosProgressListener showInstance(@NonNull FragmentManager fragmentManager, int maxProgress, OnDismissListener listener) { UploadPhotoProgressBottomSheet fragment = new UploadPhotoProgressBottomSheet(); fragment.setRetainInstance(true); fragment.setMaxProgress(maxProgress); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java index 08c3994668..5cfdd0a281 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java @@ -36,6 +36,7 @@ import androidx.core.content.ContextCompat; import androidx.core.graphics.drawable.DrawableCompat; import net.osmand.AndroidUtils; +import net.osmand.PlatformUtil; import net.osmand.data.Amenity; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; @@ -51,6 +52,7 @@ import net.osmand.plus.activities.ActivityResultListener; import net.osmand.plus.activities.ActivityResultListener.OnActivityResultListener; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.helpers.FontCache; +import net.osmand.plus.mapcontextmenu.UploadPhotosAsyncTask.UploadPhotosListener; import net.osmand.plus.mapcontextmenu.builders.cards.AbstractCard; import net.osmand.plus.mapcontextmenu.builders.cards.CardsRowBuilder; import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard; @@ -75,6 +77,10 @@ import net.osmand.plus.widgets.tools.ClickableSpanTouchListener; import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; +import org.apache.commons.logging.Log; +import org.json.JSONException; +import org.json.JSONObject; + import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -85,8 +91,9 @@ import java.util.Set; import static net.osmand.plus.mapcontextmenu.builders.cards.ImageCard.GetImageCardsTask.GetImageCardsListener; -public class MenuBuilder { +public class MenuBuilder implements UploadPhotosListener { + private static final Log LOG = PlatformUtil.getLog(MenuBuilder.class); private static final int PICK_IMAGE = 1231; public static final float SHADOW_HEIGHT_TOP_DP = 17f; public static final int TITLE_LIMIT = 60; @@ -168,6 +175,25 @@ public class MenuBuilder { } } + @Override + public void uploadPhotosSuccess(final String response) { + app.runInUIThread(new Runnable() { + @Override + public void run() { + if (AndroidUtils.isActivityNotDestroyed(mapActivity)) { + try { + ImageCard imageCard = ImageCard.createCardOpr(mapActivity, new JSONObject(response)); + if (imageCard != null) { + addImageCard(imageCard); + } + } catch (JSONException e) { + LOG.error(e); + } + } + } + }); + } + public interface CollapseExpandListener { void onCollapseExpand(boolean collapsed); } @@ -490,7 +516,7 @@ public class MenuBuilder { } } } - execute(new UploadPhotosAsyncTask(mapActivity, imagesUri, getLatLon(), placeId, getAdditionalCardParams(), imageCardListener)); + execute(new UploadPhotosAsyncTask(mapActivity, imagesUri, placeId, MenuBuilder.this)); } } })); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/UploadPhotosAsyncTask.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/UploadPhotosAsyncTask.java index afe492af6f..cf0587e06f 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/UploadPhotosAsyncTask.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/UploadPhotosAsyncTask.java @@ -13,14 +13,11 @@ import androidx.fragment.app.FragmentManager; import net.osmand.AndroidUtils; import net.osmand.PlatformUtil; -import net.osmand.data.LatLon; import net.osmand.osm.io.NetworkUtils; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.dialogs.UploadPhotoProgressBottomSheet; -import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard.GetImageCardsTask; -import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard.GetImageCardsTask.GetImageCardsListener; import net.osmand.plus.openplacereviews.OPRConstants; import net.osmand.plus.openplacereviews.OprStartFragment; import net.osmand.plus.osmedit.opr.OpenDBAPI; @@ -36,7 +33,6 @@ import java.io.InputStream; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; -import java.util.Map; public class UploadPhotosAsyncTask extends AsyncTask { @@ -47,22 +43,17 @@ public class UploadPhotosAsyncTask extends AsyncTask { private final OsmandApplication app; private final WeakReference activityRef; private final OpenDBAPI openDBAPI = new OpenDBAPI(); - private final LatLon latLon; private final List data; private final String[] placeId; - private final Map params; - private final GetImageCardsListener imageCardListener; - private UploadPhotosListener listener; + private final UploadPhotosListener listener; + private UploadPhotosProgressListener progressListener; - public UploadPhotosAsyncTask(MapActivity activity, List data, LatLon latLon, String[] placeId, - Map params, GetImageCardsListener imageCardListener) { + public UploadPhotosAsyncTask(MapActivity activity, List data, String[] placeId, UploadPhotosListener listener) { app = (OsmandApplication) activity.getApplicationContext(); activityRef = new WeakReference<>(activity); this.data = data; - this.latLon = latLon; - this.params = params; this.placeId = placeId; - this.imageCardListener = imageCardListener; + this.listener = listener; } @Override @@ -70,7 +61,7 @@ public class UploadPhotosAsyncTask extends AsyncTask { FragmentActivity activity = activityRef.get(); if (AndroidUtils.isActivityNotDestroyed(activity)) { FragmentManager manager = activity.getSupportFragmentManager(); - listener = UploadPhotoProgressBottomSheet.showInstance(manager, data.size(), new OnDismissListener() { + progressListener = UploadPhotoProgressBottomSheet.showInstance(manager, data.size(), new OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { cancel(false); @@ -81,8 +72,8 @@ public class UploadPhotosAsyncTask extends AsyncTask { @Override protected void onProgressUpdate(Integer... values) { - if (listener != null) { - listener.uploadPhotosProgressUpdate(values[0]); + if (progressListener != null) { + progressListener.uploadPhotosProgressUpdate(values[0]); } } @@ -103,8 +94,8 @@ public class UploadPhotosAsyncTask extends AsyncTask { @Override protected void onPostExecute(Void aVoid) { - if (listener != null) { - listener.uploadPhotosFinished(); + if (progressListener != null) { + progressListener.uploadPhotosFinished(); } } @@ -156,13 +147,9 @@ public class UploadPhotosAsyncTask extends AsyncTask { checkTokenAndShowScreen(); } else { success = true; - String str = app.getString(R.string.successfully_uploaded_pattern, 1, 1); - app.showToastMessage(str); //refresh the image - - MapActivity activity = activityRef.get(); - if (activity != null) { - MenuBuilder.execute(new GetImageCardsTask(activity, latLon, params, imageCardListener)); + if (listener != null) { + listener.uploadPhotosSuccess(response); } } } else { @@ -215,12 +202,17 @@ public class UploadPhotosAsyncTask extends AsyncTask { return os.toByteArray(); } - - public interface UploadPhotosListener { + public interface UploadPhotosProgressListener { void uploadPhotosProgressUpdate(int progress); void uploadPhotosFinished(); } + + public interface UploadPhotosListener { + + void uploadPhotosSuccess(String response); + + } } \ No newline at end of file