Search in sources :

Example 1 with ReorderItemTouchHelperCallback

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

the class EditProfilesFragment method onCreateView.

@Nullable
@Override
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    OsmandApplication app = requireMyApplication();
    if (savedInstanceState != null && savedInstanceState.containsKey(APP_MODES_ORDER_KEY) && savedInstanceState.containsKey(DELETED_APP_MODES_KEY)) {
        appModesOrders = (HashMap<String, Integer>) savedInstanceState.getSerializable(APP_MODES_ORDER_KEY);
        deletedModesKeys = savedInstanceState.getStringArrayList(DELETED_APP_MODES_KEY);
    } else {
        for (ApplicationMode mode : ApplicationMode.allPossibleValues()) {
            appModesOrders.put(mode.getStringKey(), mode.getOrder());
        }
    }
    nightMode = !app.getSettings().isLightContent();
    View mainView = UiUtilities.getInflater(getContext(), nightMode).inflate(R.layout.edit_arrangement_list_fragment, container, false);
    AppBarLayout appbar = mainView.findViewById(R.id.appbar);
    View toolbar = UiUtilities.getInflater(getContext(), nightMode).inflate(R.layout.global_preference_toolbar, container, false);
    appbar.addView(toolbar);
    ImageButton closeButton = mainView.findViewById(R.id.close_button);
    closeButton.setImageResource(R.drawable.ic_action_remove_dark);
    closeButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            FragmentActivity fragmentActivity = getActivity();
            if (fragmentActivity != null) {
                fragmentActivity.onBackPressed();
            }
        }
    });
    TextView toolbarTitle = mainView.findViewById(R.id.toolbar_title);
    toolbarTitle.setText(R.string.edit_profiles);
    RecyclerView recyclerView = mainView.findViewById(R.id.profiles_list);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    adapter = new EditProfilesAdapter(app);
    updateItems();
    final ItemTouchHelper touchHelper = new ItemTouchHelper(new ReorderItemTouchHelperCallback(adapter));
    touchHelper.attachToRecyclerView(recyclerView);
    adapter.setAdapterListener(new ProfilesAdapterListener() {

        private int fromPosition;

        private int toPosition;

        @Override
        public void onDragStarted(RecyclerView.ViewHolder holder) {
            fromPosition = holder.getAdapterPosition();
            touchHelper.startDrag(holder);
        }

        @Override
        public void onDragOrSwipeEnded(RecyclerView.ViewHolder holder) {
            toPosition = holder.getAdapterPosition();
            if (toPosition >= 0 && fromPosition >= 0 && toPosition != fromPosition) {
                adapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onButtonClicked(int pos) {
            Object item = adapter.getItem(pos);
            if (item instanceof EditProfileDataObject) {
                EditProfileDataObject profileDataObject = (EditProfileDataObject) item;
                profileDataObject.toggleDeleted();
                if (profileDataObject.deleted) {
                    deletedModesKeys.add(profileDataObject.getStringKey());
                } else {
                    deletedModesKeys.remove(profileDataObject.getStringKey());
                }
                updateItems();
            }
        }
    });
    recyclerView.setAdapter(adapter);
    View cancelButton = mainView.findViewById(R.id.dismiss_button);
    UiUtilities.setupDialogButton(nightMode, cancelButton, UiUtilities.DialogButtonType.SECONDARY, R.string.shared_string_cancel);
    cancelButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            FragmentActivity fragmentActivity = getActivity();
            if (fragmentActivity != null) {
                fragmentActivity.onBackPressed();
            }
        }
    });
    mainView.findViewById(R.id.buttons_divider).setVisibility(View.VISIBLE);
    View applyButton = mainView.findViewById(R.id.right_bottom_button);
    UiUtilities.setupDialogButton(nightMode, applyButton, UiUtilities.DialogButtonType.PRIMARY, R.string.shared_string_apply);
    applyButton.setVisibility(View.VISIBLE);
    applyButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            MapActivity mapActivity = (MapActivity) getActivity();
            if (mapActivity != null) {
                OsmandApplication app = mapActivity.getMyApplication();
                if (!deletedModesKeys.isEmpty()) {
                    List<ApplicationMode> deletedModes = new ArrayList<>();
                    for (String modeKey : deletedModesKeys) {
                        ApplicationMode mode = ApplicationMode.valueOfStringKey(modeKey, null);
                        if (mode != null) {
                            deletedModes.add(mode);
                        }
                    }
                    ApplicationMode.deleteCustomModes(deletedModes, app);
                }
                for (ApplicationMode mode : ApplicationMode.allPossibleValues()) {
                    String modeKey = mode.getStringKey();
                    Integer order = appModesOrders.get(modeKey);
                    if (order == null) {
                        order = mode.getOrder();
                    }
                    mode.setOrder(order);
                }
                ApplicationMode.reorderAppModes();
                mapActivity.onBackPressed();
            }
        }
    });
    if (Build.VERSION.SDK_INT >= 21) {
        AndroidUtils.addStatusBarPadding21v(app, mainView);
    }
    return mainView;
}
Also used : ReorderItemTouchHelperCallback(net.osmand.plus.views.controls.ReorderItemTouchHelperCallback) OsmandApplication(net.osmand.plus.OsmandApplication) SpannableString(android.text.SpannableString) ApplicationMode(net.osmand.plus.settings.backend.ApplicationMode) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) SuppressLint(android.annotation.SuppressLint) ItemTouchHelper(androidx.recyclerview.widget.ItemTouchHelper) FragmentActivity(androidx.fragment.app.FragmentActivity) ImageButton(android.widget.ImageButton) TextView(android.widget.TextView) RecyclerView(androidx.recyclerview.widget.RecyclerView) ProfileDataObject(net.osmand.plus.profiles.data.ProfileDataObject) ArrayList(java.util.ArrayList) List(java.util.List) AppBarLayout(com.google.android.material.appbar.AppBarLayout) MapActivity(net.osmand.plus.activities.MapActivity) Nullable(androidx.annotation.Nullable)

Example 2 with ReorderItemTouchHelperCallback

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

the class RearrangePoiFiltersFragment method onCreateView.

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final OsmandApplication app = requireMyApplication();
    final boolean nightMode = isNightMode(app, usedOnMap);
    View mainView = UiUtilities.getInflater(app, nightMode).inflate(R.layout.edit_arrangement_list_fragment, container, false);
    createToolbar(mainView, nightMode);
    RecyclerView recyclerView = mainView.findViewById(R.id.profiles_list);
    recyclerView.setLayoutManager(new LinearLayoutManager(app));
    adapter = new EditPoiFiltersAdapter(app, nightMode);
    initFiltersOrders(app, false);
    final ItemTouchHelper touchHelper = new ItemTouchHelper(new ReorderItemTouchHelperCallback(adapter));
    touchHelper.attachToRecyclerView(recyclerView);
    orderModified = app.getSettings().POI_FILTERS_ORDER.get() != null;
    activationModified = app.getSettings().INACTIVE_POI_FILTERS.get() != null;
    adapter.setListener(new PoiAdapterListener() {

        private int fromPosition;

        private int toPosition;

        @Override
        public void onDragStarted(RecyclerView.ViewHolder holder) {
            fromPosition = holder.getAdapterPosition();
            touchHelper.startDrag(holder);
        }

        @Override
        public void onDragOrSwipeEnded(RecyclerView.ViewHolder holder) {
            toPosition = holder.getAdapterPosition();
            if (toPosition >= 0 && fromPosition >= 0 && toPosition != fromPosition) {
                adapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onButtonClicked(int pos) {
            ListItem item = items.get(pos);
            if (item.value instanceof PoiUIFilterDataObject) {
                isChanged = true;
                activationModified = true;
                PoiUIFilterDataObject poiInfo = (PoiUIFilterDataObject) item.value;
                poiInfo.toggleActive();
                if (!poiInfo.isActive) {
                    availableFiltersKeys.add(poiInfo.filterId);
                } else {
                    availableFiltersKeys.remove(poiInfo.filterId);
                }
                updateItems();
            }
        }

        @Override
        public void onDeleteClicked(final int position) {
            final ListItem item = items.get(position);
            if (item.value instanceof PoiUIFilterDataObject) {
                PoiUIFilterDataObject poiInfo = (PoiUIFilterDataObject) item.value;
                final PoiUIFilter filter = app.getPoiFilters().getFilterById(poiInfo.filterId);
                if (filter != null && app.getPoiFilters().removePoiFilter(filter)) {
                    filter.setDeleted(true);
                    filterDeleted = true;
                }
                items.remove(item);
                adapter.notifyDataSetChanged();
                Snackbar snackbar = Snackbar.make(requireView(), getString(R.string.item_deleted, poiInfo.name), Snackbar.LENGTH_LONG).setAction(R.string.shared_string_undo, new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        items.add(position, item);
                        adapter.notifyDataSetChanged();
                        if (filter != null) {
                            filter.setDeleted(false);
                            app.getPoiFilters().createPoiFilter(filter, false);
                        }
                    }
                });
                ViewCompat.setElevation(snackbar.getView(), 0f);
                snackbar.setAnchorView(buttonsContainer);
                snackbar.setAnimationMode(BaseTransientBottomBar.ANIMATION_MODE_FADE);
                UiUtilities.setupSnackbar(snackbar, nightMode);
                snackbar.show();
            }
        }
    });
    recyclerView.setAdapter(adapter);
    View cancelButton = mainView.findViewById(R.id.dismiss_button);
    UiUtilities.setupDialogButton(nightMode, cancelButton, UiUtilities.DialogButtonType.SECONDARY, R.string.shared_string_cancel);
    cancelButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            dismiss();
        }
    });
    mainView.findViewById(R.id.buttons_divider).setVisibility(View.VISIBLE);
    View applyButton = mainView.findViewById(R.id.right_bottom_button);
    UiUtilities.setupDialogButton(nightMode, applyButton, UiUtilities.DialogButtonType.PRIMARY, R.string.shared_string_apply);
    applyButton.setVisibility(View.VISIBLE);
    applyButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ApplicationMode selectedAppMode = getSelectedAppMode();
            if (isChanged) {
                if (activationModified) {
                    app.getPoiFilters().saveInactiveFilters(selectedAppMode, availableFiltersKeys);
                } else if (wasReset) {
                    app.getPoiFilters().saveInactiveFilters(selectedAppMode, null);
                }
                if (orderModified) {
                    List<PoiUIFilter> dataToSave = new ArrayList<>();
                    for (PoiUIFilter filter : getSortedPoiUiFilters(selectedAppMode, app)) {
                        String filterId = filter.getFilterId();
                        Integer order = poiFiltersOrders.get(filterId);
                        if (order == null) {
                            order = filter.getOrder();
                        }
                        boolean isActive = !availableFiltersKeys.contains(filterId);
                        filter.setActive(isActive);
                        filter.setOrder(order);
                        if (isActive) {
                            dataToSave.add(filter);
                        }
                    }
                    Collections.sort(dataToSave);
                    List<String> filterIds = new ArrayList<>();
                    for (PoiUIFilter filter : dataToSave) {
                        filterIds.add(filter.getFilterId());
                    }
                    app.getPoiFilters().saveFiltersOrder(selectedAppMode, filterIds);
                } else if (wasReset) {
                    app.getPoiFilters().saveFiltersOrder(selectedAppMode, null);
                }
            }
            if (resultCallback != null) {
                resultCallback.onApplyPoiFiltersState(selectedAppMode, isChanged);
            }
            dismiss();
        }
    });
    buttonsContainer = mainView.findViewById(R.id.buttons_container);
    return mainView;
}
Also used : ReorderItemTouchHelperCallback(net.osmand.plus.views.controls.ReorderItemTouchHelperCallback) OsmandApplication(net.osmand.plus.OsmandApplication) ApplicationMode(net.osmand.plus.settings.backend.ApplicationMode) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) SuppressLint(android.annotation.SuppressLint) ItemTouchHelper(androidx.recyclerview.widget.ItemTouchHelper) RecyclerView(androidx.recyclerview.widget.RecyclerView) List(java.util.List) ArrayList(java.util.ArrayList) QuickSearchListItem(net.osmand.plus.search.listitems.QuickSearchListItem) Snackbar(com.google.android.material.snackbar.Snackbar) Nullable(androidx.annotation.Nullable)

Example 3 with ReorderItemTouchHelperCallback

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

the class QuickActionListFragment method onCreateView.

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    OsmandApplication app = requireMyApplication();
    nightMode = !app.getSettings().isLightContent();
    View view = UiUtilities.getInflater(getContext(), nightMode).inflate(R.layout.quick_action_list, container, false);
    rv = (RecyclerView) view.findViewById(R.id.recycler_view);
    fab = (FloatingActionButton) view.findViewById(R.id.fabButton);
    fab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showAddQuickActionDialog();
        }
    });
    if (Build.VERSION.SDK_INT >= 21) {
        AndroidUtils.addStatusBarPadding21v(getContext(), view);
    }
    bottomPanel = view.findViewById(R.id.bottom_panel);
    View btnSelectAll = bottomPanel.findViewById(R.id.select_all);
    View btnDelete = bottomPanel.findViewById(R.id.delete);
    btnSelectAll.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            actionsToDelete.clear();
            for (QuickAction action : adapter.getQuickActions()) {
                actionsToDelete.add(action.id);
            }
            updateListItems();
            updateToolbarTitle();
        }
    });
    btnDelete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            showConfirmDeleteActionsBottomSheet(getMapActivity());
        }
    });
    UiUtilities.setupDialogButton(nightMode, btnDelete, UiUtilities.DialogButtonType.SECONDARY, R.string.shared_string_delete);
    quickActionRegistry = requireMyApplication().getQuickActionRegistry();
    toolbar = (Toolbar) view.findViewById(R.id.toolbar);
    navigationIcon = toolbar.findViewById(R.id.close_button);
    deleteIconContainer = toolbar.findViewById(R.id.action_button);
    toolbarSwitchContainer = toolbar.findViewById(R.id.toolbar_switch_container);
    setUpToolbar();
    adapter = new QuickActionAdapter(new OnStartDragListener() {

        @Override
        public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
            touchHelper.startDrag(viewHolder);
        }
    });
    rv.setAdapter(adapter);
    rv.setLayoutManager(new LinearLayoutManager(getContext()));
    ItemTouchHelper.Callback touchHelperCallback = new ReorderItemTouchHelperCallback(adapter);
    touchHelper = new ItemTouchHelper(touchHelperCallback);
    touchHelper.attachToRecyclerView(rv);
    rv.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            if (screenType == SCREEN_TYPE_REORDER) {
                if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                    fab.hide();
                } else if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    fab.show();
                }
            }
        }
    });
    updateListItems();
    updateVisibility();
    return view;
}
Also used : ReorderItemTouchHelperCallback(net.osmand.plus.views.controls.ReorderItemTouchHelperCallback) OsmandApplication(net.osmand.plus.OsmandApplication) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) ItemTouchHelper(androidx.recyclerview.widget.ItemTouchHelper) RecyclerView(androidx.recyclerview.widget.RecyclerView) Nullable(androidx.annotation.Nullable)

Example 4 with ReorderItemTouchHelperCallback

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

the class SwitchableAction method drawUI.

@Override
public void drawUI(@NonNull ViewGroup parent, @NonNull final MapActivity mapActivity) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.quick_action_switchable_action, parent, false);
    final SwitchCompat showDialog = view.findViewById(R.id.saveButton);
    if (!getParams().isEmpty()) {
        showDialog.setChecked(Boolean.valueOf(getParams().get(KEY_DIALOG)));
    }
    view.findViewById(R.id.saveButtonContainer).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            boolean selected = showDialog.isChecked();
            showDialog.setChecked(!selected);
        }
    });
    RecyclerView list = view.findViewById(R.id.list);
    adapter = new Adapter(mapActivity, new QuickActionListFragment.OnStartDragListener() {

        @Override
        public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
            touchHelper.startDrag(viewHolder);
        }
    });
    ReorderItemTouchHelperCallback touchHelperCallback = new ReorderItemTouchHelperCallback(adapter);
    touchHelper = new ItemTouchHelper(touchHelperCallback);
    touchHelper.attachToRecyclerView(list);
    if (!getParams().isEmpty()) {
        adapter.addItems(loadListFromParams());
    }
    list.setAdapter(adapter);
    TextView dscrTitle = view.findViewById(R.id.textDscrTitle);
    TextView dscrHint = view.findViewById(R.id.textDscrHint);
    Button addBtn = view.findViewById(R.id.btnAdd);
    dscrTitle.setText(parent.getContext().getString(getDiscrTitle()) + ":");
    dscrHint.setText(getDiscrHint());
    addBtn.setText(getAddBtnText());
    addBtn.setOnClickListener(getOnAddBtnClickListener(mapActivity, adapter));
    parent.addView(view);
}
Also used : ItemTouchHelper(androidx.recyclerview.widget.ItemTouchHelper) ReorderItemTouchHelperCallback(net.osmand.plus.views.controls.ReorderItemTouchHelperCallback) Button(android.widget.Button) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) SwitchCompat(androidx.appcompat.widget.SwitchCompat)

Example 5 with ReorderItemTouchHelperCallback

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

the class PointsCard method updateContent.

@Override
protected void updateContent() {
    MeasurementEditingContext editingCtx = fragment.getEditingCtx();
    adapter = new MeasurementToolAdapter(mapActivity, editingCtx.getPoints());
    RecyclerView pointsRv = view.findViewById(R.id.measure_points_recycler_view);
    ItemTouchHelper touchHelper = new ItemTouchHelper(new ReorderItemTouchHelperCallback(adapter));
    touchHelper.attachToRecyclerView(pointsRv);
    adapter.setAdapterListener(fragment.createMeasurementAdapterListener(touchHelper));
    pointsRv.setLayoutManager(new LinearLayoutManager(app));
    pointsRv.setAdapter(adapter);
}
Also used : ItemTouchHelper(androidx.recyclerview.widget.ItemTouchHelper) ReorderItemTouchHelperCallback(net.osmand.plus.views.controls.ReorderItemTouchHelperCallback) RecyclerView(androidx.recyclerview.widget.RecyclerView) MeasurementToolAdapter(net.osmand.plus.measurementtool.adapter.MeasurementToolAdapter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager)

Aggregations

ItemTouchHelper (androidx.recyclerview.widget.ItemTouchHelper)6 ReorderItemTouchHelperCallback (net.osmand.plus.views.controls.ReorderItemTouchHelperCallback)6 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)5 RecyclerView (androidx.recyclerview.widget.RecyclerView)5 View (android.view.View)4 ImageView (android.widget.ImageView)4 TextView (android.widget.TextView)4 Nullable (androidx.annotation.Nullable)3 OsmandApplication (net.osmand.plus.OsmandApplication)3 SuppressLint (android.annotation.SuppressLint)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ApplicationMode (net.osmand.plus.settings.backend.ApplicationMode)2 SpannableString (android.text.SpannableString)1 Button (android.widget.Button)1 ImageButton (android.widget.ImageButton)1 SwitchCompat (androidx.appcompat.widget.SwitchCompat)1 FragmentActivity (androidx.fragment.app.FragmentActivity)1 AppBarLayout (com.google.android.material.appbar.AppBarLayout)1 Snackbar (com.google.android.material.snackbar.Snackbar)1