Merge pull request #8780 from osmandapp/select_gpx_track

Add GPX waypoint action Bottom Sheet
This commit is contained in:
max-klaus 2020-04-16 10:39:24 +03:00 committed by GitHub
commit 9b7f9b3cb6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 145 additions and 111 deletions

View file

@ -1,14 +0,0 @@
{
"categories" : {
"special" : {
"icons" : [
"special_star", "special_star_stroked", "special_marker", "special_flag_stroke", "special_house", "special_building"
]
},
"amenity" : {
"icons": [
"amenity_bar", "amenity_cafe", "amenity_atm", "amenity_biergarten", "amenity_cinema", "amenity_fire_station", "amenity_parking"
]
}
}
}

View file

@ -47,23 +47,4 @@
tools:listitem="@layout/gpx_track_select_item">
</androidx.recyclerview.widget.RecyclerView>
<LinearLayout
android:id="@+id/buttons_container"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:paddingStart="@dimen/content_padding"
android:paddingLeft="@dimen/content_padding"
android:paddingTop="@dimen/content_padding_small"
android:paddingEnd="@dimen/content_padding"
android:paddingRight="@dimen/content_padding"
android:paddingBottom="@dimen/content_padding_small">
<include
android:id="@+id/dismiss_button"
layout="@layout/bottom_sheet_dialog_button"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>

View file

@ -2,7 +2,8 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="@dimen/settings_divider_margin_start"
android:layout_height="wrap_content"
android:minHeight="@dimen/settings_divider_margin_start"
android:background="?attr/selectableItemBackground"
android:orientation="horizontal">
@ -21,7 +22,7 @@
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/list_content_padding"
android:layout_marginLeft="@dimen/list_content_padding"
android:layout_marginEnd="@dimen/list_content_padding"

View file

@ -1,6 +1,6 @@
package net.osmand.plus.helpers;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -15,6 +15,7 @@ import net.osmand.AndroidUtils;
import net.osmand.GPXUtilities;
import net.osmand.IndexConstants;
import net.osmand.plus.GPXDatabase;
import net.osmand.plus.GpxDbHelper;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
@ -35,14 +36,11 @@ public class GpxTrackAdapter extends RecyclerView.Adapter<GpxTrackAdapter.TrackV
private OnItemClickListener onItemClickListener;
private UiUtilities iconsCache;
GpxTrackAdapter(Activity activity, List<GpxUiHelper.GPXInfo> gpxInfoList, boolean showCurrentGpx,
OnItemClickListener onItemClickListener) {
GpxTrackAdapter(Context ctx, List<GpxUiHelper.GPXInfo> gpxInfoList, boolean showCurrentGpx) {
this.showCurrentGpx = showCurrentGpx;
this.onItemClickListener = onItemClickListener;
app = (OsmandApplication) activity.getApplication();
app = (OsmandApplication) ctx.getApplicationContext();
boolean nightMode = app.getDaynightHelper().isNightModeForMapControls();
themedInflater = UiUtilities.getInflater(activity, nightMode);
themedInflater = UiUtilities.getInflater(ctx, nightMode);
this.gpxInfoList = gpxInfoList;
iconsCache = app.getUIUtilities();
}
@ -123,8 +121,26 @@ public class GpxTrackAdapter extends RecyclerView.Adapter<GpxTrackAdapter.TrackV
}
}
private GPXDatabase.GpxDataItem getDataItem(GpxUiHelper.GPXInfo info) {
return app.getGpxDbHelper().getItem(new File(app.getAppPath(IndexConstants.GPX_INDEX_DIR), info.getFileName()));
private GPXDatabase.GpxDataItem getDataItem(final GpxUiHelper.GPXInfo info) {
GpxDbHelper.GpxDataItemCallback gpxDataItemCallback = new GpxDbHelper.GpxDataItemCallback() {
@Override
public boolean isCancelled() {
return false;
}
@Override
public void onGpxDataItemReady(GPXDatabase.GpxDataItem item) {
if (item != null && gpxInfoList != null && info != null) {
notifyItemChanged(gpxInfoList.indexOf(info));
}
}
};
return app.getGpxDbHelper().getItem(new File(app.getAppPath(IndexConstants.GPX_INDEX_DIR), info.getFileName())
, gpxDataItemCallback);
}
void setAdapterListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
static class TrackViewHolder extends RecyclerView.ViewHolder {

View file

@ -37,8 +37,7 @@ import androidx.appcompat.widget.ListPopupWindow;
import androidx.appcompat.widget.SwitchCompat;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.fragment.app.FragmentActivity;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.charts.LineChart;
@ -259,8 +258,8 @@ public class GpxUiHelper {
return null;
}
public static AlertDialog selectSingleGPXFile(final Activity activity, boolean showCurrentGpx,
final CallbackWithObject<GPXFile[]> callbackWithObject) {
public static void selectSingleGPXFile(final FragmentActivity activity, boolean showCurrentGpx,
final CallbackWithObject<GPXFile[]> callbackWithObject) {
OsmandApplication app = (OsmandApplication) activity.getApplication();
int gpxDirLength = app.getAppPath(IndexConstants.GPX_INDEX_DIR).getAbsolutePath().length();
List<SelectedGpxFile> selectedGpxFiles = app.getSelectedGpxHelper().getSelectedGPXFiles();
@ -279,11 +278,8 @@ public class GpxUiHelper {
list.add(new GPXInfo(gpxFile.path.substring(gpxDirLength + 1), gpxFile.modifiedTime, 0));
}
}
final ContextMenuAdapter adapter = createGpxContextMenuAdapter(list, null, showCurrentGpx, app);
return createSingleChoiceDialog(activity, showCurrentGpx, callbackWithObject, list, adapter);
SelectGpxTrackBottomSheet.showInstance(activity.getSupportFragmentManager(), showCurrentGpx, callbackWithObject, list);
}
return null;
}
private static ContextMenuAdapter createGpxContextMenuAdapter(List<GPXInfo> allGpxList,
@ -351,65 +347,6 @@ public class GpxUiHelper {
}, dir, null, filename);
}
private static AlertDialog createSingleChoiceDialog(final Activity activity,
final boolean showCurrentGpx,
final CallbackWithObject<GPXFile[]> callbackWithObject,
final List<GPXInfo> list,
final ContextMenuAdapter adapter) {
final OsmandApplication app = (OsmandApplication) activity.getApplication();
boolean nightMode = app.getDaynightHelper().isNightModeForMapControls();
final View customLayout = UiUtilities.getInflater(activity, nightMode).inflate(R.layout.gpx_track_select_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(UiUtilities.getThemedContext(activity, nightMode));
builder.setView(customLayout);
final AlertDialog dlg = builder.create();
View cancelButton = customLayout.findViewById(R.id.dismiss_button);
UiUtilities.setupDialogButton(nightMode, cancelButton, UiUtilities.DialogButtonType.SECONDARY, R.string.shared_string_cancel);
TextView gpxCounter = customLayout.findViewById(R.id.counter);
gpxCounter.setText(String.valueOf(adapter.length()));
GpxTrackAdapter gpxTrackAdapter = new GpxTrackAdapter(activity, list, showCurrentGpx,
new GpxTrackAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
if (position != -1 && position < list.size()) {
if (showCurrentGpx && position == 0) {
callbackWithObject.processResult(null);
app.getSettings().LAST_SELECTED_GPX_TRACK_FOR_NEW_POINT.set(null);
} else {
String fileName = list.get(position).getFileName();
app.getSettings().LAST_SELECTED_GPX_TRACK_FOR_NEW_POINT.set(fileName);
SelectedGpxFile selectedGpxFile =
app.getSelectedGpxHelper().getSelectedFileByName(fileName);
if (selectedGpxFile != null) {
callbackWithObject.processResult(new GPXUtilities.GPXFile[]{selectedGpxFile.getGpxFile()});
} else {
File dir = app.getAppPath(IndexConstants.GPX_INDEX_DIR);
GpxUiHelper.loadGPXFileInDifferentThread(activity, callbackWithObject, dir, null, fileName);
}
}
}
dlg.dismiss();
}
});
RecyclerView recyclerView = customLayout.findViewById(R.id.gpx_track_list);
recyclerView.setAdapter(gpxTrackAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(app, LinearLayoutManager.VERTICAL, false));
dlg.setCanceledOnTouchOutside(false);
dlg.show();
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dlg.dismiss();
}
});
try {
dlg.getListView().setFastScrollEnabled(true);
} catch (Exception e) {
// java.lang.ClassCastException: com.android.internal.widget.RoundCornerListAdapter
// Unknown reason but on some devices fail
}
return dlg;
}
private static class DialogGpxDataItemCallback implements GpxDataItemCallback {
private static final int UPDATE_GPX_ITEM_MSG_ID = OsmAndConstants.UI_HANDLER_LOCATION_SERVICE + 6;
private static final long MIN_UPDATE_INTERVAL = 500;

View file

@ -0,0 +1,112 @@
package net.osmand.plus.helpers;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import net.osmand.CallbackWithObject;
import net.osmand.GPXUtilities;
import net.osmand.IndexConstants;
import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
import java.io.File;
import java.util.List;
public class SelectGpxTrackBottomSheet extends MenuBottomSheetDialogFragment {
public static final String TAG = SelectGpxTrackBottomSheet.class.getSimpleName();
protected View mainView;
protected GpxTrackAdapter adapter;
private List<GpxUiHelper.GPXInfo> gpxInfoList;
private boolean showCurrentGpx;
private CallbackWithObject<GPXUtilities.GPXFile[]> callbackWithObject;
private void setGpxInfoList(List<GpxUiHelper.GPXInfo> gpxInfoList) {
this.gpxInfoList = gpxInfoList;
}
private void setShowCurrentGpx(boolean showCurrentGpx) {
this.showCurrentGpx = showCurrentGpx;
}
private void setCallbackWithObject(CallbackWithObject<GPXUtilities.GPXFile[]> callbackWithObject) {
this.callbackWithObject = callbackWithObject;
}
@Override
public void createMenuItems(Bundle savedInstanceState) {
final int themeRes = nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme;
mainView = View.inflate(new ContextThemeWrapper(getContext(), themeRes),
R.layout.gpx_track_select_dialog, null);
final RecyclerView recyclerView = mainView.findViewById(R.id.gpx_track_list);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
adapter = new GpxTrackAdapter(requireContext(), gpxInfoList, showCurrentGpx);
adapter.setAdapterListener(new GpxTrackAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
if (position != RecyclerView.NO_POSITION) {
SelectGpxTrackBottomSheet.this.onItemClick(position);
}
}
});
recyclerView.setAdapter(adapter);
TextView gpxCounter = mainView.findViewById(R.id.counter);
gpxCounter.setText(String.valueOf(adapter.getItemCount()));
items.add(new BaseBottomSheetItem.Builder().setCustomView(mainView).create());
}
private void onItemClick(int position) {
if (position != -1 && position < gpxInfoList.size()) {
OsmandApplication app = (OsmandApplication) requireActivity().getApplication();
if (showCurrentGpx && position == 0) {
callbackWithObject.processResult(null);
app.getSettings().LAST_SELECTED_GPX_TRACK_FOR_NEW_POINT.set(null);
} else {
String fileName = gpxInfoList.get(position).getFileName();
app.getSettings().LAST_SELECTED_GPX_TRACK_FOR_NEW_POINT.set(fileName);
GpxSelectionHelper.SelectedGpxFile selectedGpxFile =
app.getSelectedGpxHelper().getSelectedFileByName(fileName);
if (selectedGpxFile != null) {
callbackWithObject.processResult(new GPXUtilities.GPXFile[]{selectedGpxFile.getGpxFile()});
} else {
File dir = app.getAppPath(IndexConstants.GPX_INDEX_DIR);
Activity activity = getActivity();
if (activity != null) {
GpxUiHelper.loadGPXFileInDifferentThread(activity, callbackWithObject, dir, null, fileName);
}
}
}
}
dismiss();
}
public static void showInstance(FragmentManager fragmentManager, boolean showCurrentGpx,
CallbackWithObject<GPXUtilities.GPXFile[]> callbackWithObject, List<GpxUiHelper.GPXInfo> gpxInfoList) {
if (!fragmentManager.isStateSaved()) {
SelectGpxTrackBottomSheet fragment = new SelectGpxTrackBottomSheet();
fragment.setUsedOnMap(true);
fragment.setRetainInstance(true);
fragment.setShowCurrentGpx(showCurrentGpx);
fragment.setCallbackWithObject(callbackWithObject);
fragment.setGpxInfoList(gpxInfoList);
fragment.show(fragmentManager, SelectGpxTrackBottomSheet.TAG);
}
}
@Override
protected int getDismissButtonTextId() {
return R.string.shared_string_cancel;
}
}

View file

@ -625,6 +625,7 @@ public abstract class PointEditorFragmentNew extends BaseOsmAndFragment {
}
public void dismiss(boolean includingMenu) {
hideKeyboard();
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
if (includingMenu) {