Add sort for history

This commit is contained in:
PavelRatushny 2017-09-07 18:17:08 +03:00
parent 23749c1088
commit 3a6d81bd52
7 changed files with 292 additions and 44 deletions

View file

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<include layout="@layout/list_item_divider"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:orientation="horizontal"
android:background="?attr/bg_color">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/date_title"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="56dp"
android:paddingLeft="16dp"
android:paddingStart="16dp"
android:gravity="center_vertical"
tools:text="Today"/>
<ImageButton
android:id="@+id/date_options_button"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_gravity="center_vertical|end"
android:background="@null"
android:focusableInTouchMode="true"
tools:src="@drawable/ic_overflow_menu_white"/>
</LinearLayout>
</LinearLayout>

View file

@ -102,7 +102,6 @@
android:id="@+id/map_marker_options_button"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_gravity="center_vertical|end"
android:background="@drawable/marker_circle_background_light_with_inset"
android:focusableInTouchMode="true"
tools:src="@drawable/ic_action_marker_passed"/>

View file

@ -9,6 +9,10 @@
3. All your modified/created strings are in the top of the file (to make easier find what\'s translated).
PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy
-->
<string name="today">Today</string>
<string name="yesterday">Yesterday</string>
<string name="last_seven_days">Last 7 days</string>
<string name="this_year">This year</string>
<string name="widget">Widget</string>
<string name="top_bar">Top bar</string>
<string name="move_all_to_history">Move all to history</string>

View file

@ -10,6 +10,8 @@ import net.osmand.plus.mapmarkers.MapMarkersDbHelper;
import net.osmand.util.Algorithms;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class MapMarkersHelper {
@ -172,14 +174,50 @@ public class MapMarkersHelper {
private void loadMarkers() {
mapMarkers.clear();
mapMarkersHistory.clear();
mapMarkers.addAll(markersDbHelper.getActiveMarkers());
mapMarkersHistory.addAll(markersDbHelper.getMarkersHistory());
List<MapMarker> activeMarkers = markersDbHelper.getActiveMarkers();
sortActiveMarkers(activeMarkers);
mapMarkers.addAll(activeMarkers);
List<MapMarker> markersHistory = markersDbHelper.getMarkersHistory();
sortHistoryMarkers(markersHistory);
mapMarkersHistory.addAll(markersHistory);
if (!ctx.isApplicationInitializing()) {
lookupAddressAll();
}
}
private void sortActiveMarkers(List<MapMarker> markers) {
Collections.sort(markers, new Comparator<MapMarker>() {
@Override
public int compare(MapMarker mapMarker1, MapMarker mapMarker2) {
if (mapMarker1.creationDate > mapMarker2.creationDate) {
return -1;
} else if (mapMarker1.creationDate == mapMarker2.creationDate) {
return 0;
} else {
return 1;
}
}
});
}
private void sortHistoryMarkers(List<MapMarker> markers) {
Collections.sort(markers, new Comparator<MapMarker>() {
@Override
public int compare(MapMarker mapMarker1, MapMarker mapMarker2) {
if (mapMarker1.visitedDate > mapMarker2.visitedDate) {
return -1;
} else if (mapMarker1.visitedDate == mapMarker2.visitedDate) {
return 0;
} else {
return 1;
}
}
});
}
private void lookupAddress(final MapMarker mapMarker) {
if (mapMarker != null && mapMarker.pointDescription.isSearchingAddress(ctx)) {
cancelPointAddressRequests(mapMarker.point);

View file

@ -11,17 +11,28 @@ import android.view.View;
import android.view.ViewGroup;
import net.osmand.data.PointDescription;
import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.MapMarkersHelper.MapMarker;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapmarkers.adapters.MapMarkersHistoryAdapter;
public class MapMarkersHistoryFragment extends Fragment {
public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHelper.MapMarkerChangedListener {
MapMarkersHistoryAdapter adapter;
OsmandApplication app;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = getMyApplication();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
app = getMyApplication();
final RecyclerView recyclerView = new RecyclerView(getContext());
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
final MapActivity mapActivity = (MapActivity) getActivity();
@ -31,22 +42,48 @@ public class MapMarkersHistoryFragment extends Fragment {
@Override
public void onItemClick(View view) {
int pos = recyclerView.indexOfChild(view);
MapMarker marker = adapter.getItem(pos);
Object item = adapter.getItem(pos);
if (item instanceof MapMarker) {
MapMarker marker = (MapMarker) item;
mapActivity.getMyApplication().getSettings().setMapLocationToShow(marker.getLatitude(), marker.getLongitude(),
15, new PointDescription(PointDescription.POINT_TYPE_LOCATION, marker.getPointDescription(mapActivity).getName()),
false, null);
MapActivity.launchMapActivityMoveToTop(mapActivity);
((DialogFragment) getParentFragment()).dismiss();
}
}
});
recyclerView.setAdapter(adapter);
app.getMapMarkersHelper().addListener(this);
return recyclerView;
}
@Override
public void onDestroy() {
app.getMapMarkersHelper().removeListener(this);
super.onDestroy();
}
void updateAdapter() {
adapter.createHeaders();
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
public OsmandApplication getMyApplication() {
return (OsmandApplication)getActivity().getApplication();
}
@Override
public void onMapMarkerChanged(MapMarker mapMarker) {
updateAdapter();
}
@Override
public void onMapMarkersChanged() {
updateAdapter();
}
}

View file

@ -0,0 +1,20 @@
package net.osmand.plus.mapmarkers.adapters;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import net.osmand.plus.R;
public class MapMarkerDateViewHolder extends RecyclerView.ViewHolder {
final TextView date;
final ImageButton optionsBtn;
public MapMarkerDateViewHolder(View itemView) {
super(itemView);
date = itemView.findViewById(R.id.date_title);
optionsBtn = itemView.findViewById(R.id.date_options_button);
}
}

View file

@ -10,17 +10,87 @@ import net.osmand.plus.MapMarkersHelper.MapMarker;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class MapMarkersHistoryAdapter extends RecyclerView.Adapter<MapMarkerItemViewHolder> {
public class MapMarkersHistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int DATE_TYPE = 1;
private static final int MARKER_TYPE = 2;
private static final int TODAY_HEADER = 56;
private static final int YESTERDAY_HEADER = 57;
private static final int LAST_SEVEN_DAYS_HEADER = 58;
private static final int THIS_YEAR_HEADER = 59;
private OsmandApplication app;
private List<MapMarker> markers;
private List<Object> items = new ArrayList<>();
private MapMarkersHistoryAdapterListener listener;
public MapMarkersHistoryAdapter(OsmandApplication app) {
this.app = app;
markers = app.getMapMarkersHelper().getMapMarkersHistory();
createHeaders();
}
public void createHeaders() {
items.clear();
List<MapMarker> markersHistory = app.getMapMarkersHelper().getMapMarkersHistory();
int previousHeader = -1;
int monthsDisplayed = 0;
Calendar currentDateCalendar = Calendar.getInstance();
currentDateCalendar.setTimeInMillis(System.currentTimeMillis());
int currentDay = currentDateCalendar.get(Calendar.DAY_OF_YEAR);
int currentYear = currentDateCalendar.get(Calendar.YEAR);
Calendar markerCalendar = Calendar.getInstance();
for (int i = 0; i < markersHistory.size(); i++) {
MapMarker marker = markersHistory.get(i);
markerCalendar.setTimeInMillis(marker.visitedDate);
int markerDay = markerCalendar.get(Calendar.DAY_OF_YEAR);
int markerMonth = markerCalendar.get(Calendar.MONTH);
int markerYear = markerCalendar.get(Calendar.YEAR);
if (markerYear == currentYear) {
if (markerDay == currentDay) {
if (previousHeader != TODAY_HEADER) {
items.add(TODAY_HEADER);
previousHeader = TODAY_HEADER;
}
items.add(marker);
} else if (markerDay == currentDay - 1) {
if (previousHeader != YESTERDAY_HEADER) {
items.add(YESTERDAY_HEADER);
previousHeader = YESTERDAY_HEADER;
}
items.add(marker);
} else if (currentDay - 7 >= markerDay && markerYear != 1970) {
if (previousHeader != LAST_SEVEN_DAYS_HEADER) {
items.add(LAST_SEVEN_DAYS_HEADER);
previousHeader = LAST_SEVEN_DAYS_HEADER;
}
items.add(marker);
} else {
if (previousHeader != markerMonth && monthsDisplayed < 3) {
items.add(markerMonth);
previousHeader = markerMonth;
monthsDisplayed += 1;
} else if (previousHeader != markerMonth && previousHeader != THIS_YEAR_HEADER) {
items.add(THIS_YEAR_HEADER);
previousHeader = THIS_YEAR_HEADER;
}
items.add(marker);
}
} else {
if (previousHeader != markerYear) {
items.add(markerYear);
previousHeader = markerYear;
}
items.add(marker);
}
}
}
public void setAdapterListener(MapMarkersHistoryAdapterListener listener) {
@ -28,7 +98,8 @@ public class MapMarkersHistoryAdapter extends RecyclerView.Adapter<MapMarkerItem
}
@Override
public MapMarkerItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
if (viewType == MARKER_TYPE) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.map_marker_item_new, viewGroup, false);
view.setOnClickListener(new View.OnClickListener() {
@Override
@ -37,44 +108,85 @@ public class MapMarkersHistoryAdapter extends RecyclerView.Adapter<MapMarkerItem
}
});
return new MapMarkerItemViewHolder(view);
} else if (viewType == DATE_TYPE) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.map_marker_item_date, viewGroup, false);
return new MapMarkerDateViewHolder(view);
} else {
throw new IllegalArgumentException("Unsupported view type");
}
}
@Override
public void onBindViewHolder(final MapMarkerItemViewHolder holder, int pos) {
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
IconsCache iconsCache = app.getIconsCache();
MapMarker marker = markers.get(pos);
holder.iconReorder.setVisibility(View.GONE);
if (holder instanceof MapMarkerItemViewHolder) {
final MapMarkerItemViewHolder itemViewHolder = (MapMarkerItemViewHolder) holder;
final MapMarker marker = (MapMarker) getItem(position);
itemViewHolder.iconReorder.setVisibility(View.GONE);
int color = MapMarker.getColorId(marker.colorIndex);
holder.icon.setImageDrawable(iconsCache.getIcon(R.drawable.ic_action_flag_dark, color));
itemViewHolder.icon.setImageDrawable(iconsCache.getIcon(R.drawable.ic_action_flag_dark, color));
holder.title.setText(marker.getName(app));
itemViewHolder.title.setText(marker.getName(app));
holder.description.setText(marker.visitedDate + "");
itemViewHolder.description.setText(marker.visitedDate + "");
holder.optionsBtn.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_history));
holder.optionsBtn.setOnClickListener(new View.OnClickListener() {
itemViewHolder.optionsBtn.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_history));
itemViewHolder.optionsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = holder.getAdapterPosition();
int position = itemViewHolder.getAdapterPosition();
if (position < 0) {
return;
}
MapMarker marker = markers.get(position);
app.getMapMarkersHelper().restoreMarkerFromHistory(marker, 0);
notifyItemRemoved(position);
}
});
} else if (holder instanceof MapMarkerDateViewHolder) {
final MapMarkerDateViewHolder dateViewHolder = (MapMarkerDateViewHolder) holder;
final Integer dateHeader = (Integer) getItem(position);
String dateString;
if (dateHeader == TODAY_HEADER) {
dateString = app.getString(R.string.today);
} else if (dateHeader == YESTERDAY_HEADER) {
dateString = app.getString(R.string.yesterday);
} else if (dateHeader == LAST_SEVEN_DAYS_HEADER) {
dateString = app.getString(R.string.last_seven_days);
} else if (dateHeader == THIS_YEAR_HEADER) {
dateString = app.getString(R.string.this_year);
} else if (dateHeader % 100 == 0) {
dateString = getMonth(dateHeader);
} else {
dateString = String.valueOf(dateHeader);
}
dateViewHolder.date.setText(dateString);
}
}
@Override
public int getItemViewType(int position) {
Object item = items.get(position);
if (item instanceof MapMarker) {
return MARKER_TYPE;
} else if (item instanceof Integer) {
return DATE_TYPE;
} else {
throw new IllegalArgumentException("Unsupported view type");
}
}
@Override
public int getItemCount() {
return markers.size();
return items.size();
}
public MapMarker getItem(int position) {
return markers.get(position);
public Object getItem(int position) {
return items.get(position);
}
private String getMonth(int month) {
return new DateFormatSymbols().getMonths()[month-1];
}
public interface MapMarkersHistoryAdapterListener {