From 7aa3948188902ded3a5f6100d276153388e12677 Mon Sep 17 00:00:00 2001 From: veliymolfar Date: Wed, 15 Apr 2020 17:10:54 +0300 Subject: [PATCH] refactor --- OsmAnd/res/values/strings.xml | 2 +- .../settings/ConfigureMenuItemsFragment.java | 68 ++++++++++--------- .../settings/ConfigureMenuRootFragment.java | 9 +-- .../settings/RearrangeMenuItemsAdapter.java | 33 +++++---- 4 files changed, 60 insertions(+), 52 deletions(-) diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 618022bd1a..fb40bd06d3 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -41,7 +41,7 @@ Drawer UI Customization Drawer items, context menu - Customize the quantity of items in Drawer, Configure map and context menu.\n\nYou can disable unused plugins, to hide all its controls from the application. + Customize the quantity of items in Drawer, Configure map and context menu.\n\nYou can disable unused plugins, to hide all its controls from the application %1$s. Min Square Octagon diff --git a/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuItemsFragment.java b/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuItemsFragment.java index b0c0b55b60..8790889a1b 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuItemsFragment.java @@ -39,7 +39,7 @@ import net.osmand.plus.profiles.SelectCopyAppModeBottomSheet; import net.osmand.plus.settings.ConfigureMenuRootFragment.ScreenType; import net.osmand.plus.settings.bottomsheets.ChangeGeneralProfilesPrefBottomSheet; import net.osmand.plus.views.controls.ReorderItemTouchHelperCallback; -import net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItem; +import net.osmand.plus.settings.RearrangeMenuItemsAdapter.RearrangeMenuAdapterItem; import net.osmand.plus.settings.RearrangeMenuItemsAdapter.MenuItemsAdapterListener; import org.apache.commons.logging.Log; @@ -61,6 +61,7 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment implements SelectCopyAppModeBottomSheet.CopyAppModePrefsListener { public static final String TAG = ConfigureMenuItemsFragment.class.getName(); + public static final int MAIN_BUTTONS_QUANTITY = 4; private static final Log LOG = PlatformUtil.getLog(ConfigureMenuItemsFragment.class.getName()); private static final String APP_MODE_KEY = "app_mode_key"; private static final String ITEM_TYPE_KEY = "item_type_key"; @@ -172,7 +173,7 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment TextView toolbarTitle = root.findViewById(R.id.toolbar_title); ImageButton toolbarButton = root.findViewById(R.id.close_button); RecyclerView recyclerView = root.findViewById(R.id.profiles_list); - recyclerView.setPadding(0, 0, 0, AndroidUtils.dpToPx(app, 72)); + recyclerView.setPadding(0, 0, 0, (int) app.getResources().getDimension(R.dimen.dialog_button_ex_min_width)); toolbar.setBackgroundColor(nightMode ? getResources().getColor(R.color.list_background_color_dark) : getResources().getColor(R.color.list_background_color_light)); @@ -213,9 +214,9 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment @Override public void onButtonClicked(int position) { - AdapterItem adapterItem = rearrangeAdapter.getItem(position); - if (adapterItem.getValue() instanceof ContextMenuItem) { - ContextMenuItem menuItemBase = (ContextMenuItem) adapterItem.getValue(); + RearrangeMenuAdapterItem rearrangeMenuAdapterItem = rearrangeAdapter.getItem(position); + if (rearrangeMenuAdapterItem.getValue() instanceof ContextMenuItem) { + ContextMenuItem menuItemBase = (ContextMenuItem) rearrangeMenuAdapterItem.getValue(); menuItemBase.setHidden(!menuItemBase.isHidden()); if (menuItemBase.isHidden()) { hiddenMenuItems.add(menuItemBase.getId()); @@ -292,35 +293,36 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment return root; } - private List getAdapterItems() { - List items = new ArrayList<>(); - items.add(new AdapterItem(DESCRIPTION, screenType)); + private List getAdapterItems() { + List items = new ArrayList<>(); + items.add(new RearrangeMenuAdapterItem(DESCRIPTION, screenType)); - List visible = getItemsForRearrangeAdapter(hiddenMenuItems, wasReset ? null : menuItemsOrder, false); - List hiddenItems = getItemsForRearrangeAdapter(hiddenMenuItems, wasReset ? null : menuItemsOrder, true); + List visible = getItemsForRearrangeAdapter(hiddenMenuItems, wasReset ? null : menuItemsOrder, false); + List hiddenItems = getItemsForRearrangeAdapter(hiddenMenuItems, wasReset ? null : menuItemsOrder, true); if (screenType == ScreenType.CONTEXT_MENU_ACTIONS) { - for (int i =0; i3){ - AdapterItem third = visible.get(3); - visible.set(3,visible.get(i)); - menuItemsOrder.put(((ContextMenuItem) third.getValue()).getId(),i); - menuItemsOrder.put(((ContextMenuItem) visible.get(i).getValue()).getId(),3); + if (value.getId() != null && value.getId().equals(MAP_CONTEXT_MENU_MORE_ID)) { + int buttonMoreIndex = MAIN_BUTTONS_QUANTITY - 1; + if (i > buttonMoreIndex) { + RearrangeMenuAdapterItem third = visible.get(buttonMoreIndex); + visible.set(buttonMoreIndex, visible.get(i)); + menuItemsOrder.put(((ContextMenuItem) third.getValue()).getId(), i); + menuItemsOrder.put(((ContextMenuItem) visible.get(i).getValue()).getId(), buttonMoreIndex); } } } - List main = new ArrayList<>(); - int actionsIndex = Math.min(4, visible.size()); + List main = new ArrayList<>(); + int actionsIndex = Math.min(MAIN_BUTTONS_QUANTITY, visible.size()); for (int i = 0; i < actionsIndex; i++) { main.add(visible.get(i)); } - items.add(new AdapterItem(HEADER, new RearrangeMenuItemsAdapter.HeaderItem(R.string.main_actions, R.string.main_actions_descr))); + items.add(new RearrangeMenuAdapterItem(HEADER, new RearrangeMenuItemsAdapter.HeaderItem(R.string.main_actions, R.string.main_actions_descr))); items.addAll(main); - items.add(new AdapterItem(HEADER, new RearrangeMenuItemsAdapter.HeaderItem(R.string.additional_actions, R.string.additional_actions_descr))); - List additional = new ArrayList<>(); - for (int i = 4; i < visible.size(); i++) { + items.add(new RearrangeMenuAdapterItem(HEADER, new RearrangeMenuItemsAdapter.HeaderItem(R.string.additional_actions, R.string.additional_actions_descr))); + List additional = new ArrayList<>(); + for (int i = MAIN_BUTTONS_QUANTITY; i < visible.size(); i++) { additional.add(visible.get(i)); } items.addAll(additional); @@ -328,11 +330,11 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment items.addAll(visible); } if (!hiddenItems.isEmpty()) { - items.add(new AdapterItem(HEADER, new RearrangeMenuItemsAdapter.HeaderItem(R.string.shared_string_hidden, R.string.hidden_items_descr))); + items.add(new RearrangeMenuAdapterItem(HEADER, new RearrangeMenuItemsAdapter.HeaderItem(R.string.shared_string_hidden, R.string.hidden_items_descr))); items.addAll(hiddenItems); } - items.add(new AdapterItem(DIVIDER, 1)); - items.add(new AdapterItem(BUTTON, new RearrangeMenuItemsAdapter.ButtonItem( + items.add(new RearrangeMenuAdapterItem(DIVIDER, 1)); + items.add(new RearrangeMenuAdapterItem(BUTTON, new RearrangeMenuItemsAdapter.ButtonItem( R.string.reset_to_default, R.drawable.ic_action_reset_to_default_dark, new View.OnClickListener() { @@ -347,7 +349,7 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment rearrangeAdapter.updateItems(getAdapterItems()); } }))); - items.add(new AdapterItem(BUTTON, new RearrangeMenuItemsAdapter.ButtonItem( + items.add(new RearrangeMenuAdapterItem(BUTTON, new RearrangeMenuItemsAdapter.ButtonItem( R.string.copy_from_other_profile, R.drawable.ic_action_copy, new View.OnClickListener() { @@ -411,7 +413,7 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment } } - public static OsmandSettings.ContextMenuItemsPreference getSettingForScreen(OsmandApplication app, ScreenType screenType) { + public static OsmandSettings.ContextMenuItemsPreference getSettingForScreen(OsmandApplication app, ScreenType screenType) throws IllegalArgumentException { switch (screenType) { case DRAWER: return app.getSettings().DRAWER_ITEMS; @@ -430,23 +432,23 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment } } - public List getItemsForRearrangeAdapter(@Nullable List hiddenItemsIds, @Nullable HashMap itemsOrderIds, boolean hidden) { + public List getItemsForRearrangeAdapter(@Nullable List hiddenItemsIds, @Nullable HashMap itemsOrderIds, boolean hidden) { List defItems = contextMenuAdapter.getDefaultItems(); if (itemsOrderIds == null || itemsOrderIds.isEmpty()) { initDefaultOrders(defItems); } else { sortByCustomOrder(defItems, itemsOrderIds); } - List visibleItems = new ArrayList<>(); - List hiddenItems = new ArrayList<>(); + List visibleItems = new ArrayList<>(); + List hiddenItems = new ArrayList<>(); for (ContextMenuItem item : defItems) { String id = item.getId(); if (hiddenItemsIds != null && hiddenItemsIds.contains(id)) { item.setHidden(true); - hiddenItems.add(new AdapterItem(MENU_ITEM, item)); + hiddenItems.add(new RearrangeMenuAdapterItem(MENU_ITEM, item)); } else { item.setHidden(false); - visibleItems.add(new AdapterItem(MENU_ITEM, item)); + visibleItems.add(new RearrangeMenuAdapterItem(MENU_ITEM, item)); } } return hidden ? hiddenItems : visibleItems; diff --git a/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuRootFragment.java b/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuRootFragment.java index fe432fa37c..a45a72dc11 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuRootFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuRootFragment.java @@ -117,7 +117,9 @@ public class ConfigureMenuRootFragment extends BaseOsmAndFragment { toolbarSubTitle.setText(appMode.toHumanString()); toolbarSubTitle.setVisibility(View.VISIBLE); List items = new ArrayList<>(); - items.add(getString(R.string.ui_customization_description)); + String plugins = getString(R.string.prefs_plugins); + String description = String.format(getString(R.string.ui_customization_description), plugins); + items.add(description); items.addAll(Arrays.asList(ScreenType.values())); CustomizationItemsAdapter adapter = new CustomizationItemsAdapter(items, new OnCustomizationItemClickListener() { @Override @@ -199,10 +201,9 @@ public class ConfigureMenuRootFragment extends BaseOsmAndFragment { final Object currentItem = items.get(position); if (holder instanceof DescriptionHolder) { DescriptionHolder descriptionHolder = (DescriptionHolder) holder; - String plugins = getString(R.string.prefs_plugins) + '.'; - String description = String.format(getString(R.string.ltr_or_rtl_combine_via_space), (String) currentItem, plugins); + String plugins = getString(R.string.prefs_plugins); setupClickableText( - descriptionHolder.description, description, plugins, new Intent(app, PluginsActivity.class)); + descriptionHolder.description, (String) currentItem, plugins, new Intent(app, PluginsActivity.class)); descriptionHolder.image.setVisibility(View.GONE); } else { final ScreenType item = (ScreenType) currentItem; diff --git a/OsmAnd/src/net/osmand/plus/settings/RearrangeMenuItemsAdapter.java b/OsmAnd/src/net/osmand/plus/settings/RearrangeMenuItemsAdapter.java index 43786a34db..5ad66aa816 100644 --- a/OsmAnd/src/net/osmand/plus/settings/RearrangeMenuItemsAdapter.java +++ b/OsmAnd/src/net/osmand/plus/settings/RearrangeMenuItemsAdapter.java @@ -2,6 +2,7 @@ package net.osmand.plus.settings; import android.annotation.SuppressLint; import android.graphics.drawable.Drawable; +import android.util.TypedValue; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; @@ -37,6 +38,7 @@ import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_RENDERING_CATE import static net.osmand.aidlapi.OsmAndCustomizationConstants.RENDERING_ITEMS_ID_SCHEME; import static net.osmand.aidlapi.OsmAndCustomizationConstants.SHOW_CATEGORY_ID; import static net.osmand.aidlapi.OsmAndCustomizationConstants.SHOW_ITEMS_ID_SCHEME; +import static net.osmand.plus.settings.ConfigureMenuItemsFragment.MAIN_BUTTONS_QUANTITY; public class RearrangeMenuItemsAdapter extends RecyclerView.Adapter implements ReorderItemTouchHelperCallback.OnItemMoveCallback { @@ -44,14 +46,14 @@ public class RearrangeMenuItemsAdapter extends RecyclerView.Adapter items; + private List items; private MenuItemsAdapterListener listener; private int activeColorRes; private int textColorRes; public RearrangeMenuItemsAdapter(OsmandApplication app, - List items) { + List items) { this.app = app; this.items = items; uiUtilities = app.getUIUtilities(); @@ -66,7 +68,7 @@ public class RearrangeMenuItemsAdapter extends RecyclerView.Adapter 5) { + if (to > buttonMoreIndex) { return false; } } else if (menuItemTo.getId().equals(MAP_CONTEXT_MENU_MORE_ID)) { - if (from > 5) { + if (from > buttonMoreIndex) { return false; } } @@ -359,11 +364,11 @@ public class RearrangeMenuItemsAdapter extends RecyclerView.Adapter items) { + public void updateItems(List items) { this.items = items; notifyDataSetChanged(); }