Sorting by category

This commit is contained in:
Daniele 2021-09-22 21:41:21 +02:00
parent 718c654056
commit 9aa2652059
9 changed files with 146 additions and 2 deletions

View File

@ -14,6 +14,7 @@
<entry key="app/src/main/res/layout/activity_list_view.xml" value="0.4" /> <entry key="app/src/main/res/layout/activity_list_view.xml" value="0.4" />
<entry key="app/src/main/res/layout/activity_main.xml" value="0.5307291666666667" /> <entry key="app/src/main/res/layout/activity_main.xml" value="0.5307291666666667" />
<entry key="app/src/main/res/layout/item_geofav.xml" value="0.5307291666666667" /> <entry key="app/src/main/res/layout/item_geofav.xml" value="0.5307291666666667" />
<entry key="app/src/main/res/layout/sorting_order_fragment.xml" value="0.4740740740740741" />
<entry key="app/src/main/res/menu/list_context_menu.xml" value="0.41944444444444445" /> <entry key="app/src/main/res/menu/list_context_menu.xml" value="0.41944444444444445" />
</map> </map>
</option> </option>

View File

@ -55,6 +55,8 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.
public static final int SORT_BY_TITLE = 0; public static final int SORT_BY_TITLE = 0;
public static final int SORT_BY_CREATED = 1; public static final int SORT_BY_CREATED = 1;
public static final int SORT_BY_CATEGORY = 2;
public static final int SORT_BY_DISTANCE = 3;
private Context context; private Context context;
private ItemClickListener itemClickListener; private ItemClickListener itemClickListener;
@ -207,6 +209,10 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.
Collections.sort(geofavoriteListFiltered, Geofavorite.ByTitleAZ); Collections.sort(geofavoriteListFiltered, Geofavorite.ByTitleAZ);
} else if (sortRule == SORT_BY_CREATED) { } else if (sortRule == SORT_BY_CREATED) {
Collections.sort(geofavoriteListFiltered, Geofavorite.ByLastCreated); Collections.sort(geofavoriteListFiltered, Geofavorite.ByLastCreated);
} else if (sortRule == SORT_BY_CATEGORY) {
Collections.sort(geofavoriteListFiltered, Geofavorite.ByCategory);
} else if (sortRule == SORT_BY_DISTANCE) {
Collections.sort(geofavoriteListFiltered, Geofavorite.ByDistance);
} }
} }

View File

@ -289,6 +289,12 @@ public class MainActivity extends AppCompatActivity implements OnSortingOrderLis
case SORT_BY_CREATED: case SORT_BY_CREATED:
sortButton.setImageResource(R.drawable.ic_modification_asc); sortButton.setImageResource(R.drawable.ic_modification_asc);
break; break;
case SORT_BY_CATEGORY:
sortButton.setImageResource(R.drawable.ic_category_asc);
break;
case SORT_BY_DISTANCE:
sortButton.setImageResource(R.drawable.ic_distance_asc);
break;
} }
} }

View File

@ -91,13 +91,14 @@ public class SortingOrderDialogFragment extends DialogFragment {
/** /**
* find all relevant UI elements and set their values. * find all relevant UI elements and set their values.
* TODO: this is REALLY ugly.
* *
* @param view the parent view * @param view the parent view
*/ */
private void setupDialogElements(View view) { private void setupDialogElements(View view) {
mCancel = view.findViewById(R.id.cancel); mCancel = view.findViewById(R.id.cancel);
mTaggedViews = new View[4]; mTaggedViews = new View[8];
mTaggedViews[0] = view.findViewById(R.id.sortByTitleAscending); mTaggedViews[0] = view.findViewById(R.id.sortByTitleAscending);
mTaggedViews[0].setTag(GeofavoriteAdapter.SORT_BY_TITLE); mTaggedViews[0].setTag(GeofavoriteAdapter.SORT_BY_TITLE);
mTaggedViews[1] = view.findViewById(R.id.sortByTitleAscendingText); mTaggedViews[1] = view.findViewById(R.id.sortByTitleAscendingText);
@ -106,6 +107,14 @@ public class SortingOrderDialogFragment extends DialogFragment {
mTaggedViews[2].setTag(GeofavoriteAdapter.SORT_BY_CREATED); mTaggedViews[2].setTag(GeofavoriteAdapter.SORT_BY_CREATED);
mTaggedViews[3] = view.findViewById(R.id.sortByCreationDateDescendingText); mTaggedViews[3] = view.findViewById(R.id.sortByCreationDateDescendingText);
mTaggedViews[3].setTag(GeofavoriteAdapter.SORT_BY_CREATED); mTaggedViews[3].setTag(GeofavoriteAdapter.SORT_BY_CREATED);
mTaggedViews[4] = view.findViewById(R.id.sortByCategoryAscending);
mTaggedViews[4].setTag(GeofavoriteAdapter.SORT_BY_CATEGORY);
mTaggedViews[5] = view.findViewById(R.id.sortByCategoryAscendingText);
mTaggedViews[5].setTag(GeofavoriteAdapter.SORT_BY_CATEGORY);
mTaggedViews[6] = view.findViewById(R.id.sortByDistanceAscending);
mTaggedViews[6].setTag(GeofavoriteAdapter.SORT_BY_DISTANCE);
mTaggedViews[7] = view.findViewById(R.id.sortByDistanceAscendingText);
mTaggedViews[7].setTag(GeofavoriteAdapter.SORT_BY_DISTANCE);
setupActiveOrderSelection(); setupActiveOrderSelection();
} }

View File

@ -38,6 +38,7 @@ import java.util.Comparator;
public class Geofavorite implements Serializable { public class Geofavorite implements Serializable {
public static final String DEFAULT_CATEGORY = "Personal"; public static final String DEFAULT_CATEGORY = "Personal";
private static final double EARTH_RADIUS = 6371; // https://en.wikipedia.org/wiki/Earth_radius
/** /**
* JSON Definition: * JSON Definition:
@ -148,12 +149,18 @@ public class Geofavorite implements Serializable {
this.comment = comment; this.comment = comment;
} }
/**
* Comparators for list order
*/
public static Comparator<Geofavorite> ByTitleAZ = (gf0, gf1) -> gf0.name.compareTo(gf1.name); public static Comparator<Geofavorite> ByTitleAZ = (gf0, gf1) -> gf0.name.compareTo(gf1.name);
public static Comparator<Geofavorite> ByLastCreated = (gf0, gf1) -> (int) (gf1.dateCreated - gf0.dateCreated); public static Comparator<Geofavorite> ByLastCreated = (gf0, gf1) -> (int) (gf1.dateCreated - gf0.dateCreated);
public static Comparator<Geofavorite> ByCategory = (gf0, gf1) -> gf0.category.compareTo(gf1.category);
public static Comparator<Geofavorite> ByDistance = (gf0, gf1) -> 0; // (int) ((gf1.getDistanceFrom(userPosition) - gf0.getDistanceFrom(userPosition)) * 1000);
public String getCoordinatesString() { public String getCoordinatesString() {
return this.lat + " N, " + this.lng + " E"; return this.lat + " N, " + this.lng + " E";
} }
public Uri getGeoUri() { public Uri getGeoUri() {
return Uri.parse("geo:" + this.lat + "," + this.lng + "(" + this.name + ")"); 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; 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. * Based on Nextcloud Maps's getLetterColor util.
* Assigns a color to a category based on its two first letters. * Assigns a color to a category based on its two first letters.
@ -196,4 +219,5 @@ public class Geofavorite implements Serializable {
public String toString() { public String toString() {
return "[" + getName() + " (" + getLat() + "," + getLng() + ")]"; return "[" + getName() + " (" + getLat() + "," + getLng() + ")]";
} }
} }

View File

@ -0,0 +1,16 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M12,2l-5.5,9h11z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M17.5,17.5m-4.5,0a4.5,4.5 0,1 1,9 0a4.5,4.5 0,1 1,-9 0"/>
<path
android:fillColor="@android:color/white"
android:pathData="M3,13.5h8v8H3z"/>
</vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M21,3L3,10.53v0.98l6.84,2.65L12.48,21h0.98L21,3z"/>
</vector>

View File

@ -111,6 +111,77 @@
</TableRow> </TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/standard_half_margin">
<ImageButton
android:id="@+id/sortByCategoryAscending"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@color/transparent"
android:paddingStart="@dimen/standard_padding"
android:paddingTop="@dimen/standard_half_padding"
android:paddingEnd="@dimen/standard_half_padding"
android:paddingBottom="@dimen/standard_half_padding"
android:contentDescription="@string/menu_item_sort_by_category_a_z"
android:src="@drawable/ic_category_asc"/>
<TextView
android:id="@+id/sortByCategoryAscendingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:ellipsize="middle"
android:paddingStart="@dimen/zero"
android:paddingTop="@dimen/standard_half_padding"
android:paddingEnd="@dimen/standard_half_padding"
android:paddingBottom="@dimen/standard_half_padding"
android:singleLine="true"
android:text="@string/menu_item_sort_by_category_a_z"
android:textSize="@dimen/two_line_primary_text_size" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/standard_half_margin"
android:visibility="gone"> <!-- TODO: complete sorting by distance -->
<ImageButton
android:id="@+id/sortByDistanceAscending"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@color/transparent"
android:paddingStart="@dimen/standard_padding"
android:paddingTop="@dimen/standard_half_padding"
android:paddingEnd="@dimen/standard_half_padding"
android:paddingBottom="@dimen/standard_half_padding"
android:contentDescription="@string/menu_item_sort_by_distance_nearest_first"
android:src="@drawable/ic_distance_asc"/>
<TextView
android:id="@+id/sortByDistanceAscendingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:ellipsize="middle"
android:paddingStart="@dimen/zero"
android:paddingTop="@dimen/standard_half_padding"
android:paddingEnd="@dimen/standard_half_padding"
android:paddingBottom="@dimen/standard_half_padding"
android:singleLine="true"
android:text="@string/menu_item_sort_by_distance_nearest_first"
android:textSize="@dimen/two_line_primary_text_size" />
</TableRow>
</TableLayout> </TableLayout>
</ScrollView> </ScrollView>

View File

@ -44,7 +44,8 @@
<string name="sort_by">Sort by</string> <string name="sort_by">Sort by</string>
<string name="menu_item_sort_by_title_a_z">A - Z</string> <string name="menu_item_sort_by_title_a_z">A - Z</string>
<string name="menu_item_sort_by_date_newest_first">Newest first</string> <string name="menu_item_sort_by_date_newest_first">Newest first</string>
<string name="menu_item_sort_by_distance">Distance</string> <string name="menu_item_sort_by_category_a_z">Category</string>
<string name="menu_item_sort_by_distance_nearest_first">Distance</string>
<!-- Geobookmarks detail --> <!-- Geobookmarks detail -->
<string name="name">Name</string> <string name="name">Name</string>