[android] Simplify layers management

Signed-off-by: Arnaud Vergnet <arnaud.vergnet@mailo.com>
This commit is contained in:
Arnaud Vergnet 2022-03-14 13:41:34 +01:00 committed by Viktor Govako
parent 0d90ff0cf2
commit b3ba0d6a1c
19 changed files with 306 additions and 634 deletions

View file

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/nav_menu_height"
@ -8,27 +9,13 @@
android:theme="?navButtonsTheme">
<ImageButton
android:id="@+id/traffic"
android:id="@+id/layers_button"
style="@style/MwmWidget.MapButton.Layers"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:visibility="invisible"/>
<ImageButton
android:id="@+id/subway"
style="@style/MwmWidget.MapButton.Layers"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="?attr/nav_bg_subway"
android:visibility="invisible"/>
<ImageButton
android:id="@+id/isolines"
style="@style/MwmWidget.MapButton.Layers"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="?nav_bg_isolines"
android:visibility="invisible"/>
android:background="?toggleMapLayerBtnBg"
android:visibility="invisible"
tools:visibility="visible" />
<ImageButton
android:id="@+id/nav_zoom_in"

View file

@ -1,24 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_marginTop="@dimen/margin_direction_big"
android:layout_width="wrap_content"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:layout_marginTop="@dimen/margin_direction_big"
android:orientation="vertical">
<FrameLayout
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageButton
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/margin_quarter"
android:background="?attr/toggleMapLayerBtnBg"
android:background="@null"
android:minWidth="@dimen/margin_double_plus"
android:minHeight="@dimen/margin_double_plus" />
android:minHeight="@dimen/margin_double_plus"
tools:background="?attr/subwayMenuDisabled" />
<View
android:id="@+id/marker"
android:layout_width="@dimen/margin_half_plus_eight"
@ -29,15 +32,17 @@
android:visibility="gone"
tools:visibility="visible" />
</FrameLayout>
<TextView
android:id="@+id/name"
android:layout_marginTop="@dimen/margin_quarter"
android:maxLines="2"
android:ellipsize="end"
android:layout_gravity="center"
android:gravity="center"
android:textAppearance="@style/MwmTextAppearance.Body5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="@dimen/margin_quarter"
android:ellipsize="end"
android:gravity="center"
android:maxLines="2"
android:textAppearance="@style/MwmTextAppearance.Body5"
android:textColor="?attr/accentColorSelector"
android:layout_height="wrap_content"/>
tools:text="Layer Name" />
</LinearLayout>

View file

@ -17,23 +17,6 @@
android:visibility="invisible"
tools:visibility="visible" />
<ImageButton
android:id="@+id/traffic"
style="@style/MwmWidget.MapButton.Layers"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:visibility="invisible"
tools:src="@drawable/anim_traffic_loading" />
<ImageButton
android:id="@+id/subway"
style="@style/MwmWidget.MapButton.Layers"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:visibility="invisible"
android:background="?attr/nav_bg_subway"
tools:src="@drawable/anim_traffic_loading"/>
<ImageButton
android:id="@+id/my_position"
style="@style/MwmWidget.MapButton"

View file

@ -57,7 +57,7 @@ import com.mapswithme.maps.intent.Factory;
import com.mapswithme.maps.intent.MapTask;
import com.mapswithme.maps.location.CompassData;
import com.mapswithme.maps.location.LocationHelper;
import com.mapswithme.maps.maplayer.MapLayerCompositeController;
import com.mapswithme.maps.maplayer.MapLayersController;
import com.mapswithme.maps.maplayer.Mode;
import com.mapswithme.maps.maplayer.ToggleMapLayerFragment;
import com.mapswithme.maps.maplayer.isolines.IsolinesManager;
@ -188,7 +188,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
private NavigationButtonsAnimationController mNavAnimationController;
@SuppressWarnings("NullableProblems")
@NonNull
private MapLayerCompositeController mToggleMapLayerController;
private MapLayersController mToggleMapLayerController;
private boolean mIsTabletLayout;
private boolean mIsFullscreen;
@ -434,7 +434,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
private void initBottomSheets()
{
mToggleMapLayerFragment = new ToggleMapLayerFragment(this::onIsolinesLayerOptionSelected, this::onSubwayLayerOptionSelected);
mToggleMapLayerFragment = new ToggleMapLayerFragment(this::onLayerItemClicked);
mLayersBottomSheet = new MenuBottomSheetFragment(mToggleMapLayerFragment);
mMainMenuBottomSheet = new MenuBottomSheetFragment(mToggleMapLayerFragment, getMainMenuItems());
}
@ -591,7 +591,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
View mLayersButton = frame.findViewById(R.id.layers_button);
mToggleMapLayerController = new MapLayerCompositeController(mLayersButton, this::toggleMapLayerMenu,this);
mToggleMapLayerController = new MapLayersController(mLayersButton, this::toggleMapLayerMenu,this);
mNavAnimationController = new NavigationButtonsAnimationController(
zoomIn, zoomOut, myPosition, getWindow().getDecorView().getRootView(), this);
@ -1036,7 +1036,6 @@ public class MwmActivity extends BaseMwmFragmentActivity
{
super.onSafeDestroy();
mNavigationController.destroy();
mToggleMapLayerController.detachCore();
TrafficManager.INSTANCE.detachAll();
mPlacePageController.destroy();
}
@ -1197,7 +1196,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
mNavAnimationController.disappearZoomButtons();
if (mNavMyPosition != null)
mNavMyPosition.hide();
mToggleMapLayerController.hide();
mToggleMapLayerController.hideButton();
}
else
{
@ -1207,6 +1206,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
appearMenu(menu);
else
mIsAppearMenuLater = true;
mToggleMapLayerController.showButton();
}
}
@ -1214,7 +1214,6 @@ public class MwmActivity extends BaseMwmFragmentActivity
{
showLineFrame(true);
showNavMyPositionBtn();
mToggleMapLayerController.applyLastActiveMode();
}
private void showNavMyPositionBtn()
@ -1498,7 +1497,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
}
showAddStartOrFinishFrame(RoutingController.get(), false);
int width = UiUtils.dimen(context, R.dimen.panel_width);
adjustTraffic(width, UiUtils.getStatusBarHeight(context));
adjustLayersButton(width, UiUtils.getStatusBarHeight(context));
mNavigationController.adjustSearchButtons(width);
}
else
@ -1541,10 +1540,10 @@ public class MwmActivity extends BaseMwmFragmentActivity
return true;
}
});
adjustTraffic(0, offsetY);
adjustLayersButton(0, offsetY);
}
private void adjustTraffic(int offsetX, int offsetY)
private void adjustLayersButton(int offsetX, int offsetY)
{
mToggleMapLayerController.adjust(offsetX, offsetY);
}
@ -1779,7 +1778,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
if (RoutingController.get().isPlanning() || RoutingController.get().isNavigating())
{
if (UiUtils.isLandscape(this))
mToggleMapLayerController.hide();
mToggleMapLayerController.hideButton();
else
mNavigationController.fadeOutSearchButtons();
}
@ -2114,15 +2113,9 @@ public class MwmActivity extends BaseMwmFragmentActivity
shareMyLocation();
}
public void onSubwayLayerOptionSelected()
public void onLayerItemClicked(@NonNull Mode mode)
{
closeFloatingPanels();
toggleLayer(Mode.SUBWAY);
}
public void onIsolinesLayerOptionSelected()
{
closeFloatingPanels();
toggleLayer(Mode.ISOLINES);
toggleLayer(mode);
}
}

View file

@ -1,29 +0,0 @@
package com.mapswithme.maps.maplayer;
import android.content.Context;
import android.view.View;
import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import com.mapswithme.maps.R;
import com.mapswithme.maps.maplayer.isolines.IsolinesManager;
import com.mapswithme.util.Utils;
public abstract class AbstractIsoLinesClickListener extends DefaultClickListener
{
public AbstractIsoLinesClickListener(@NonNull LayersAdapter adapter)
{
super(adapter);
}
@CallSuper
@Override
public void onItemClickInternal(@NonNull View v, @NonNull BottomSheetItem item)
{
Context context = v.getContext();
if (IsolinesManager.from(context).shouldShowNotification())
Utils.showSnackbar(context, v.getRootView(), R.string.isolines_toast_zooms_1_10);
}
}

View file

@ -1,102 +0,0 @@
package com.mapswithme.maps.maplayer;
import android.content.Context;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import com.mapswithme.maps.R;
import com.mapswithme.util.ThemeUtils;
public abstract class BottomSheetItem
{
@DrawableRes
private final int mEnabledStateDrawableResId;
@DrawableRes
private final int mDisabledStateDrawableResId;
@StringRes
private final int mTitleResId;
@NonNull
private final Mode mMode;
BottomSheetItem(@DrawableRes int enabledStateDrawableResId,
@DrawableRes int disabledStateDrawableResId,
@StringRes int titleResId,
@NonNull Mode mode)
{
mEnabledStateDrawableResId = enabledStateDrawableResId;
mDisabledStateDrawableResId = disabledStateDrawableResId;
mTitleResId = titleResId;
mMode = mode;
}
@NonNull
public Mode getMode()
{
return mMode;
}
@DrawableRes
public int getEnabledStateDrawable()
{
return mEnabledStateDrawableResId;
}
@DrawableRes
public int getDisabledStateDrawable()
{
return mDisabledStateDrawableResId;
}
@StringRes
public int getTitle()
{
return mTitleResId;
}
public static class Subway extends BottomSheetItem
{
private Subway(int drawableResId, int disabledStateDrawableResId)
{
super(drawableResId, disabledStateDrawableResId, R.string.button_layer_subway, Mode.SUBWAY);
}
public static BottomSheetItem makeInstance(@NonNull Context mContext)
{
int disabled = ThemeUtils.getResource(mContext, R.attr.subwayMenuDisabled);
int enabled = ThemeUtils.getResource(mContext, R.attr.subwayMenuEnabled);
return new Subway(enabled, disabled);
}
}
public static class Traffic extends BottomSheetItem
{
private Traffic(int drawableResId, int disabledStateDrawableResId)
{
super(drawableResId, disabledStateDrawableResId, R.string.button_layer_traffic, Mode.TRAFFIC);
}
public static BottomSheetItem makeInstance(@NonNull Context mContext)
{
int disabled = ThemeUtils.getResource(mContext, R.attr.trafficMenuDisabled);
int enabled = ThemeUtils.getResource(mContext, R.attr.trafficMenuEnabled);
return new Traffic(enabled, disabled);
}
}
public static class Isolines extends BottomSheetItem
{
private Isolines(int drawableResId, int disabledStateDrawableResId)
{
super(drawableResId, disabledStateDrawableResId, R.string.button_layer_isolines, Mode.ISOLINES);
}
public static BottomSheetItem makeInstance(@NonNull Context mContext)
{
int disabled = ThemeUtils.getResource(mContext, R.attr.isoLinesMenuDisabled);
int enabled = ThemeUtils.getResource(mContext, R.attr.isoLinesMenuEnabled);
return new Isolines(enabled, disabled);
}
}
}

View file

@ -1,31 +0,0 @@
package com.mapswithme.maps.maplayer;
import android.view.View;
import androidx.annotation.NonNull;
import com.mapswithme.maps.adapter.OnItemClickListener;
import com.mapswithme.util.SharedPropertiesUtils;
public abstract class DefaultClickListener implements OnItemClickListener<BottomSheetItem>
{
@NonNull
private final LayersAdapter mAdapter;
public DefaultClickListener(@NonNull LayersAdapter adapter)
{
mAdapter = adapter;
}
@Override
public void onItemClick(@NonNull View v, @NonNull BottomSheetItem item)
{
Mode mode = item.getMode();
SharedPropertiesUtils.setLayerMarkerShownForLayerMode(v.getContext(), mode);
mode.toggle(v.getContext());
onItemClickInternal(v, item);
mAdapter.notifyDataSetChanged();
}
public abstract void onItemClickInternal(@NonNull View v, @NonNull BottomSheetItem item);
}

View file

@ -0,0 +1,96 @@
package com.mapswithme.maps.maplayer;
import android.content.Context;
import android.view.View;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import com.mapswithme.maps.R;
import com.mapswithme.maps.adapter.OnItemClickListener;
import com.mapswithme.util.ThemeUtils;
public class LayerBottomSheetItem
{
@DrawableRes
private final int mEnabledStateDrawableResId;
@DrawableRes
private final int mDisabledStateDrawableResId;
@StringRes
private final int mTitleResId;
@NonNull
private final Mode mMode;
@NonNull
private final OnItemClickListener<LayerBottomSheetItem> mItemClickListener;
LayerBottomSheetItem(@DrawableRes int enabledStateDrawableResId,
@DrawableRes int disabledStateDrawableResId,
@StringRes int titleResId,
@NonNull Mode mode,
@NonNull OnItemClickListener<LayerBottomSheetItem> itemClickListener)
{
mEnabledStateDrawableResId = enabledStateDrawableResId;
mDisabledStateDrawableResId = disabledStateDrawableResId;
mTitleResId = titleResId;
mMode = mode;
mItemClickListener = itemClickListener;
}
public static LayerBottomSheetItem create(@NonNull Context mContext, Mode mode, @NonNull OnItemClickListener<LayerBottomSheetItem> layerItemClickListener)
{
int disabledResource = R.attr.toggleMapLayerBtnBg;
int enabledResource = R.attr.toggleMapLayerBtnBg;
int buttonTextResource = R.string.layers_title;
switch (mode)
{
case SUBWAY:
disabledResource = R.attr.subwayMenuDisabled;
enabledResource = R.attr.subwayMenuEnabled;
buttonTextResource = R.string.button_layer_subway;
break;
case ISOLINES:
disabledResource = R.attr.isoLinesMenuDisabled;
enabledResource = R.attr.isoLinesMenuEnabled;
buttonTextResource = R.string.button_layer_isolines;
break;
case TRAFFIC:
disabledResource = R.attr.trafficMenuDisabled;
enabledResource = R.attr.trafficMenuEnabled;
buttonTextResource = R.string.button_layer_traffic;
break;
}
int disabled = ThemeUtils.getResource(mContext, disabledResource);
int enabled = ThemeUtils.getResource(mContext, enabledResource);
return new LayerBottomSheetItem(enabled, disabled, buttonTextResource, mode, layerItemClickListener);
}
@NonNull
public Mode getMode()
{
return mMode;
}
@DrawableRes
public int getEnabledStateDrawable()
{
return mEnabledStateDrawableResId;
}
@DrawableRes
public int getDisabledStateDrawable()
{
return mDisabledStateDrawableResId;
}
@StringRes
public int getTitle()
{
return mTitleResId;
}
public void onClick(@NonNull View v, @NonNull LayerBottomSheetItem item)
{
mItemClickListener.onItemClick(v, item);
}
}

View file

@ -11,8 +11,6 @@ import androidx.recyclerview.widget.RecyclerView;
import com.mapswithme.maps.R;
import com.mapswithme.maps.adapter.OnItemClickListener;
import java.util.Objects;
class LayerHolder extends RecyclerView.ViewHolder
{
@NonNull
@ -22,9 +20,9 @@ class LayerHolder extends RecyclerView.ViewHolder
@NonNull
final View mNewMarker;
@Nullable
BottomSheetItem mItem;
LayerBottomSheetItem mItem;
@Nullable
OnItemClickListener<BottomSheetItem> mListener;
OnItemClickListener<LayerBottomSheetItem> mListener;
LayerHolder(@NonNull View root)
{
@ -35,20 +33,9 @@ class LayerHolder extends RecyclerView.ViewHolder
mButton.setOnClickListener(this::onItemClicked);
}
@NonNull
public BottomSheetItem getItem()
public void onItemClicked(@NonNull View v)
{
return Objects.requireNonNull(mItem);
}
@NonNull
public OnItemClickListener<BottomSheetItem> getListener()
{
return Objects.requireNonNull(mListener);
}
private void onItemClicked(@NonNull View v)
{
getListener().onItemClick(v, getItem());
if (mListener != null && mItem != null)
mListener.onItemClick(v, mItem);
}
}

View file

@ -1,52 +1,43 @@
package com.mapswithme.maps.maplayer;
import android.content.Context;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import com.mapswithme.maps.R;
import com.mapswithme.maps.adapter.OnItemClickListener;
import com.mapswithme.util.SharedPropertiesUtils;
import com.mapswithme.util.UiUtils;
import java.util.List;
import java.util.Objects;
public class LayersAdapter extends RecyclerView.Adapter<LayerHolder>
{
@Nullable
private List<Pair<BottomSheetItem, OnItemClickListener<BottomSheetItem>>> mItems;
@NonNull
private final List<LayerBottomSheetItem> mItems;
public LayersAdapter()
public LayersAdapter(@NonNull List<LayerBottomSheetItem> items)
{
mItems = items;
}
public void setLayerModes(@NonNull List<Pair<BottomSheetItem, OnItemClickListener<BottomSheetItem>>> modes)
{
mItems = modes;
}
@NonNull
@Override
public LayerHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View root = inflater.inflate(R.layout.item_bottomsheet_dialog, parent, false);
View root = inflater.inflate(R.layout.item_layer, parent, false);
return new LayerHolder(root);
}
@Override
public void onBindViewHolder(LayerHolder holder, int position)
{
Objects.requireNonNull(mItems);
Context context = holder.itemView.getContext();
Pair<BottomSheetItem, OnItemClickListener<BottomSheetItem>> pair = mItems.get(position);
BottomSheetItem item = pair.first;
LayerBottomSheetItem item = mItems.get(position);
holder.mItem = item;
boolean isEnabled = item.getMode().isEnabled(context);
@ -55,17 +46,16 @@ public class LayersAdapter extends RecyclerView.Adapter<LayerHolder>
holder.mTitle.setSelected(isEnabled);
holder.mTitle.setText(item.getTitle());
boolean isNewLayer = SharedPropertiesUtils.shouldShowNewMarkerForLayerMode(context,
item.getMode());
item.getMode());
UiUtils.showIf(isNewLayer, holder.mNewMarker);
holder.mButton.setImageResource(isEnabled ? item.getEnabledStateDrawable()
: item.getDisabledStateDrawable());
holder.mListener = pair.second;
: item.getDisabledStateDrawable());
holder.mListener = item::onClick;
}
@Override
public int getItemCount()
{
Objects.requireNonNull(mItems);
return mItems.size();
}
}

View file

@ -1,34 +1,15 @@
package com.mapswithme.maps.maplayer;
import android.content.Context;
import android.util.Pair;
import androidx.annotation.NonNull;
import com.mapswithme.maps.adapter.OnItemClickListener;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
public class LayersUtils
{
@NonNull
public static List<Pair<BottomSheetItem, OnItemClickListener<BottomSheetItem>>> createItems(
@NonNull Context context, @NonNull OnItemClickListener<BottomSheetItem> subwayListener,
@NonNull OnItemClickListener<BottomSheetItem> trafficListener,
@NonNull OnItemClickListener<BottomSheetItem> isoLinesListener)
public static List<Mode> getAvailableLayers()
{
Pair<BottomSheetItem, OnItemClickListener<BottomSheetItem>> subway
= new Pair<>(BottomSheetItem.Subway.makeInstance(context), subwayListener);
/*
Pair<BottomSheetItem, OnItemClickListener<BottomSheetItem>> traffic
= new Pair<>(BottomSheetItem.Traffic.makeInstance(context), trafficListener);
*/
Pair<BottomSheetItem, OnItemClickListener<BottomSheetItem>> isoLines
= new Pair<>(BottomSheetItem.Isolines.makeInstance(context), isoLinesListener);
return Arrays.asList(/*traffic,*/ isoLines, subway);
List<Mode> availableLayers = new ArrayList<>();
availableLayers.add(Mode.ISOLINES);
availableLayers.add(Mode.SUBWAY);
return availableLayers;
}
}

View file

@ -1,190 +0,0 @@
package com.mapswithme.maps.maplayer;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.mapswithme.util.InputUtils;
import com.mapswithme.util.UiUtils;
import java.util.ArrayList;
import java.util.List;
public class MapLayerCompositeController implements MapLayerController
{
@NonNull
private final AppCompatActivity mActivity;
@NonNull
private final List<Mode> mLayers;
@NonNull
private Mode mCurrentLayer;
@NonNull
private final View mLayersButton;
@NonNull
OnShowMenuListener mOnshowMenuListener;
public MapLayerCompositeController(@NonNull View layersButton, @NonNull OnShowMenuListener onshowMenuListener, @NonNull AppCompatActivity activity)
{
mActivity = activity;
mLayersButton = layersButton;
mLayersButton.setOnClickListener(view -> onLayersButtonClick());
mOnshowMenuListener = onshowMenuListener;
mLayers = new ArrayList<>();
mLayers.add(Mode.SUBWAY);
mLayers.add(Mode.ISOLINES);
mCurrentLayer = getCurrentLayer();
toggleMode(mCurrentLayer);
}
public void toggleMode(@NonNull Mode mode)
{
toggleMode(mode, false);
}
private void toggleMode(@NonNull Mode mode, boolean animate)
{
setMasterController(mode);
showMasterController(animate);
boolean enabled = mode.isEnabled(mActivity);
if (enabled)
{
turnOn();
}
else
{
turnOff();
turnInitialMode();
}
}
private void turnInitialMode()
{
UiUtils.hide(mLayersButton);
mCurrentLayer = mLayers.iterator().next();
UiUtils.show(mLayersButton);
}
public void applyLastActiveMode()
{
toggleMode(mCurrentLayer, true);
}
@Override
public void attachCore()
{
// Do nothing
}
@Override
public void detachCore()
{
// Do nothing
}
private void setMasterController(@NonNull Mode mode)
{
for (Mode each : mLayers)
{
if (each == mode)
{
mCurrentLayer = each;
}
else
{
UiUtils.hide(mLayersButton);
each.setEnabled(mActivity, false);
}
}
}
private void showMasterController(boolean animate)
{
UiUtils.show(mLayersButton);
}
@NonNull
private Mode getCurrentLayer()
{
for (Mode each : mLayers)
{
if (each.isEnabled(mActivity))
return each;
}
return mLayers.iterator().next();
}
@Override
public void turnOn()
{
mLayersButton.setSelected(true);
mCurrentLayer.setEnabled(mActivity, true);
}
@Override
public void turnOff()
{
mLayersButton.setSelected(false);
mCurrentLayer.setEnabled(mActivity, false);
}
@Override
public void show()
{
UiUtils.show(mLayersButton);
}
@Override
public void showImmediately()
{
UiUtils.show(mLayersButton);
}
@Override
public void hide()
{
UiUtils.hide(mLayersButton);
}
@Override
public void hideImmediately()
{
UiUtils.hide(mLayersButton);
}
@Override
public void adjust(int offsetX, int offsetY)
{
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mLayersButton.getLayoutParams();
params.setMargins(offsetX, offsetY, 0, 0);
mLayersButton.setLayoutParams(params);
}
public void turnOnView(@NonNull Mode mode)
{
setMasterController(mode);
UiUtils.show(mLayersButton);
mLayersButton.setSelected(true);
}
private void onLayersButtonClick()
{
if (mCurrentLayer.isEnabled(mActivity))
{
Mode mode = getCurrentLayer();
turnOff();
toggleMode(mode);
} else
{
InputUtils.hideKeyboard(mActivity.getWindow().getDecorView());
mOnshowMenuListener.onShow();
}
}
public interface OnShowMenuListener
{
void onShow();
}
}

View file

@ -1,14 +0,0 @@
package com.mapswithme.maps.maplayer;
import com.mapswithme.maps.content.CoreDetachable;
public interface MapLayerController extends CoreDetachable
{
void turnOn();
void turnOff();
void show();
void showImmediately();
void hide();
void hideImmediately();
void adjust(int offsetX, int offsetY);
}

View file

@ -0,0 +1,107 @@
package com.mapswithme.maps.maplayer;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.mapswithme.util.UiUtils;
import java.util.List;
public class MapLayersController
{
@NonNull
private final AppCompatActivity mActivity;
@NonNull
private final List<Mode> mLayers;
@NonNull
private final View mLayersButton;
@NonNull
OnShowMenuListener mOnShowMenuListener;
@NonNull
private Mode mCurrentLayer;
public MapLayersController(@NonNull View layersButton, @NonNull OnShowMenuListener onShowMenuListener, @NonNull AppCompatActivity activity)
{
mActivity = activity;
mLayersButton = layersButton;
mLayersButton.setOnClickListener(view -> onLayersButtonClick());
mOnShowMenuListener = onShowMenuListener;
mLayers = LayersUtils.getAvailableLayers();
mCurrentLayer = getCurrentLayer();
initMode();
}
private void initMode()
{
setEnabled(mCurrentLayer.isEnabled(mActivity));
showButton();
}
@NonNull
private Mode getCurrentLayer()
{
for (Mode each : mLayers)
{
if (each.isEnabled(mActivity))
return each;
}
return mLayers.iterator().next();
}
private void setCurrentLayer(@NonNull Mode mode)
{
for (Mode each : mLayers)
{
if (each == mode)
mCurrentLayer = each;
else
each.setEnabled(mActivity, false);
}
}
private void setEnabled(boolean enabled)
{
mLayersButton.setSelected(enabled);
mCurrentLayer.setEnabled(mActivity, enabled);
}
private void onLayersButtonClick()
{
if (mCurrentLayer.isEnabled(mActivity))
setEnabled(false);
else
mOnShowMenuListener.onShow();
}
public void toggleMode(@NonNull Mode mode)
{
setCurrentLayer(mode);
showButton();
setEnabled(!mode.isEnabled(mActivity));
}
public void showButton()
{
UiUtils.show(mLayersButton);
}
public void hideButton()
{
UiUtils.hide(mLayersButton);
}
public void adjust(int offsetX, int offsetY)
{
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mLayersButton.getLayoutParams();
params.setMargins(offsetX, offsetY, 0, 0);
mLayersButton.setLayoutParams(params);
}
public interface OnShowMenuListener
{
void onShow();
}
}

View file

@ -25,14 +25,6 @@ public enum Mode
{
TrafficManager.INSTANCE.setEnabled(isEnabled);
}
@Override
public void toggle(@NonNull Context context)
{
TrafficManager.INSTANCE.toggle();
SubwayManager.from(context).setEnabled(false);
IsolinesManager.from(context).setEnabled(false);
}
},
SUBWAY
{
@ -47,14 +39,6 @@ public enum Mode
{
SubwayManager.from(context).setEnabled(isEnabled);
}
@Override
public void toggle(@NonNull Context context)
{
SubwayManager.from(context).toggle();
TrafficManager.INSTANCE.setEnabled(false);
IsolinesManager.from(context).setEnabled(false);
}
},
ISOLINES
@ -70,22 +54,12 @@ public enum Mode
{
IsolinesManager.from(context).setEnabled(isEnabled);
}
@Override
public void toggle(@NonNull Context context)
{
IsolinesManager.from(context).toggle();
TrafficManager.INSTANCE.setEnabled(false);
SubwayManager.from(context).setEnabled(false);
}
};
public abstract boolean isEnabled(@NonNull Context context);
public abstract void setEnabled(@NonNull Context context, boolean isEnabled);
public abstract void toggle(@NonNull Context context);
public boolean isNew(@NonNull Context context)
{
return SharedPropertiesUtils.shouldShowNewMarkerForLayerMode(context, this);

View file

@ -1,5 +1,6 @@
package com.mapswithme.maps.maplayer;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
@ -12,21 +13,24 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.mapswithme.maps.R;
import com.mapswithme.maps.maplayer.isolines.IsolinesManager;
import com.mapswithme.maps.widget.recycler.SpanningLinearLayoutManager;
import com.mapswithme.util.SharedPropertiesUtils;
import com.mapswithme.util.Utils;
import java.util.ArrayList;
import java.util.List;
public class ToggleMapLayerFragment extends Fragment
{
@NonNull
private final LayerItemClickListener mIsolinesListener;
@NonNull
private final LayerItemClickListener mSubwayListener;
@NonNull
private final LayerItemClickListener mLayerItemClickListener;
@Nullable
private LayersAdapter mAdapter;
public ToggleMapLayerFragment(@NonNull LayerItemClickListener isolinesListener, @NonNull LayerItemClickListener subwayListener)
public ToggleMapLayerFragment(@NonNull LayerItemClickListener layerItemClickListener)
{
this.mIsolinesListener = isolinesListener;
this.mSubwayListener = subwayListener;
mLayerItemClickListener = layerItemClickListener;
}
@Nullable
@ -42,61 +46,38 @@ public class ToggleMapLayerFragment extends Fragment
{
RecyclerView recycler = root.findViewById(R.id.recycler);
RecyclerView.LayoutManager layoutManager = new SpanningLinearLayoutManager(requireContext(),
LinearLayoutManager.HORIZONTAL,
false);
LinearLayoutManager.HORIZONTAL,
false);
recycler.setLayoutManager(layoutManager);
mAdapter = new LayersAdapter();
mAdapter.setLayerModes(LayersUtils.createItems(requireContext(),
new SubwayItemClickListener(),
new TrafficItemClickListener(),
new IsolinesItemClickListener()));
mAdapter = new LayersAdapter(getLayersItems());
recycler.setAdapter(mAdapter);
recycler.setNestedScrollingEnabled(false);
}
private List<LayerBottomSheetItem> getLayersItems()
{
List<Mode> availableLayers = LayersUtils.getAvailableLayers();
List<LayerBottomSheetItem> items = new ArrayList<>();
for (Mode layer : availableLayers)
{
items.add(LayerBottomSheetItem.create(requireContext(), layer, this::onItemClick));
}
return items;
}
private void onItemClick(@NonNull View v, @NonNull LayerBottomSheetItem item)
{
Mode mode = item.getMode();
Context context = v.getContext();
SharedPropertiesUtils.setLayerMarkerShownForLayerMode(context, mode);
mAdapter.notifyDataSetChanged();
if (IsolinesManager.from(context).shouldShowNotification())
Utils.showSnackbar(context, v.getRootView(), R.string.isolines_toast_zooms_1_10);
mLayerItemClickListener.onClick(mode);
}
public interface LayerItemClickListener
{
void onClick();
}
private class SubwayItemClickListener extends DefaultClickListener
{
private SubwayItemClickListener()
{
super(mAdapter);
}
@Override
public void onItemClickInternal(@NonNull View v, @NonNull BottomSheetItem item)
{
mSubwayListener.onClick();
}
}
private class TrafficItemClickListener extends DefaultClickListener
{
private TrafficItemClickListener()
{
super(mAdapter);
}
@Override
public void onItemClickInternal(@NonNull View v, @NonNull BottomSheetItem item)
{}
}
private class IsolinesItemClickListener extends AbstractIsoLinesClickListener
{
private IsolinesItemClickListener()
{
super(mAdapter);
}
@Override
public void onItemClickInternal(@NonNull View v, @NonNull BottomSheetItem item)
{
super.onItemClickInternal(v, item);
mIsolinesListener.onClick();
}
void onClick(@NonNull Mode mode);
}
}

View file

@ -9,11 +9,10 @@ import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import com.mapswithme.maps.R;
import com.mapswithme.maps.maplayer.MapLayerController;
import com.mapswithme.maps.maplayer.traffic.TrafficManager;
import com.mapswithme.util.Utils;
public class TrafficButtonController implements TrafficManager.TrafficCallback, MapLayerController
public class TrafficButtonController implements TrafficManager.TrafficCallback
{
@NonNull
private final TrafficButton mButton;
@ -35,61 +34,51 @@ public class TrafficButtonController implements TrafficManager.TrafficCallback,
turnOn();
}
@Override
public void turnOn()
{
mButton.turnOn();
}
@Override
public void hideImmediately()
{
mButton.hideImmediately();
}
@Override
public void adjust(int offsetX, int offsetY)
{
mButton.setOffset(offsetX, offsetY);
}
@Override
public void attachCore()
{
TrafficManager.INSTANCE.attach(this);
}
@Override
public void detachCore()
{
destroy();
}
@Override
public void onDisabled()
{
turnOff();
}
@Override
public void turnOff()
{
mButton.turnOff();
}
@Override
public void show()
{
mButton.show();
}
@Override
public void showImmediately()
{
mButton.showImmediately();
}
@Override
public void hide()
{
mButton.hide();

View file

@ -58,16 +58,6 @@ public class MainMenu extends BaseMenu
return new MwmActivity.MenuClickDelegate(activity, item);
}
},
ADD_PLACE(R.id.add_place)
{
@NonNull
@Override
public ClickMenuDelegate createClickDelegate(@NonNull MwmActivity activity,
@NonNull Item item)
{
throw new UnsupportedOperationException("Main menu option doesn't support it!");
}
},
SEARCH(R.id.search)
{
@NonNull
@ -98,26 +88,6 @@ public class MainMenu extends BaseMenu
return new MwmActivity.BookmarksDelegate(activity, item);
}
},
SHARE_MY_LOCATION(R.id.share)
{
@NonNull
@Override
public ClickMenuDelegate createClickDelegate(@NonNull MwmActivity activity,
@NonNull Item item)
{
throw new UnsupportedOperationException("Main menu option doesn't support it!");
}
},
DOWNLOAD_MAPS(R.id.download_maps)
{
@NonNull
@Override
public ClickMenuDelegate createClickDelegate(@NonNull MwmActivity activity,
@NonNull Item item)
{
throw new UnsupportedOperationException("Main menu option doesn't support it!");
}
},
SETTINGS(R.id.settings)
{
@NonNull
@ -233,12 +203,9 @@ public class MainMenu extends BaseMenu
private void init()
{
mapItem(Item.ADD_PLACE);
mapItem(Item.SEARCH);
mapItem(Item.HELP);
mapItem(Item.BOOKMARKS);
mapItem(Item.SHARE_MY_LOCATION);
mapItem(Item.DOWNLOAD_MAPS);
mapItem(Item.SETTINGS);
setState(State.MENU, false);
@ -283,8 +250,6 @@ public class MainMenu extends BaseMenu
if (isRouting)
mToggle.hide();
}
setVisible(Item.ADD_PLACE, !isRouting);
}
show(state != State.NAVIGATION && !isFullScreen);

View file

@ -24,8 +24,8 @@ public class NavMenu extends BaseMenu
private final int mAnimationDuration;
@NonNull
private final ImageView mTts;
@NonNull
private final ImageView mTraffic;
// @NonNull
// private final ImageView mTraffic;
ImageView mToggle;
@ -44,8 +44,8 @@ public class NavMenu extends BaseMenu
TOGGLE(R.id.toggle),
TTS_VOLUME(R.id.tts_volume),
STOP(R.id.stop),
SETTINGS(R.id.settings),
TRAFFIC(R.id.traffic);
SETTINGS(R.id.settings);
// TRAFFIC(R.id.traffic);
private final int mViewId;
@ -94,7 +94,7 @@ public class NavMenu extends BaseMenu
mapItem(Item.SETTINGS, mFrame);
mTts = (ImageView) mapItem(Item.TTS_VOLUME, mFrame);
mTraffic = (ImageView) mapItem(Item.TRAFFIC, mFrame);
// mTraffic = (ImageView) mapItem(Item.TRAFFIC, mFrame);
}
@Override
@ -235,7 +235,7 @@ public class NavMenu extends BaseMenu
Drawable onIcon = Graphics.tint(mFrame.getContext(), R.drawable.ic_setting_traffic_on,
R.attr.colorAccent);
Drawable offIcon = Graphics.tint(mFrame.getContext(), R.drawable.ic_setting_traffic_off);
mTraffic.setImageDrawable(TrafficManager.INSTANCE.isEnabled() ? onIcon : offIcon);
// mTraffic.setImageDrawable(TrafficManager.INSTANCE.isEnabled() ? onIcon : offIcon);
}
@Override