Show uploaded images in the context menu immediately after upload

This commit is contained in:
Vitaliy 2021-02-03 14:04:36 +02:00
parent d6b1f24b55
commit d63e21668f
3 changed files with 49 additions and 31 deletions

View file

@ -19,9 +19,9 @@ import net.osmand.plus.base.MenuBottomSheetDialogFragment;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerSpaceItem; 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(); 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; 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(); UploadPhotoProgressBottomSheet fragment = new UploadPhotoProgressBottomSheet();
fragment.setRetainInstance(true); fragment.setRetainInstance(true);
fragment.setMaxProgress(maxProgress); fragment.setMaxProgress(maxProgress);

View file

@ -36,6 +36,7 @@ import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat; import androidx.core.graphics.drawable.DrawableCompat;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil;
import net.osmand.data.Amenity; import net.osmand.data.Amenity;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.PointDescription; 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.ActivityResultListener.OnActivityResultListener;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.FontCache; 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.AbstractCard;
import net.osmand.plus.mapcontextmenu.builders.cards.CardsRowBuilder; import net.osmand.plus.mapcontextmenu.builders.cards.CardsRowBuilder;
import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard; 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.Algorithms;
import net.osmand.util.MapUtils; 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.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
@ -85,8 +91,9 @@ import java.util.Set;
import static net.osmand.plus.mapcontextmenu.builders.cards.ImageCard.GetImageCardsTask.GetImageCardsListener; 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; private static final int PICK_IMAGE = 1231;
public static final float SHADOW_HEIGHT_TOP_DP = 17f; public static final float SHADOW_HEIGHT_TOP_DP = 17f;
public static final int TITLE_LIMIT = 60; 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 { public interface CollapseExpandListener {
void onCollapseExpand(boolean collapsed); 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));
} }
} }
})); }));

View file

@ -13,14 +13,11 @@ import androidx.fragment.app.FragmentManager;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.data.LatLon;
import net.osmand.osm.io.NetworkUtils; import net.osmand.osm.io.NetworkUtils;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.dialogs.UploadPhotoProgressBottomSheet; 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.OPRConstants;
import net.osmand.plus.openplacereviews.OprStartFragment; import net.osmand.plus.openplacereviews.OprStartFragment;
import net.osmand.plus.osmedit.opr.OpenDBAPI; import net.osmand.plus.osmedit.opr.OpenDBAPI;
@ -36,7 +33,6 @@ import java.io.InputStream;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> { public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
@ -47,22 +43,17 @@ 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 final OpenDBAPI openDBAPI = new OpenDBAPI(); private final OpenDBAPI openDBAPI = new OpenDBAPI();
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 UploadPhotosListener listener;
private final GetImageCardsListener imageCardListener; private UploadPhotosProgressListener progressListener;
private UploadPhotosListener listener;
public UploadPhotosAsyncTask(MapActivity activity, List<Uri> data, LatLon latLon, String[] placeId, public UploadPhotosAsyncTask(MapActivity activity, List<Uri> data, String[] placeId, UploadPhotosListener listener) {
Map<String, String> params, GetImageCardsListener imageCardListener) {
app = (OsmandApplication) activity.getApplicationContext(); app = (OsmandApplication) activity.getApplicationContext();
activityRef = new WeakReference<>(activity); activityRef = new WeakReference<>(activity);
this.data = data; this.data = data;
this.latLon = latLon;
this.params = params;
this.placeId = placeId; this.placeId = placeId;
this.imageCardListener = imageCardListener; this.listener = listener;
} }
@Override @Override
@ -70,7 +61,7 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
FragmentActivity activity = activityRef.get(); FragmentActivity activity = activityRef.get();
if (AndroidUtils.isActivityNotDestroyed(activity)) { if (AndroidUtils.isActivityNotDestroyed(activity)) {
FragmentManager manager = activity.getSupportFragmentManager(); FragmentManager manager = activity.getSupportFragmentManager();
listener = UploadPhotoProgressBottomSheet.showInstance(manager, data.size(), new OnDismissListener() { progressListener = UploadPhotoProgressBottomSheet.showInstance(manager, data.size(), new OnDismissListener() {
@Override @Override
public void onDismiss(DialogInterface dialog) { public void onDismiss(DialogInterface dialog) {
cancel(false); cancel(false);
@ -81,8 +72,8 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
@Override @Override
protected void onProgressUpdate(Integer... values) { protected void onProgressUpdate(Integer... values) {
if (listener != null) { if (progressListener != null) {
listener.uploadPhotosProgressUpdate(values[0]); progressListener.uploadPhotosProgressUpdate(values[0]);
} }
} }
@ -103,8 +94,8 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
@Override @Override
protected void onPostExecute(Void aVoid) { protected void onPostExecute(Void aVoid) {
if (listener != null) { if (progressListener != null) {
listener.uploadPhotosFinished(); progressListener.uploadPhotosFinished();
} }
} }
@ -156,13 +147,9 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
checkTokenAndShowScreen(); checkTokenAndShowScreen();
} else { } else {
success = true; success = true;
String str = app.getString(R.string.successfully_uploaded_pattern, 1, 1);
app.showToastMessage(str);
//refresh the image //refresh the image
if (listener != null) {
MapActivity activity = activityRef.get(); listener.uploadPhotosSuccess(response);
if (activity != null) {
MenuBuilder.execute(new GetImageCardsTask(activity, latLon, params, imageCardListener));
} }
} }
} else { } else {
@ -215,12 +202,17 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
return os.toByteArray(); return os.toByteArray();
} }
public interface UploadPhotosProgressListener {
public interface UploadPhotosListener {
void uploadPhotosProgressUpdate(int progress); void uploadPhotosProgressUpdate(int progress);
void uploadPhotosFinished(); void uploadPhotosFinished();
} }
public interface UploadPhotosListener {
void uploadPhotosSuccess(String response);
}
} }