Review fixes

This commit is contained in:
r.kuznetsov 2018-05-07 12:49:44 +03:00 committed by Arsentiy Milchakov
parent d0faaca2ea
commit d0a762cbaa
5 changed files with 241 additions and 102 deletions

View file

@ -3,7 +3,6 @@ package com.mapswithme.maps.widget.placepage;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.view.LayoutInflater;
import android.view.MenuItem;
@ -12,261 +11,394 @@ import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.R;
import com.mapswithme.maps.routing.RoutingController;
import com.mapswithme.util.BottomSheetHelper;
import com.mapswithme.util.ThemeUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
final class PlacePageButtons
{
private final int MAX_BUTTONS;
private static final Map<Integer, PartnerItem> PARTNERS_ITEMS = new HashMap<Integer, PartnerItem>()
{{
put(PartnerItem.PARTNER1.getIndex(), PartnerItem.PARTNER1);
put(PartnerItem.PARTNER3.getIndex(), PartnerItem.PARTNER3);
}};
private final int mMaxButtons;
private final PlacePageView mPlacePage;
private final ViewGroup mFrame;
private final ItemListener mItemListener;
private List<Item> mPrevItems;
private List<ButtonInterface> mPrevItems;
enum Item
interface ButtonInterface
{
@StringRes
int getTitle();
@DrawableRes
int getIcon();
@NonNull
ButtonType getType();
}
enum ButtonType
{
PARTNER1, PARTNER3, BOOKING, BOOKING_SEARCH, OPENTABLE, BACK, BOOKMARK,
ROUTE_FROM, ROUTE_TO, ROUTE_ADD, ROUTE_REMOVE, SHARE, MORE, CALL
}
private enum PartnerItem implements ButtonInterface
{
PARTNER1(1)
{
@Override
public int getTitle()
{
return R.string.sponsored_partner1_action;
}
@Override
public int getIcon()
{
return R.drawable.ic_24px_logo_partner1;
}
@NonNull
public ButtonType getType()
{
return ButtonType.PARTNER1;
}
},
PARTNER3(3)
{
@Override
public int getTitle()
{
return R.string.sponsored_partner3_action;
}
@Override
public int getIcon()
{
return R.drawable.ic_24px_logo_partner3;
}
@Override
@NonNull
public ButtonType getType()
{
return ButtonType.PARTNER3;
}
};
private final int mIndex;
PartnerItem(int index)
{
mIndex = index;
}
public int getIndex()
{
return mIndex;
}
}
enum Item implements ButtonInterface
{
BOOKING
{
@Override
int getTitle()
public int getTitle()
{
return R.string.book_button;
}
@Override
int getIcon()
public int getIcon()
{
return R.drawable.ic_booking;
}
@Override
@NonNull
public ButtonType getType()
{
return ButtonType.BOOKING;
}
},
BOOKING_SEARCH
{
@Override
int getTitle()
public int getTitle()
{
return R.string.booking_search;
}
@Override
int getIcon()
public int getIcon()
{
return R.drawable.ic_menu_search;
}
@NonNull
@Override
public ButtonType getType()
{
return ButtonType.BOOKING_SEARCH;
}
},
OPENTABLE
{
@Override
int getTitle()
public int getTitle()
{
return R.string.book_button;
}
@Override
int getIcon()
public int getIcon()
{
return R.drawable.ic_opentable;
}
},
PARTNER1
{
@NonNull
@Override
int getTitle()
public ButtonType getType()
{
return R.string.sponsored_partner1_action;
}
@Override
int getIcon()
{
return R.drawable.ic_24px_logo_partner1;
}
},
PARTNER3
{
@Override
int getTitle()
{
return R.string.sponsored_partner3_action;
}
@Override
int getIcon()
{
return R.drawable.ic_24px_logo_partner3;
return ButtonType.OPENTABLE;
}
},
BACK
{
@Override
int getTitle()
public int getTitle()
{
return R.string.back;
}
@Override
int getIcon()
public int getIcon()
{
return ThemeUtils.getResource(MwmApplication.get(), android.R.attr.homeAsUpIndicator);
}
@NonNull
@Override
public ButtonType getType()
{
return ButtonType.BACK;
}
},
BOOKMARK
{
@Override
int getTitle()
public int getTitle()
{
return R.string.bookmark;
}
@Override
int getIcon()
public int getIcon()
{
return R.drawable.ic_bookmarks_off;
}
@NonNull
@Override
public ButtonType getType()
{
return ButtonType.BOOKMARK;
}
},
ROUTE_FROM
{
@Override
int getTitle()
public int getTitle()
{
return R.string.p2p_from_here;
}
@Override
int getIcon()
public int getIcon()
{
return R.drawable.ic_route_from;
}
@NonNull
@Override
public ButtonType getType()
{
return ButtonType.ROUTE_FROM;
}
},
ROUTE_TO
{
@Override
int getTitle()
public int getTitle()
{
return R.string.p2p_to_here;
}
@Override
int getIcon()
public int getIcon()
{
return R.drawable.ic_route_to;
}
@NonNull
@Override
public ButtonType getType()
{
return ButtonType.ROUTE_TO;
}
},
ROUTE_ADD
{
@Override
int getTitle()
public int getTitle()
{
return R.string.placepage_add_stop;
}
@Override
int getIcon()
public int getIcon()
{
return R.drawable.ic_route_via;
}
@NonNull
@Override
public ButtonType getType()
{
return ButtonType.ROUTE_ADD;
}
},
ROUTE_REMOVE
{
@Override
int getTitle()
public int getTitle()
{
return R.string.placepage_remove_stop;
}
@Override
int getIcon()
public int getIcon()
{
return R.drawable.ic_route_remove;
}
@NonNull
@Override
public ButtonType getType()
{
return ButtonType.ROUTE_REMOVE;
}
},
SHARE
{
@Override
int getTitle()
public int getTitle()
{
return R.string.share;
}
@Override
int getIcon()
public int getIcon()
{
return R.drawable.ic_share;
}
@NonNull
@Override
public ButtonType getType()
{
return ButtonType.SHARE;
}
},
// Must not be used outside
MORE
{
@Override
int getTitle()
public int getTitle()
{
return R.string.placepage_more_button;
}
@Override
int getIcon()
public int getIcon()
{
return R.drawable.bs_ic_more;
}
@NonNull
@Override
public ButtonType getType()
{
return ButtonType.MORE;
}
},
CALL
{
@Override
int getTitle()
public int getTitle()
{
return R.string.placepage_call_button;
}
@Override
int getIcon()
public int getIcon()
{
return R.drawable.ic_place_page_phone;
}
@NonNull
@Override
public ButtonType getType()
{
return ButtonType.CALL;
}
};
@StringRes int getTitle()
@StringRes
public int getTitle()
{
throw new UnsupportedOperationException("Not supported!");
}
@DrawableRes int getIcon()
@DrawableRes
public int getIcon()
{
throw new UnsupportedOperationException("Not supported!");
}
}
private static final Map<Integer, Item> PARTNERS_ITEMS = new HashMap<Integer, Item>()
{{
put(1, Item.PARTNER1);
put(3, Item.PARTNER3);
}};
interface ItemListener
{
void onPrepareVisibleView(Item item, View frame, ImageView icon, TextView title);
void onItemClick(Item item);
void onPrepareVisibleView(ButtonInterface item, View frame, ImageView icon, TextView title);
void onItemClick(ButtonInterface item);
}
PlacePageButtons(PlacePageView placePage, ViewGroup frame, ItemListener itemListener)
@ -275,22 +407,23 @@ final class PlacePageButtons
mFrame = frame;
mItemListener = itemListener;
MAX_BUTTONS = mPlacePage.getContext().getResources().getInteger(R.integer.pp_buttons_max);
mMaxButtons = mPlacePage.getContext().getResources().getInteger(R.integer.pp_buttons_max);
}
@NonNull
static Item getPartnerItem(int partnerIndex)
static ButtonInterface getPartnerItem(int partnerIndex)
{
if (!PARTNERS_ITEMS.containsKey(partnerIndex))
ButtonInterface item = PARTNERS_ITEMS.get(partnerIndex);
if (item == null)
throw new AssertionError("Wrong partner index: " + partnerIndex);
return PARTNERS_ITEMS.get(partnerIndex);
return item;
}
private @NonNull List<Item> collectButtons(List<Item> items)
private @NonNull List<ButtonInterface> collectButtons(List<ButtonInterface> items)
{
List<Item> res = new ArrayList<>(items);
if (res.size() > MAX_BUTTONS)
res.add(MAX_BUTTONS - 1, Item.MORE);
List<ButtonInterface> res = new ArrayList<>(items);
if (res.size() > mMaxButtons)
res.add(mMaxButtons - 1, Item.MORE);
// Swap ROUTE_FROM and ROUTE_TO if the latter one was pressed out to bottomsheet
int from = res.indexOf(Item.ROUTE_FROM);
@ -298,10 +431,10 @@ final class PlacePageButtons
{
int addStop = res.indexOf(Item.ROUTE_ADD);
int to = res.indexOf(Item.ROUTE_TO);
if ((to > from && to >= MAX_BUTTONS) || (to > from && addStop >= MAX_BUTTONS))
if ((to > from && to >= mMaxButtons) || (to > from && addStop >= mMaxButtons))
Collections.swap(res, from, to);
if (addStop >= MAX_BUTTONS)
if (addStop >= mMaxButtons)
{
from = res.indexOf(Item.ROUTE_FROM);
if (addStop > from)
@ -313,14 +446,14 @@ final class PlacePageButtons
preserveRoutingButtons(res, Item.BOOKING_SEARCH);
from = res.indexOf(Item.ROUTE_FROM);
to = res.indexOf(Item.ROUTE_TO);
if (from < MAX_BUTTONS && from > to)
if (from < mMaxButtons && from > to)
Collections.swap(res, to, from);
}
return res;
}
private void preserveRoutingButtons(@NonNull List<Item> items, @NonNull Item itemToShift)
private void preserveRoutingButtons(@NonNull List<ButtonInterface> items, @NonNull Item itemToShift)
{
if (!RoutingController.get().isNavigating() && !RoutingController.get().isPlanning())
return;
@ -329,14 +462,14 @@ final class PlacePageButtons
if (pos > -1)
{
items.remove(pos);
items.add(MAX_BUTTONS, itemToShift);
items.add(mMaxButtons, itemToShift);
int to = items.indexOf(Item.ROUTE_TO);
if (items.indexOf(Item.ROUTE_ADD) > -1)
{
items.remove(Item.ROUTE_ADD);
items.remove(Item.ROUTE_FROM);
items.add(to + 1, Item.ROUTE_ADD);
items.add(MAX_BUTTONS, Item.ROUTE_FROM);
items.add(mMaxButtons, Item.ROUTE_FROM);
}
else
{
@ -346,12 +479,12 @@ final class PlacePageButtons
}
}
private void showPopup(final List<Item> buttons)
private void showPopup(final List<ButtonInterface> buttons)
{
BottomSheetHelper.Builder bs = new BottomSheetHelper.Builder(mPlacePage.getActivity());
for (int i = MAX_BUTTONS; i < buttons.size(); i++)
for (int i = mMaxButtons; i < buttons.size(); i++)
{
Item bsItem = buttons.get(i);
ButtonInterface bsItem = buttons.get(i);
bs.sheet(i, bsItem.getIcon(), bsItem.getTitle());
}
@ -368,7 +501,7 @@ final class PlacePageButtons
bs.tint().show();
}
private View createButton(final List<Item> items, final Item current)
private View createButton(final List<ButtonInterface> items, final ButtonInterface current)
{
View res = LayoutInflater.from(mPlacePage.getContext()).inflate(R.layout.place_page_button, mFrame, false);
@ -395,14 +528,14 @@ final class PlacePageButtons
return res;
}
void setItems(List<Item> items)
void setItems(List<ButtonInterface> items)
{
final List<Item> buttons = collectButtons(items);
final List<ButtonInterface> buttons = collectButtons(items);
if (buttons.equals(mPrevItems))
return;
mFrame.removeAllViews();
int count = Math.min(buttons.size(), MAX_BUTTONS);
int count = Math.min(buttons.size(), mMaxButtons);
for (int i = 0; i < count; i++)
mFrame.addView(createButton(buttons, buttons.get(i)));

View file

@ -107,6 +107,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Locale;
import static com.mapswithme.maps.widget.placepage.PlacePageButtons.Item.BOOKING;
import static com.mapswithme.util.statistics.Destination.EXTERNAL;
import static com.mapswithme.util.statistics.GalleryPlacement.PLACEPAGE;
import static com.mapswithme.util.statistics.Statistics.EventName.PP_HOTEL_DESCRIPTION_LAND;
@ -448,11 +449,11 @@ public class PlacePageView extends RelativeLayout
mButtons = new PlacePageButtons(this, ppButtons, new PlacePageButtons.ItemListener()
{
@Override
public void onPrepareVisibleView(PlacePageButtons.Item item, View frame, ImageView icon, TextView title)
public void onPrepareVisibleView(PlacePageButtons.ButtonInterface item, View frame, ImageView icon, TextView title)
{
int color;
switch (item)
switch (item.getType())
{
case BOOKING:
frame.setBackgroundResource(R.drawable.button_booking);
@ -495,9 +496,9 @@ public class PlacePageView extends RelativeLayout
}
@Override
public void onItemClick(PlacePageButtons.Item item)
public void onItemClick(PlacePageButtons.ButtonInterface item)
{
switch (item)
switch (item.getType())
{
case BOOKMARK:
if (mMapObject == null)
@ -1591,7 +1592,7 @@ public class PlacePageView extends RelativeLayout
private void setButtons(@NonNull MapObject mapObject, boolean showBackButton, boolean showRoutingButton)
{
List<PlacePageButtons.Item> buttons = new ArrayList<>();
List<PlacePageButtons.ButtonInterface> buttons = new ArrayList<>();
if (RoutingController.get().isRoutePoint(mapObject))
{
buttons.add(PlacePageButtons.Item.ROUTE_REMOVE);
@ -1607,7 +1608,7 @@ public class PlacePageView extends RelativeLayout
switch (mSponsored.getType())
{
case Sponsored.TYPE_BOOKING:
buttons.add(PlacePageButtons.Item.BOOKING);
buttons.add(BOOKING);
break;
case Sponsored.TYPE_OPENTABLE:
buttons.add(PlacePageButtons.Item.OPENTABLE);

View file

@ -71,9 +71,9 @@ Mopub::Mopub()
AppendEntry({{"sponsored", "banner"}}, kSponsoredBannerPlacementId);
for (auto const & p : kPartners)
for (auto const & p : GetPartners())
{
auto const placementId = p.GetBannerPlacementId();
auto const & placementId = p.GetBannerPlacementId();
if (!placementId.empty())
AppendEntry({{"sponsored", p.m_type.c_str()}}, placementId);
}

View file

@ -46,7 +46,7 @@ PartnerInfo::PartnerInfo(int partnerIndex, std::string && name)
: PartnerInfo(partnerIndex, std::move(name), false /* hasButton */)
{}
std::string PartnerInfo::GetBannerPlacementId() const
std::string const & PartnerInfo::GetBannerPlacementId() const
{
#if defined(OMIM_OS_IPHONE)
return m_iosBannerPlacementId;
@ -77,6 +77,11 @@ int PartnerChecker::GetPartnerIndex(FeatureType const & ft) const
return kFakePartnerIndex;
}
std::vector<PartnerInfo> const & GetPartners()
{
return kPartners;
}
PartnerInfo const & GetPartnerByIndex(int partnerIndex)
{
auto const it = std::find_if(kPartners.cbegin(), kPartners.cend(),

View file

@ -20,7 +20,7 @@ struct PartnerInfo
PartnerInfo(int partnerIndex, std::string && name);
std::string GetBannerPlacementId() const;
std::string const & GetBannerPlacementId() const;
};
class PartnerChecker : public ftypes::BaseChecker
@ -34,6 +34,6 @@ public:
DECLARE_CHECKER_INSTANCE(PartnerChecker);
};
extern std::vector<PartnerInfo> const kPartners;
extern std::vector<PartnerInfo> const & GetPartners();
extern PartnerInfo const & GetPartnerByIndex(int partnerIndex);