From cf84c562438bc4d22c4d6d8b51961c4c42f970cf Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Wed, 22 Apr 2020 23:11:18 +0300 Subject: [PATCH] Add view pager for images --- OsmAnd/res/layout/group_description_item.xml | 14 +-- OsmAnd/res/values/strings.xml | 1 + OsmAnd/src/net/osmand/plus/CustomRegion.java | 8 +- ...Info.java => DownloadDescriptionInfo.java} | 52 ++++----- .../ui/DownloadResourceGroupFragment.java | 102 +++++++++++++----- 5 files changed, 102 insertions(+), 75 deletions(-) rename OsmAnd/src/net/osmand/plus/download/ui/{DescriptionInfo.java => DownloadDescriptionInfo.java} (56%) diff --git a/OsmAnd/res/layout/group_description_item.xml b/OsmAnd/res/layout/group_description_item.xml index 184199854c..512e65a606 100644 --- a/OsmAnd/res/layout/group_description_item.xml +++ b/OsmAnd/res/layout/group_description_item.xml @@ -13,18 +13,10 @@ android:orientation="vertical" android:paddingBottom="@dimen/list_header_settings_top_margin"> - - - - - + android:layout_height="132dp" /> + Unsupported action %1$s Extra maps Custom color Lombard diff --git a/OsmAnd/src/net/osmand/plus/CustomRegion.java b/OsmAnd/src/net/osmand/plus/CustomRegion.java index ecfbdfd9a1..a6f72891df 100644 --- a/OsmAnd/src/net/osmand/plus/CustomRegion.java +++ b/OsmAnd/src/net/osmand/plus/CustomRegion.java @@ -9,7 +9,7 @@ import net.osmand.map.WorldRegion; import net.osmand.plus.download.CustomIndexItem; import net.osmand.plus.download.DownloadActivityType; import net.osmand.plus.download.IndexItem; -import net.osmand.plus.download.ui.DescriptionInfo; +import net.osmand.plus.download.ui.DownloadDescriptionInfo; import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; @@ -36,7 +36,7 @@ public class CustomRegion extends WorldRegion { private JSONArray downloadItemsJson; - private DescriptionInfo descriptionInfo; + private DownloadDescriptionInfo descriptionInfo; private Map names = new HashMap<>(); private Map icons = new HashMap<>(); @@ -70,7 +70,7 @@ public class CustomRegion extends WorldRegion { } @Nullable - public DescriptionInfo getDescriptionInfo() { + public DownloadDescriptionInfo getDescriptionInfo() { return descriptionInfo; } @@ -106,7 +106,7 @@ public class CustomRegion extends WorldRegion { } catch (IllegalArgumentException e) { region.headerColor = 0; } - region.descriptionInfo = DescriptionInfo.fromJson(object.optJSONObject("description")); + region.descriptionInfo = DownloadDescriptionInfo.fromJson(object.optJSONObject("description")); return region; } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DescriptionInfo.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadDescriptionInfo.java similarity index 56% rename from OsmAnd/src/net/osmand/plus/download/ui/DescriptionInfo.java rename to OsmAnd/src/net/osmand/plus/download/ui/DownloadDescriptionInfo.java index cf1aa4352b..20f685bb1c 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DescriptionInfo.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadDescriptionInfo.java @@ -15,9 +15,9 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -public class DescriptionInfo { +public class DownloadDescriptionInfo { - private static final Log LOG = PlatformUtil.getLog(DescriptionInfo.class); + private static final Log LOG = PlatformUtil.getLog(DownloadDescriptionInfo.class); private JSONArray buttonsJson; private List imageUrls; @@ -32,49 +32,44 @@ public class DescriptionInfo { return description != null ? Html.fromHtml(description) : null; } - public List getDownloadActionButtons(Context ctx) { - List downloadActionButtons = new ArrayList<>(); + public List getActionButtons(Context ctx) { + List actionButtons = new ArrayList<>(); if (buttonsJson != null) { try { for (int i = 0; i < buttonsJson.length(); i++) { - DescriptionActionButtonType type = null; String url = null; + String actionType = null; JSONObject object = buttonsJson.getJSONObject(i); if (object.has("url")) { url = object.optString("url"); } else if (object.has("action")) { - String action = object.optString("action"); - for (DescriptionActionButtonType buttonType : DescriptionActionButtonType.values()) { - if (buttonType.name().equalsIgnoreCase(action)) { - type = buttonType; - } - } + actionType = object.optString("action"); } Map localizedMap = JsonUtils.getLocalizedMapFromJson(object); String name = JsonUtils.getLocalizedResFromMap(ctx, localizedMap, null); - DownloadActionButton actionButton = new DownloadActionButton(type, name, url); - downloadActionButtons.add(actionButton); + ActionButton actionButton = new ActionButton(actionType, name, url); + actionButtons.add(actionButton); } } catch (JSONException e) { e.printStackTrace(); } } - return downloadActionButtons; + return actionButtons; } - public static DescriptionInfo fromJson(JSONObject json) { + public static DownloadDescriptionInfo fromJson(JSONObject json) { if (json != null) { - DescriptionInfo descriptionInfo = new DescriptionInfo(); + DownloadDescriptionInfo downloadDescriptionInfo = new DownloadDescriptionInfo(); try { - descriptionInfo.texts = JsonUtils.getLocalizedMapFromJson("text", json); - descriptionInfo.imageUrls = JsonUtils.jsonArrayToList("image", json); - descriptionInfo.buttonsJson = json.optJSONArray("button"); + downloadDescriptionInfo.texts = JsonUtils.getLocalizedMapFromJson("text", json); + downloadDescriptionInfo.imageUrls = JsonUtils.jsonArrayToList("image", json); + downloadDescriptionInfo.buttonsJson = json.optJSONArray("button"); } catch (JSONException e) { LOG.error(e); } - return descriptionInfo; + return downloadDescriptionInfo; } return null; } @@ -90,25 +85,20 @@ public class DescriptionInfo { return descrJson; } - public enum DescriptionActionButtonType { - DOWNLOAD, - URL - } + public static class ActionButton { - public static class DownloadActionButton { - - private DescriptionInfo.DescriptionActionButtonType type; + private String actionType; private String name; private String url; - public DownloadActionButton(DescriptionInfo.DescriptionActionButtonType type, String name, String url) { - this.type = type; + public ActionButton(String actionType, String name, String url) { + this.actionType = actionType; this.name = name; this.url = url; } - public DescriptionInfo.DescriptionActionButtonType getType() { - return type; + public String getActionType() { + return actionType; } public String getName() { diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java index fe8743514a..2904339a51 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java @@ -22,19 +22,21 @@ import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ImageView; import android.widget.TextView; +import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.Toolbar; import androidx.core.view.MenuItemCompat; import androidx.fragment.app.DialogFragment; +import androidx.viewpager.widget.PagerAdapter; import com.squareup.picasso.Callback; import com.squareup.picasso.Picasso; -import com.squareup.picasso.RequestCreator; import net.osmand.AndroidNetworkUtils; import net.osmand.AndroidUtils; import net.osmand.PicassoUtils; import net.osmand.plus.CustomRegion; +import net.osmand.plus.LockableViewPager; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; @@ -49,6 +51,7 @@ import net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType; import net.osmand.plus.download.DownloadResources; import net.osmand.plus.download.DownloadValidationManager; import net.osmand.plus.download.IndexItem; +import net.osmand.plus.download.ui.DownloadDescriptionInfo.ActionButton; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.inapp.InAppPurchaseHelper; import net.osmand.plus.inapp.InAppPurchaseHelper.InAppPurchaseListener; @@ -251,16 +254,16 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow if (descriptionView != null) { if (group != null && group.getRegion() instanceof CustomRegion) { CustomRegion customRegion = (CustomRegion) group.getRegion(); - net.osmand.plus.download.ui.DescriptionInfo descriptionInfo = customRegion.getDescriptionInfo(); - if (descriptionInfo != null) { + DownloadDescriptionInfo downloadDescriptionInfo = customRegion.getDescriptionInfo(); + if (downloadDescriptionInfo != null) { TextView description = descriptionView.findViewById(R.id.description); - CharSequence descr = descriptionInfo.getLocalizedDescription(activity); + CharSequence descr = downloadDescriptionInfo.getLocalizedDescription(activity); description.setText(descr); AndroidUiHelper.updateVisibility(description, !Algorithms.isEmpty(descr)); ViewGroup buttonsContainer = descriptionView.findViewById(R.id.buttons_container); buttonsContainer.removeAllViews(); - for (final DescriptionInfo.DownloadActionButton actionButton : descriptionInfo.getDownloadActionButtons(activity)) { + for (final ActionButton actionButton : downloadDescriptionInfo.getActionButtons(activity)) { String name = actionButton.getName(); if (!Algorithms.isEmpty(name)) { TextView buttonText = (TextView) activity.getLayoutInflater().inflate(R.layout.download_description_button, buttonsContainer, false); @@ -270,37 +273,22 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow public void onClick(View v) { if (actionButton.getUrl() != null) { WikipediaDialogFragment.showFullArticle(activity, Uri.parse(actionButton.getUrl()), nightMode); + } else { + activity.getMyApplication().showShortToastMessage(R.string.download_unsupported_action, actionButton.getActionType()); } } }); buttonsContainer.addView(buttonText); } } - - ViewGroup imagesContainer = descriptionView.findViewById(R.id.images_container); - imagesContainer.removeAllViews(); - - final PicassoUtils picassoUtils = PicassoUtils.getPicasso(getMyApplication()); - Picasso picasso = Picasso.get(); - for (final String imageUrl : descriptionInfo.getImageUrls()) { - final ImageView image = new ImageView(getContext()); - imagesContainer.addView(image); - - RequestCreator rc = picasso.load(imageUrl); - rc.into(image, new Callback() { - @Override - public void onSuccess() { - image.setVisibility(View.VISIBLE); - picassoUtils.setResultLoaded(imageUrl, true); - } - - @Override - public void onError(Exception e) { - image.setVisibility(View.GONE); - picassoUtils.setResultLoaded(imageUrl, false); - } - }); + LockableViewPager viewPager = descriptionView.findViewById(R.id.images_pager); + if (!Algorithms.isEmpty(downloadDescriptionInfo.getImageUrls())) { + ImagesPagerAdapter adapter = new ImagesPagerAdapter(downloadDescriptionInfo.getImageUrls()); + viewPager.setAdapter(adapter); + } else { + viewPager.setVisibility(View.GONE); } + descriptionView.findViewById(R.id.container).setVisibility(View.VISIBLE); return; } @@ -810,4 +798,60 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow return true; } } + + private class ImagesPagerAdapter extends PagerAdapter { + + private PicassoUtils picassoUtils; + + private List imageUrls; + + public ImagesPagerAdapter(List imageUrls) { + this.imageUrls = imageUrls; + picassoUtils = PicassoUtils.getPicasso(getMyApplication()); + } + + @Override + public int getCount() { + return imageUrls.size(); + } + + @Override + public Object instantiateItem(ViewGroup container, int position) { + View view = createImageView(position); + container.addView(view, 0); + + return view; + } + + @Override + public void destroyItem(ViewGroup collection, int position, @NonNull Object view) { + collection.removeView((View) view); + } + + @Override + public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { + return view == object; + } + + private View createImageView(int position) { + final ImageView imageView = new ImageView(getContext()); + imageView.setScaleType(ImageView.ScaleType.FIT_XY); + + final String imageUrl = imageUrls.get(position); + Picasso.get().load(imageUrl).into(imageView, new Callback() { + @Override + public void onSuccess() { + imageView.setVisibility(View.VISIBLE); + picassoUtils.setResultLoaded(imageUrl, true); + } + + @Override + public void onError(Exception e) { + imageView.setVisibility(View.INVISIBLE); + picassoUtils.setResultLoaded(imageUrl, false); + } + }); + return imageView; + } + } } \ No newline at end of file