Merge pull request #9878 from osmandapp/add_sort_to_tracks

Fix UI track sort by
This commit is contained in:
vshcherb 2020-09-23 15:14:53 +02:00 committed by GitHub
commit 7712d144d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 99 additions and 66 deletions

View file

@ -22,7 +22,9 @@
android:textColor="?android:textColorPrimary"
android:layout_gravity="center_vertical"
android:paddingLeft="@dimen/content_padding"
android:paddingStart="@dimen/content_padding"
android:paddingRight="@dimen/content_padding"
android:paddingEnd="@dimen/content_padding"
android:paddingTop="@dimen/content_padding_small"
android:paddingBottom="@dimen/content_padding_small"
android:textSize="@dimen/default_list_text_size"

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_sort"
android:icon="@drawable/ic_action_time_start"
app:showAsAction="always"
android:title="@string/sort_last_modified" />
</menu>

View file

@ -12,6 +12,8 @@ import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.osmand.AndroidUtils;
import static net.osmand.plus.SimplePopUpMenuItemAdapter.SimplePopUpMenuItem;
import java.util.List;
@ -50,6 +52,10 @@ public class SimplePopUpMenuItemAdapter
} else {
ivIcon.setVisibility(View.GONE);
}
if (item.selected) {
convertView.setBackgroundColor(UiUtilities.getColorWithAlpha(
AndroidUtils.getColorFromAttr(getContext(), R.attr.active_color_basic), 0.1f));
}
}
return convertView;
}
@ -64,6 +70,7 @@ public class SimplePopUpMenuItemAdapter
private CharSequence title;
private Drawable icon;
private View.OnClickListener onClickListener;
boolean selected;
public SimplePopUpMenuItem(CharSequence title, Drawable icon) {
this.title = title;
@ -75,6 +82,12 @@ public class SimplePopUpMenuItemAdapter
this.onClickListener = onClickListener;
}
public SimplePopUpMenuItem(CharSequence title, Drawable icon, View.OnClickListener onClickListener,
boolean selected) {
this(title, icon, onClickListener);
this.selected = selected;
}
public CharSequence getTitle() {
return title;
}

View file

@ -784,7 +784,7 @@ public class UiUtilities {
titles.add(String.valueOf(item.getTitle()));
hasIcon = hasIcon || item.getIcon() != null;
}
float itemWidth = AndroidUtils.getTextMaxWidth(defaultListTextSize, titles) + contentPadding;
float itemWidth = AndroidUtils.getTextMaxWidth(defaultListTextSize, titles) + contentPadding * 2;
float iconPartWidth = hasIcon ? standardIconSize + contentPaddingHalf : 0;
int totalWidth = (int) (Math.max(itemWidth, minWidth) + iconPartWidth);
@ -808,4 +808,19 @@ public class UiUtilities {
});
return listPopupWindow;
}
public static void showPopUpMenu(View v, final List<SimplePopUpMenuItemAdapter.SimplePopUpMenuItem> items) {
UiUtilities.createListPopupWindow(
v.getContext(), v, v.getWidth(), items, new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position < items.size()) {
View.OnClickListener listener = items.get(position).getOnClickListener();
if (listener != null) {
listener.onClick(view);
}
}
}
}).show();
}
}

View file

@ -3,8 +3,6 @@ package net.osmand.plus.measurementtool;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
@ -19,6 +17,7 @@ import net.osmand.AndroidUtils;
import net.osmand.IndexConstants;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.base.BottomSheetBehaviourDialogFragment;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
import net.osmand.plus.helpers.GpxTrackAdapter;
@ -26,7 +25,6 @@ import net.osmand.plus.helpers.GpxTrackAdapter.OnItemClickListener;
import net.osmand.plus.helpers.GpxUiHelper.GPXInfo;
import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter;
import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter.HorizontalSelectionAdapterListener;
import net.osmand.plus.widgets.IconPopupMenu;
import java.io.File;
import java.util.ArrayList;
@ -36,6 +34,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static net.osmand.plus.SimplePopUpMenuItemAdapter.*;
import static net.osmand.plus.helpers.GpxUiHelper.getSortedGPXFilesInfo;
import static net.osmand.plus.settings.backend.OsmandSettings.*;
import static net.osmand.util.Algorithms.collectDirs;
@ -73,7 +72,7 @@ public class SelectFileBottomSheet extends BottomSheetBehaviourDialogFragment {
private Mode fragmentMode;
private String selectedFolder;
private String allFilesFolder;
TracksSortByMode tracksSortBy = TracksSortByMode.BY_DATE;
TracksSortByMode sortByMode = TracksSortByMode.BY_DATE;
public void setFragmentMode(Mode fragmentMode) {
this.fragmentMode = fragmentMode;
@ -105,30 +104,29 @@ public class SelectFileBottomSheet extends BottomSheetBehaviourDialogFragment {
? R.color.inactive_buttons_and_links_bg_dark
: R.color.inactive_buttons_and_links_bg_light);
AndroidUtils.setBackground(sortButton, background);
sortButton.setImageResource(tracksSortBy.getIconId());
sortButton.setImageResource(sortByMode.getIconId());
sortButton.setVisibility(View.VISIBLE);
sortButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IconPopupMenu popup = new IconPopupMenu(v.getContext(), v);
final Menu menu = popup.getMenu();
MenuItem mi;
final List<SimplePopUpMenuItem> items = new ArrayList<>();
for (final TracksSortByMode mode : TracksSortByMode.values()) {
mi = createMenuItem(app, menu, mode.getNameId(), mode.getNameId(), mode.getIconId(),
MenuItem.SHOW_AS_ACTION_ALWAYS, false, R.color.icon_color_default_light);
mi.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
items.add(new SimplePopUpMenuItem(
getString(mode.getNameId()),
app.getUIUtilities().getThemedIcon(mode.getIconId()),
new View.OnClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
tracksSortBy = mode;
public void onClick(View v) {
sortByMode = mode;
sortButton.setImageResource(mode.getIconId());
updateDescription(descriptionView);
sortFileList();
adapter.notifyDataSetChanged();
return false;
}
});
}, sortByMode == mode
));
}
popup.show();
UiUtilities.showPopUpMenu(v, items);
}
});
}
@ -202,25 +200,12 @@ public class SelectFileBottomSheet extends BottomSheetBehaviourDialogFragment {
}
private void updateDescription(TextView descriptionView) {
String string = getString(tracksSortBy.getNameId());
String string = getString(sortByMode.getNameId());
descriptionView.setText(String.format(getString(R.string.ltr_or_rtl_combine_via_space),
getString(fragmentMode.description),
Character.toLowerCase(string.charAt(0)) + string.substring(1)));
}
public MenuItem createMenuItem(OsmandApplication app, Menu m, int id, int titleRes, int iconId, int menuItemType,
boolean flipIconForRtl, int iconColor) {
Drawable d = iconId == 0 ? null : app.getUIUtilities().getIcon(iconId, iconColor);
MenuItem menuItem = m.add(0, id, 0, titleRes);
if (d != null) {
if (flipIconForRtl) {
d = AndroidUtils.getDrawableForDirection(app, d);
}
menuItem.setIcon(d);
}
return menuItem;
}
private void updateFileList(HorizontalSelectionAdapter folderAdapter) {
sortFileList();
adapter.setShowFolderName(showFoldersName());
@ -238,9 +223,9 @@ public class SelectFileBottomSheet extends BottomSheetBehaviourDialogFragment {
Collections.sort(gpxInfoList, new Comparator<GPXInfo>() {
@Override
public int compare(GPXInfo i1, GPXInfo i2) {
if (tracksSortBy == TracksSortByMode.BY_NAME_ASCENDING) {
if (sortByMode == TracksSortByMode.BY_NAME_ASCENDING) {
return i1.getFileName().toLowerCase().compareTo(i2.getFileName().toLowerCase());
} else if (tracksSortBy == TracksSortByMode.BY_NAME_DESCENDING) {
} else if (sortByMode == TracksSortByMode.BY_NAME_DESCENDING) {
return -i1.getFileName().toLowerCase().compareTo(i2.getFileName().toLowerCase());
} else {
long time1 = i1.getLastModified();

View file

@ -19,7 +19,6 @@ import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
@ -70,7 +69,7 @@ import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R;
import net.osmand.plus.SimplePopUpMenuItemAdapter;
import net.osmand.plus.SimplePopUpMenuItemAdapter.SimplePopUpMenuItem;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.OsmandBaseExpandableListAdapter;
@ -475,25 +474,12 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
}
});
menu.addSubMenu(Menu.NONE, R.string.shared_string_sort, Menu.NONE, R.string.shared_string_sort);
final SubMenu sortMenu = menu.findItem(R.string.shared_string_sort).getSubMenu();
mi = sortMenu.getItem();
inflater.inflate(R.menu.track_sort_menu_item, menu);
mi = menu.findItem(R.id.action_sort);
mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
final int iconColorId = isLightActionBar() ? R.color.active_buttons_and_links_text_light
int iconColorId = isLightActionBar() ? R.color.active_buttons_and_links_text_light
: R.color.active_buttons_and_links_text_dark;
mi.setIcon(getIcon(sortByMode.getIconId(), iconColorId));
for (final TracksSortByMode mode : TracksSortByMode.values()) {
mi = createMenuItem(sortMenu, mode.getNameId(), mode.getNameId(), mode.getIconId(),
MenuItem.SHOW_AS_ACTION_ALWAYS, false, R.color.icon_color_default_light);
mi.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
updateTracksSort(mode);
sortMenu.setIcon(getIcon(mode.getIconId(), iconColorId));
return false;
}
});
}
if (AndroidUiHelper.isOrientationPortrait(getActivity())) {
menu = ((FavoritesActivity) getActivity()).getClearToolbar(true).getMenu();
@ -578,7 +564,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
public boolean onOptionsItemSelected(final MenuItem item) {
int itemId = item.getItemId();
for (int i = 0; i < optionsMenuAdapter.length(); i++) {
ContextMenuItem contextMenuItem = optionsMenuAdapter.getItem(i);
@ -587,6 +573,29 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
return true;
}
}
if (itemId == R.id.action_sort) {
Activity activity = getActivity();
if (activity != null) {
View menuSortItemView = getActivity().findViewById(R.id.action_sort);
final List<SimplePopUpMenuItem> items = new ArrayList<>();
for (final TracksSortByMode mode : TracksSortByMode.values()) {
items.add(new SimplePopUpMenuItem(
getString(mode.getNameId()),
app.getUIUtilities().getThemedIcon(mode.getIconId()),
new View.OnClickListener() {
@Override
public void onClick(View v) {
updateTracksSort(mode);
int iconColorId = isLightActionBar() ? R.color.active_buttons_and_links_text_light
: R.color.active_buttons_and_links_text_dark;
item.setIcon(getIcon(mode.getIconId(), iconColorId));
}
}, sortByMode == mode
));
}
UiUtilities.showPopUpMenu(menuSortItemView, items);
}
}
return super.onOptionsItemSelected(item);
}
@ -1492,10 +1501,10 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
}
private void openPopUpMenu(View v, final GpxInfo gpxInfo) {
final List<SimplePopUpMenuItemAdapter.SimplePopUpMenuItem> items = new ArrayList<>();
final List<SimplePopUpMenuItem> items = new ArrayList<>();
UiUtilities iconsCache = getMyApplication().getUIUtilities();
items.add(new SimplePopUpMenuItemAdapter.SimplePopUpMenuItem(
items.add(new SimplePopUpMenuItem(
getString(R.string.shared_string_show_on_map),
iconsCache.getThemedIcon(R.drawable.ic_show_on_map),
new View.OnClickListener() {
@ -1509,7 +1518,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
GPXTrackAnalysis analysis;
if ((analysis = getGpxTrackAnalysis(gpxInfo, app, null)) != null) {
if (analysis.totalDistance != 0 && !gpxInfo.currentlyRecordingTrack) {
items.add(new SimplePopUpMenuItemAdapter.SimplePopUpMenuItem(
items.add(new SimplePopUpMenuItem(
getString(R.string.analyze_on_map),
iconsCache.getThemedIcon(R.drawable.ic_action_info_dark),
new View.OnClickListener() {
@ -1522,7 +1531,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
}
}
items.add(new SimplePopUpMenuItemAdapter.SimplePopUpMenuItem(
items.add(new SimplePopUpMenuItem(
getString(R.string.shared_string_move),
iconsCache.getThemedIcon(R.drawable.ic_action_folder_stroke),
new View.OnClickListener() {
@ -1533,7 +1542,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
}
));
items.add(new SimplePopUpMenuItemAdapter.SimplePopUpMenuItem(
items.add(new SimplePopUpMenuItem(
getString(R.string.shared_string_rename),
iconsCache.getThemedIcon(R.drawable.ic_action_edit_dark),
new View.OnClickListener() {
@ -1551,7 +1560,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
));
Drawable shareIcon = iconsCache.getThemedIcon((R.drawable.ic_action_gshare_dark));
items.add(new SimplePopUpMenuItemAdapter.SimplePopUpMenuItem(
items.add(new SimplePopUpMenuItem(
getString(R.string.shared_string_share),
AndroidUtils.getDrawableForDirection(app, shareIcon),
new View.OnClickListener() {
@ -1569,7 +1578,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
final OsmEditingPlugin osmEditingPlugin = OsmandPlugin.getEnabledPlugin(OsmEditingPlugin.class);
if (osmEditingPlugin != null && osmEditingPlugin.isActive()) {
items.add(new SimplePopUpMenuItemAdapter.SimplePopUpMenuItem(
items.add(new SimplePopUpMenuItem(
getString(R.string.shared_string_export),
iconsCache.getThemedIcon(R.drawable.ic_action_export),
new View.OnClickListener() {
@ -1581,7 +1590,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
));
}
items.add(new SimplePopUpMenuItemAdapter.SimplePopUpMenuItem(
items.add(new SimplePopUpMenuItem(
getString(R.string.shared_string_delete),
iconsCache.getThemedIcon(R.drawable.ic_action_delete_dark),
new View.OnClickListener() {