From 97c248dc98f85ccd775a64115e172a466774209a Mon Sep 17 00:00:00 2001 From: PavelRatushny Date: Fri, 10 Nov 2017 14:25:38 +0200 Subject: [PATCH] Add gpx menu --- ...eader.xml => add_markers_group_header.xml} | 0 ..._marker_add_group_bottom_sheet_dialog.xml} | 10 +- ...ouritesGroupBottomSheetDialogFragment.java | 67 +-------- ...MarkersGroupBottomSheetDialogFragment.java | 68 +++++---- ...dTracksGroupBottomSheetDialogFragment.java | 138 ++++++++++++++++++ .../mapmarkers/MapMarkersGroupsFragment.java | 42 +++--- ...MarkersGroupBottomSheetDialogFragment.java | 75 ++++++++++ .../adapters/FavouritesGroupsAdapter.java | 43 +----- .../mapmarkers/adapters/GroupsAdapter.java | 58 ++++++++ .../adapters/TracksGroupsAdapter.java | 43 ++++++ 10 files changed, 391 insertions(+), 153 deletions(-) rename OsmAnd/res/layout/{add_favourites_group_header.xml => add_markers_group_header.xml} (100%) rename OsmAnd/res/layout/{fragment_marker_add_favourites_group_bottom_sheet_dialog.xml => fragment_marker_add_group_bottom_sheet_dialog.xml} (81%) create mode 100644 OsmAnd/src/net/osmand/plus/mapmarkers/AddTracksGroupBottomSheetDialogFragment.java create mode 100644 OsmAnd/src/net/osmand/plus/mapmarkers/SelectionMarkersGroupBottomSheetDialogFragment.java create mode 100644 OsmAnd/src/net/osmand/plus/mapmarkers/adapters/GroupsAdapter.java create mode 100644 OsmAnd/src/net/osmand/plus/mapmarkers/adapters/TracksGroupsAdapter.java diff --git a/OsmAnd/res/layout/add_favourites_group_header.xml b/OsmAnd/res/layout/add_markers_group_header.xml similarity index 100% rename from OsmAnd/res/layout/add_favourites_group_header.xml rename to OsmAnd/res/layout/add_markers_group_header.xml diff --git a/OsmAnd/res/layout/fragment_marker_add_favourites_group_bottom_sheet_dialog.xml b/OsmAnd/res/layout/fragment_marker_add_group_bottom_sheet_dialog.xml similarity index 81% rename from OsmAnd/res/layout/fragment_marker_add_favourites_group_bottom_sheet_dialog.xml rename to OsmAnd/res/layout/fragment_marker_add_group_bottom_sheet_dialog.xml index 3dd4b75cb7..3e6b37668a 100644 --- a/OsmAnd/res/layout/fragment_marker_add_favourites_group_bottom_sheet_dialog.xml +++ b/OsmAnd/res/layout/fragment_marker_add_group_bottom_sheet_dialog.xml @@ -6,12 +6,20 @@ android:orientation="vertical"> + + favoriteGroups; - private MapMarkersHelper mapMarkersHelper; - - public void setListener(AddFavouriteGroupListener listener) { - this.listener = listener; - } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); favoriteGroups = getMyApplication().getFavorites().getFavoriteGroups(); - mapMarkersHelper = getMyApplication().getMapMarkersHelper(); } - @Nullable @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - final int themeRes = nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme; - final View mainView = View.inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.fragment_marker_add_favourites_group_bottom_sheet_dialog, container); - - final RecyclerView recyclerView = mainView.findViewById(R.id.favourites_group_recycler_view); - recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); - final FavouritesGroupsAdapter adapter = new FavouritesGroupsAdapter(getContext(), favoriteGroups); - adapter.setAdapterListener(new FavouritesGroupsAdapter.FavouritesGroupsAdapterListener() { - @Override - public void onItemClick(View view) { - int position = recyclerView.getChildAdapterPosition(view); - if (position == RecyclerView.NO_POSITION) { - return; - } - FavoriteGroup group = favoriteGroups.get(position - 1); - MarkersSyncGroup markersSyncGroup = new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE, group.color); - mapMarkersHelper.addMarkersSyncGroup(markersSyncGroup); - mapMarkersHelper.syncGroup(markersSyncGroup); - if (listener != null) { - listener.onFavouriteGroupAdded(); - } - dismiss(); - } - }); - recyclerView.setAdapter(adapter); - - mainView.findViewById(R.id.close_row).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - dismiss(); - } - }); - - setupHeightAndBackground(mainView, R.id.favourites_group_recycler_view); - - return mainView; + public MarkersSyncGroup createMapMarkersSyncGroup(int position) { + FavoriteGroup group = favoriteGroups.get(position - 1); + return new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE, group.color); } - public interface AddFavouriteGroupListener { - void onFavouriteGroupAdded(); + @Override + public void createAdapter() { + adapter = new FavouritesGroupsAdapter(getContext(), favoriteGroups); } } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/AddMarkersGroupBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/AddMarkersGroupBottomSheetDialogFragment.java index 738ccf1766..fb5324a303 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/AddMarkersGroupBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/AddMarkersGroupBottomSheetDialogFragment.java @@ -2,57 +2,64 @@ package net.osmand.plus.mapmarkers; import android.os.Bundle; import android.support.annotation.Nullable; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; import android.view.ContextThemeWrapper; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ImageView; -import android.widget.TextView; +import net.osmand.plus.MapMarkersHelper; +import net.osmand.plus.MapMarkersHelper.MarkersSyncGroup; import net.osmand.plus.R; import net.osmand.plus.base.MenuBottomSheetDialogFragment; +import net.osmand.plus.mapmarkers.adapters.GroupsAdapter; -public class AddMarkersGroupBottomSheetDialogFragment extends MenuBottomSheetDialogFragment { +public abstract class AddMarkersGroupBottomSheetDialogFragment extends MenuBottomSheetDialogFragment { - public final static String TAG = "AddMarkersGroupBottomSheetDialogFragment"; + public static final String TAG = "AddMarkersGroupBottomSheetDialogFragment"; - private AddMarkersGroupFragmentListener listener; + private AddGroupListener listener; + protected View mainView; + protected GroupsAdapter adapter; + protected MapMarkersHelper mapMarkersHelper; - public void setListener(AddMarkersGroupFragmentListener listener) { + public void setListener(AddGroupListener listener) { this.listener = listener; } + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + mapMarkersHelper = getMyApplication().getMapMarkersHelper(); + } + @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final int themeRes = nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme; - final View mainView = View.inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.fragment_marker_add_markers_group_bottom_sheet_dialog, container); + mainView = View.inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.fragment_marker_add_group_bottom_sheet_dialog, container); - if (nightMode) { - ((TextView) mainView.findViewById(R.id.add_group_title)).setTextColor(getResources().getColor(R.color.ctx_menu_info_text_dark)); - } - - ((ImageView) mainView.findViewById(R.id.favourites_icon)).setImageDrawable(getContentIcon(R.drawable.ic_action_fav_dark)); - ((ImageView) mainView.findViewById(R.id.waypoints_icon)).setImageDrawable(getContentIcon(R.drawable.ic_action_polygom_dark)); - - mainView.findViewById(R.id.favourites_row).setOnClickListener(new View.OnClickListener() { + final RecyclerView recyclerView = mainView.findViewById(R.id.groups_recycler_view); + recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + createAdapter(); + adapter.setAdapterListener(new GroupsAdapter.GroupsAdapterListener() { @Override - public void onClick(View view) { + public void onItemClick(View view) { + int position = recyclerView.getChildAdapterPosition(view); + if (position == RecyclerView.NO_POSITION) { + return; + } + MarkersSyncGroup group = createMapMarkersSyncGroup(position); + mapMarkersHelper.addMarkersSyncGroup(group); + mapMarkersHelper.syncGroup(group); if (listener != null) { - listener.favouritesOnClick(); - } - dismiss(); - } - }); - mainView.findViewById(R.id.waypoints_row).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - if (listener != null) { - listener.waypointsOnClick(); + listener.onGroupAdded(); } dismiss(); } }); + recyclerView.setAdapter(adapter); mainView.findViewById(R.id.close_row).setOnClickListener(new View.OnClickListener() { @Override @@ -61,15 +68,16 @@ public class AddMarkersGroupBottomSheetDialogFragment extends MenuBottomSheetDia } }); - setupHeightAndBackground(mainView, R.id.add_markers_group_scroll_view); + setupHeightAndBackground(mainView, R.id.groups_recycler_view); return mainView; } - interface AddMarkersGroupFragmentListener { + protected abstract void createAdapter(); - void favouritesOnClick(); + protected abstract MarkersSyncGroup createMapMarkersSyncGroup(int position); - void waypointsOnClick(); + public interface AddGroupListener { + void onGroupAdded(); } } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/AddTracksGroupBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/AddTracksGroupBottomSheetDialogFragment.java new file mode 100644 index 0000000000..82859e338a --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/AddTracksGroupBottomSheetDialogFragment.java @@ -0,0 +1,138 @@ +package net.osmand.plus.mapmarkers; + +import android.os.AsyncTask; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v7.widget.RecyclerView; +import android.view.View; +import android.widget.ProgressBar; + +import net.osmand.AndroidUtils; +import net.osmand.IndexConstants; +import net.osmand.plus.GPXDatabase; +import net.osmand.plus.GPXDatabase.GpxDataItem; +import net.osmand.plus.GPXUtilities; +import net.osmand.plus.GPXUtilities.GPXFile; +import net.osmand.plus.GPXUtilities.GPXTrackAnalysis; +import net.osmand.plus.MapMarkersHelper.MarkersSyncGroup; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.mapmarkers.adapters.TracksGroupsAdapter; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class AddTracksGroupBottomSheetDialogFragment extends AddMarkersGroupBottomSheetDialogFragment { + + private ProcessGpxTask asyncProcessor; + private List gpxList = new ArrayList<>(); + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + asyncProcessor = new ProcessGpxTask(); + asyncProcessor.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + + @Override + public void createAdapter() { + adapter = new TracksGroupsAdapter(getContext(), gpxList); + } + + @Override + public MarkersSyncGroup createMapMarkersSyncGroup(int position) { + GpxDataItem gpxDataItem = gpxList.get(position - 1); + File gpx = gpxDataItem.getFile(); + return new MarkersSyncGroup(gpx.getAbsolutePath(), AndroidUtils.trimExtension(gpx.getName()), MarkersSyncGroup.GPX_TYPE); + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + if (asyncProcessor != null) { + asyncProcessor.cancel(false); + asyncProcessor = null; + } + } + + public class ProcessGpxTask extends AsyncTask { + + private OsmandApplication app = getMyApplication(); + private Map processedDataFiles = new HashMap<>(); + private GPXDatabase db = app.getGpxDatabase(); + private ProgressBar progressBar = (ProgressBar) mainView.findViewById(R.id.progress_bar);; + private RecyclerView recyclerView = (RecyclerView) mainView.findViewById(R.id.groups_recycler_view); + + ProcessGpxTask() { + List dataItems = db.getItems(); + for (GpxDataItem item : dataItems) { + processedDataFiles.put(item.getFile(), item); + } + } + + @Override + protected void onPreExecute() { + recyclerView.setVisibility(View.GONE); + progressBar.setVisibility(View.VISIBLE); + } + + @Override + protected Void doInBackground(Void... params) { + File gpxPath = app.getAppPath(IndexConstants.GPX_INDEX_DIR); + if (gpxPath.canRead()) { + processGPXFolder(gpxPath, ""); + } + return null; + } + + private File[] listFilesSorted(File dir) { + File[] listFiles = dir.listFiles(); + if (listFiles == null) { + return new File[0]; + } + Arrays.sort(listFiles); + return listFiles; + } + + private void processGPXFolder(File gpxPath, String gpxSubfolder) { + for (File gpxFile : listFilesSorted(gpxPath)) { + if (gpxFile.isDirectory()) { + String sub = gpxSubfolder.length() == 0 ? + gpxFile.getName() : gpxSubfolder + "/" + gpxFile.getName(); + processGPXFolder(gpxFile, sub); + } else if (gpxFile.isFile() && gpxFile.getName().toLowerCase().endsWith(".gpx")) { + GpxDataItem item = processedDataFiles.get(gpxFile); + if (item == null || item.getFileLastModifiedTime() != gpxFile.lastModified()) { + GPXFile f = GPXUtilities.loadGPXFile(app, gpxFile); + GPXTrackAnalysis analysis = f.getAnalysis(gpxFile.lastModified()); + if (item == null) { + item = new GpxDataItem(gpxFile, analysis); + db.add(item); + } else { + db.updateAnalysis(item, analysis); + } + } + processedDataFiles.put(gpxFile, item); + if (item.getAnalysis().wptPoints > 0) { + gpxList.add(item); + } + } + if (isCancelled()) { + break; + } + } + } + + @Override + protected void onPostExecute(Void aVoid) { + asyncProcessor = null; + adapter.notifyDataSetChanged(); + progressBar.setVisibility(View.GONE); + recyclerView.setVisibility(View.VISIBLE); + } + } +} diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroupsFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroupsFragment.java index f6d7b732f3..e537f0ac98 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroupsFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroupsFragment.java @@ -35,8 +35,7 @@ import net.osmand.plus.base.MapViewTrackingUtilities; import net.osmand.plus.dashboard.DashLocationFragment; import net.osmand.plus.mapmarkers.adapters.MapMarkerItemViewHolder; import net.osmand.plus.mapmarkers.adapters.MapMarkersGroupsAdapter; -import net.osmand.plus.mapmarkers.AddMarkersGroupBottomSheetDialogFragment.AddMarkersGroupFragmentListener; -import net.osmand.plus.mapmarkers.AddFavouritesGroupBottomSheetDialogFragment.AddFavouriteGroupListener; +import net.osmand.plus.mapmarkers.SelectionMarkersGroupBottomSheetDialogFragment.AddMarkersGroupFragmentListener; import net.osmand.plus.widgets.EmptyStateRecyclerView; import net.osmand.util.MapUtils; @@ -62,13 +61,13 @@ public class MapMarkersGroupsFragment extends Fragment implements OsmAndCompassL final boolean night = !mapActivity.getMyApplication().getSettings().isLightContent(); mainView = inflater.inflate(R.layout.fragment_map_markers_groups, container, false); - Fragment addMarkersGroupFragment = getChildFragmentManager().findFragmentByTag(AddMarkersGroupBottomSheetDialogFragment.TAG); - if (addMarkersGroupFragment != null) { - ((AddMarkersGroupBottomSheetDialogFragment) addMarkersGroupFragment).setListener(createAddMarkersGroupFragmentListener()); + Fragment selectionMarkersGroupFragment = getChildFragmentManager().findFragmentByTag(SelectionMarkersGroupBottomSheetDialogFragment.TAG); + if (selectionMarkersGroupFragment != null) { + ((SelectionMarkersGroupBottomSheetDialogFragment) selectionMarkersGroupFragment).setListener(createAddMarkersGroupFragmentListener()); } - Fragment addFavouritesGroupFragment = getChildFragmentManager().findFragmentByTag(AddFavouritesGroupBottomSheetDialogFragment.TAG); - if (addFavouritesGroupFragment != null) { - ((AddFavouritesGroupBottomSheetDialogFragment) addFavouritesGroupFragment).setListener(createAddFavouritesGroupListener()); + Fragment addGroupFragment = getChildFragmentManager().findFragmentByTag(AddMarkersGroupBottomSheetDialogFragment.TAG); + if (addGroupFragment != null) { + ((AddMarkersGroupBottomSheetDialogFragment) addGroupFragment).setListener(createAddGroupListener()); } final EmptyStateRecyclerView recyclerView = (EmptyStateRecyclerView) mainView.findViewById(R.id.list); @@ -279,23 +278,22 @@ public class MapMarkersGroupsFragment extends Fragment implements OsmAndCompassL } private void openAddGroupMenu() { - AddMarkersGroupBottomSheetDialogFragment fragment = new AddMarkersGroupBottomSheetDialogFragment(); + SelectionMarkersGroupBottomSheetDialogFragment fragment = new SelectionMarkersGroupBottomSheetDialogFragment(); fragment.setListener(createAddMarkersGroupFragmentListener()); fragment.setUsedOnMap(false); + fragment.show(getChildFragmentManager(), SelectionMarkersGroupBottomSheetDialogFragment.TAG); + } + + private void openAddGroupMenu(AddMarkersGroupBottomSheetDialogFragment fragment) { + fragment.setListener(createAddGroupListener()); + fragment.setUsedOnMap(false); fragment.show(getChildFragmentManager(), AddMarkersGroupBottomSheetDialogFragment.TAG); } - private void openAddFavouritesGroupMenu() { - AddFavouritesGroupBottomSheetDialogFragment fragment = new AddFavouritesGroupBottomSheetDialogFragment(); - fragment.setListener(createAddFavouritesGroupListener()); - fragment.setUsedOnMap(false); - fragment.show(getChildFragmentManager(), AddFavouritesGroupBottomSheetDialogFragment.TAG); - } - - private AddFavouriteGroupListener createAddFavouritesGroupListener() { - return new AddFavouriteGroupListener() { + private AddMarkersGroupBottomSheetDialogFragment.AddGroupListener createAddGroupListener() { + return new AddMarkersGroupBottomSheetDialogFragment.AddGroupListener() { @Override - public void onFavouriteGroupAdded() { + public void onGroupAdded() { updateAdapter(); } }; @@ -305,12 +303,14 @@ public class MapMarkersGroupsFragment extends Fragment implements OsmAndCompassL return new AddMarkersGroupFragmentListener() { @Override public void favouritesOnClick() { - openAddFavouritesGroupMenu(); + AddFavouritesGroupBottomSheetDialogFragment fragment = new AddFavouritesGroupBottomSheetDialogFragment(); + openAddGroupMenu(fragment); } @Override public void waypointsOnClick() { - + AddTracksGroupBottomSheetDialogFragment fragment = new AddTracksGroupBottomSheetDialogFragment(); + openAddGroupMenu(fragment); } }; } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/SelectionMarkersGroupBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/SelectionMarkersGroupBottomSheetDialogFragment.java new file mode 100644 index 0000000000..720cb2068e --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/SelectionMarkersGroupBottomSheetDialogFragment.java @@ -0,0 +1,75 @@ +package net.osmand.plus.mapmarkers; + +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.view.ContextThemeWrapper; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import net.osmand.plus.R; +import net.osmand.plus.base.MenuBottomSheetDialogFragment; + +public class SelectionMarkersGroupBottomSheetDialogFragment extends MenuBottomSheetDialogFragment { + + public final static String TAG = "SelectionMarkersGroupBottomSheetDialogFragment"; + + private AddMarkersGroupFragmentListener listener; + + public void setListener(AddMarkersGroupFragmentListener listener) { + this.listener = listener; + } + + @Nullable + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + final int themeRes = nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme; + final View mainView = View.inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.fragment_marker_add_markers_group_bottom_sheet_dialog, container); + + if (nightMode) { + ((TextView) mainView.findViewById(R.id.add_group_title)).setTextColor(getResources().getColor(R.color.ctx_menu_info_text_dark)); + } + + ((ImageView) mainView.findViewById(R.id.favourites_icon)).setImageDrawable(getContentIcon(R.drawable.ic_action_fav_dark)); + ((ImageView) mainView.findViewById(R.id.waypoints_icon)).setImageDrawable(getContentIcon(R.drawable.ic_action_polygom_dark)); + + mainView.findViewById(R.id.favourites_row).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (listener != null) { + listener.favouritesOnClick(); + } + dismiss(); + } + }); + mainView.findViewById(R.id.waypoints_row).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (listener != null) { + listener.waypointsOnClick(); + } + dismiss(); + } + }); + + mainView.findViewById(R.id.close_row).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + dismiss(); + } + }); + + setupHeightAndBackground(mainView, R.id.add_markers_group_scroll_view); + + return mainView; + } + + interface AddMarkersGroupFragmentListener { + + void favouritesOnClick(); + + void waypointsOnClick(); + } +} diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/FavouritesGroupsAdapter.java b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/FavouritesGroupsAdapter.java index 1ae1f03948..99f6fe9827 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/FavouritesGroupsAdapter.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/FavouritesGroupsAdapter.java @@ -14,43 +14,13 @@ import net.osmand.plus.R; import java.util.List; -public class FavouritesGroupsAdapter extends RecyclerView.Adapter { +public class FavouritesGroupsAdapter extends GroupsAdapter { - private static final int TYPE_HEADER = 12; - private static final int TYPE_ITEM = 13; - - private FavouritesGroupsAdapterListener listener; - private OsmandApplication app; private List favoriteGroups; - private IconsCache iconsCache; public FavouritesGroupsAdapter(Context context, List favoriteGroups) { - this.app = (OsmandApplication) context.getApplicationContext(); + super(context); this.favoriteGroups = favoriteGroups; - this.iconsCache = app.getIconsCache(); - } - - public void setAdapterListener(FavouritesGroupsAdapterListener listener) { - this.listener = listener; - } - - @Override - public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - if (viewType == TYPE_HEADER) { - View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.add_favourites_group_header, parent, false); - return new MapMarkersGroupHeaderViewHolder(view); - } else { - View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.markers_group_view_holder, parent, false); - view.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - if (listener != null) { - listener.onItemClick(view); - } - } - }); - return new MapMarkersGroupViewHolder(view); - } } @Override @@ -69,11 +39,6 @@ public class FavouritesGroupsAdapter extends RecyclerView.Adapter { + + private static final int TYPE_HEADER = 12; + private static final int TYPE_ITEM = 13; + + private GroupsAdapterListener listener; + protected OsmandApplication app; + protected IconsCache iconsCache; + + public GroupsAdapter(Context context) { + this.app = (OsmandApplication) context.getApplicationContext(); + this.iconsCache = app.getIconsCache(); + } + + public void setAdapterListener(GroupsAdapterListener listener) { + this.listener = listener; + } + + @Override + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + if (viewType == TYPE_HEADER) { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.add_markers_group_header, parent, false); + return new MapMarkersGroupHeaderViewHolder(view); + } else { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.markers_group_view_holder, parent, false); + view.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (listener != null) { + listener.onItemClick(view); + } + } + }); + return new MapMarkersGroupViewHolder(view); + } + } + + @Override + public int getItemViewType(int position) { + return position == 0 ? TYPE_HEADER : TYPE_ITEM; + } + + public interface GroupsAdapterListener { + void onItemClick(View view); + } +} diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/TracksGroupsAdapter.java b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/TracksGroupsAdapter.java new file mode 100644 index 0000000000..cce88139e2 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/TracksGroupsAdapter.java @@ -0,0 +1,43 @@ +package net.osmand.plus.mapmarkers.adapters; + +import android.content.Context; +import android.support.v7.widget.RecyclerView; + +import net.osmand.plus.GPXDatabase.GpxDataItem; +import net.osmand.plus.R; + +import java.util.List; + +public class TracksGroupsAdapter extends GroupsAdapter { + + private List gpxFiles; + + public TracksGroupsAdapter(Context context, List gpxFiles) { + super(context); + this.gpxFiles = gpxFiles; + } + + @Override + public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { + if (holder instanceof MapMarkersGroupHeaderViewHolder) { + MapMarkersGroupHeaderViewHolder markersGroupHeaderViewHolder = (MapMarkersGroupHeaderViewHolder) holder; + markersGroupHeaderViewHolder.title.setText(app.getText(R.string.shared_string_tracks)); + markersGroupHeaderViewHolder.description.setText(app.getText(R.string.add_track_to_markers_descr)); + } else if (holder instanceof MapMarkersGroupViewHolder) { + GpxDataItem gpx = getItem(position); + MapMarkersGroupViewHolder markersGroupViewHolder = (MapMarkersGroupViewHolder) holder; + markersGroupViewHolder.icon.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_polygom_dark)); + markersGroupViewHolder.name.setText(gpx.getFile().getName().replace(".gpx", "").replace("/", " ").replace("_", " ")); + markersGroupViewHolder.numberCount.setText(String.valueOf(gpx.getAnalysis().wptPoints)); + } + } + + @Override + public int getItemCount() { + return gpxFiles.size() + 1; + } + + private GpxDataItem getItem(int position) { + return gpxFiles.get(position - 1); + } +}