From a5b040cf17fa788b4cdd5da5c58ffff86a2679d9 Mon Sep 17 00:00:00 2001 From: simonpoole Date: Sat, 4 Nov 2023 12:46:48 +0100 Subject: [PATCH] Remove some code duplication --- src/main/java/de/blau/android/util/Util.java | 24 ++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/java/de/blau/android/util/Util.java b/src/main/java/de/blau/android/util/Util.java index 13de20853d..6f25c3fab3 100644 --- a/src/main/java/de/blau/android/util/Util.java +++ b/src/main/java/de/blau/android/util/Util.java @@ -225,14 +225,7 @@ public static IntCoordinates getCenter(@NonNull final StorageDelegator delegator */ @NonNull public static String getBearingString(@NonNull Context context, double sLon, double sLat, double eLon, double eLat) { - long bearing = GeoMath.bearing(sLon, sLat, eLon, eLat); - - int index = (int) (bearing - 22.5); - if (index < 0) { - index += 360; - } - index = index / 45; - return context.getString(bearingStrings[index]); + return context.getString(bearingStrings[getBearingIndex(sLon, sLat, eLon, eLat)]); } private static final char[] bearingArrows = { '↗', '→', '↘', '↓', '↙', '←', '↖', '↑' }; @@ -248,6 +241,19 @@ public static String getBearingString(@NonNull Context context, double sLon, dou */ @NonNull public static char getBearingArrow(double sLon, double sLat, double eLon, double eLat) { + return bearingArrows[getBearingIndex(sLon, sLat, eLon, eLat)]; + } + + /** + * Calculate the bearing and return an index in to an array holding display values + * + * @param sLon start lon + * @param sLat start lat + * @param eLon end lon + * @param eLat end lat + * @return the index + */ + private static int getBearingIndex(double sLon, double sLat, double eLon, double eLat) { long bearing = GeoMath.bearing(sLon, sLat, eLon, eLat); int index = (int) (bearing - 22.5); @@ -255,7 +261,7 @@ public static char getBearingArrow(double sLon, double sLat, double eLon, double index += 360; } index = index / 45; - return bearingArrows[index]; + return index; } /**