Search in sources :

Example 1 with Undoable

use of net.osmand.plus.views.controls.SwipeDismissListViewTouchListener.Undoable in project Osmand by osmandapp.

the class DashboardOnMap method createDashboardView.

public void createDashboardView() {
    baseColor = ContextCompat.getColor(mapActivity, R.color.osmand_orange) & 0x00ffffff;
    waypointDialogHelper = new WaypointDialogHelper(mapActivity);
    waypointDialogHelper.setHelperCallbacks(this);
    landscape = !AndroidUiHelper.isOrientationPortrait(mapActivity);
    dashboardView = (FrameLayout) mapActivity.findViewById(R.id.dashboard);
    AndroidUtils.addStatusBarPadding21v(mapActivity, dashboardView);
    final View.OnClickListener listener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            hideDashboard();
        }
    };
    toolbar = ((Toolbar) dashboardView.findViewById(R.id.toolbar));
    ObservableScrollView scrollView = ((ObservableScrollView) dashboardView.findViewById(R.id.main_scroll));
    listView = (DynamicListView) dashboardView.findViewById(R.id.dash_list_view);
    // listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    listView.setDrawSelectorOnTop(true);
    listView.setDynamicListViewCallbacks(this);
    listEmptyTextView = (TextView) dashboardView.findViewById(R.id.emptyTextView);
    // Create a ListView-specific touch listener. ListViews are given special treatment because
    // by default they handle touches for their list items... i.e. they're in charge of drawing
    // the pressed state (the list selector), handling list item clicks, etc.
    swipeDismissListener = new SwipeDismissListViewTouchListener(mapActivity, listView, new DismissCallbacks() {

        @Override
        public boolean canDismiss(int position) {
            if (listAdapter instanceof StableArrayAdapter && (visibleType == DashboardType.WAYPOINTS || visibleType == DashboardType.WAYPOINTS_FLAT)) {
                List<Object> activeObjects = ((StableArrayAdapter) listAdapter).getActiveObjects();
                Object obj = listAdapter.getItem(position);
                if (obj instanceof LocationPointWrapper) {
                    LocationPointWrapper w = (LocationPointWrapper) obj;
                    if (w.getPoint() instanceof TargetPoint) {
                        return !((TargetPoint) w.getPoint()).start;
                    }
                }
                return activeObjects.contains(obj);
            }
            return false;
        }

        @Override
        public Undoable onDismiss(final int position) {
            final Object item;
            final StableArrayAdapter stableAdapter;
            final int activeObjPos;
            if (listAdapter instanceof StableArrayAdapter) {
                stableAdapter = (StableArrayAdapter) listAdapter;
                item = stableAdapter.getItem(position);
                stableAdapter.setNotifyOnChange(false);
                stableAdapter.remove(item);
                stableAdapter.getObjects().remove(item);
                activeObjPos = stableAdapter.getActiveObjects().indexOf(item);
                stableAdapter.getActiveObjects().remove(item);
                stableAdapter.refreshData();
                stableAdapter.notifyDataSetChanged();
            } else {
                item = null;
                stableAdapter = null;
                activeObjPos = 0;
            }
            return new Undoable() {

                @Override
                public void undo() {
                    if (item != null) {
                        stableAdapter.setNotifyOnChange(false);
                        stableAdapter.insert(item, position);
                        stableAdapter.getObjects().add(position, item);
                        stableAdapter.getActiveObjects().add(activeObjPos, item);
                        stableAdapter.refreshData();
                        if (visibleType == DashboardType.WAYPOINTS || visibleType == DashboardType.WAYPOINTS_FLAT) {
                            onItemsSwapped(stableAdapter.getActiveObjects());
                        }
                    }
                }

                @Override
                public String getTitle() {
                    List<Object> activeObjects;
                    if ((visibleType == DashboardType.WAYPOINTS || visibleType == DashboardType.WAYPOINTS_FLAT) && (getMyApplication().getRoutingHelper().isRoutePlanningMode() || getMyApplication().getRoutingHelper().isFollowingMode()) && item != null && ((activeObjects = stableAdapter.getActiveObjects()).isEmpty() || isContainsOnlyStart(activeObjects))) {
                        return mapActivity.getResources().getString(R.string.cancel_navigation);
                    } else {
                        return null;
                    }
                }
            };
        }

        @Override
        public void onHidePopup() {
            if (listAdapter instanceof StableArrayAdapter) {
                StableArrayAdapter stableAdapter = (StableArrayAdapter) listAdapter;
                stableAdapter.refreshData();
                if (visibleType == DashboardType.WAYPOINTS || visibleType == DashboardType.WAYPOINTS_FLAT) {
                    onItemsSwapped(stableAdapter.getActiveObjects());
                }
                List<Object> activeObjects = stableAdapter.getActiveObjects();
                if (activeObjects.isEmpty() || isContainsOnlyStart(activeObjects)) {
                    hideDashboard();
                    if (visibleType == DashboardType.WAYPOINTS || visibleType == DashboardType.WAYPOINTS_FLAT) {
                        mapActivity.getMapActions().stopNavigationWithoutConfirm();
                        getMyApplication().getTargetPointsHelper().removeAllWayPoints(false, true);
                        mapActivity.getMapLayers().getMapControlsLayer().getMapRouteInfoMenu().hide();
                    }
                }
            }
        }

        private boolean isContainsOnlyStart(List<Object> items) {
            if (items.size() == 1) {
                Object item = items.get(0);
                if (item instanceof LocationPointWrapper) {
                    LocationPointWrapper w = (LocationPointWrapper) item;
                    if (w.getPoint() instanceof TargetPoint) {
                        return ((TargetPoint) w.getPoint()).start;
                    }
                }
            }
            return false;
        }
    });
    gradientToolbar = ContextCompat.getDrawable(mapActivity, R.drawable.gradient_toolbar).mutate();
    if (AndroidUiHelper.isOrientationPortrait(mapActivity)) {
        this.portrait = true;
        scrollView.setScrollViewCallbacks(this);
        listView.setScrollViewCallbacks(this);
        mFlexibleSpaceImageHeight = mapActivity.getResources().getDimensionPixelSize(R.dimen.dashboard_map_top_padding);
        mFlexibleBlurSpaceHeight = mapActivity.getResources().getDimensionPixelSize(R.dimen.dashboard_map_toolbar);
        // Set padding view for ListView. This is the flexible space.
        paddingView = new FrameLayout(mapActivity);
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, mFlexibleSpaceImageHeight);
        paddingView.setLayoutParams(lp);
        // This is required to disable header's list selector effect
        paddingView.setClickable(true);
        paddingView.setOnClickListener(listener);
        FrameLayout shadowContainer = new FrameLayout(mapActivity);
        FrameLayout.LayoutParams fl = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        fl.gravity = Gravity.BOTTOM;
        shadowContainer.setLayoutParams(fl);
        ImageView shadow = new ImageView(mapActivity);
        shadow.setImageDrawable(ContextCompat.getDrawable(mapActivity, R.drawable.bg_shadow_onmap));
        shadow.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM));
        shadow.setScaleType(ScaleType.FIT_XY);
        shadowContainer.addView(shadow);
        ((FrameLayout) paddingView).addView(shadowContainer);
        listView.addHeaderView(paddingView);
        listBackgroundView = mapActivity.findViewById(R.id.dash_list_background);
    }
    dashboardView.findViewById(R.id.animateContent).setOnClickListener(listener);
    dashboardView.findViewById(R.id.map_part_dashboard).setOnClickListener(listener);
    initActionButtons();
    dashboardView.addView(actionButton);
}
Also used : Undoable(net.osmand.plus.views.controls.SwipeDismissListViewTouchListener.Undoable) StableArrayAdapter(net.osmand.plus.views.controls.StableArrayAdapter) ObservableScrollView(com.github.ksoichiro.android.observablescrollview.ObservableScrollView) WaypointDialogHelper(net.osmand.plus.helpers.WaypointDialogHelper) LocationPointWrapper(net.osmand.plus.helpers.WaypointHelper.LocationPointWrapper) AbsListView(android.widget.AbsListView) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) ImageView(android.widget.ImageView) DynamicListView(net.osmand.plus.views.controls.DynamicListView) OsmandMapTileView(net.osmand.plus.views.OsmandMapTileView) ScrollView(android.widget.ScrollView) ObservableScrollView(com.github.ksoichiro.android.observablescrollview.ObservableScrollView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) AbsListView(android.widget.AbsListView) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) SuppressLint(android.annotation.SuppressLint) SwipeDismissListViewTouchListener(net.osmand.plus.views.controls.SwipeDismissListViewTouchListener) FrameLayout(android.widget.FrameLayout) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) ImageView(android.widget.ImageView) DismissCallbacks(net.osmand.plus.views.controls.SwipeDismissListViewTouchListener.DismissCallbacks) Toolbar(android.support.v7.widget.Toolbar)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 Toolbar (android.support.v7.widget.Toolbar)1 View (android.view.View)1 AbsListView (android.widget.AbsListView)1 AdapterView (android.widget.AdapterView)1 FrameLayout (android.widget.FrameLayout)1 ImageView (android.widget.ImageView)1 ListView (android.widget.ListView)1 ScrollView (android.widget.ScrollView)1 TextView (android.widget.TextView)1 ObservableScrollView (com.github.ksoichiro.android.observablescrollview.ObservableScrollView)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 TargetPoint (net.osmand.plus.TargetPointsHelper.TargetPoint)1 WaypointDialogHelper (net.osmand.plus.helpers.WaypointDialogHelper)1 LocationPointWrapper (net.osmand.plus.helpers.WaypointHelper.LocationPointWrapper)1 OsmandMapTileView (net.osmand.plus.views.OsmandMapTileView)1 DynamicListView (net.osmand.plus.views.controls.DynamicListView)1 StableArrayAdapter (net.osmand.plus.views.controls.StableArrayAdapter)1