Replace IntDef with Enum for Coordinate Input Formats

This commit is contained in:
Alex Sytnyk 2018-06-12 10:25:44 +03:00
parent 038aac52c8
commit dad801c456
4 changed files with 64 additions and 102 deletions

View file

@ -31,7 +31,7 @@ import net.osmand.plus.api.SettingsAPI.SettingsEditor;
import net.osmand.plus.dialogs.RateUsBottomSheetDialog;
import net.osmand.plus.helpers.SearchHistoryHelper;
import net.osmand.plus.mapillary.MapillaryPlugin;
import net.osmand.plus.mapmarkers.CoordinateInputFormats;
import net.osmand.plus.mapmarkers.CoordinateInputFormats.Format;
import net.osmand.plus.render.RendererRegistry;
import net.osmand.plus.routing.RouteProvider.RouteService;
import net.osmand.render.RenderingRulesStorage;
@ -723,7 +723,7 @@ public class OsmandSettings {
public final CommonPreference<Boolean> SELECT_MARKER_ON_SINGLE_TAP = new BooleanPreference("select_marker_on_single_tap", false).makeProfile();
public final CommonPreference<Boolean> COORDS_INPUT_USE_RIGHT_SIDE = new BooleanPreference("coords_input_use_right_side", true).makeGlobal();
public final CommonPreference<Integer> COORDS_INPUT_FORMAT = new IntPreference("coords_input_format", CoordinateInputFormats.DD_MM_MMM);
public final OsmandPreference<Format> COORDS_INPUT_FORMAT = new EnumIntPreference<>("coords_input_format", Format.DD_MM_MMM, Format.values()).makeGlobal();
public final CommonPreference<Boolean> COORDS_INPUT_USE_OSMAND_KEYBOARD = new BooleanPreference("coords_input_use_osmand_keyboard", Build.VERSION.SDK_INT >= 16).makeGlobal();
public final CommonPreference<Boolean> COORDS_INPUT_TWO_DIGITS_LONGTITUDE = new BooleanPreference("coords_input_two_digits_longitude", false).makeGlobal();

View file

@ -16,7 +16,7 @@ import net.osmand.plus.base.bottomsheetmenu.simpleitems.SubtitleDividerItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.SubtitleItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.mapmarkers.CoordinateInputFormats.CoordinateInputFormatDef;
import net.osmand.plus.mapmarkers.CoordinateInputFormats.Format;
public class CoordinateInputBottomSheetDialogFragment extends MenuBottomSheetDialogFragment {
@ -103,12 +103,12 @@ public class CoordinateInputBottomSheetDialogFragment extends MenuBottomSheetDia
items.add(new SubtitleItem(getString(R.string.coordinates_format)));
int selectedFormat = settings.COORDS_INPUT_FORMAT.get();
Format selectedFormat = settings.COORDS_INPUT_FORMAT.get();
Drawable formatIcon = getContentIcon(R.drawable.ic_action_coordinates_latitude);
View.OnClickListener formatsOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
int format = (int) v.getTag();
Format format = (Format) v.getTag();
settings.COORDS_INPUT_FORMAT.set(format);
if (listener != null) {
listener.onInputSettingsChanged();
@ -117,7 +117,7 @@ public class CoordinateInputBottomSheetDialogFragment extends MenuBottomSheetDia
}
};
for (@CoordinateInputFormatDef int format : CoordinateInputFormats.VALUES) {
for (Format format : Format.values()) {
boolean selectedItem = format == selectedFormat;
BaseBottomSheetItem formatItem = new BottomSheetItemWithCompoundButton.Builder()
@ -126,7 +126,7 @@ public class CoordinateInputBottomSheetDialogFragment extends MenuBottomSheetDia
? ColorStateList.valueOf(getResolvedColor(getActiveColorId()))
: null)
.setIcon(selectedItem ? getActiveIcon(R.drawable.ic_action_coordinates_latitude) : formatIcon)
.setTitle(CoordinateInputFormats.formatToHumanString(context, format))
.setTitle(format.toHumanString(context))
.setTitleColorId(selectedItem ? getActiveColorId() : BaseBottomSheetItem.INVALID_ID)
.setLayoutId(R.layout.bottom_sheet_item_with_radio_btn)
.setOnClickListener(formatsOnClickListener)

View file

@ -61,6 +61,7 @@ import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.mapmarkers.CoordinateInputBottomSheetDialogFragment.CoordinateInputFormatChangeListener;
import net.osmand.plus.mapmarkers.CoordinateInputFormats.Format;
import net.osmand.plus.mapmarkers.adapters.CoordinateInputAdapter;
import net.osmand.plus.widgets.EditTextEx;
import net.osmand.util.LocationParser;
@ -737,20 +738,20 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
clearInputs();
int format = getMyApplication().getSettings().COORDS_INPUT_FORMAT.get();
Format format = getMyApplication().getSettings().COORDS_INPUT_FORMAT.get();
boolean useTwoDigitsLongtitude = getMyApplication().getSettings().COORDS_INPUT_TWO_DIGITS_LONGTITUDE.get();
setupEditTextEx(R.id.lat_first_input_et, CoordinateInputFormats.getFirstPartSymbolsCount(format, true, useTwoDigitsLongtitude), true);
setupEditTextEx(R.id.lon_first_input_et, CoordinateInputFormats.getFirstPartSymbolsCount(format, false, useTwoDigitsLongtitude), false);
setupEditTextEx(R.id.lat_first_input_et, format.getFirstPartSymbolsCount(true, useTwoDigitsLongtitude), true);
setupEditTextEx(R.id.lon_first_input_et, format.getFirstPartSymbolsCount(false, useTwoDigitsLongtitude), false);
String firstSeparator = CoordinateInputFormats.getFirstSeparator(format);
String firstSeparator = format.getFirstSeparator();
((TextView) mainView.findViewById(R.id.lat_first_separator_tv)).setText(firstSeparator);
((TextView) mainView.findViewById(R.id.lon_first_separator_tv)).setText(firstSeparator);
int secondPartSymbols = CoordinateInputFormats.getSecondPartSymbolsCount(format);
int secondPartSymbols = format.getSecondPartSymbolsCount();
setupEditTextEx(R.id.lat_second_input_et, secondPartSymbols, true);
setupEditTextEx(R.id.lon_second_input_et, secondPartSymbols, false);
boolean containsThirdPart = CoordinateInputFormats.containsThirdPart(format);
boolean containsThirdPart = format.isContainsThirdPart();
int visibility = containsThirdPart ? View.VISIBLE : View.GONE;
mainView.findViewById(R.id.lat_second_separator_tv).setVisibility(visibility);
@ -759,11 +760,11 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
mainView.findViewById(R.id.lon_third_input_et).setVisibility(visibility);
if (containsThirdPart) {
String secondSeparator = CoordinateInputFormats.getSecondSeparator(format);
String secondSeparator = format.getSecondSeparator();
((TextView) mainView.findViewById(R.id.lat_second_separator_tv)).setText(secondSeparator);
((TextView) mainView.findViewById(R.id.lon_second_separator_tv)).setText(secondSeparator);
int thirdPartSymbols = CoordinateInputFormats.getThirdPartSymbolsCount(format);
int thirdPartSymbols = format.getThirdPartSymbolsCount();
setupEditTextEx(R.id.lat_third_input_et, thirdPartSymbols, true);
setupEditTextEx(R.id.lon_third_input_et, thirdPartSymbols, false);
}
@ -948,17 +949,17 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
secondPart = "0";
}
int format = getMyApplication().getSettings().COORDS_INPUT_FORMAT.get();
Format format = getMyApplication().getSettings().COORDS_INPUT_FORMAT.get();
StringBuilder res = new StringBuilder();
if ((latitude && !north) || (!latitude && !east)) {
res.append("-");
}
res.append(firstPart);
if (!secondPart.isEmpty()) {
res.append(CoordinateInputFormats.getFirstSeparator(format)).append(secondPart);
res.append(format.getFirstSeparator()).append(secondPart);
}
if (!thirdPart.isEmpty() && CoordinateInputFormats.containsThirdPart(format)) {
res.append(CoordinateInputFormats.getSecondSeparator(format)).append(thirdPart);
if (!thirdPart.isEmpty() && format.isContainsThirdPart()) {
res.append(format.getSecondSeparator()).append(thirdPart);
}
return res.toString();
}

View file

@ -1,108 +1,69 @@
package net.osmand.plus.mapmarkers;
import android.content.Context;
import android.support.annotation.IntDef;
import net.osmand.plus.R;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
public class CoordinateInputFormats {
public static final int DD_MM_MMM = 0;
public static final int DD_MM_MMMM = 1;
public static final int DD_DDDDD = 2;
public static final int DD_DDDDDD = 3;
public static final int DD_MM_SS = 4;
public enum Format {
public static int[] VALUES = new int[]{DD_MM_MMM, DD_MM_MMMM, DD_DDDDD, DD_DDDDDD, DD_MM_SS};
DD_MM_MMM(R.string.dd_mm_mmm_format, true, 2, 3, "°", "."),
DD_MM_MMMM(R.string.dd_mm_mmmm_format, true, 2, 4, "°", "."),
DD_DDDDD(R.string.dd_ddddd_format, false, 5, 0, ".", "°"),
DD_DDDDDD(R.string.dd_dddddd_format, false, 6, 0, ".", "°"),
DD_MM_SS(R.string.dd_mm_ss_format, true, 2, 2, "°", "");
@Retention(RetentionPolicy.SOURCE)
@IntDef({DD_MM_MMM, DD_MM_MMMM, DD_DDDDD, DD_DDDDDD, DD_MM_SS})
@interface CoordinateInputFormatDef {
private final int humanStringId;
private final boolean containsThirdPart;
private final int secondPartSymbolsCount;
private final int thirdPartSymbolsCount;
private final String firstSeparator;
private final String secondSeparator;
Format(int humanStringId,
boolean containsThirdPart,
int secondPartSymbolsCount,
int thirdPartSymbolsCount,
String firstSeparator,
String secondSeparator) {
this.humanStringId = humanStringId;
this.containsThirdPart = containsThirdPart;
this.secondPartSymbolsCount = secondPartSymbolsCount;
this.thirdPartSymbolsCount = thirdPartSymbolsCount;
this.firstSeparator = firstSeparator;
this.secondSeparator = secondSeparator;
}
public static String formatToHumanString(Context ctx, @CoordinateInputFormatDef int format) {
switch (format) {
case DD_MM_MMM:
return ctx.getString(R.string.dd_mm_mmm_format);
case DD_MM_MMMM:
return ctx.getString(R.string.dd_mm_mmmm_format);
case DD_DDDDD:
return ctx.getString(R.string.dd_ddddd_format);
case DD_DDDDDD:
return ctx.getString(R.string.dd_dddddd_format);
case DD_MM_SS:
return ctx.getString(R.string.dd_mm_ss_format);
}
return "Unknown format";
public String toHumanString(Context context) {
return context.getString(humanStringId);
}
public static boolean containsThirdPart(@CoordinateInputFormatDef int format) {
return format == DD_MM_MMM || format == DD_MM_MMMM || format == DD_MM_SS;
public boolean isContainsThirdPart() {
return containsThirdPart;
}
public static int getFirstPartSymbolsCount(@CoordinateInputFormatDef int format, boolean latitude, boolean isTwoDigitsLongtitute) {
public int getFirstPartSymbolsCount(boolean latitude, boolean twoDigitsLongitude) {
if (latitude) {
return 2;
} else {
return isTwoDigitsLongtitute ? 2 : 3;
}
return twoDigitsLongitude ? 2 : 3;
}
public static int getSecondPartSymbolsCount(@CoordinateInputFormatDef int format) {
switch (format) {
case DD_MM_MMM:
case DD_MM_MMMM:
case DD_MM_SS:
return 2;
case DD_DDDDD:
return 5;
case DD_DDDDDD:
return 6;
}
return 0;
public int getSecondPartSymbolsCount() {
return secondPartSymbolsCount;
}
public static int getThirdPartSymbolsCount(@CoordinateInputFormatDef int format) {
switch (format) {
case DD_MM_MMM:
return 3;
case DD_MM_MMMM:
return 4;
case DD_MM_SS:
return 2;
case DD_DDDDD:
case DD_DDDDDD:
}
return 0;
public int getThirdPartSymbolsCount() {
return thirdPartSymbolsCount;
}
public static String getFirstSeparator(@CoordinateInputFormatDef int format) {
switch (format) {
case DD_MM_MMM:
case DD_MM_MMMM:
case DD_MM_SS:
return "°";
case DD_DDDDD:
case DD_DDDDDD:
return ".";
}
return "";
public String getFirstSeparator() {
return firstSeparator;
}
public static String getSecondSeparator(@CoordinateInputFormatDef int format) {
switch (format) {
case DD_MM_MMM:
case DD_MM_MMMM:
return ".";
case DD_DDDDD:
case DD_DDDDDD:
return "°";
case DD_MM_SS:
return "";
}
return "";
public String getSecondSeparator() {
return secondSeparator;
}
}
}