[android] Implemented building route from discovery screen

This commit is contained in:
Александр Зацепин 2017-12-05 21:13:48 +03:00 committed by Ilya Grechuhin
parent 2e626cc38d
commit d5b8cc5937
18 changed files with 363 additions and 197 deletions

View file

@ -11,49 +11,54 @@
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="@dimen/viator_product_width"
android:layout_height="wrap_content"
android:foreground="?clickableBackground">
<TextView
android:id="@+id/title"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/infoLayout"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_half_double_plus"
android:layout_marginLeft="@dimen/margin_half_plus"
android:layout_marginRight="@dimen/margin_half_plus"
style="@style/MwmWidget.Discovery.Item.Title"
android:textAppearance="@style/MwmTextAppearance.Discovery.Item.Title"
tools:text="Las Ramblas"/>
<TextView
android:id="@+id/subtitle"
android:layout_below="@id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_eighth"
android:layout_marginLeft="@dimen/margin_half_plus"
android:layout_marginRight="@dimen/margin_half_plus"
style="@style/MwmWidget.Discovery.Item.Subtitle"
android:textAppearance="@style/MwmTextAppearance.Body3"
tools:text="Attraction"/>
<TextView
android:id="@+id/distance"
android:layout_below="@id/subtitle"
android:layout_marginTop="@dimen/margin_eighth"
android:layout_marginLeft="@dimen/margin_half_plus"
android:layout_marginStart="@dimen/margin_half_plus"
android:layout_marginRight="@dimen/margin_half_plus"
android:layout_marginEnd="@dimen/margin_half_plus"
android:textAppearance="@style/MwmTextAppearance.Discovery.Item.Distance"
android:ellipsize="end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="950 m"/>
android:paddingBottom="@dimen/margin_half_double_plus"
android:foreground="?clickableBackground"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_half_double_plus"
android:layout_marginLeft="@dimen/margin_half_plus"
android:layout_marginRight="@dimen/margin_half_plus"
style="@style/MwmWidget.Discovery.Item.Title"
android:textAppearance="@style/MwmTextAppearance.Discovery.Item.Title"
tools:text="Las Ramblas"/>
<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_eighth"
android:layout_marginLeft="@dimen/margin_half_plus"
android:layout_marginRight="@dimen/margin_half_plus"
style="@style/MwmWidget.Discovery.Item.Subtitle"
android:textAppearance="@style/MwmTextAppearance.Body3"
tools:text="Attraction"/>
<TextView
android:id="@+id/distance"
android:layout_marginTop="@dimen/margin_eighth"
android:layout_marginLeft="@dimen/margin_half_plus"
android:layout_marginStart="@dimen/margin_half_plus"
android:layout_marginRight="@dimen/margin_half_plus"
android:layout_marginEnd="@dimen/margin_half_plus"
android:textAppearance="@style/MwmTextAppearance.Discovery.Item.Distance"
android:ellipsize="end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="950 m"/>
</LinearLayout>
<TextView
android:id="@+id/button"
android:layout_below="@id/distance"
android:foreground="?clickableBackground"
android:layout_below="@id/infoLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_half_double_plus"
android:textAppearance="@style/MwmTextAppearance.Discovery.Item.Button"
style="@style/MwmWidget.Discovery.Item.Button"
android:layout_gravity="bottom"

View file

@ -1,6 +1,7 @@
package com.mapswithme.maps;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
@ -142,7 +143,8 @@ public class MwmActivity extends BaseMwmFragmentActivity
private static final String STATE_MAP_OBJECT = "MapObject";
private static final String EXTRA_LOCATION_DIALOG_IS_ANNOYING = "LOCATION_DIALOG_IS_ANNOYING";
private static final int LOCATION_REQUEST = 1;
private static final int REQ_CODE_LOCATION_PERMISSION = 1;
private static final int REQ_CODE_DISCOVERY = 2;
// Map tasks that we run AFTER rendering initialized
private final Stack<MapTask> mTasks = new Stack<>();
@ -212,7 +214,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
if (!PermissionsUtils.isLocationGranted())
{
if (PermissionsUtils.isLocationExplanationNeeded(MwmActivity.this))
PermissionsUtils.requestLocationPermission(MwmActivity.this, LOCATION_REQUEST);
PermissionsUtils.requestLocationPermission(MwmActivity.this, REQ_CODE_LOCATION_PERMISSION);
else
Toast.makeText(MwmActivity.this, R.string.enable_location_services, Toast.LENGTH_SHORT)
.show();
@ -846,7 +848,8 @@ public class MwmActivity extends BaseMwmFragmentActivity
break;
case DISCOVERY:
DiscoveryActivity.start(MwmActivity.this);
Intent i = new Intent(MwmActivity.this, DiscoveryActivity.class);
startActivityForResult(i, REQ_CODE_DISCOVERY);
break;
case BOOKMARKS:
@ -1027,12 +1030,47 @@ public class MwmActivity extends BaseMwmFragmentActivity
mFilterController.onRestoreState(savedInstanceState);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK)
return;
switch (requestCode)
{
case REQ_CODE_DISCOVERY:
handleDiscoveryResult(data);
break;
}
}
private void handleDiscoveryResult(@NonNull Intent data)
{
final MapObject destination = data.getParcelableExtra(DiscoveryActivity
.EXTRA_DISCOVERY_OBJECT);
if (destination == null)
return;
addTask(new MapTask()
{
@Override
public boolean run(MwmActivity target)
{
RoutingController.get().setRouterType(Framework.ROUTER_TYPE_PEDESTRIAN);
RoutingController.get().prepare(true, destination);
return false;
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode != LOCATION_REQUEST || grantResults.length == 0)
if (requestCode != REQ_CODE_LOCATION_PERMISSION || grantResults.length == 0)
return;
PermissionsResult result = PermissionsUtils.computePermissionsResult(permissions, grantResults);

View file

@ -1,19 +1,12 @@
package com.mapswithme.maps.discovery;
import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import com.mapswithme.maps.base.BaseMwmFragmentActivity;
public class DiscoveryActivity extends BaseMwmFragmentActivity
{
public static void start(@NonNull Activity activity)
{
final Intent i = new Intent(activity, DiscoveryActivity.class);
activity.startActivity(i);
}
public static final String EXTRA_DISCOVERY_OBJECT = "extra_discovery_object";
@Override
protected Class<? extends Fragment> getFragmentClass()

View file

@ -1,6 +1,8 @@
package com.mapswithme.maps.discovery;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.MainThread;
@ -15,7 +17,11 @@ import android.view.ViewGroup;
import com.mapswithme.maps.R;
import com.mapswithme.maps.base.BaseMwmToolbarFragment;
import com.mapswithme.maps.gallery.GalleryAdapter;
import com.mapswithme.maps.bookmarks.data.FeatureId;
import com.mapswithme.maps.bookmarks.data.MapObject;
import com.mapswithme.maps.gallery.ItemSelectedListener;
import com.mapswithme.maps.gallery.Items;
import com.mapswithme.maps.gallery.impl.BaseItemSelectedListener;
import com.mapswithme.maps.gallery.impl.Factory;
import com.mapswithme.maps.search.SearchResult;
import com.mapswithme.maps.viator.ViatorProduct;
@ -32,7 +38,8 @@ public class DiscoveryFragment extends BaseMwmToolbarFragment implements UICallb
private static final int[] ITEM_TYPES = { DiscoveryParams.ITEM_TYPE_VIATOR,
DiscoveryParams.ITEM_TYPE_ATTRACTIONS,
DiscoveryParams.ITEM_TYPE_CAFES };
private static final GalleryAdapter.ItemSelectedListener LISTENER = new BaseItemSelectedListener();
@Nullable
private BaseItemSelectedListener<Items.Item> mDefaultListener;
private boolean mOnlineMode;
@Nullable
@ -99,6 +106,7 @@ public class DiscoveryFragment extends BaseMwmToolbarFragment implements UICallb
{
super.onViewCreated(view, savedInstanceState);
mToolbarController.setTitle(R.string.discovery_button_title);
mDefaultListener = new BaseItemSelectedListener<>(getActivity());
view.findViewById(R.id.viatorLogo).setOnClickListener(new View.OnClickListener()
{
@Override
@ -142,7 +150,7 @@ public class DiscoveryFragment extends BaseMwmToolbarFragment implements UICallb
// TODO: set loading adapter for local experts here.
RecyclerView thinsToDo = getGallery(R.id.thingsToDo);
thinsToDo.setAdapter(Factory.createViatorLoadingAdapter(DiscoveryManager.nativeGetViatorUrl(),
LISTENER));
mDefaultListener));
return;
}
@ -157,7 +165,8 @@ public class DiscoveryFragment extends BaseMwmToolbarFragment implements UICallb
{
UiUtils.show(getView(), R.id.thingsToDoLayout, R.id.thingsToDo);
RecyclerView thinsToDo = getGallery(R.id.thingsToDo);
thinsToDo.setAdapter(Factory.createViatorOfflineAdapter(new ViatorOfflineSelectedListener()));
thinsToDo.setAdapter(Factory.createViatorOfflineAdapter(new ViatorOfflineSelectedListener
(getActivity())));
}
}
@ -185,7 +194,8 @@ public class DiscoveryFragment extends BaseMwmToolbarFragment implements UICallb
if (results == null)
return;
getGallery(R.id.attractions).setAdapter(Factory.createSearchBasedAdapter(results, LISTENER));
ItemSelectedListener<Items.SearchItem> listener = new SearchBasedListener(getActivity());
getGallery(R.id.attractions).setAdapter(Factory.createSearchBasedAdapter(results, listener));
}
@MainThread
@ -195,7 +205,8 @@ public class DiscoveryFragment extends BaseMwmToolbarFragment implements UICallb
if (results == null)
return;
getGallery(R.id.food).setAdapter(Factory.createSearchBasedAdapter(results, LISTENER));
ItemSelectedListener<Items.SearchItem> listener = new SearchBasedListener(getActivity());
getGallery(R.id.food).setAdapter(Factory.createSearchBasedAdapter(results, listener));
}
@MainThread
@ -206,7 +217,8 @@ public class DiscoveryFragment extends BaseMwmToolbarFragment implements UICallb
return;
String url = DiscoveryManager.nativeGetViatorUrl();
getGallery(R.id.thingsToDo).setAdapter(Factory.createViatorAdapter(products, url, LISTENER));
ItemSelectedListener<Items.ViatorItem> listener = new BaseItemSelectedListener<>(getActivity());
getGallery(R.id.thingsToDo).setAdapter(Factory.createViatorAdapter(products, url, listener));
}
@MainThread
@ -223,7 +235,7 @@ public class DiscoveryFragment extends BaseMwmToolbarFragment implements UICallb
{
case VIATOR:
String url = DiscoveryManager.nativeGetViatorUrl();
getGallery(R.id.thingsToDo).setAdapter(Factory.createViatorErrorAdapter(url, LISTENER));
getGallery(R.id.thingsToDo).setAdapter(Factory.createViatorErrorAdapter(url, mDefaultListener));
break;
case ATTRACTIONS:
getGallery(R.id.attractions).setAdapter(Factory.createSearchBasedErrorAdapter());
@ -239,37 +251,45 @@ public class DiscoveryFragment extends BaseMwmToolbarFragment implements UICallb
}
}
private static class BaseItemSelectedListener implements GalleryAdapter.ItemSelectedListener
private static class ViatorOfflineSelectedListener extends BaseItemSelectedListener<Items.Item>
{
@Override
public void onItemSelected(@NonNull Context context, @NonNull String url)
private ViatorOfflineSelectedListener(@NonNull Activity context)
{
Utils.openUrl(context, url);
super(context);
}
@Override
public void onMoreItemSelected(@NonNull Context context, @NonNull String url)
public void onActionButtonSelected(@NonNull Items.Item item)
{
Utils.openUrl(context, url);
}
@Override
public void onDetailsSelected(@NonNull Context context, @Nullable String url)
{
if (TextUtils.isEmpty(url))
return;
Utils.openUrl(context, url);
Utils.showWirelessSettings(getContext());
}
}
private static class ViatorOfflineSelectedListener extends BaseItemSelectedListener
private static class SearchBasedListener extends BaseItemSelectedListener<Items.SearchItem>
{
@Override
public void onDetailsSelected(@NonNull Context context, @Nullable String url)
private SearchBasedListener(@NonNull Activity context)
{
Utils.showWirelessSettings(context);
super(context);
}
@Override
public void onItemSelected(@NonNull Items.SearchItem item)
{
// TODO: Show point on the map. Coming soon.
}
@Override
public void onActionButtonSelected(@NonNull Items.SearchItem item)
{
Intent intent = new Intent();
String title = TextUtils.isEmpty(item.getTitle()) ? "" : item.getTitle();
String subtitle = TextUtils.isEmpty(item.getSubtitle()) ? "" : item.getSubtitle();
MapObject poi = MapObject.createMapObject(FeatureId.EMPTY, MapObject.SEARCH, title, subtitle,
item.getLat(), item.getLon());
intent.putExtra(DiscoveryActivity.EXTRA_DISCOVERY_OBJECT, poi);
getContext().setResult(Activity.RESULT_OK, intent);
getContext().finish();
}
}
}

View file

@ -12,7 +12,8 @@ public abstract class AdapterStrategy<VH extends Holders.BaseViewHolder<I>, I ex
protected final List<I> mItems = new ArrayList<>();
@NonNull
abstract VH createViewHolder(@NonNull ViewGroup parent, int viewType, @NonNull GalleryAdapter adapter);
abstract VH createViewHolder(@NonNull ViewGroup parent, int viewType,
@NonNull GalleryAdapter<?, I> adapter);
protected abstract void onBindViewHolder(Holders.BaseViewHolder<I> holder, int position);

View file

@ -1,6 +1,5 @@
package com.mapswithme.maps.gallery;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
@ -12,20 +11,21 @@ public class GalleryAdapter<VH extends Holders.BaseViewHolder<I>, I extends Item
@NonNull
private final AdapterStrategy<VH, I> mStrategy;
@Nullable
private ItemSelectedListener mListener;
@Nullable
public ItemSelectedListener getListener()
{
return mListener;
}
private final ItemSelectedListener<I> mListener;
public GalleryAdapter(@NonNull AdapterStrategy<VH, I> strategy,
@Nullable ItemSelectedListener listener)
@Nullable ItemSelectedListener<I> listener)
{
mStrategy = strategy;
mListener = listener;
}
@Nullable
public ItemSelectedListener<I> getListener()
{
return mListener;
}
@Override
public VH onCreateViewHolder(ViewGroup parent, int viewType)
{
@ -49,11 +49,4 @@ public class GalleryAdapter<VH extends Holders.BaseViewHolder<I>, I extends Item
{
return mStrategy.getItemViewType(position);
}
public interface ItemSelectedListener
{
void onItemSelected(@NonNull Context context, @NonNull String url);
void onMoreItemSelected(@NonNull Context context, @NonNull String url);
void onDetailsSelected(@NonNull Context context, @Nullable String url);
}
}

View file

@ -40,7 +40,7 @@ public class Holders
Context mContext;
public ViatorProductViewHolder(@NonNull View itemView, @NonNull List<ViatorItem> items,
@NonNull GalleryAdapter adapter)
@NonNull GalleryAdapter<?, ViatorItem> adapter)
{
super(itemView, items, adapter);
mContext = itemView.getContext();
@ -72,13 +72,13 @@ public class Holders
}
@Override
protected void onItemSelected(@NonNull Context context, @NonNull ViatorItem item)
protected void onItemSelected(@NonNull ViatorItem item)
{
GalleryAdapter.ItemSelectedListener listener = mAdapter.getListener();
ItemSelectedListener<ViatorItem> listener = mAdapter.getListener();
if (listener == null || TextUtils.isEmpty(item.getUrl()))
return;
listener.onItemSelected(context, item.getUrl());
listener.onItemSelected(item);
}
}
@ -86,19 +86,19 @@ public class Holders
{
public ViatorMoreItemViewHolder(@NonNull View itemView, @NonNull List<Items.ViatorItem> items,
@NonNull GalleryAdapter adapter)
@NonNull GalleryAdapter<?, Items.ViatorItem> adapter)
{
super(itemView, items, adapter);
}
@Override
protected void onItemSelected(@NonNull Context context, @NonNull Items.ViatorItem item)
protected void onItemSelected(@NonNull Items.ViatorItem item)
{
GalleryAdapter.ItemSelectedListener listener = mAdapter.getListener();
ItemSelectedListener<ViatorItem> listener = mAdapter.getListener();
if (listener == null || TextUtils.isEmpty(item.getUrl()))
return;
listener.onMoreItemSelected(context, item.getUrl());
listener.onMoreItemSelected(item);
}
}
@ -109,7 +109,8 @@ public class Holders
@NonNull
TextView mAddress;
public CianProductViewHolder(@NonNull View itemView, @NonNull List<CianItem> items, @NonNull GalleryAdapter adapter)
public CianProductViewHolder(@NonNull View itemView, @NonNull List<CianItem> items,
@NonNull GalleryAdapter<?, CianItem> adapter)
{
super(itemView, items, adapter);
mPrice = (TextView) itemView.findViewById(R.id.tv__price);
@ -129,19 +130,19 @@ public class Holders
{
public CianMoreItemViewHolder(@NonNull View itemView, @NonNull List<T> items,
@NonNull GalleryAdapter adapter)
@NonNull GalleryAdapter<?, T> adapter)
{
super(itemView, items, adapter);
}
@Override
protected void onItemSelected(@NonNull Context context, @NonNull T item)
protected void onItemSelected(@NonNull T item)
{
GalleryAdapter.ItemSelectedListener listener = mAdapter.getListener();
ItemSelectedListener<T> listener = mAdapter.getListener();
if (listener == null || TextUtils.isEmpty(item.getUrl()))
return;
listener.onMoreItemSelected(context, item.getUrl());
listener.onMoreItemSelected(item);
}
}
@ -155,13 +156,15 @@ public class Holders
private final TextView mButton;
public SearchViewHolder(@NonNull View itemView, @NonNull List<Items.SearchItem> items,
@NonNull GalleryAdapter adapter)
@NonNull GalleryAdapter<?, Items.SearchItem> adapter)
{
super(itemView, items, adapter);
mTitle = (TextView) itemView.findViewById(R.id.title);
mSubtitle = (TextView) itemView.findViewById(R.id.subtitle);
mDistance = (TextView) itemView.findViewById(R.id.distance);
mButton = (TextView) itemView.findViewById(R.id.button);
mButton.setOnClickListener(this);
itemView.findViewById(R.id.infoLayout).setOnClickListener(this);
mButton.setText(R.string.p2p_to_here);
}
@ -173,6 +176,29 @@ public class Holders
UiUtils.setTextAndHideIfEmpty(mSubtitle, item.getSubtitle());
UiUtils.setTextAndHideIfEmpty(mDistance, item.getDistance());
}
@Override
public void onClick(View v)
{
int position = getAdapterPosition();
if (position == RecyclerView.NO_POSITION || mItems.isEmpty())
return;
ItemSelectedListener<Items.SearchItem> listener = mAdapter.getListener();
if (listener == null)
return;
Items.SearchItem item = mItems.get(position);
switch (v.getId())
{
case R.id.infoLayout:
listener.onItemSelected(item);
break;
case R.id.button:
listener.onActionButtonSelected(item);
break;
}
}
}
public static class BaseViewHolder<I extends Items.Item> extends RecyclerView.ViewHolder
@ -183,10 +209,10 @@ public class Holders
@NonNull
protected List<I> mItems;
@NonNull
GalleryAdapter mAdapter;
GalleryAdapter<?, I> mAdapter;
BaseViewHolder(@NonNull View itemView, @NonNull List<I> items,
@NonNull GalleryAdapter adapter)
@NonNull GalleryAdapter<?, I> adapter)
{
super(itemView);
mTitle = (TextView) itemView.findViewById(R.id.tv__title);
@ -208,10 +234,10 @@ public class Holders
if (position == RecyclerView.NO_POSITION || mItems.isEmpty())
return;
onItemSelected(mTitle.getContext(), mItems.get(position));
onItemSelected(mItems.get(position));
}
protected void onItemSelected(@NonNull Context context, @NonNull I item)
protected void onItemSelected(@NonNull I item)
{
}
}
@ -226,7 +252,8 @@ public class Holders
@NonNull
TextView mMore;
LoadingViewHolder(@NonNull View itemView, @NonNull List<Items.Item> items, @NonNull GalleryAdapter adapter)
LoadingViewHolder(@NonNull View itemView, @NonNull List<Items.Item> items,
@NonNull GalleryAdapter<?, Items.Item> adapter)
{
super(itemView, items, adapter);
mProgressBar = (ProgressBar) itemView.findViewById(R.id.pb__progress);
@ -249,23 +276,23 @@ public class Holders
if (position == RecyclerView.NO_POSITION)
return;
onItemSelected(mProgressBar.getContext(), mItems.get(position));
onItemSelected(mItems.get(position));
}
@Override
protected void onItemSelected(@NonNull Context context, @NonNull Items.Item item)
protected void onItemSelected(@NonNull Items.Item item)
{
if (mAdapter.getListener() == null || TextUtils.isEmpty(item.getUrl()))
return;
mAdapter.getListener().onDetailsSelected(context, item.getUrl());
mAdapter.getListener().onActionButtonSelected(item);
}
}
public static class SimpleViewHolder extends BaseViewHolder<Items.Item>
{
public SimpleViewHolder(@NonNull View itemView, @NonNull List<Items.Item> items,
@NonNull GalleryAdapter adapter)
@NonNull GalleryAdapter<?, Items.Item> adapter)
{
super(itemView, items, adapter);
mTitle = (TextView) itemView.findViewById(R.id.message);
@ -275,7 +302,8 @@ public class Holders
static class ErrorViewHolder extends LoadingViewHolder
{
ErrorViewHolder(@NonNull View itemView, @NonNull List<Items.Item> items, @NonNull GalleryAdapter adapter)
ErrorViewHolder(@NonNull View itemView, @NonNull List<Items.Item> items,
@NonNull GalleryAdapter<?, Items.Item> adapter)
{
super(itemView, items, adapter);
UiUtils.hide(mProgressBar);
@ -285,7 +313,7 @@ public class Holders
public static class OfflineViewHolder extends LoadingViewHolder
{
OfflineViewHolder(@NonNull View itemView, @NonNull List<Items.Item> items,
@NonNull GalleryAdapter adapter)
@NonNull GalleryAdapter<?, Items.Item> adapter)
{
super(itemView, items, adapter);
UiUtils.hide(mProgressBar);
@ -300,12 +328,12 @@ public class Holders
}
@Override
protected void onItemSelected(@NonNull Context context, @NonNull Items.Item item)
protected void onItemSelected(@NonNull Items.Item item)
{
if (mAdapter.getListener() == null)
return;
mAdapter.getListener().onDetailsSelected(mTitle.getContext(), item.getUrl());
mAdapter.getListener().onActionButtonSelected(item);
}
}
}

View file

@ -0,0 +1,12 @@
package com.mapswithme.maps.gallery;
import android.support.annotation.NonNull;
public interface ItemSelectedListener<I>
{
void onItemSelected(@NonNull I item);
void onMoreItemSelected(@NonNull I item);
void onActionButtonSelected(@NonNull I item);
}

View file

@ -5,6 +5,7 @@ import android.support.annotation.Nullable;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.R;
import com.mapswithme.maps.search.SearchResult;
import static com.mapswithme.maps.gallery.Constants.TYPE_MORE;
import static com.mapswithme.maps.gallery.Constants.TYPE_PRODUCT;
@ -77,19 +78,29 @@ public class Items
public static class SearchItem extends RegularAdapterStrategy.Item
{
@NonNull
private final String mDistance;
private final SearchResult mResult;
public SearchItem(@NonNull String title, @Nullable String url,
@Nullable String subtitle, @NonNull String distance)
public SearchItem(@NonNull SearchResult result)
{
super(TYPE_PRODUCT, title, url, subtitle);
mDistance = distance;
super(TYPE_PRODUCT, result.name, null, result.description.featureType);
mResult = result;
}
@NonNull
public String getDistance()
{
return mDistance;
SearchResult.Description d = mResult.description;
return d != null ? d.distance : "";
}
public double getLat()
{
return mResult.lat;
}
public double getLon()
{
return mResult.lon;
}
}

View file

@ -30,7 +30,7 @@ public abstract class RegularAdapterStrategy<T extends RegularAdapterStrategy.It
@NonNull
@Override
Holders.BaseViewHolder<T> createViewHolder(@NonNull ViewGroup parent, int viewType,
@NonNull GalleryAdapter adapter)
@NonNull GalleryAdapter<?, T> adapter)
{
switch (viewType)
{
@ -59,11 +59,11 @@ public abstract class RegularAdapterStrategy<T extends RegularAdapterStrategy.It
@NonNull
protected abstract Holders.BaseViewHolder<T> createProductViewHodler(@NonNull ViewGroup parent,
int viewType,
@NonNull GalleryAdapter adapter);
@NonNull GalleryAdapter<?, T> adapter);
@NonNull
protected abstract Holders.BaseViewHolder<T> createMoreProductsViewHolder(@NonNull ViewGroup parent,
int viewType,
@NonNull GalleryAdapter adapter);
@NonNull GalleryAdapter<?, T> adapter);
public static class Item extends Items.Item
{

View file

@ -0,0 +1,44 @@
package com.mapswithme.maps.gallery.impl;
import android.app.Activity;
import android.support.annotation.NonNull;
import com.mapswithme.maps.gallery.ItemSelectedListener;
import com.mapswithme.maps.gallery.Items;
import com.mapswithme.util.Utils;
public class BaseItemSelectedListener<I extends Items.Item>
implements ItemSelectedListener<I>
{
@NonNull
private final Activity mContext;
public BaseItemSelectedListener(@NonNull Activity context)
{
mContext = context;
}
@NonNull
protected Activity getContext()
{
return mContext;
}
@Override
public void onItemSelected(@NonNull I item)
{
Utils.openUrl(mContext, item.getUrl());
}
@Override
public void onMoreItemSelected(@NonNull I item)
{
Utils.openUrl(mContext, item.getUrl());
}
@Override
public void onActionButtonSelected(@NonNull I item)
{
Utils.openUrl(mContext, item.getUrl());
}
}

View file

@ -35,9 +35,8 @@ class CianAdapterStrategy extends RegularAdapterStrategy<Items.CianItem>
@NonNull
@Override
protected Holders.BaseViewHolder<Items.CianItem> createProductViewHodler(@NonNull ViewGroup parent,
int viewType,
@NonNull GalleryAdapter adapter)
protected Holders.BaseViewHolder<Items.CianItem> createProductViewHodler
(@NonNull ViewGroup parent, int viewType, @NonNull GalleryAdapter<?, Items.CianItem> adapter)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cian_product, parent,
false);
@ -46,9 +45,8 @@ class CianAdapterStrategy extends RegularAdapterStrategy<Items.CianItem>
@NonNull
@Override
protected Holders.BaseViewHolder<Items.CianItem> createMoreProductsViewHolder(@NonNull ViewGroup parent,
int viewType,
@NonNull GalleryAdapter adapter)
protected Holders.BaseViewHolder<Items.CianItem> createMoreProductsViewHolder
(@NonNull ViewGroup parent, int viewType, @NonNull GalleryAdapter<?, Items.CianItem> adapter)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cian_more, parent,
false);

View file

@ -5,6 +5,8 @@ import android.support.annotation.Nullable;
import com.mapswithme.maps.cian.RentPlace;
import com.mapswithme.maps.gallery.GalleryAdapter;
import com.mapswithme.maps.gallery.ItemSelectedListener;
import com.mapswithme.maps.gallery.Items;
import com.mapswithme.maps.search.SearchResult;
import com.mapswithme.maps.viator.ViatorProduct;
@ -12,34 +14,38 @@ public class Factory
{
@NonNull
public static GalleryAdapter createViatorLoadingAdapter(@Nullable String cityUrl,
@Nullable GalleryAdapter.ItemSelectedListener listener)
@Nullable ItemSelectedListener<Items.Item>
listener)
{
return new GalleryAdapter<>(new ViatorLoadingAdapterStrategy(cityUrl), listener);
}
@NonNull
public static GalleryAdapter createViatorOfflineAdapter(@Nullable GalleryAdapter.ItemSelectedListener listener)
public static GalleryAdapter createViatorOfflineAdapter(@Nullable ItemSelectedListener<Items.Item> listener)
{
return new GalleryAdapter<>(new ViatorOfflineAdapterStrategy(null), listener);
}
@NonNull
public static GalleryAdapter createViatorErrorAdapter(@Nullable String url,
@Nullable GalleryAdapter.ItemSelectedListener listener)
@Nullable ItemSelectedListener<Items.Item>
listener)
{
return new GalleryAdapter<>(new ViatorErrorAdapterStrategy(url), listener);
}
@NonNull
public static GalleryAdapter createCianLoadingAdapter(@Nullable String url,
@Nullable GalleryAdapter.ItemSelectedListener listener)
@Nullable ItemSelectedListener<Items.Item>
listener)
{
return new GalleryAdapter<>(new CianLoadingAdapterStrategy(url), listener);
}
@NonNull
public static GalleryAdapter createCianErrorAdapter(@Nullable String url,
@Nullable GalleryAdapter.ItemSelectedListener listener)
@Nullable ItemSelectedListener<Items.Item>
listener)
{
return new GalleryAdapter<>(new CianErrorAdapterStrategy(url), listener);
}
@ -47,21 +53,23 @@ public class Factory
@NonNull
public static GalleryAdapter createViatorAdapter(@NonNull ViatorProduct[] products,
@Nullable String cityUrl,
@Nullable GalleryAdapter.ItemSelectedListener listener)
@Nullable ItemSelectedListener<Items.ViatorItem>
listener)
{
return new GalleryAdapter<>(new ViatorAdapterStrategy(products, cityUrl), listener);
}
@NonNull
public static GalleryAdapter createCianAdapter(@NonNull RentPlace[] products, @NonNull String url,
@Nullable GalleryAdapter.ItemSelectedListener listener)
@Nullable ItemSelectedListener<Items.CianItem> listener)
{
return new GalleryAdapter<>(new CianAdapterStrategy(products, url), listener);
}
@NonNull
public static GalleryAdapter createSearchBasedAdapter(@NonNull SearchResult[] results,
@Nullable GalleryAdapter.ItemSelectedListener listener)
@Nullable ItemSelectedListener<Items
.SearchItem> listener)
{
return new GalleryAdapter<>(new SearchBasedAdapterStrategy(results), listener);
}

View file

@ -31,9 +31,8 @@ class SearchBasedAdapterStrategy extends RegularAdapterStrategy<Items.SearchItem
@NonNull
@Override
protected Holders.BaseViewHolder<Items.SearchItem> createProductViewHodler(@NonNull ViewGroup parent,
int viewType,
@NonNull GalleryAdapter adapter)
protected Holders.BaseViewHolder<Items.SearchItem> createProductViewHodler
(@NonNull ViewGroup parent, int viewType, @NonNull GalleryAdapter<?, Items.SearchItem> adapter)
{
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_discovery_search, parent, false);
@ -42,9 +41,8 @@ class SearchBasedAdapterStrategy extends RegularAdapterStrategy<Items.SearchItem
@NonNull
@Override
protected final Holders.BaseViewHolder<Items.SearchItem> createMoreProductsViewHolder(@NonNull ViewGroup parent,
int viewType,
@NonNull GalleryAdapter adapter)
protected final Holders.BaseViewHolder<Items.SearchItem> createMoreProductsViewHolder
(@NonNull ViewGroup parent, int viewType, @NonNull GalleryAdapter<?, Items.SearchItem> adapter)
{
throw new UnsupportedOperationException("More item is not supported yet for this strategy!");
}
@ -54,10 +52,7 @@ class SearchBasedAdapterStrategy extends RegularAdapterStrategy<Items.SearchItem
{
List<Items.SearchItem> viewItems = new ArrayList<>();
for (SearchResult result : results)
{
SearchResult.Description d = result.description;
viewItems.add(new Items.SearchItem(result.name, null, d.featureType, d.distance));
}
viewItems.add(new Items.SearchItem(result));
return viewItems;
}
}

View file

@ -33,9 +33,8 @@ public class ViatorAdapterStrategy
@NonNull
@Override
protected Holders.BaseViewHolder<Items.ViatorItem> createProductViewHodler(@NonNull ViewGroup parent,
int viewType,
@NonNull GalleryAdapter adapter)
protected Holders.BaseViewHolder<Items.ViatorItem> createProductViewHodler
(@NonNull ViewGroup parent, int viewType, @NonNull GalleryAdapter<?, Items.ViatorItem> adapter)
{
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_viator_product, parent,
@ -45,8 +44,8 @@ public class ViatorAdapterStrategy
@NonNull
@Override
protected Holders.BaseViewHolder<Items.ViatorItem> createMoreProductsViewHolder(
@NonNull ViewGroup parent, int viewType, @NonNull GalleryAdapter adapter)
protected Holders.BaseViewHolder<Items.ViatorItem> createMoreProductsViewHolder
(@NonNull ViewGroup parent, int viewType, @NonNull GalleryAdapter<?, Items.ViatorItem> adapter)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_viator_more, parent,
false);

View file

@ -409,7 +409,7 @@ public class RoutingController implements TaxiManager.TaxiListener
prepare(canUseMyPositionAsStart, endPoint, false);
}
public void prepare(boolean canUseMyPositionAsStart, @Nullable MapObject endPoint, boolean fromApi)
public void prepare(boolean canUseMyPositionAsStart, @Nullable MapObject endPoint, boolean fromApi)
{
MapObject startPoint = canUseMyPositionAsStart ? LocationHelper.INSTANCE.getMyPosition() : null;
prepare(startPoint, endPoint, fromApi);

View file

@ -1,5 +1,6 @@
package com.mapswithme.maps.widget.placepage;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.res.Resources;
@ -63,6 +64,9 @@ import com.mapswithme.maps.editor.data.Timetable;
import com.mapswithme.maps.gallery.FullScreenGalleryActivity;
import com.mapswithme.maps.gallery.GalleryActivity;
import com.mapswithme.maps.gallery.Image;
import com.mapswithme.maps.gallery.ItemSelectedListener;
import com.mapswithme.maps.gallery.Items;
import com.mapswithme.maps.gallery.impl.BaseItemSelectedListener;
import com.mapswithme.maps.gallery.impl.Factory;
import com.mapswithme.maps.location.LocationHelper;
import com.mapswithme.maps.review.Review;
@ -121,7 +125,6 @@ public class PlacePageView extends RelativeLayout
EditBookmarkFragment.EditBookmarkListener,
BannerController.BannerListener,
Viator.ViatorListener,
com.mapswithme.maps.gallery.GalleryAdapter.ItemSelectedListener,
Cian.CianListener
{
private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
@ -226,6 +229,8 @@ public class PlacePageView extends RelativeLayout
private final NearbyAdapter mNearbyAdapter = new NearbyAdapter(this);
@NonNull
private final ReviewAdapter mReviewAdapter = new ReviewAdapter();
@NonNull
private final ItemSelectedListener<Items.Item> mDefaultGalleryItemListener;
// Downloader`s stuff
private DownloaderStatusIcon mDownloaderIcon;
@ -294,7 +299,7 @@ public class PlacePageView extends RelativeLayout
public PlacePageView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs);
mDefaultGalleryItemListener = new SponsoredItemSelectedListener<>((Activity) context);
mIsLatLonDms = MwmApplication.prefs().getBoolean(PREF_USE_DMS, false);
mGalleryAdapter = new com.mapswithme.maps.widget.placepage.GalleryAdapter(context);
mMarginBase = (int) getResources().getDimension(R.dimen.margin_base);
@ -834,7 +839,7 @@ public class PlacePageView extends RelativeLayout
public void onErrorReceived(int errorCode)
{
String url = mSponsored != null ? mSponsored.getUrl() : "";
mRvSponsoredProducts.setAdapter(Factory.createCianErrorAdapter(url, this));
mRvSponsoredProducts.setAdapter(Factory.createCianErrorAdapter(url, mDefaultGalleryItemListener));
Statistics.INSTANCE.trackSponsoredGalleryError(Sponsored.TYPE_CIAN, String.valueOf(errorCode));
}
@ -857,13 +862,16 @@ public class PlacePageView extends RelativeLayout
{
if (products.length == 0)
{
mRvSponsoredProducts.setAdapter(Factory.createViatorErrorAdapter(cityUrl, this));
mRvSponsoredProducts.setAdapter(Factory.createViatorErrorAdapter(cityUrl,
mDefaultGalleryItemListener));
Statistics.INSTANCE.trackSponsoredGalleryError(Sponsored.TYPE_VIATOR,
Statistics.ParamValue.NO_PRODUCTS);
}
else
{
mRvSponsoredProducts.setAdapter(Factory.createViatorAdapter(products, cityUrl, this));
ItemSelectedListener<Items.ViatorItem> listener
= new SponsoredItemSelectedListener<>(getActivity());
mRvSponsoredProducts.setAdapter(Factory.createViatorAdapter(products, cityUrl, listener));
}
}
@ -871,13 +879,14 @@ public class PlacePageView extends RelativeLayout
{
if (products.length == 0)
{
mRvSponsoredProducts.setAdapter(Factory.createCianErrorAdapter(url, this));
mRvSponsoredProducts.setAdapter(Factory.createCianErrorAdapter(url, mDefaultGalleryItemListener));
Statistics.INSTANCE.trackSponsoredGalleryError(Sponsored.TYPE_CIAN,
Statistics.ParamValue.NO_PRODUCTS);
}
else
{
mRvSponsoredProducts.setAdapter(Factory.createCianAdapter(products, url, this));
ItemSelectedListener<Items.CianItem> listener = new SponsoredItemSelectedListener<>(getActivity());
mRvSponsoredProducts.setAdapter(Factory.createCianAdapter(products, url, listener));
}
}
@ -897,13 +906,15 @@ public class PlacePageView extends RelativeLayout
private void showLoadingViatorProducts(@NonNull String id, @NonNull String cityUrl)
{
if (!Viator.hasCache(id))
mRvSponsoredProducts.setAdapter(Factory.createViatorLoadingAdapter(cityUrl, this));
mRvSponsoredProducts.setAdapter(Factory.createViatorLoadingAdapter(cityUrl,
mDefaultGalleryItemListener));
}
private void showLoadingCianProducts(@NonNull FeatureId id, @NonNull String url)
{
if (!Cian.hasCache(id))
mRvSponsoredProducts.setAdapter(Factory.createCianLoadingAdapter(url, this));
mRvSponsoredProducts.setAdapter(Factory.createCianLoadingAdapter(url,
mDefaultGalleryItemListener));
}
private void updateGallerySponsoredTitle(@Sponsored.SponsoredType int type)
@ -922,33 +933,6 @@ public class PlacePageView extends RelativeLayout
mRvSponsoredProducts.swapAdapter(null /* adapter */ , false);
}
@Override
public void onItemSelected(@NonNull Context context, @NonNull String url)
{
Utils.openUrl(context, url);
if (mSponsored != null)
Statistics.INSTANCE.trackSponsoredEvent(Statistics.EventName.PP_SPONSOR_ITEM_SELECTED,
mSponsored.getType());
}
@Override
public void onMoreItemSelected(@NonNull Context context, @NonNull String url)
{
Utils.openUrl(context, url);
if (mSponsored != null)
Statistics.INSTANCE.trackSponsoredEvent(Statistics.EventName.PP_SPONSOR_MORE_SELECTED,
mSponsored.getType());
}
@Override
public void onDetailsSelected(@NonNull Context context, @Nullable String url)
{
if (TextUtils.isEmpty(url))
return;
Utils.openUrl(context, url);
}
@Override
public void onLineCountCalculated(boolean grater)
{
@ -2178,4 +2162,38 @@ public class PlacePageView extends RelativeLayout
getPaddingRight(), mMarginBase);
}
}
private class SponsoredItemSelectedListener<I extends Items.Item>
extends BaseItemSelectedListener<I>
{
SponsoredItemSelectedListener(@NonNull Activity context)
{
super(context);
}
@Override
public void onItemSelected(@NonNull I item)
{
Utils.openUrl(getActivity(), item.getUrl());
if (mSponsored != null)
Statistics.INSTANCE.trackSponsoredEvent(Statistics.EventName.PP_SPONSOR_ITEM_SELECTED,
mSponsored.getType());
}
@Override
public void onMoreItemSelected(@NonNull I item)
{
Utils.openUrl(getActivity(), item.getUrl());
if (mSponsored != null)
Statistics.INSTANCE.trackSponsoredEvent(Statistics.EventName.PP_SPONSOR_MORE_SELECTED,
mSponsored.getType());
}
@Override
public void onActionButtonSelected(@NonNull I item)
{
Utils.openUrl(getActivity(), item.getUrl());
}
}
}

View file

@ -211,8 +211,11 @@ public class Utils
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.Url.TWITTER_MAPSME_HTTP)));
}
public static void openUrl(@NonNull Context activity, @NonNull String url)
public static void openUrl(@NonNull Context activity, @Nullable String url)
{
if (TextUtils.isEmpty(url))
return;
final Intent intent = new Intent(Intent.ACTION_VIEW);
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;