diff --git a/.idea/misc.xml b/.idea/misc.xml index 2cb344c..30d4ca9 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -14,6 +14,7 @@ + diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteAdapter.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteAdapter.java index a1df7e3..3a32db3 100644 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteAdapter.java +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteAdapter.java @@ -55,6 +55,8 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter ByTitleAZ = (gf0, gf1) -> gf0.name.compareTo(gf1.name); public static Comparator ByLastCreated = (gf0, gf1) -> (int) (gf1.dateCreated - gf0.dateCreated); + public static Comparator ByCategory = (gf0, gf1) -> gf0.category.compareTo(gf1.category); + public static Comparator ByDistance = (gf0, gf1) -> 0; // (int) ((gf1.getDistanceFrom(userPosition) - gf0.getDistanceFrom(userPosition)) * 1000); public String getCoordinatesString() { return this.lat + " N, " + this.lng + " E"; } + public Uri getGeoUri() { return Uri.parse("geo:" + this.lat + "," + this.lng + "(" + this.name + ")"); } @@ -162,6 +169,22 @@ public class Geofavorite implements Serializable { return getLat() != 0 && getLng() != 0 && getName() != null && getName().length() > 0; } + /** + * Returns the distance between the current Geofavorite and the provided one, in kilometers. + * @param other Geovavorite + * @return the distance in kilometers + */ + public double getDistanceFrom(Geofavorite other) { + double latDistance = (other.lat-lat) * Math.PI / 180; + double lonDistance = (other.lng-lng) * Math.PI / 180; + double a = + Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + + Math.cos(lat * Math.PI / 180) * Math.cos(other.lat * Math.PI / 180) * + Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2); + double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); + return EARTH_RADIUS * c; + } + /** * Based on Nextcloud Maps's getLetterColor util. * Assigns a color to a category based on its two first letters. @@ -196,4 +219,5 @@ public class Geofavorite implements Serializable { public String toString() { return "[" + getName() + " (" + getLat() + "," + getLng() + ")]"; } + } diff --git a/app/src/main/res/drawable/ic_category_asc.xml b/app/src/main/res/drawable/ic_category_asc.xml new file mode 100644 index 0000000..b45741e --- /dev/null +++ b/app/src/main/res/drawable/ic_category_asc.xml @@ -0,0 +1,16 @@ + + + + + diff --git a/app/src/main/res/drawable/ic_distance_asc.xml b/app/src/main/res/drawable/ic_distance_asc.xml new file mode 100644 index 0000000..957e58c --- /dev/null +++ b/app/src/main/res/drawable/ic_distance_asc.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/sorting_order_fragment.xml b/app/src/main/res/layout/sorting_order_fragment.xml index 73e6c8a..386b0a8 100644 --- a/app/src/main/res/layout/sorting_order_fragment.xml +++ b/app/src/main/res/layout/sorting_order_fragment.xml @@ -111,6 +111,77 @@ + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b01dae3..0c1300d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -44,7 +44,8 @@ Sort by A - Z Newest first - Distance + Category + Distance Name