refactor OnItemDeleteAction

This commit is contained in:
veliymolfar 2020-05-06 14:37:40 +03:00
parent ee0d9677af
commit 9c7597f0cb
8 changed files with 59 additions and 63 deletions

View file

@ -37,6 +37,7 @@ import net.osmand.plus.dialogs.ConfigureMapMenu;
import net.osmand.plus.dialogs.HelpArticleDialogFragment;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.OsmandSettings.ContextMenuItemsPreference;
import net.osmand.plus.OsmandSettings.OsmandPreference;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
@ -49,8 +50,6 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.APP_PROFILES_ID;
public class ContextMenuAdapter {
private static final Log LOG = PlatformUtil.getLog(ContextMenuAdapter.class);
@ -614,4 +613,34 @@ public class ContextMenuAdapter {
}
return visible;
}
public static OnItemDeleteAction makeDeleteAction(final OsmandPreference... prefs) {
return new OnItemDeleteAction() {
@Override
public void itemWasDeleted(ApplicationMode appMode, boolean profileOnly) {
for (OsmandPreference pref : prefs) {
resetSetting(appMode, pref, profileOnly);
}
}
};
}
public static OnItemDeleteAction makeDeleteAction(final List<OsmandPreference> prefs) {
return makeDeleteAction(prefs.toArray(new OsmandPreference[prefs.size()]));
}
private static void resetSetting(ApplicationMode appMode, OsmandSettings.OsmandPreference preference, boolean profileOnly) {
if (profileOnly) {
preference.resetModeToDefault(appMode);
} else {
for (ApplicationMode mode : ApplicationMode.allPossibleValues()) {
preference.resetModeToDefault(mode);
}
}
}
// when action is deleted or reset
public interface OnItemDeleteAction {
void itemWasDeleted(ApplicationMode appMode, boolean profileOnly);
}
}

View file

@ -9,6 +9,7 @@ import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import net.osmand.plus.ContextMenuAdapter.OnItemDeleteAction;
public class ContextMenuItem {
public static final int INVALID_ID = -1;
@ -408,9 +409,4 @@ public class ContextMenuItem {
mHideDivider, mHideCompoundButton, mMinHeight, mTag, mId);
}
}
// when action is deleted or reset
public interface OnItemDeleteAction {
void itemWasDeleted(ApplicationMode appMode, boolean profileOnly);
}
}

View file

@ -61,7 +61,6 @@ import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.activities.TabActivity.TabItem;
import net.osmand.plus.dashboard.tools.DashFragmentData;
import net.osmand.plus.dialogs.ConfigureMapMenu;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.mapcontextmenu.MapContextMenu;
import net.osmand.plus.monitoring.OsmandMonitoringPlugin;
@ -101,6 +100,7 @@ import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_A
import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_PHOTO_NOTE;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_VIDEO_NOTE;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.RECORDING_LAYER;
import static net.osmand.plus.ContextMenuAdapter.makeDeleteAction;
public class AudioVideoNotesPlugin extends OsmandPlugin {
@ -653,7 +653,7 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
.setSelected(SHOW_RECORDINGS.get())
.setIcon(R.drawable.ic_action_micro_dark)
.setColor(SHOW_RECORDINGS.get() ? R.color.osmand_orange : ContextMenuItem.INVALID_ID)
.setItemDeleteAction(new ConfigureMapMenu.ItemResetListener(Arrays.<OsmandPreference>asList(SHOW_RECORDINGS)))
.setItemDeleteAction(makeDeleteAction(SHOW_RECORDINGS))
.setListener(listener).createItem());
}

View file

@ -31,7 +31,6 @@ import net.osmand.CallbackWithObject;
import net.osmand.GPXUtilities;
import net.osmand.PlatformUtil;
import net.osmand.core.android.MapRendererContext;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.ContextMenuAdapter.ItemClickListener;
import net.osmand.plus.ContextMenuAdapter.OnRowItemClick;
@ -104,6 +103,7 @@ import static net.osmand.aidlapi.OsmAndCustomizationConstants.TEXT_SIZE_ID;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.TRANSPORT_ID;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.TRANSPORT_RENDERING_ID;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.WIKIPEDIA_ID;
import static net.osmand.plus.ContextMenuAdapter.makeDeleteAction;
import static net.osmand.plus.poi.PoiFiltersHelper.PoiTemplateList;
import static net.osmand.plus.srtmplugin.SRTMPlugin.CONTOUR_DENSITY_ATTR;
import static net.osmand.plus.srtmplugin.SRTMPlugin.CONTOUR_LINES_ATTR;
@ -318,32 +318,6 @@ public class ConfigureMapMenu {
}
}
public static final class ItemResetListener implements ContextMenuItem.OnItemDeleteAction {
private List<OsmandPreference> prefs;
public ItemResetListener(@NonNull List<OsmandPreference> prefs) {
this.prefs = prefs;
}
@Override
public void itemWasDeleted(@NonNull ApplicationMode appMode, boolean profileOnly) {
for (OsmandSettings.OsmandPreference pref : prefs) {
resetSetting(appMode, pref, profileOnly);
}
}
private void resetSetting(ApplicationMode appMode, OsmandPreference preference, boolean profileOnly) {
if (profileOnly) {
preference.resetModeToDefault(appMode);
} else {
for (ApplicationMode mode : ApplicationMode.allPossibleValues()) {
preference.resetModeToDefault(mode);
}
}
}
}
private void createLayersItems(List<RenderingRuleProperty> customRules, ContextMenuAdapter adapter,
final MapActivity activity, final int themeRes, final boolean nightMode) {
final OsmandApplication app = activity.getMyApplication();
@ -363,7 +337,7 @@ public class ConfigureMapMenu {
.setSelected(settings.SHOW_FAVORITES.get())
.setColor(selected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID)
.setIcon(R.drawable.ic_action_fav_dark)
.setItemDeleteAction(new ItemResetListener(Arrays.<OsmandPreference>asList(settings.SHOW_FAVORITES)))
.setItemDeleteAction(makeDeleteAction(settings.SHOW_FAVORITES))
.setListener(l)
.createItem());
selected = app.getPoiFilters().isShowingAnyPoi(PoiTemplateList.POI);
@ -383,7 +357,7 @@ public class ConfigureMapMenu {
.setSelected(settings.SHOW_POI_LABEL.get())
.setColor(selected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID)
.setIcon(R.drawable.ic_action_text_dark)
.setItemDeleteAction(new ItemResetListener(Arrays.<OsmandPreference>asList(settings.SHOW_POI_LABEL)))
.setItemDeleteAction(makeDeleteAction(settings.SHOW_POI_LABEL))
.setListener(l).createItem());
/*
@ -422,7 +396,7 @@ public class ConfigureMapMenu {
.setSecondaryIcon(R.drawable.ic_action_additional_option)
.setSelected(transportSelected)
.setColor(transportSelected ? selectedProfileColorRes : ContextMenuItem.INVALID_ID)
.setItemDeleteAction(new ItemResetListener(new ArrayList<OsmandPreference>(transportPrefs)))
.setItemDeleteAction(makeDeleteAction(new ArrayList<OsmandPreference>(transportPrefs)))
.setListener(new ContextMenuAdapter.OnRowItemClick() {
ArrayAdapter<CharSequence> adapter;
boolean transportSelectedInner = transportSelected;
@ -588,7 +562,7 @@ public class ConfigureMapMenu {
.setColor(selected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID)
.setIcon(R.drawable.ic_plugin_wikipedia)
.setSecondaryIcon(R.drawable.ic_action_additional_option)
.setItemDeleteAction(new ItemResetListener(Arrays.<OsmandPreference>asList(settings.SHOW_WIKIPEDIA_POI)))
.setItemDeleteAction(makeDeleteAction(settings.SHOW_WIKIPEDIA_POI))
.setListener(l).createItem());
selected = settings.SHOW_MAP_MARKERS.get();
@ -598,7 +572,7 @@ public class ConfigureMapMenu {
.setSelected(selected)
.setColor(selected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID)
.setIcon(R.drawable.ic_action_flag_dark)
.setItemDeleteAction(new ItemResetListener(Arrays.<OsmandPreference>asList(settings.SHOW_MAP_MARKERS)))
.setItemDeleteAction(makeDeleteAction(settings.SHOW_MAP_MARKERS))
.setListener(l).createItem());
adapter.addItem(new ContextMenuItem.ItemBuilder()
@ -606,7 +580,7 @@ public class ConfigureMapMenu {
.setTitleId(R.string.layer_map, activity)
.setIcon(R.drawable.ic_world_globe_dark)
.setDescription(settings.MAP_ONLINE_DATA.get() ? settings.MAP_TILE_SOURCES.get() : null)
.setItemDeleteAction(new ItemResetListener(Arrays.<OsmandPreference>asList(settings.MAP_ONLINE_DATA, settings.MAP_TILE_SOURCES)))
.setItemDeleteAction(makeDeleteAction(settings.MAP_ONLINE_DATA, settings.MAP_TILE_SOURCES))
.setListener(l).createItem());
OsmandPlugin.registerLayerContextMenu(activity.getMapView(), adapter, activity);
@ -650,7 +624,7 @@ public class ConfigureMapMenu {
return false;
}
})
.setItemDeleteAction(new ConfigureMapMenu.ItemResetListener(Arrays.<OsmandSettings.OsmandPreference>asList(settings.RENDERER)))
.setItemDeleteAction(makeDeleteAction(settings.RENDERER))
.createItem());
String description = "";
@ -707,7 +681,7 @@ public class ConfigureMapMenu {
return false;
}
})
.setItemDeleteAction(new ConfigureMapMenu.ItemResetListener(Arrays.<OsmandSettings.OsmandPreference>asList(settings.DAYNIGHT_MODE)))
.setItemDeleteAction(makeDeleteAction(settings.DAYNIGHT_MODE))
.createItem());
adapter.addItem(new ContextMenuItem.ItemBuilder()
@ -773,7 +747,7 @@ public class ConfigureMapMenu {
return false;
}
})
.setItemDeleteAction(new ConfigureMapMenu.ItemResetListener(Arrays.<OsmandSettings.OsmandPreference>asList(settings.MAP_DENSITY)))
.setItemDeleteAction(makeDeleteAction(settings.MAP_DENSITY))
.createItem());
ContextMenuItem props;
@ -819,7 +793,7 @@ public class ConfigureMapMenu {
return false;
}
})
.setItemDeleteAction(new ConfigureMapMenu.ItemResetListener(Arrays.<OsmandSettings.OsmandPreference>asList(settings.TEXT_SCALE)))
.setItemDeleteAction(makeDeleteAction(settings.TEXT_SCALE))
.createItem());
String localeDescr = activity.getMyApplication().getSettings().MAP_PREFERRED_LOCALE.get();
@ -919,7 +893,7 @@ public class ConfigureMapMenu {
return false;
}
})
.setItemDeleteAction(new ConfigureMapMenu.ItemResetListener(Arrays.<OsmandSettings.OsmandPreference>asList(settings.MAP_PREFERRED_LOCALE)))
.setItemDeleteAction(makeDeleteAction(settings.MAP_PREFERRED_LOCALE))
.createItem());
props = createProperties(customRules, null, R.string.rendering_category_transport, R.drawable.ic_action_transport_bus,
@ -1107,7 +1081,7 @@ public class ConfigureMapMenu {
builder.setSecondaryIcon(R.drawable.ic_action_additional_option);
builder.setSelected(selected);
}
builder.setItemDeleteAction(new ConfigureMapMenu.ItemResetListener(new ArrayList<OsmandPreference>(prefs)));
builder.setItemDeleteAction(makeDeleteAction(new ArrayList<OsmandPreference>(prefs)));
return builder.createItem();
// createCustomRenderingProperties(adapter, activity, ps);
}
@ -1444,7 +1418,7 @@ public class ConfigureMapMenu {
}
})
.setDescription(descr)
.setItemDeleteAction(new ConfigureMapMenu.ItemResetListener(Arrays.<OsmandSettings.OsmandPreference>asList(pref)))
.setItemDeleteAction(makeDeleteAction(pref))
.setLayout(R.layout.list_item_single_line_descrition_narrow);
if (icon != 0) {
builder.setIcon(icon);

View file

@ -31,7 +31,6 @@ import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.MapActivityLayers;
import net.osmand.plus.base.BottomSheetDialogFragment;
import net.osmand.plus.dashboard.DashboardOnMap;
import net.osmand.plus.dialogs.ConfigureMapMenu;
import net.osmand.plus.views.MapInfoLayer;
import net.osmand.plus.views.MapTileLayer;
import net.osmand.plus.views.OsmandMapTileView;
@ -40,11 +39,11 @@ import net.osmand.plus.views.mapwidgets.TextInfoWidget;
import net.osmand.util.Algorithms;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import static android.content.Intent.ACTION_VIEW;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAPILLARY;
import static net.osmand.plus.ContextMenuAdapter.makeDeleteAction;
public class MapillaryPlugin extends OsmandPlugin {
@ -187,7 +186,7 @@ public class MapillaryPlugin extends OsmandPlugin {
.setColor(settings.SHOW_MAPILLARY.get() ? R.color.osmand_orange : ContextMenuItem.INVALID_ID)
.setIcon(R.drawable.ic_action_mapillary)
.setSecondaryIcon(R.drawable.ic_action_additional_option)
.setItemDeleteAction(new ConfigureMapMenu.ItemResetListener(Arrays.<OsmandSettings.OsmandPreference>asList(settings.SHOW_MAPILLARY)))
.setItemDeleteAction(makeDeleteAction(settings.SHOW_MAPILLARY))
.setListener(listener)
.createItem());
}

View file

@ -38,7 +38,6 @@ import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.TabActivity;
import net.osmand.plus.dashboard.DashboardOnMap.DashboardType;
import net.osmand.plus.dashboard.tools.DashFragmentData;
import net.osmand.plus.dialogs.ConfigureMapMenu;
import net.osmand.plus.myplaces.AvailableGPXFragment;
import net.osmand.plus.myplaces.AvailableGPXFragment.GpxInfo;
import net.osmand.plus.myplaces.FavoritesActivity;
@ -51,7 +50,6 @@ import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_CREATE_POI;
@ -61,6 +59,7 @@ import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_M
import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_OPEN_OSM_NOTE;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.OSM_EDITS;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.OSM_NOTES;
import static net.osmand.plus.ContextMenuAdapter.makeDeleteAction;
public class OsmEditingPlugin extends OsmandPlugin {
@ -366,7 +365,7 @@ public class OsmEditingPlugin extends OsmandPlugin {
return true;
}
})
.setItemDeleteAction(new ConfigureMapMenu.ItemResetListener(Arrays.<OsmandSettings.OsmandPreference>asList(settings.SHOW_OSM_BUGS)))
.setItemDeleteAction(makeDeleteAction(settings.SHOW_OSM_BUGS))
.createItem());
adapter.addItem(new ContextMenuItem.ItemBuilder()
@ -388,7 +387,7 @@ public class OsmEditingPlugin extends OsmandPlugin {
return true;
}
})
.setItemDeleteAction(new ConfigureMapMenu.ItemResetListener(Arrays.<OsmandSettings.OsmandPreference>asList(settings.SHOW_OSM_EDITS)))
.setItemDeleteAction(makeDeleteAction(settings.SHOW_OSM_EDITS))
.createItem());
}

View file

@ -45,7 +45,6 @@ import net.osmand.plus.activities.DownloadTilesDialog;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.MapActivityLayers;
import net.osmand.plus.dashboard.DashboardOnMap.DashboardType;
import net.osmand.plus.dialogs.ConfigureMapMenu;
import net.osmand.plus.dialogs.RasterMapMenu;
import net.osmand.plus.quickaction.QuickActionType;
import net.osmand.plus.views.MapTileLayer;
@ -54,7 +53,6 @@ import net.osmand.util.Algorithms;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@ -62,6 +60,7 @@ import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_D
import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_UPDATE_MAP;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.OVERLAY_MAP;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.UNDERLAY_MAP;
import static net.osmand.plus.ContextMenuAdapter.makeDeleteAction;
import static net.osmand.plus.UiUtilities.CompoundButtonType.PROFILE_DEPENDENT;
public class OsmandRasterMapsPlugin extends OsmandPlugin {
@ -360,7 +359,7 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
.setIcon(R.drawable.ic_layer_top)
.setSecondaryIcon(R.drawable.ic_action_additional_option)
.setListener(listener)
.setItemDeleteAction(new ConfigureMapMenu.ItemResetListener(Arrays.<OsmandSettings.OsmandPreference>asList(settings.MAP_OVERLAY, settings.MAP_OVERLAY_PREVIOUS, settings.MAP_OVERLAY_TRANSPARENCY)))
.setItemDeleteAction(makeDeleteAction(settings.MAP_OVERLAY, settings.MAP_OVERLAY_PREVIOUS, settings.MAP_OVERLAY_TRANSPARENCY))
.createItem());
String underlayMapDescr = settings.MAP_UNDERLAY.get();
if (underlayMapDescr!=null && underlayMapDescr.contains(".sqlitedb")) {
@ -376,7 +375,7 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
.setIcon(R.drawable.ic_layer_bottom)
.setSecondaryIcon(R.drawable.ic_action_additional_option)
.setListener(listener)
.setItemDeleteAction(new ConfigureMapMenu.ItemResetListener(Arrays.<OsmandSettings.OsmandPreference>asList(settings.MAP_UNDERLAY, settings.MAP_UNDERLAY_PREVIOUS)))
.setItemDeleteAction(makeDeleteAction(settings.MAP_UNDERLAY, settings.MAP_UNDERLAY_PREVIOUS))
.createItem());
}

View file

@ -27,7 +27,6 @@ import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.SettingsActivity;
import net.osmand.plus.dashboard.DashboardOnMap;
import net.osmand.plus.dialogs.ConfigureMapMenu;
import net.osmand.plus.download.DownloadActivityType;
import net.osmand.plus.download.DownloadIndexesThread;
import net.osmand.plus.download.DownloadResources;
@ -46,6 +45,7 @@ import java.util.List;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.CONTOUR_LINES;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.TERRAIN;
import static net.osmand.plus.ContextMenuAdapter.makeDeleteAction;
public class SRTMPlugin extends OsmandPlugin {
@ -348,7 +348,7 @@ public class SRTMPlugin extends OsmandPlugin {
.setIcon(R.drawable.ic_plugin_srtm)
.setDescription(app.getString(R.string.display_zoom_level, descr))
.setColor(contourLinesSelected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID)
.setItemDeleteAction(new ConfigureMapMenu.ItemResetListener(Arrays.<OsmandSettings.OsmandPreference>asList(settings.CONTOUR_LINES_ZOOM)))
.setItemDeleteAction(makeDeleteAction(settings.CONTOUR_LINES_ZOOM))
.setSecondaryIcon(R.drawable.ic_action_additional_option)
.setListener(listener).createItem());
}
@ -364,7 +364,7 @@ public class SRTMPlugin extends OsmandPlugin {
.setColor(terrainEnabled ? R.color.osmand_orange : ContextMenuItem.INVALID_ID)
.setIcon(R.drawable.ic_action_hillshade_dark)
.setSecondaryIcon(R.drawable.ic_action_additional_option)
.setItemDeleteAction(new ConfigureMapMenu.ItemResetListener(Arrays.<OsmandSettings.OsmandPreference>asList(settings.TERRAIN, settings.TERRAIN_MODE)))
.setItemDeleteAction(makeDeleteAction(settings.TERRAIN, settings.TERRAIN_MODE))
.setListener(listener)
.createItem()
);