forked from organicmaps/organicmaps
[android] Viator refactoring
This commit is contained in:
parent
85f0ffaff1
commit
08a47fd0df
3 changed files with 204 additions and 139 deletions
165
android/src/com/mapswithme/maps/base/BaseSponsoredAdapter.java
Normal file
165
android/src/com/mapswithme/maps/base/BaseSponsoredAdapter.java
Normal file
|
@ -0,0 +1,165 @@
|
|||
package com.mapswithme.maps.base;
|
||||
|
||||
import android.support.annotation.CallSuper;
|
||||
import android.support.annotation.IntDef;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.mapswithme.maps.MwmApplication;
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.widget.placepage.Sponsored;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class BaseSponsoredAdapter extends RecyclerView.Adapter<BaseSponsoredAdapter.ViewHolder>
|
||||
{
|
||||
private static final int MAX_ITEMS = 5;
|
||||
|
||||
protected static final int TYPE_PRODUCT = 0;
|
||||
private static final int TYPE_MORE = 1;
|
||||
|
||||
private static final String MORE = MwmApplication.get().getString(R.string.placepage_more_button);
|
||||
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@IntDef({ TYPE_PRODUCT, TYPE_MORE })
|
||||
@interface ViewType{}
|
||||
|
||||
@NonNull
|
||||
private final List<Item> mItems;
|
||||
@Nullable
|
||||
private final ItemSelectedListener mListener;
|
||||
|
||||
public BaseSponsoredAdapter(@Sponsored.SponsoredType int sponsoredType,
|
||||
@NonNull List<? extends Item> items, @NonNull String url,
|
||||
@Nullable ItemSelectedListener listener)
|
||||
{
|
||||
mItems = new ArrayList<>();
|
||||
mListener = listener;
|
||||
boolean showMoreItem = items.size() >= MAX_ITEMS;
|
||||
int size = showMoreItem ? MAX_ITEMS : items.size();
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
Item product = items.get(i);
|
||||
mItems.add(product);
|
||||
}
|
||||
if (showMoreItem)
|
||||
mItems.add(new Item(TYPE_MORE, sponsoredType, MORE, url));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(ViewGroup parent, @ViewType int viewType)
|
||||
{
|
||||
switch (viewType)
|
||||
{
|
||||
case TYPE_PRODUCT:
|
||||
return createViewHolder(LayoutInflater.from(parent.getContext()), parent);
|
||||
case TYPE_MORE:
|
||||
return new ViewHolder(LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.item_viator_more,
|
||||
parent, false), this);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(ViewHolder holder, int position)
|
||||
{
|
||||
holder.bind(mItems.get(position));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount()
|
||||
{
|
||||
return mItems.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ViewType
|
||||
public int getItemViewType(int position)
|
||||
{
|
||||
return mItems.get(position).mType;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
protected abstract ViewHolder createViewHolder(@NonNull LayoutInflater inflater,
|
||||
@NonNull ViewGroup parent);
|
||||
|
||||
public static class ViewHolder extends RecyclerView.ViewHolder
|
||||
implements View.OnClickListener
|
||||
{
|
||||
@NonNull
|
||||
TextView mTitle;
|
||||
@NonNull
|
||||
BaseSponsoredAdapter mAdapter;
|
||||
|
||||
protected ViewHolder(@NonNull View itemView, @NonNull BaseSponsoredAdapter adapter)
|
||||
{
|
||||
super(itemView);
|
||||
mTitle = (TextView) itemView.findViewById(R.id.tv__title);
|
||||
mAdapter = adapter;
|
||||
itemView.setOnClickListener(this);
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
public void bind(@NonNull Item item)
|
||||
{
|
||||
mTitle.setText(item.mTitle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v)
|
||||
{
|
||||
int position = getAdapterPosition();
|
||||
if (position == RecyclerView.NO_POSITION)
|
||||
return;
|
||||
|
||||
onItemSelected(mAdapter.mItems.get(position));
|
||||
}
|
||||
|
||||
void onItemSelected(@NonNull Item item)
|
||||
{
|
||||
if (mAdapter.mListener != null)
|
||||
{
|
||||
if (item.mType == TYPE_PRODUCT)
|
||||
mAdapter.mListener.onItemSelected(item.mUrl, item.mSponsoredType);
|
||||
else if (item.mType == TYPE_MORE)
|
||||
mAdapter.mListener.onMoreItemSelected(item.mUrl, item.mSponsoredType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class Item
|
||||
{
|
||||
@ViewType
|
||||
private final int mType;
|
||||
@Sponsored.SponsoredType
|
||||
private final int mSponsoredType;
|
||||
@NonNull
|
||||
private final String mTitle;
|
||||
@NonNull
|
||||
private final String mUrl;
|
||||
|
||||
protected Item(@ViewType int type, @Sponsored.SponsoredType int sponsoredType,
|
||||
@NonNull String title, @NonNull String url)
|
||||
{
|
||||
mType = type;
|
||||
mSponsoredType = sponsoredType;
|
||||
mTitle = title;
|
||||
mUrl = url;
|
||||
}
|
||||
}
|
||||
|
||||
public interface ItemSelectedListener
|
||||
{
|
||||
void onItemSelected(@NonNull String url, @Sponsored.SponsoredType int type);
|
||||
void onMoreItemSelected(@NonNull String url, @Sponsored.SponsoredType int type);
|
||||
}
|
||||
}
|
|
@ -1,11 +1,8 @@
|
|||
package com.mapswithme.maps.viator;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.CallSuper;
|
||||
import android.support.annotation.IntDef;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
@ -14,131 +11,46 @@ import android.widget.RatingBar;
|
|||
import android.widget.TextView;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.mapswithme.maps.MwmApplication;
|
||||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.base.BaseSponsoredAdapter;
|
||||
import com.mapswithme.maps.widget.placepage.Sponsored;
|
||||
import com.mapswithme.util.UiUtils;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public final class ViatorAdapter extends RecyclerView.Adapter<ViatorAdapter.ViewHolder>
|
||||
public final class ViatorAdapter extends BaseSponsoredAdapter
|
||||
{
|
||||
private static final int MAX_ITEMS = 5;
|
||||
|
||||
private static final int TYPE_PRODUCT = 0;
|
||||
private static final int TYPE_MORE = 1;
|
||||
|
||||
private static final String MORE = MwmApplication.get().getString(R.string.placepage_more_button);
|
||||
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@IntDef({ TYPE_PRODUCT, TYPE_MORE })
|
||||
private @interface ViewType{}
|
||||
|
||||
@NonNull
|
||||
private final List<Item> mItems;
|
||||
@Nullable
|
||||
private final ItemSelectedListener mListener;
|
||||
|
||||
public ViatorAdapter(@NonNull ViatorProduct[] items, @NonNull String cityUrl,
|
||||
@Nullable ItemSelectedListener listener)
|
||||
{
|
||||
mItems = new ArrayList<>();
|
||||
mListener = listener;
|
||||
boolean showMoreItem = items.length >= MAX_ITEMS;
|
||||
int size = showMoreItem ? MAX_ITEMS : items.length;
|
||||
for (int i = 0; i < size; i++)
|
||||
super(Sponsored.TYPE_VIATOR, convertItems(items), cityUrl, listener);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private static List<Item> convertItems(@NonNull ViatorProduct[] items)
|
||||
{
|
||||
List<Item> viewItems = new ArrayList<>();
|
||||
for (ViatorProduct product : items)
|
||||
{
|
||||
ViatorProduct product = items[i];
|
||||
mItems.add(new Item(TYPE_PRODUCT, product.getPhotoUrl(), product.getTitle(),
|
||||
product.getDuration(), product.getRating(), product.getPriceFormatted(),
|
||||
product.getPageUrl()));
|
||||
viewItems.add(new Item(product.getPhotoUrl(), product.getTitle(),
|
||||
product.getDuration(), product.getRating(), product.getPriceFormatted(),
|
||||
product.getPageUrl()));
|
||||
}
|
||||
if (showMoreItem)
|
||||
mItems.add(new Item(TYPE_MORE, null, MORE, null, 0.0, null, cityUrl));
|
||||
|
||||
return viewItems;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(ViewGroup parent, @ViewType int viewType)
|
||||
@NonNull
|
||||
protected BaseSponsoredAdapter.ViewHolder createViewHolder(@NonNull LayoutInflater inflater,
|
||||
@NonNull ViewGroup parent)
|
||||
{
|
||||
switch (viewType)
|
||||
{
|
||||
case TYPE_PRODUCT:
|
||||
return new ProductViewHolder(LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.item_viator_product,
|
||||
parent, false), this);
|
||||
case TYPE_MORE:
|
||||
return new ViewHolder(LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.item_viator_more,
|
||||
parent, false), this);
|
||||
}
|
||||
return null;
|
||||
return new ProductViewHolder(inflater.inflate(R.layout.item_viator_product, parent, false),
|
||||
this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(ViewHolder holder, int position)
|
||||
{
|
||||
holder.bind(mItems.get(position));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount()
|
||||
{
|
||||
return mItems.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ViewType
|
||||
public int getItemViewType(int position)
|
||||
{
|
||||
return mItems.get(position).mType;
|
||||
}
|
||||
|
||||
static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
|
||||
{
|
||||
@NonNull
|
||||
TextView mTitle;
|
||||
@NonNull
|
||||
ViatorAdapter mAdapter;
|
||||
|
||||
ViewHolder(@NonNull View itemView, @NonNull ViatorAdapter adapter)
|
||||
{
|
||||
super(itemView);
|
||||
mTitle = (TextView) itemView.findViewById(R.id.tv__title);
|
||||
mAdapter = adapter;
|
||||
itemView.setOnClickListener(this);
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
void bind(@NonNull Item item)
|
||||
{
|
||||
mTitle.setText(item.mTitle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v)
|
||||
{
|
||||
int position = getAdapterPosition();
|
||||
if (position == RecyclerView.NO_POSITION)
|
||||
return;
|
||||
|
||||
onItemSelected(mAdapter.mItems.get(position));
|
||||
}
|
||||
|
||||
void onItemSelected(@NonNull Item item)
|
||||
{
|
||||
if (mAdapter.mListener != null)
|
||||
{
|
||||
if (item.mType == TYPE_PRODUCT)
|
||||
mAdapter.mListener.onViatorItemSelected(item.mUrl);
|
||||
else if (item.mType == TYPE_MORE)
|
||||
mAdapter.mListener.onViatorMoreItemSelected(item.mUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class ProductViewHolder extends ViewHolder
|
||||
private static final class ProductViewHolder extends ViewHolder
|
||||
{
|
||||
@NonNull
|
||||
ImageView mImage;
|
||||
|
@ -163,57 +75,44 @@ public final class ViatorAdapter extends RecyclerView.Adapter<ViatorAdapter.View
|
|||
}
|
||||
|
||||
@Override
|
||||
void bind(@NonNull Item item)
|
||||
public void bind(@NonNull BaseSponsoredAdapter.Item item)
|
||||
{
|
||||
super.bind(item);
|
||||
|
||||
if (item.mPhotoUrl != null)
|
||||
Item product = (Item) item;
|
||||
if (product.mPhotoUrl != null)
|
||||
{
|
||||
Glide.with(mContext)
|
||||
.load(item.mPhotoUrl)
|
||||
.load(product.mPhotoUrl)
|
||||
.centerCrop()
|
||||
.into(mImage);
|
||||
}
|
||||
|
||||
UiUtils.setTextAndHideIfEmpty(mDuration, item.mDuration);
|
||||
UiUtils.setTextAndHideIfEmpty(mPrice, item.mPrice);
|
||||
mRating.setRating((float) item.mRating);
|
||||
UiUtils.setTextAndHideIfEmpty(mDuration, product.mDuration);
|
||||
UiUtils.setTextAndHideIfEmpty(mPrice, product.mPrice);
|
||||
mRating.setRating((float) product.mRating);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Item
|
||||
private static final class Item extends BaseSponsoredAdapter.Item
|
||||
{
|
||||
@ViewType
|
||||
private final int mType;
|
||||
@Nullable
|
||||
private final String mPhotoUrl;
|
||||
@NonNull
|
||||
private final String mTitle;
|
||||
@Nullable
|
||||
private final String mDuration;
|
||||
private final double mRating;
|
||||
@Nullable
|
||||
private final String mPrice;
|
||||
@NonNull
|
||||
private final String mUrl;
|
||||
|
||||
private Item(int type, @Nullable String photoUrl, @NonNull String title,
|
||||
private Item(@Nullable String photoUrl, @NonNull String title,
|
||||
@Nullable String duration, double rating, @Nullable String price,
|
||||
@NonNull String url)
|
||||
{
|
||||
mType = type;
|
||||
super(TYPE_PRODUCT, Sponsored.TYPE_VIATOR, title, url);
|
||||
mPhotoUrl = photoUrl;
|
||||
mTitle = title;
|
||||
mDuration = duration;
|
||||
mRating = rating;
|
||||
mPrice = price;
|
||||
mUrl = url;
|
||||
}
|
||||
}
|
||||
|
||||
public interface ItemSelectedListener
|
||||
{
|
||||
void onViatorItemSelected(@NonNull String url);
|
||||
void onViatorMoreItemSelected(@NonNull String url);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,6 +48,7 @@ import com.mapswithme.maps.ads.DefaultAdTracker;
|
|||
import com.mapswithme.maps.ads.Factory;
|
||||
import com.mapswithme.maps.ads.LocalAdInfo;
|
||||
import com.mapswithme.maps.api.ParsedMwmRequest;
|
||||
import com.mapswithme.maps.base.BaseSponsoredAdapter;
|
||||
import com.mapswithme.maps.bookmarks.data.Bookmark;
|
||||
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
|
||||
import com.mapswithme.maps.bookmarks.data.DistanceAndAzimut;
|
||||
|
@ -121,9 +122,9 @@ public class PlacePageView extends RelativeLayout
|
|||
EditBookmarkFragment.EditBookmarkListener,
|
||||
BannerController.BannerListener,
|
||||
Viator.ViatorListener,
|
||||
ViatorAdapter.ItemSelectedListener,
|
||||
UGC.UGCListener,
|
||||
UgcAverageRatingController.OnUgcRatingChangedListener
|
||||
UgcAverageRatingController.OnUgcRatingChangedListener,
|
||||
BaseSponsoredAdapter.ItemSelectedListener
|
||||
{
|
||||
private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
|
||||
private static final String TAG = PlacePageView.class.getSimpleName();
|
||||
|
@ -874,19 +875,19 @@ public class PlacePageView extends RelativeLayout
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onViatorItemSelected(@NonNull String url)
|
||||
public void onItemSelected(@NonNull String url, @Sponsored.SponsoredType int type)
|
||||
{
|
||||
Utils.openUrl(getContext(), url);
|
||||
Statistics.INSTANCE.trackSponsoredGalleryItemSelected(Statistics.EventName.PP_SPONSOR_ITEM_SELECTED,
|
||||
Sponsored.TYPE_VIATOR);
|
||||
type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViatorMoreItemSelected(@NonNull String url)
|
||||
public void onMoreItemSelected(@NonNull String url, @Sponsored.SponsoredType int type)
|
||||
{
|
||||
Utils.openUrl(getContext(), url);
|
||||
Statistics.INSTANCE.trackSponsoredGalleryItemSelected(Statistics.EventName.PP_SPONSOR_MORE_SELECTED,
|
||||
Sponsored.TYPE_VIATOR);
|
||||
type);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Add table
Reference in a new issue