From 7d48fc86431a0e898980cacecbb9165b77893b5f Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Tue, 12 Nov 2024 15:25:26 +0200 Subject: [PATCH 01/18] Use TracksComparator to sort TrackTab instances --- .../shared/gpx/data/ComparableTracksGroup.kt | 2 +- .../plus/auto/screens/TracksFoldersScreen.kt | 4 +- .../tracks/TrackGroupsBottomSheet.java | 5 +- .../plus/configmap/tracks/TrackTab.java | 75 ++++++++++------ .../plus/configmap/tracks/TrackTabType.java | 2 +- .../configmap/tracks/TrackTabsHelper.java | 58 ++++++------ .../configmap/tracks/TracksComparator.java | 89 ++++++++++++------- .../configmap/tracks/TracksTabsFragment.java | 4 +- .../plus/track/BaseTracksTabsFragment.java | 22 +++-- .../plus/track/SelectTrackTabsFragment.java | 2 +- .../plus/track/helpers/GpxUiHelper.java | 4 +- 11 files changed, 160 insertions(+), 107 deletions(-) diff --git a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/ComparableTracksGroup.kt b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/ComparableTracksGroup.kt index 62573f09aea..ce44c96008c 100644 --- a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/ComparableTracksGroup.kt +++ b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/ComparableTracksGroup.kt @@ -3,7 +3,7 @@ package net.osmand.shared.gpx.data import net.osmand.shared.gpx.filters.TrackFolderAnalysis interface ComparableTracksGroup { - fun getFolderAnalysis(): TrackFolderAnalysis + fun getFolderAnalysis(): TrackFolderAnalysis? fun getDirName(): String fun lastModified(): Long } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/auto/screens/TracksFoldersScreen.kt b/OsmAnd/src/net/osmand/plus/auto/screens/TracksFoldersScreen.kt index 10ccde8c56d..32e17c4e6e7 100644 --- a/OsmAnd/src/net/osmand/plus/auto/screens/TracksFoldersScreen.kt +++ b/OsmAnd/src/net/osmand/plus/auto/screens/TracksFoldersScreen.kt @@ -81,7 +81,7 @@ class TracksFoldersScreen( .setTitle(app.getString(R.string.sort_last_modified)) .setImage(iconLastModified) .setBrowsable(true) - .setOnClickListener { onClickTabFolder(trackTabsHelper.trackTabs[TrackTabType.ALL.name]!!) } + .setOnClickListener { onClickTabFolder(trackTabsHelper.getTrackTab(TrackTabType.ALL.name)!!) } .build()) if (trackTabsHelper.trackTabs.isEmpty()) { @@ -93,7 +93,7 @@ class TracksFoldersScreen( } templateBuilder.setLoading(false) var itemsCount = 1 - for (trackTab in trackTabsHelper.trackTabs.values) { + for (trackTab in trackTabsHelper.getSortedTrackTabs(false)) { if (trackTab.type != TrackTabType.FOLDER) { continue } diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackGroupsBottomSheet.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackGroupsBottomSheet.java index 3874b86d0b8..3214ddbb6c5 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackGroupsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackGroupsBottomSheet.java @@ -42,9 +42,8 @@ public void onCreate(@Nullable Bundle savedInstanceState) { nightMode = isNightMode(true); Fragment target = getTargetFragment(); - if (target instanceof TracksTabsFragment) { - TracksTabsFragment fragment = (TracksTabsFragment) target; - trackTabs = fragment.getTrackTabs(); + if (target instanceof TracksTabsFragment fragment) { + trackTabs = fragment.getSortedTrackTabs(true); selectedTab = fragment.getSelectedTab(); } } diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java index 2d268606f58..42e2fa750d6 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java @@ -6,47 +6,56 @@ import androidx.annotation.Nullable; import net.osmand.plus.settings.enums.TracksSortMode; +import net.osmand.shared.gpx.data.ComparableTracksGroup; import net.osmand.shared.gpx.data.SmartFolder; import net.osmand.shared.gpx.data.TrackFolder; import net.osmand.plus.track.helpers.GpxUiHelper; import net.osmand.shared.gpx.TrackItem; +import net.osmand.shared.gpx.filters.TrackFolderAnalysis; import java.io.File; import java.util.ArrayList; import java.util.List; -public class TrackTab { +public class TrackTab implements ComparableTracksGroup { + public static final String SMART_FOLDER_TAB_NAME_PREFIX = "SMART_FOLDER___"; + @NonNull public final TrackTabType type; public final List items = new ArrayList<>(); @Nullable public final File directory; - @Nullable - private final String name; - private String typeName = null; + public final SmartFolder smartFolder; private TracksSortMode sortMode = TracksSortMode.getDefaultSortMode(); public TrackTab(@NonNull File directory) { this.directory = directory; - this.name = null; + this.smartFolder = null; this.type = TrackTabType.FOLDER; } + public TrackTab(@NonNull SmartFolder smartFolder) { + this.directory = null; + this.smartFolder = smartFolder; + this.type = TrackTabType.SMART_FOLDER; + } + public TrackTab(@NonNull TrackTabType type) { this.directory = null; - this.name = null; + this.smartFolder = null; this.type = type; } - public TrackTab(@NonNull SmartFolder smartFolder) { - this.directory = null; - this.name = smartFolder.getFolderName(); - this.type = TrackTabType.SMART_FOLDER; - typeName = SMART_FOLDER_TAB_NAME_PREFIX + name; + @Nullable + @Override + public TrackFolderAnalysis getFolderAnalysis() { + // Analysis should be prepared and accessible here. + // It's needed for proper sorting order on UI. + return null; } @NonNull @@ -65,28 +74,33 @@ public String getName(@NonNull Context context) { @NonNull public String getName(@NonNull Context context, boolean includeParentDir) { - if (type.titleId != -1) { - return context.getString(type.titleId); - } + return type.titleId != -1 ? context.getString(type.titleId) : getDirName(includeParentDir); + } + + @NonNull + @Override + public String getDirName() { + return getDirName(false); + } + + @NonNull + public String getDirName(boolean includeParentName) { if (directory != null) { - return GpxUiHelper.getFolderName(context, directory, includeParentDir); + return GpxUiHelper.getFolderName(directory, includeParentName); } - if (name != null) { - return name; + if (smartFolder != null) { + return smartFolder.getDirName(); } return ""; } @NonNull public String getTypeName() { - switch (type) { - case FOLDER: - return directory != null ? directory.getName() : ""; - case SMART_FOLDER: - return typeName != null ? typeName : ""; - default: - return type.name(); - } + return switch (type) { + case FOLDER -> directory != null ? directory.getName() : ""; + case SMART_FOLDER -> smartFolder != null ? SMART_FOLDER_TAB_NAME_PREFIX + smartFolder.getDirName() : ""; + default -> type.name(); + }; } @NonNull @@ -111,6 +125,17 @@ public List getTrackFolders() { return trackFolders; } + @Override + public long lastModified() { + if (directory != null) { + return directory.lastModified(); + } + if (smartFolder != null) { + return smartFolder.lastModified(); + } + return 0; + } + @NonNull @Override public String toString() { diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabType.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabType.java index 63b4ed63c5c..cdfda0dd3d0 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabType.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabType.java @@ -9,8 +9,8 @@ public enum TrackTabType { ON_MAP(R.string.shared_string_on_map, R.drawable.ic_show_on_map), ALL(R.string.shared_string_all, R.drawable.ic_action_list_header), - FOLDER(-1, R.drawable.ic_action_folder), SMART_FOLDER(-1, R.drawable.ic_action_folder_smart), + FOLDER(-1, R.drawable.ic_action_folder), FOLDERS(R.string.shared_string_folders, R.drawable.ic_action_folder); diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java index 2c334f04c72..11d12f740aa 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java @@ -8,9 +8,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import net.osmand.Collator; import net.osmand.IndexConstants; -import net.osmand.OsmAndCollator; import net.osmand.plus.shared.SharedUtil; import net.osmand.data.LatLon; import net.osmand.shared.gpx.GpxFile; @@ -47,7 +45,6 @@ public class TrackTabsHelper { private final Set recentlyVisibleTrackItem = new HashSet<>(); private final Map trackTabs = new LinkedHashMap<>(); - private final Collator collator = OsmAndCollator.primaryCollator(); public TrackTabsHelper(@NonNull OsmandApplication app) { this.app = app; @@ -61,6 +58,18 @@ public ItemsSelectionHelper getItemsSelectionHelper() { return itemsSelectionHelper; } + @NonNull + public List getSortedTrackTabs(boolean checkParentName) { + List result = new ArrayList<>(trackTabs.values()); + result.sort(new TracksComparator(getRootSortMode(), getDefaultLocation(), checkParentName)); + return result; + } + + @Nullable + public TrackTab getTrackTab(@NonNull String key) { + return trackTabs.get(key); + } + @NonNull public Map getTrackTabs() { return trackTabs; @@ -103,35 +112,16 @@ private void updateTrackTabs(@NonNull Map folderTabs) { trackTabs.clear(); trackTabs.put(TrackTabType.ON_MAP.name(), getTracksOnMapTab()); trackTabs.put(TrackTabType.ALL.name(), getAllTracksTab()); - for (TrackTab tab : sortTrackTabs(getAllSmartFoldersTabs())) { + for (TrackTab tab : getAllSmartFoldersTabs()) { trackTabs.put(tab.getTypeName(), tab); } - for (TrackTab tab : sortTrackTabs(folderTabs)) { + for (TrackTab tab : folderTabs.values()) { trackTabs.put(tab.getTypeName(), tab); } loadTabsSortModes(); sortTrackTabs(); } - private List sortTrackTabs(Map folderTabs) { - List sortedTabs = new ArrayList<>(folderTabs.values()); - Map tabsSortModes = settings.getTrackSortModes(); - TracksSortMode sortMode = TracksSortMode.getDefaultSortMode(); - String trackRootDir = app.getAppPath(IndexConstants.GPX_INDEX_DIR).getName(); - String sortModeKey = tabsSortModes.get(trackRootDir); - if (sortModeKey != null) { - sortMode = TracksSortMode.getByValue(sortModeKey); - } - int sign = sortMode == TracksSortMode.NAME_DESCENDING ? -1 : 1; - // PRELIMINARY: Should ultimately use TracksComparator for trackTabs, similar to trackFolders - Collections.sort(sortedTabs, (tab1, tab2) -> { - String name1 = tab1.getTypeName(); - String name2 = tab2.getTypeName(); - return sign * collator.compare(name1, name2); - }); - return sortedTabs; - } - private void updateSelectTrackTabs(@NonNull TrackFolder folder) { processVisibleTracks(); processRecentlyVisibleTracks(); @@ -245,13 +235,13 @@ public void processVisibleTracks() { } @NonNull - private Map getAllSmartFoldersTabs() { - Map smartFoldersTabs = new LinkedHashMap<>(); + private List getAllSmartFoldersTabs() { + List smartFoldersTabs = new ArrayList<>(); for (SmartFolder folder : app.getSmartFolderHelper().getSmartFolders()) { TrackTab folderTab = new TrackTab(folder); folderTab.items.add(TYPE_SORT_TRACKS); folderTab.items.addAll(folder.getTrackItems()); - smartFoldersTabs.put(folderTab.getTypeName(), folderTab); + smartFoldersTabs.add(folderTab); } return smartFoldersTabs; } @@ -307,7 +297,7 @@ private void sortTrackTabs() { } public void sortTrackTab(@NonNull TrackTab trackTab) { - LatLon latLon = app.getMapViewTrackingUtilities().getDefaultLocation(); + LatLon latLon = getDefaultLocation(); if (trackTab.type == TrackTabType.ON_MAP) { List visibleItems = getVisibleItems(); List recentlyVisibleItems = getRecentlyVisibleItems(); @@ -346,4 +336,16 @@ public void saveTabsSortModes() { } settings.saveTabsSortModes(tabsSortModes); } + + @NonNull + private TracksSortMode getRootSortMode() { + String rootDir = app.getAppPath(IndexConstants.GPX_INDEX_DIR).getName(); + String key = settings.getTrackSortModes().get(rootDir); + return key != null ? TracksSortMode.getByValue(key) : TracksSortMode.getDefaultSortMode(); + } + + @NonNull + private LatLon getDefaultLocation() { + return app.getMapViewTrackingUtilities().getDefaultLocation(); + } } diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java index 2121fa93041..24ebe56659b 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java @@ -1,6 +1,9 @@ package net.osmand.plus.configmap.tracks; import static com.jwetherell.openmap.common.LatLonPoint.EQUIVALENT_TOLERANCE; +import static net.osmand.plus.settings.enums.TracksSortMode.DATE_DESCENDING; +import static net.osmand.plus.settings.enums.TracksSortMode.DISTANCE_ASCENDING; +import static net.osmand.plus.settings.enums.TracksSortMode.DURATION_ASCENDING; import static net.osmand.plus.settings.enums.TracksSortMode.LAST_MODIFIED; import static net.osmand.plus.settings.enums.TracksSortMode.NAME_ASCENDING; import static net.osmand.plus.settings.enums.TracksSortMode.NAME_DESCENDING; @@ -33,6 +36,7 @@ public class TracksComparator implements Comparator { public final TrackTab trackTab; public final TracksSortMode sortMode; public final Collator collator = OsmAndCollator.primaryCollator(); + private boolean checkParentNameForTabs = false; public TracksComparator(@NonNull TrackTab trackTab, @NonNull LatLon latLon) { this.trackTab = trackTab; @@ -40,8 +44,14 @@ public TracksComparator(@NonNull TrackTab trackTab, @NonNull LatLon latLon) { this.latLon = SharedUtil.kLatLon(latLon); } + public TracksComparator(@NonNull TracksSortMode sortMode, + @NonNull LatLon latLon, boolean checkParentNameForTabs) { + this(sortMode, latLon); + this.checkParentNameForTabs = checkParentNameForTabs; + } + public TracksComparator(@NonNull TracksSortMode sortMode, @NonNull LatLon latLon) { - trackTab = null; + this.trackTab = null; this.sortMode = sortMode; this.latLon = SharedUtil.kLatLon(latLon); } @@ -79,43 +89,51 @@ public int compare(Object o1, Object o2) { } private int compareTrackFolders(@NonNull ComparableTracksGroup folder1, @NonNull ComparableTracksGroup folder2) { - TrackFolderAnalysis folderAnalysis1; - TrackFolderAnalysis folderAnalysis2; + if (folder1 instanceof TrackTab tab1 && folder2 instanceof TrackTab tab2) { + if (tab1.type != tab2.type) { + return Integer.compare(tab1.type.ordinal(), tab2.type.ordinal()); + } + } + TrackFolderAnalysis analysis1; + TrackFolderAnalysis analysis2; switch (sortMode) { case NAME_ASCENDING: - return compareTrackFolderNames(folder1, folder2); - case NAME_DESCENDING: - return -compareTrackFolderNames(folder1, folder2); - case DATE_ASCENDING: - return compareFolderFilesByLastModified(folder1, folder2); - case DATE_DESCENDING: - return -compareFolderFilesByLastModified(folder1, folder2); + case NAME_DESCENDING: { + int sign = sortMode == NAME_ASCENDING ? 1 : -1; + return sign * compareTrackFolderNames(folder1, folder2); + } case LAST_MODIFIED: - return compareFolderFilesByLastModified(folder1, folder2); - case DISTANCE_DESCENDING: - folderAnalysis1 = folder1.getFolderAnalysis(); - folderAnalysis2 = folder2.getFolderAnalysis(); - if (Math.abs(folderAnalysis1.getTotalDistance() - folderAnalysis2.getTotalDistance()) >= EQUIVALENT_TOLERANCE) { - return -Float.compare(folderAnalysis1.getTotalDistance(), folderAnalysis2.getTotalDistance()); - } + case DATE_ASCENDING: + case DATE_DESCENDING: { + int sign = sortMode == DATE_DESCENDING ? -1 : 1; + return sign * compareFolderFilesByLastModified(folder1, folder2); + } case DISTANCE_ASCENDING: - folderAnalysis1 = folder1.getFolderAnalysis(); - folderAnalysis2 = folder2.getFolderAnalysis(); - if (Math.abs(folderAnalysis1.getTotalDistance() - folderAnalysis2.getTotalDistance()) >= EQUIVALENT_TOLERANCE) { - return Float.compare(folderAnalysis1.getTotalDistance(), folderAnalysis2.getTotalDistance()); - } - case DURATION_DESCENDING: - folderAnalysis1 = folder1.getFolderAnalysis(); - folderAnalysis2 = folder2.getFolderAnalysis(); - if (folderAnalysis1.getTimeSpan() != folderAnalysis2.getTimeSpan()) { - return -Long.compare(folderAnalysis1.getTimeSpan(), folderAnalysis2.getTimeSpan()); + case DISTANCE_DESCENDING: { + analysis1 = folder1.getFolderAnalysis(); + analysis2 = folder2.getFolderAnalysis(); + if (analysis1 != null && analysis2 != null) { + float dist1 = analysis1.getTotalDistance(); + float dist2 = analysis2.getTotalDistance(); + if (Math.abs(dist1 - dist2) >= EQUIVALENT_TOLERANCE) { + int sign = sortMode == DISTANCE_ASCENDING ? 1 : -1; + return sign * Float.compare(dist1, dist2); + } } + } case DURATION_ASCENDING: - folderAnalysis1 = folder1.getFolderAnalysis(); - folderAnalysis2 = folder2.getFolderAnalysis(); - if (folderAnalysis1.getTimeSpan() != folderAnalysis2.getTimeSpan()) { - return Long.compare(folderAnalysis1.getTimeSpan(), folderAnalysis2.getTimeSpan()); + case DURATION_DESCENDING: { + analysis1 = folder1.getFolderAnalysis(); + analysis2 = folder2.getFolderAnalysis(); + if (analysis1 != null && analysis2 != null) { + int timeSpan1 = analysis1.getTimeSpan(); + int timeSpan2 = analysis2.getTimeSpan(); + if (timeSpan1 != timeSpan2) { + int sign = sortMode == DURATION_ASCENDING ? 1 : -1; + return sign * Long.compare(timeSpan1, timeSpan2); + } } + } } return compareTrackFolderNames(folder1, folder2); } @@ -292,8 +310,13 @@ private int compareTrackItemNames(@NonNull TrackItem item1, @NonNull TrackItem i return compareNames(item1.getName(), item2.getName()); } - private int compareTrackFolderNames(@NonNull ComparableTracksGroup folder1, @NonNull ComparableTracksGroup folder2) { - return compareNames(folder1.getDirName(), folder2.getDirName()); + private int compareTrackFolderNames(@NonNull ComparableTracksGroup folder1, + @NonNull ComparableTracksGroup folder2) { + if (folder1 instanceof TrackTab tab1 && folder2 instanceof TrackTab tab2) { + return compareNames(tab1.getDirName(checkParentNameForTabs), tab2.getDirName(checkParentNameForTabs)); + } else { + return compareNames(folder1.getDirName(), folder2.getDirName()); + } } private int compareNames(@NonNull String item1, @NonNull String item2) { diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java index 12174dc0de6..fb666755da3 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java @@ -228,7 +228,7 @@ private void updateButtonsState() { selectionButton.setEnabled(!Algorithms.isEmpty(itemsSelectionHelper.getSelectedItems()) || notAllSelected); } applyButton.setEnabled(itemsSelectionHelper.hasItemsToApply()); - TrackTab allTracksTab = trackTabsHelper.getTrackTabs().get(TrackTabType.ALL.name()); + TrackTab allTracksTab = trackTabsHelper.getTrackTab(TrackTabType.ALL.name()); searchButton.setVisibility(allTracksTab == null ? View.GONE : View.VISIBLE); } } @@ -277,7 +277,7 @@ public void deferredLoadTracksFinished(@NonNull TrackFolder folder) { private void applyPreselectedParams() { if (preselectedTabParams != null) { - String tabName = preselectedTabParams.getPreselectedTabName(app, getTrackTabs()); + String tabName = preselectedTabParams.getPreselectedTabName(app, getSortedTrackTabs()); TrackTab trackTab = getTab(tabName); if (trackTab != null) { setSelectedTab(tabName); diff --git a/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java b/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java index fa30aa525e9..50e89dbeca4 100644 --- a/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java +++ b/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java @@ -61,7 +61,6 @@ import net.osmand.util.Algorithms; import java.io.File; -import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Set; @@ -115,13 +114,13 @@ public void onCreate(@Nullable Bundle savedInstanceState) { protected void setupTabLayout(@NonNull View view) { viewPager = view.findViewById(R.id.view_pager); - List tabs = getTrackTabs(); + List tabs = getSortedTrackTabs(); tabLayout = view.findViewById(R.id.sliding_tabs); tabLayout.setTabBackground(nightMode ? R.color.app_bar_main_dark : R.color.card_and_list_background_light); tabLayout.setCustomTabProvider(new PagerSlidingTabStrip.CustomTabProvider() { @Override public View getCustomTabView(@NonNull ViewGroup parent, int position) { - TrackTab trackTab = getTrackTabs().get(position); + TrackTab trackTab = getSortedTrackTabs().get(position); int activeColor = ColorUtilities.getActiveColor(app, nightMode); int textColor = ColorUtilities.getPrimaryTextColor(app, nightMode); @@ -155,8 +154,13 @@ public void tabStylesUpdated(View tabsContainer, int currentPosition) { } @NonNull - public List getTrackTabs() { - return new ArrayList<>(trackTabsHelper.getTrackTabs().values()); + public List getSortedTrackTabs() { + return getSortedTrackTabs(false); + } + + @NonNull + public List getSortedTrackTabs(boolean checkParentName) { + return trackTabsHelper.getSortedTrackTabs(checkParentName); } protected void setViewPagerAdapter(@NonNull ViewPager pager, List items) { @@ -166,12 +170,12 @@ protected void setViewPagerAdapter(@NonNull ViewPager pager, List item @Nullable public TrackTab getSelectedTab() { - List trackTabs = getTrackTabs(); + List trackTabs = getSortedTrackTabs(); return trackTabs.isEmpty() ? null : trackTabs.get(viewPager.getCurrentItem()); } public void setSelectedTab(@NonNull String name) { - List trackTabs = getTrackTabs(); + List trackTabs = getSortedTrackTabs(); for (int i = 0; i < trackTabs.size(); i++) { TrackTab tab = trackTabs.get(i); if (Algorithms.stringsEqual(tab.getTypeName(), name)) { @@ -183,7 +187,7 @@ public void setSelectedTab(@NonNull String name) { @Nullable public TrackTab getTab(@NonNull String name) { - for (TrackTab trackTab : getTrackTabs()) { + for (TrackTab trackTab : getSortedTrackTabs()) { if (Algorithms.stringsEqual(name, trackTab.getTypeName())) { return trackTab; } @@ -209,7 +213,7 @@ public TracksSortMode getTracksSortMode() { @Override public void onResume() { super.onResume(); - List tabs = getTrackTabs(); + List tabs = getSortedTrackTabs(); if (tabs.size() != tabSize) { setTabs(tabs); } diff --git a/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java b/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java index 1108cacaee3..e55598c4320 100644 --- a/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java +++ b/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java @@ -159,7 +159,7 @@ public void onTrackItemsSelected(@NonNull Set trackItems, boolean sel @Nullable public TrackTab getTab(@NonNull String name) { - for (TrackTab trackTab : getTrackTabs()) { + for (TrackTab trackTab : getSortedTrackTabs()) { if (Algorithms.stringsEqual(name, trackTab.getTypeName())) { updateTrackItemsVisibility(trackTab); return trackTab; diff --git a/OsmAnd/src/net/osmand/plus/track/helpers/GpxUiHelper.java b/OsmAnd/src/net/osmand/plus/track/helpers/GpxUiHelper.java index 4cacb0fc1a2..c4a059b1a75 100644 --- a/OsmAnd/src/net/osmand/plus/track/helpers/GpxUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/track/helpers/GpxUiHelper.java @@ -189,10 +189,10 @@ public static void selectSingleGPXFile(FragmentActivity activity, boolean showCu } @NonNull - public static String getFolderName(@NonNull Context context, @NonNull File dir, boolean includeParentDir) { + public static String getFolderName(@NonNull File dir, boolean includeParentDir) { String name = dir.getName(); if (GPX_INDEX_DIR.equals(name + File.separator)) { - return context.getString(R.string.shared_string_tracks); + return Algorithms.capitalizeFirstLetter(name); } String dirPath = dir.getPath() + File.separator; if (dirPath.endsWith(GPX_IMPORT_DIR) || dirPath.endsWith(GPX_RECORDED_INDEX_DIR)) { From e0083ff650234e95aa7db8564798fd4e099e6abf Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Mon, 18 Nov 2024 16:26:26 +0200 Subject: [PATCH 02/18] Use relative path to identify tracks folder (refactoring) --- .../net/osmand/shared/gpx/data/SmartFolder.kt | 6 ++ .../net/osmand/shared/gpx/data/TrackFolder.kt | 4 ++ .../net/osmand/shared/gpx/data/TracksGroup.kt | 2 + .../tracks/PreselectedTabParams.java | 35 ++------- .../tracks/TrackGroupsBottomSheet.java | 2 +- .../configmap/tracks/TrackItemsFragment.java | 10 +-- .../plus/configmap/tracks/TrackPathUtil.java | 28 ++++++++ .../plus/configmap/tracks/TrackTab.java | 36 ++++++---- .../configmap/tracks/TrackTabsHelper.java | 24 +++---- .../configmap/tracks/TracksTabsFragment.java | 6 +- .../net/osmand/plus/helpers/IntentHelper.java | 10 ++- .../myplaces/tracks/VisibleTracksGroup.java | 7 ++ .../dialogs/BaseTrackFolderFragment.java | 72 ++++++++----------- .../plus/track/BaseTracksTabsFragment.java | 8 +-- .../plus/track/SelectTrackTabsFragment.java | 5 +- 15 files changed, 138 insertions(+), 117 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/configmap/tracks/TrackPathUtil.java diff --git a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/SmartFolder.kt b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/SmartFolder.kt index baf2640e457..2e7623e9ec2 100644 --- a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/SmartFolder.kt +++ b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/SmartFolder.kt @@ -8,6 +8,8 @@ import net.osmand.shared.gpx.filters.TrackFilterSerializer import net.osmand.shared.gpx.filters.TrackFolderAnalysis import net.osmand.shared.util.KCollectionUtils +const val ID_PREFIX = "SMART_FOLDER___" + @Serializable class SmartFolder(@Serializable var folderName: String) : TracksGroup, ComparableTracksGroup { @@ -25,6 +27,10 @@ class SmartFolder(@Serializable var folderName: String) : TracksGroup, Comparabl @Transient private var folderAnalysis: TrackFolderAnalysis? = null + override fun getId(): String { + return ID_PREFIX + folderName + } + override fun getName() = folderName override fun getTrackItems(): List { diff --git a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt index 429754ac594..f1238cf392a 100644 --- a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt +++ b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt @@ -37,6 +37,10 @@ class TrackFolder(dirFile: KFile, parentFolder: TrackFolder?) : lastModified = folder.lastModified } + override fun getId(): String { + return relativePath + } + override fun getName(): String { return GpxHelper.getFolderName(dirFile, false) } diff --git a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TracksGroup.kt b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TracksGroup.kt index d8973bb6ddf..f47c21da226 100644 --- a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TracksGroup.kt +++ b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TracksGroup.kt @@ -3,6 +3,8 @@ package net.osmand.shared.gpx.data import net.osmand.shared.gpx.TrackItem interface TracksGroup { + fun getId(): String + fun getName(): String fun getTrackItems(): List diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/PreselectedTabParams.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/PreselectedTabParams.java index b4ab9117e06..d1d7b446253 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/PreselectedTabParams.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/PreselectedTabParams.java @@ -1,33 +1,19 @@ package net.osmand.plus.configmap.tracks; -import static net.osmand.plus.configmap.tracks.TrackTabType.SMART_FOLDER; - -import android.content.Context; - import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import net.osmand.util.Algorithms; - -import java.util.Collections; -import java.util.List; public class PreselectedTabParams { - public static final String PRESELECTED_TRACKS_TAB_NAME = "preselected_tab_name"; - public static final String PRESELECTED_TRACKS_TAB_TYPE = "preselected_tab_type"; + public static final String PRESELECTED_TRACKS_TAB_ID = "preselected_tab_id"; public static final String SELECT_ALL_ITEMS_ON_TAB = "select_all_items_on_tab"; public static final String CALLING_FRAGMENT_TAG = "calling_fragment_tag"; @NonNull - private final String name; - @NonNull - private final TrackTabType type; + private final String id; private final boolean selectAll; - public PreselectedTabParams(@NonNull String name, @NonNull TrackTabType type, boolean selectAll) { - this.name = name; - this.type = type; + public PreselectedTabParams(@NonNull String id, boolean selectAll) { + this.id = id; this.selectAll = selectAll; } @@ -36,16 +22,7 @@ public boolean shouldSelectAll() { } @NonNull - public String getPreselectedTabName(@NonNull Context context, @NonNull List trackTabs) { - if (type == SMART_FOLDER) { - for (TrackTab tab : trackTabs) { - String tabName = tab.getName(context); - if (tab.type == SMART_FOLDER && Algorithms.stringsEqual(tabName, name)) { - return tab.getTypeName(); - } - } - return ""; - } - return name; + public String getPreselectedTabId() { + return id; } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackGroupsBottomSheet.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackGroupsBottomSheet.java index 3214ddbb6c5..071b0854f68 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackGroupsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackGroupsBottomSheet.java @@ -100,7 +100,7 @@ public void onBindViewHolder(@NonNull TrackGroupViewHolder holder, int position) int adapterPosition = holder.getAdapterPosition(); if (adapterPosition != RecyclerView.NO_POSITION && target instanceof TracksTabsFragment) { TrackTab tab = trackTabs.get(adapterPosition); - ((TracksTabsFragment) target).setSelectedTab(tab.getTypeName()); + ((TracksTabsFragment) target).setSelectedTab(tab.getId()); } dismiss(); }); diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackItemsFragment.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackItemsFragment.java index da2bbb77b3c..35d28591c2d 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackItemsFragment.java @@ -27,9 +27,9 @@ public class TrackItemsFragment extends BaseOsmAndFragment implements OsmAndComp public static final String TAG = TrackItemsFragment.class.getSimpleName(); - private static final String TRACK_TAB_NAME_KEY = "track_tab_name_key"; + private static final String TRACK_TAB_ID_KEY = "track_tab_id_key"; - private String trackTabName; + private String trackTabId; private TracksAdapter adapter; private RecyclerView recyclerView; @@ -79,11 +79,11 @@ private void setupAdapter(@NonNull TrackTab trackTab) { @Nullable public TrackTab getTrackTab() { BaseTracksTabsFragment fragment = (BaseTracksTabsFragment) requireParentFragment(); - return fragment.getTab(trackTabName); + return fragment.getTab(trackTabId); } public void setTrackTab(@NonNull TrackTab trackTab) { - this.trackTabName = trackTab.getTypeName(); + this.trackTabId = trackTab.getId(); } @Override @@ -118,7 +118,7 @@ public void onPause() { @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - outState.putString(TRACK_TAB_NAME_KEY, trackTabName); + outState.putString(TRACK_TAB_ID_KEY, trackTabId); } @Override diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackPathUtil.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackPathUtil.java new file mode 100644 index 00000000000..e348116c281 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackPathUtil.java @@ -0,0 +1,28 @@ +package net.osmand.plus.configmap.tracks; + +import androidx.annotation.NonNull; + +import net.osmand.IndexConstants; +import net.osmand.shared.io.KFile; + +import java.io.File; + +public class TrackPathUtil { + + @NonNull + public static String getRelativePath(@NonNull KFile file) { + return getRelativePath(file.absolutePath()); + } + + @NonNull + public static String getRelativePath(@NonNull File file) { + return getRelativePath(file.getAbsolutePath()); + } + + @NonNull + public static String getRelativePath(@NonNull String absolutePath) { + int startIndex = absolutePath.indexOf(IndexConstants.GPX_INDEX_DIR); + return absolutePath.substring(startIndex); + } + +} diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java index 42e2fa750d6..dcd3846e90b 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java @@ -1,5 +1,7 @@ package net.osmand.plus.configmap.tracks; +import static net.osmand.plus.configmap.tracks.TrackTabType.SMART_FOLDER; + import android.content.Context; import androidx.annotation.NonNull; @@ -11,15 +13,14 @@ import net.osmand.shared.gpx.data.TrackFolder; import net.osmand.plus.track.helpers.GpxUiHelper; import net.osmand.shared.gpx.TrackItem; +import net.osmand.shared.gpx.data.TracksGroup; import net.osmand.shared.gpx.filters.TrackFolderAnalysis; import java.io.File; import java.util.ArrayList; import java.util.List; -public class TrackTab implements ComparableTracksGroup { - - public static final String SMART_FOLDER_TAB_NAME_PREFIX = "SMART_FOLDER___"; +public class TrackTab implements TracksGroup, ComparableTracksGroup { @NonNull public final TrackTabType type; @@ -41,7 +42,7 @@ public TrackTab(@NonNull File directory) { public TrackTab(@NonNull SmartFolder smartFolder) { this.directory = null; this.smartFolder = smartFolder; - this.type = TrackTabType.SMART_FOLDER; + this.type = SMART_FOLDER; } public TrackTab(@NonNull TrackTabType type) { @@ -50,6 +51,22 @@ public TrackTab(@NonNull TrackTabType type) { this.type = type; } + @NonNull + @Override + public String getId() { + return switch (type) { + case FOLDER -> directory != null ? TrackPathUtil.getRelativePath(directory) : ""; + case SMART_FOLDER -> smartFolder != null ? smartFolder.getId() : ""; + default -> type.name(); + }; + } + + @NonNull + @Override + public String getName() { + return null; + } + @Nullable @Override public TrackFolderAnalysis getFolderAnalysis() { @@ -94,15 +111,6 @@ public String getDirName(boolean includeParentName) { return ""; } - @NonNull - public String getTypeName() { - return switch (type) { - case FOLDER -> directory != null ? directory.getName() : ""; - case SMART_FOLDER -> smartFolder != null ? SMART_FOLDER_TAB_NAME_PREFIX + smartFolder.getDirName() : ""; - default -> type.name(); - }; - } - @NonNull public List getTrackItems() { List trackItems = new ArrayList<>(); @@ -139,6 +147,6 @@ public long lastModified() { @NonNull @Override public String toString() { - return "TrackTab{name=" + getTypeName() + "}"; + return "TrackTab{name=" + getId() + "}"; } } diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java index 11d12f740aa..2f775178205 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java @@ -66,8 +66,8 @@ public List getSortedTrackTabs(boolean checkParentName) { } @Nullable - public TrackTab getTrackTab(@NonNull String key) { - return trackTabs.get(key); + public TrackTab getTrackTab(@NonNull String id) { + return trackTabs.get(id); } @NonNull @@ -113,10 +113,10 @@ private void updateTrackTabs(@NonNull Map folderTabs) { trackTabs.put(TrackTabType.ON_MAP.name(), getTracksOnMapTab()); trackTabs.put(TrackTabType.ALL.name(), getAllTracksTab()); for (TrackTab tab : getAllSmartFoldersTabs()) { - trackTabs.put(tab.getTypeName(), tab); + trackTabs.put(tab.getId(), tab); } for (TrackTab tab : folderTabs.values()) { - trackTabs.put(tab.getTypeName(), tab); + trackTabs.put(tab.getId(), tab); } loadTabsSortModes(); sortTrackTabs(); @@ -252,11 +252,11 @@ private TrackTab addTrackItem(@NonNull Map trackTabs, @NonNull if (file != null && file.getParentFile() != null) { KFile dir = file.getParentFile(); if(dir != null) { - TrackTab trackTab = trackTabs.get(dir.name()); + TrackTab trackTab = trackTabs.get(TrackPathUtil.getRelativePath(dir)); if (trackTab == null) { trackTab = new TrackTab(SharedUtil.jFile(dir)); trackTab.items.add(TYPE_SORT_TRACKS); - trackTabs.put(trackTab.getTypeName(), trackTab); + trackTabs.put(trackTab.getId(), trackTab); } trackTab.items.add(item); return trackTab; @@ -316,9 +316,9 @@ public void sortTrackTab(@NonNull TrackTab trackTab) { } public void loadTabsSortModes() { - Map tabsSortModes = settings.getTrackSortModes(); - if (!Algorithms.isEmpty(tabsSortModes)) { - for (Entry entry : tabsSortModes.entrySet()) { + Map sortModes = settings.getTrackSortModes(); + if (!Algorithms.isEmpty(sortModes)) { + for (Entry entry : sortModes.entrySet()) { TrackTab trackTab = trackTabs.get(entry.getKey()); if (trackTab != null) { trackTab.setSortMode(TracksSortMode.getByValue(entry.getValue())); @@ -330,16 +330,16 @@ public void loadTabsSortModes() { public void saveTabsSortModes() { Map tabsSortModes = settings.getTrackSortModes(); for (TrackTab trackTab : trackTabs.values()) { - String name = trackTab.getTypeName(); + String id = trackTab.getId(); String sortType = trackTab.getSortMode().name(); - tabsSortModes.put(name, sortType); + tabsSortModes.put(id, sortType); } settings.saveTabsSortModes(tabsSortModes); } @NonNull private TracksSortMode getRootSortMode() { - String rootDir = app.getAppPath(IndexConstants.GPX_INDEX_DIR).getName(); + String rootDir = TrackPathUtil.getRelativePath(app.getAppPath(IndexConstants.GPX_INDEX_DIR)); String key = settings.getTrackSortModes().get(rootDir); return key != null ? TracksSortMode.getByValue(key) : TracksSortMode.getDefaultSortMode(); } diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java index fb666755da3..df3ca9f4fb3 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java @@ -277,10 +277,10 @@ public void deferredLoadTracksFinished(@NonNull TrackFolder folder) { private void applyPreselectedParams() { if (preselectedTabParams != null) { - String tabName = preselectedTabParams.getPreselectedTabName(app, getSortedTrackTabs()); - TrackTab trackTab = getTab(tabName); + String tabId = preselectedTabParams.getPreselectedTabId(); + TrackTab trackTab = getTab(tabId); if (trackTab != null) { - setSelectedTab(tabName); + setSelectedTab(tabId); if (preselectedTabParams.shouldSelectAll()) { itemsSelectionHelper.onItemsSelected(trackTab.getTrackItems(), true); diff --git a/OsmAnd/src/net/osmand/plus/helpers/IntentHelper.java b/OsmAnd/src/net/osmand/plus/helpers/IntentHelper.java index 25b8e943bfe..b7c97af043d 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/IntentHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/IntentHelper.java @@ -2,8 +2,7 @@ import static net.osmand.plus.backup.BackupListeners.OnRegisterDeviceListener; import static net.osmand.plus.configmap.tracks.PreselectedTabParams.CALLING_FRAGMENT_TAG; -import static net.osmand.plus.configmap.tracks.PreselectedTabParams.PRESELECTED_TRACKS_TAB_NAME; -import static net.osmand.plus.configmap.tracks.PreselectedTabParams.PRESELECTED_TRACKS_TAB_TYPE; +import static net.osmand.plus.configmap.tracks.PreselectedTabParams.PRESELECTED_TRACKS_TAB_ID; import static net.osmand.plus.configmap.tracks.PreselectedTabParams.SELECT_ALL_ITEMS_ON_TAB; import static net.osmand.plus.helpers.MapFragmentsHelper.CLOSE_ALL_FRAGMENTS; import static net.osmand.plus.settings.fragments.ExportSettingsFragment.SELECTED_TYPES; @@ -560,13 +559,12 @@ public void parseContentIntent() { clearIntent(intent); } Bundle extras = intent.getExtras(); - if (extras != null && intent.hasExtra(PRESELECTED_TRACKS_TAB_NAME) && intent.hasExtra(PRESELECTED_TRACKS_TAB_TYPE)) { - String name = extras.getString(PRESELECTED_TRACKS_TAB_NAME, TrackTabType.ALL.name()); + if (extras != null && intent.hasExtra(PRESELECTED_TRACKS_TAB_ID)) { + String id = extras.getString(PRESELECTED_TRACKS_TAB_ID, TrackTabType.ALL.name()); String callingFragmentTag = extras.getString(CALLING_FRAGMENT_TAG, null); - TrackTabType type = AndroidUtils.getSerializable(extras, PRESELECTED_TRACKS_TAB_TYPE, TrackTabType.class); boolean selectAllItems = intent.getBooleanExtra(SELECT_ALL_ITEMS_ON_TAB, false); - PreselectedTabParams params = new PreselectedTabParams(name, type != null ? type : TrackTabType.ALL, selectAllItems); + PreselectedTabParams params = new PreselectedTabParams(id, selectAllItems); TracksTabsFragment.showInstance(mapActivity.getSupportFragmentManager(), params, callingFragmentTag); clearIntent(intent); } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/tracks/VisibleTracksGroup.java b/OsmAnd/src/net/osmand/plus/myplaces/tracks/VisibleTracksGroup.java index 004350c6c8f..8e555593a17 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/tracks/VisibleTracksGroup.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/tracks/VisibleTracksGroup.java @@ -4,6 +4,7 @@ import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; +import net.osmand.plus.configmap.tracks.TrackTabType; import net.osmand.shared.gpx.TrackItem; import net.osmand.shared.gpx.data.TracksGroup; import net.osmand.plus.track.helpers.GpxSelectionHelper; @@ -22,6 +23,12 @@ public VisibleTracksGroup(@NonNull OsmandApplication app) { this.selectedGpxHelper = app.getSelectedGpxHelper(); } + @NonNull + @Override + public String getId() { + return TrackTabType.ON_MAP.name(); + } + @NonNull @Override public List getTrackItems() { diff --git a/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java index b96ac453ba1..b4ab0ec7fe2 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java @@ -1,10 +1,8 @@ package net.osmand.plus.myplaces.tracks.dialogs; import static net.osmand.plus.configmap.tracks.PreselectedTabParams.CALLING_FRAGMENT_TAG; -import static net.osmand.plus.configmap.tracks.PreselectedTabParams.PRESELECTED_TRACKS_TAB_NAME; -import static net.osmand.plus.configmap.tracks.PreselectedTabParams.PRESELECTED_TRACKS_TAB_TYPE; +import static net.osmand.plus.configmap.tracks.PreselectedTabParams.PRESELECTED_TRACKS_TAB_ID; import static net.osmand.plus.configmap.tracks.PreselectedTabParams.SELECT_ALL_ITEMS_ON_TAB; -import static net.osmand.plus.configmap.tracks.TrackTab.SMART_FOLDER_TAB_NAME_PREFIX; import static net.osmand.plus.configmap.tracks.TrackTabType.FOLDER; import static net.osmand.plus.configmap.tracks.TrackTabType.SMART_FOLDER; import static net.osmand.plus.importfiles.ImportHelper.IMPORT_FILE_REQUEST; @@ -332,23 +330,19 @@ public void showSortByDialog() { @NonNull @Override public TracksSortMode getTracksSortMode() { - Map tabsSortModes = settings.getTrackSortModes(); - for (Entry entry : tabsSortModes.entrySet()) { - if (KAlgorithms.INSTANCE.stringsEqual(entry.getKey(), getSortEntryName())) { + Map sortModes = settings.getTrackSortModes(); + for (Entry entry : sortModes.entrySet()) { + if (KAlgorithms.INSTANCE.stringsEqual(entry.getKey(), getSortEntryId())) { return TracksSortMode.getByValue(entry.getValue()); } } return TracksSortMode.getDefaultSortMode(); } - protected String getSortEntryName() { - if (selectedFolder != null) { - return selectedFolder.getDirName(); - } - if (smartFolder != null) { - return SMART_FOLDER_TAB_NAME_PREFIX + smartFolder.getName(); - } - return null; + @Nullable + protected String getSortEntryId() { + TracksGroup folder = selectedFolder != null ? selectedFolder : smartFolder; + return folder != null ? folder.getId() : null; } @Override @@ -356,30 +350,27 @@ public void setTracksSortMode(@NonNull TracksSortMode sortMode, boolean sortSubF if (sortSubFolders) { sortSubFolder(sortMode); } else { - Map tabsSortModes = settings.getTrackSortModes(); - if (smartFolder != null) { - tabsSortModes.put(SMART_FOLDER_TAB_NAME_PREFIX + smartFolder.getFolderName(), sortMode.name()); - } else { - tabsSortModes.put(selectedFolder.getDirName(), sortMode.name()); - } - settings.saveTabsSortModes(tabsSortModes); - + Map sortModes = settings.getTrackSortModes(); + TracksGroup folder = smartFolder != null ? smartFolder : selectedFolder; + sortModes.put(folder.getId(), sortMode.name()); + settings.saveTabsSortModes(sortModes); updateContent(); } } private void sortSubFolder(TracksSortMode sortMode) { OsmandSettings settings = app.getSettings(); - Map tabsSortModes = settings.getTrackSortModes(); - sortFolders(selectedFolder, tabsSortModes, sortMode); - settings.saveTabsSortModes(tabsSortModes); + Map sortModes = settings.getTrackSortModes(); + sortFolders(selectedFolder, sortModes, sortMode); + settings.saveTabsSortModes(sortModes); app.showToastMessage(app.getString(R.string.sorted_sufolders_toast, selectedFolder.getName(), app.getString(sortMode.getNameId()))); } - private void sortFolders(TrackFolder trackFolder, Map tabsSortModes, TracksSortMode sortMode) { + private void sortFolders(@NonNull TrackFolder trackFolder, @NonNull Map tabsSortModes, + @NonNull TracksSortMode sortMode) { for (TrackFolder folder : trackFolder.getFlattenedSubFolders()) { - tabsSortModes.put(folder.getDirName(), sortMode.name()); + tabsSortModes.put(folder.getId(), sortMode.name()); } } @@ -388,21 +379,21 @@ private void removeSurplusTabsSortModes() { // Execute only from tracks root folder to not lose valid entries return; } + Map result = new HashMap<>(); OsmandSettings settings = app.getSettings(); - Map oldTabsSortModes = settings.getTrackSortModes(); - Map tabsSortModes = new HashMap<>(); + Map oldSortModes = settings.getTrackSortModes(); - tabsSortModes.put(TrackTabType.ON_MAP.name(), oldTabsSortModes.get(TrackTabType.ON_MAP.name())); - tabsSortModes.put(TrackTabType.ALL.name(), oldTabsSortModes.get(TrackTabType.ALL.name())); - tabsSortModes.put(rootFolder.getDirName(), oldTabsSortModes.get(rootFolder.getDirName())); + result.put(TrackTabType.ON_MAP.name(), oldSortModes.get(TrackTabType.ON_MAP.name())); + result.put(TrackTabType.ALL.name(), oldSortModes.get(TrackTabType.ALL.name())); + result.put(rootFolder.getId(), oldSortModes.get(rootFolder.getId())); for (TrackFolder folder : rootFolder.getFlattenedSubFolders()) { - tabsSortModes.put(folder.getDirName(), oldTabsSortModes.get(folder.getDirName())); + result.put(folder.getId(), oldSortModes.get(folder.getId())); } for (SmartFolder folder : app.getSmartFolderHelper().getSmartFolders()) { - String key = SMART_FOLDER_TAB_NAME_PREFIX + folder.getFolderName(); - tabsSortModes.put(key, oldTabsSortModes.get(key)); + String key = folder.getId(); + result.put(key, oldSortModes.get(key)); } - settings.saveTabsSortModes(tabsSortModes); + settings.saveTabsSortModes(result); } @Override @@ -527,20 +518,19 @@ public void onFolderDeleted() { @Override public void showFolderTracksOnMap(@NonNull TrackFolder folder) { - showTracksVisibilityDialog(folder.getDirName(), FOLDER, true); + showTracksVisibilityDialog(folder.getId(), FOLDER, true); } @Override public void showSmartFolderTracksOnMap(@NonNull SmartFolder smartFolder) { - showTracksVisibilityDialog(smartFolder.getFolderName(), SMART_FOLDER, true); + showTracksVisibilityDialog(smartFolder.getId(), SMART_FOLDER, true); } - protected void showTracksVisibilityDialog(@NonNull String name, @NonNull TrackTabType type, boolean selectAll) { + protected void showTracksVisibilityDialog(@NonNull String id, @NonNull TrackTabType type, boolean selectAll) { FragmentActivity activity = getActivity(); if (activity != null) { Bundle bundle = new Bundle(); - bundle.putString(PRESELECTED_TRACKS_TAB_NAME, name); - bundle.putSerializable(PRESELECTED_TRACKS_TAB_TYPE, type); + bundle.putString(PRESELECTED_TRACKS_TAB_ID, id); bundle.putBoolean(SELECT_ALL_ITEMS_ON_TAB, selectAll); bundle.putString(CALLING_FRAGMENT_TAG, TAG); MapActivity.launchMapActivityMoveToTop(activity, storeState(), null, bundle); diff --git a/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java b/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java index 50e89dbeca4..a52ca11ca1a 100644 --- a/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java +++ b/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java @@ -174,11 +174,11 @@ public TrackTab getSelectedTab() { return trackTabs.isEmpty() ? null : trackTabs.get(viewPager.getCurrentItem()); } - public void setSelectedTab(@NonNull String name) { + public void setSelectedTab(@NonNull String id) { List trackTabs = getSortedTrackTabs(); for (int i = 0; i < trackTabs.size(); i++) { TrackTab tab = trackTabs.get(i); - if (Algorithms.stringsEqual(tab.getTypeName(), name)) { + if (Algorithms.stringsEqual(tab.getId(), id)) { viewPager.setCurrentItem(i); break; } @@ -186,9 +186,9 @@ public void setSelectedTab(@NonNull String name) { } @Nullable - public TrackTab getTab(@NonNull String name) { + public TrackTab getTab(@NonNull String id) { for (TrackTab trackTab : getSortedTrackTabs()) { - if (Algorithms.stringsEqual(name, trackTab.getTypeName())) { + if (Algorithms.stringsEqual(id, trackTab.getId())) { return trackTab; } } diff --git a/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java b/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java index e55598c4320..7575db0ce9e 100644 --- a/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java +++ b/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java @@ -158,9 +158,10 @@ public void onTrackItemsSelected(@NonNull Set trackItems, boolean sel } @Nullable - public TrackTab getTab(@NonNull String name) { + @Override + public TrackTab getTab(@NonNull String id) { for (TrackTab trackTab : getSortedTrackTabs()) { - if (Algorithms.stringsEqual(name, trackTab.getTypeName())) { + if (Algorithms.stringsEqual(id, trackTab.getId())) { updateTrackItemsVisibility(trackTab); return trackTab; } From 6939e0e767d3cf677f1738eea7c4da0d4bcde889 Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Mon, 18 Nov 2024 16:31:37 +0200 Subject: [PATCH 03/18] Use relative path for "import" tracks folder as well --- .../net/osmand/plus/configmap/tracks/TracksTabsFragment.java | 3 ++- OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java index df3ca9f4fb3..99cfd43bd7d 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java @@ -25,6 +25,7 @@ import androidx.fragment.app.FragmentManager; import androidx.viewpager.widget.ViewPager.SimpleOnPageChangeListener; +import net.osmand.IndexConstants; import net.osmand.plus.shared.SharedUtil; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; @@ -305,7 +306,7 @@ public void changeAppearance() { protected void addTrackItem(@NonNull TrackItem item) { trackTabsHelper.addTrackItem(item); updateTrackTabs(); - setSelectedTab("import"); + setSelectedTab(IndexConstants.GPX_IMPORT_DIR); updateTabsContent(); updateButtonsState(); } diff --git a/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java b/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java index 7575db0ce9e..400a043f8d6 100644 --- a/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java +++ b/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java @@ -19,6 +19,7 @@ import androidx.fragment.app.FragmentManager; import net.osmand.CallbackWithObject; +import net.osmand.IndexConstants; import net.osmand.plus.shared.SharedUtil; import net.osmand.shared.gpx.GpxFile; import net.osmand.plus.R; @@ -126,7 +127,7 @@ public void onTrackFolderSelected(@NonNull TrackFolder trackFolder) { protected void addTrackItem(@NonNull TrackItem item) { trackTabsHelper.addTrackItem(item); updateTrackTabs(); - setSelectedTab("import"); + setSelectedTab(IndexConstants.GPX_IMPORT_DIR); updateTabsContent(); } From b3d5c331c1010ef9bf59bf9d7400aceeb5ac1368 Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Mon, 18 Nov 2024 18:16:23 +0200 Subject: [PATCH 04/18] Create wrapper class to simplify interaction with track sort modes --- .../tracks/TrackSortModesCollection.java | 88 +++++++++++++++++++ .../configmap/tracks/TrackTabsHelper.java | 26 +++--- .../TrackFolderOptionsController.java | 17 +--- .../dialogs/BaseTrackFolderFragment.java | 57 +++++------- .../tracks/tasks/MoveTrackFoldersTask.java | 12 +-- .../plus/settings/backend/OsmandSettings.java | 35 ++------ .../src/net/osmand/plus/utils/FileUtils.java | 15 ++++ 7 files changed, 146 insertions(+), 104 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java new file mode 100644 index 00000000000..9566eba08d5 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java @@ -0,0 +1,88 @@ +package net.osmand.plus.configmap.tracks; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import net.osmand.IndexConstants; +import net.osmand.plus.settings.backend.OsmandSettings; +import net.osmand.plus.settings.backend.preferences.ListStringPreference; +import net.osmand.plus.settings.enums.TracksSortMode; +import net.osmand.util.Algorithms; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +public class TrackSortModesCollection { + + private static final String SEPARATOR = ",,"; + + private final Map cachedSortModes = new HashMap<>(); + private final ListStringPreference preference; + + public TrackSortModesCollection(@NonNull OsmandSettings settings) { + this.preference = settings.TRACKS_TABS_SORT_MODES; + loadFromPreference(); + } + + @NonNull + public TracksSortMode getRootSortMode() { + return requireSortMode(IndexConstants.GPX_INDEX_DIR); + } + + @NonNull + public TracksSortMode requireSortMode(@Nullable String id) { + TracksSortMode sortMode = getSortMode(id); + return sortMode != null ? sortMode : TracksSortMode.getDefaultSortMode(); + } + + @Nullable + public TracksSortMode getSortMode(@Nullable String id) { + return id != null ? cachedSortModes.get(id) : null; + } + + public void setSortMode(@NonNull String id, @NonNull TracksSortMode sortMode) { + cachedSortModes.put(id, sortMode); + } + + public void clearSurplusKeys(@NonNull Collection validKeys) { + Set keysToClear = new HashSet<>(); + for (String key : cachedSortModes.keySet()) { + if (!validKeys.contains(key)) { + keysToClear.add(key); + } + } + for (String key : keysToClear) { + cachedSortModes.remove(key); + } + } + + public void syncSettings() { + saveToPreference(); + } + + private void loadFromPreference() { + List tokens = preference.getStringsList(); + if (!Algorithms.isEmpty(tokens)) { + for (String token : tokens) { + String[] tokenParts = token.split(SEPARATOR); + if (tokenParts.length == 2) { + cachedSortModes.put(tokenParts[0], TracksSortMode.getByValue(tokenParts[1])); + } + } + } + } + + private void saveToPreference() { + List tokens = new ArrayList<>(); + for (Entry entry : cachedSortModes.entrySet()) { + tokens.add(entry.getKey() + SEPARATOR + entry.getValue().name()); + } + preference.setStringsList(tokens); + } +} diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java index 2f775178205..f549237704b 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java @@ -8,7 +8,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import net.osmand.IndexConstants; import net.osmand.plus.shared.SharedUtil; import net.osmand.data.LatLon; import net.osmand.shared.gpx.GpxFile; @@ -316,32 +315,27 @@ public void sortTrackTab(@NonNull TrackTab trackTab) { } public void loadTabsSortModes() { - Map sortModes = settings.getTrackSortModes(); - if (!Algorithms.isEmpty(sortModes)) { - for (Entry entry : sortModes.entrySet()) { - TrackTab trackTab = trackTabs.get(entry.getKey()); - if (trackTab != null) { - trackTab.setSortMode(TracksSortMode.getByValue(entry.getValue())); - } + TrackSortModesCollection sortModes = settings.getTrackSortModes(); + for (Entry entry : trackTabs.entrySet()) { + TracksSortMode sortMode = sortModes.getSortMode(entry.getKey()); + if (sortMode != null) { + TrackTab trackTab = entry.getValue(); + trackTab.setSortMode(sortMode); } } } public void saveTabsSortModes() { - Map tabsSortModes = settings.getTrackSortModes(); + TrackSortModesCollection sortModes = settings.getTrackSortModes(); for (TrackTab trackTab : trackTabs.values()) { - String id = trackTab.getId(); - String sortType = trackTab.getSortMode().name(); - tabsSortModes.put(id, sortType); + sortModes.setSortMode(trackTab.getId(), trackTab.getSortMode()); } - settings.saveTabsSortModes(tabsSortModes); + sortModes.syncSettings(); } @NonNull private TracksSortMode getRootSortMode() { - String rootDir = TrackPathUtil.getRelativePath(app.getAppPath(IndexConstants.GPX_INDEX_DIR)); - String key = settings.getTrackSortModes().get(rootDir); - return key != null ? TracksSortMode.getByValue(key) : TracksSortMode.getDefaultSortMode(); + return settings.getTrackSortModes().getRootSortMode(); } @NonNull diff --git a/OsmAnd/src/net/osmand/plus/myplaces/tracks/controller/TrackFolderOptionsController.java b/OsmAnd/src/net/osmand/plus/myplaces/tracks/controller/TrackFolderOptionsController.java index b9b1133a2ad..48964b79007 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/tracks/controller/TrackFolderOptionsController.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/tracks/controller/TrackFolderOptionsController.java @@ -20,7 +20,6 @@ import net.osmand.plus.base.dialog.data.DisplayItem; import net.osmand.plus.base.dialog.interfaces.controller.IDialogItemClicked; import net.osmand.plus.base.dialog.interfaces.controller.IDisplayDataProvider; -import net.osmand.shared.gpx.TrackItem; import net.osmand.plus.myplaces.tracks.TrackFoldersHelper; import net.osmand.plus.settings.bottomsheets.CustomizableOptionsBottomSheet; import net.osmand.shared.gpx.data.TrackFolder; @@ -37,11 +36,9 @@ import org.apache.commons.logging.Log; import java.io.File; -import java.util.ArrayList; -import java.util.List; -public class TrackFolderOptionsController extends BaseDialogController implements IDisplayDataProvider, - IDialogItemClicked, TrackFolderOptionsListener { +public class TrackFolderOptionsController extends BaseDialogController + implements IDisplayDataProvider, IDialogItemClicked, TrackFolderOptionsListener { private final static Log LOG = PlatformUtil.getLog(TrackFolderOptionsController.class); @@ -175,15 +172,7 @@ private void renameFolder(@NonNull String newName) { if (oldDir.renameTo(newDir)) { trackFolder.setDirFile(SharedUtil.kFile(newDir)); trackFolder.resetCachedData(); - - List files = new ArrayList<>(); - for (TrackItem trackItem : trackFolder.getFlattenedTrackItems()) { - KFile file = trackItem.getFile(); - if (file != null) { - files.add(SharedUtil.jFile(file)); - } - } - FileUtils.updateMovedGpxFiles(app, files, oldDir, newDir); + FileUtils.updateMovedTrackFolder(app, trackFolder, oldDir, newDir); dialogManager.askRefreshDialogCompletely(PROCESS_ID); diff --git a/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java index b4ab0ec7fe2..71bd07a2e93 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java @@ -32,6 +32,7 @@ import androidx.recyclerview.widget.RecyclerView.ViewHolder; import net.osmand.IndexConstants; +import net.osmand.plus.configmap.tracks.TrackSortModesCollection; import net.osmand.plus.shared.SharedUtil; import net.osmand.data.LatLon; import net.osmand.plus.R; @@ -60,7 +61,6 @@ import net.osmand.plus.myplaces.tracks.dialogs.MoveGpxFileBottomSheet.OnTrackFileMoveListener; import net.osmand.plus.myplaces.tracks.dialogs.viewholders.TracksGroupViewHolder.TrackGroupsListener; import net.osmand.plus.plugins.osmedit.oauth.OsmOAuthHelper.OsmAuthorizationListener; -import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.enums.TracksSortMode; import net.osmand.plus.track.fragments.TrackMenuFragment; import net.osmand.plus.track.helpers.GpxSelectionHelper; @@ -81,10 +81,8 @@ import java.io.File; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; +import java.util.HashSet; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; import java.util.Set; public abstract class BaseTrackFolderFragment extends BaseOsmAndFragment implements FragmentStateHolder, @@ -330,13 +328,7 @@ public void showSortByDialog() { @NonNull @Override public TracksSortMode getTracksSortMode() { - Map sortModes = settings.getTrackSortModes(); - for (Entry entry : sortModes.entrySet()) { - if (KAlgorithms.INSTANCE.stringsEqual(entry.getKey(), getSortEntryId())) { - return TracksSortMode.getByValue(entry.getValue()); - } - } - return TracksSortMode.getDefaultSortMode(); + return settings.getTrackSortModes().requireSortMode(getSortEntryId()); } @Nullable @@ -350,50 +342,47 @@ public void setTracksSortMode(@NonNull TracksSortMode sortMode, boolean sortSubF if (sortSubFolders) { sortSubFolder(sortMode); } else { - Map sortModes = settings.getTrackSortModes(); TracksGroup folder = smartFolder != null ? smartFolder : selectedFolder; - sortModes.put(folder.getId(), sortMode.name()); - settings.saveTabsSortModes(sortModes); + TrackSortModesCollection sortModes = settings.getTrackSortModes(); + sortModes.setSortMode(folder.getId(), sortMode); + sortModes.syncSettings(); updateContent(); } } - private void sortSubFolder(TracksSortMode sortMode) { - OsmandSettings settings = app.getSettings(); - Map sortModes = settings.getTrackSortModes(); + private void sortSubFolder(@NonNull TracksSortMode sortMode) { + TrackSortModesCollection sortModes = settings.getTrackSortModes(); sortFolders(selectedFolder, sortModes, sortMode); - settings.saveTabsSortModes(sortModes); + sortModes.syncSettings(); app.showToastMessage(app.getString(R.string.sorted_sufolders_toast, selectedFolder.getName(), app.getString(sortMode.getNameId()))); } - private void sortFolders(@NonNull TrackFolder trackFolder, @NonNull Map tabsSortModes, + private void sortFolders(@NonNull TrackFolder trackFolder, @NonNull TrackSortModesCollection sortModes, @NonNull TracksSortMode sortMode) { for (TrackFolder folder : trackFolder.getFlattenedSubFolders()) { - tabsSortModes.put(folder.getId(), sortMode.name()); + sortModes.setSortMode(folder.getId(), sortMode); } } - private void removeSurplusTabsSortModes() { + private void removeSurplusSortModes() { if (!rootFolder.getDirFile().equals(app.getAppPathKt(IndexConstants.GPX_INDEX_DIR))) { // Execute only from tracks root folder to not lose valid entries return; } - Map result = new HashMap<>(); - OsmandSettings settings = app.getSettings(); - Map oldSortModes = settings.getTrackSortModes(); - - result.put(TrackTabType.ON_MAP.name(), oldSortModes.get(TrackTabType.ON_MAP.name())); - result.put(TrackTabType.ALL.name(), oldSortModes.get(TrackTabType.ALL.name())); - result.put(rootFolder.getId(), oldSortModes.get(rootFolder.getId())); + Set validKeys = new HashSet<>(); + validKeys.add(TrackTabType.ON_MAP.name()); + validKeys.add(TrackTabType.ALL.name()); + validKeys.add(rootFolder.getId()); for (TrackFolder folder : rootFolder.getFlattenedSubFolders()) { - result.put(folder.getId(), oldSortModes.get(folder.getId())); + validKeys.add(folder.getId()); } for (SmartFolder folder : app.getSmartFolderHelper().getSmartFolders()) { - String key = folder.getId(); - result.put(key, oldSortModes.get(key)); + validKeys.add(folder.getId()); } - settings.saveTabsSortModes(result); + TrackSortModesCollection sortModes = settings.getTrackSortModes(); + sortModes.clearSurplusKeys(validKeys); + sortModes.syncSettings(); } @Override @@ -507,13 +496,13 @@ public ScreenPositionData getFirstSuitableItemScreenPosition() { @Override public void onFolderRenamed(@NonNull File newDir) { updateContent(); - removeSurplusTabsSortModes(); + removeSurplusSortModes(); } @Override public void onFolderDeleted() { reloadTracks(); - removeSurplusTabsSortModes(); + removeSurplusSortModes(); } @Override diff --git a/OsmAnd/src/net/osmand/plus/myplaces/tracks/tasks/MoveTrackFoldersTask.java b/OsmAnd/src/net/osmand/plus/myplaces/tracks/tasks/MoveTrackFoldersTask.java index 10ee98784a0..52626052551 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/tracks/tasks/MoveTrackFoldersTask.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/tracks/tasks/MoveTrackFoldersTask.java @@ -15,10 +15,8 @@ import net.osmand.util.Algorithms; import java.io.File; -import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; -import java.util.List; import java.util.Set; public class MoveTrackFoldersTask extends BaseLoadAsyncTask { @@ -68,15 +66,7 @@ private void moveTrackFolder(@NonNull TrackFolder trackFolder) { File dest = new File(destinationFolder, src.name()); if (src.renameTo(dest.getAbsolutePath())) { dest.setLastModified(System.currentTimeMillis()); - - List files = new ArrayList<>(); - for (TrackItem trackItem : trackFolder.getFlattenedTrackItems()) { - KFile file = trackItem.getFile(); - if (file != null) { - files.add(SharedUtil.jFile(file)); - } - } - FileUtils.updateMovedGpxFiles(app, files, SharedUtil.jFile(src), dest); + FileUtils.updateMovedTrackFolder(app, trackFolder, SharedUtil.jFile(src), dest); } } } diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java index 8f0f79fdba1..a7f586fa30c 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java @@ -65,6 +65,7 @@ import net.osmand.plus.charts.GPXDataSetAxisType; import net.osmand.plus.charts.GPXDataSetType; import net.osmand.plus.configmap.routes.MtbClassification; +import net.osmand.plus.configmap.tracks.TrackSortModesCollection; import net.osmand.plus.download.IndexItem; import net.osmand.plus.feedback.RateUsState; import net.osmand.plus.helpers.OsmandBackupAgent; @@ -109,7 +110,6 @@ import java.io.File; import java.io.IOException; import java.util.*; -import java.util.Map.Entry; public class OsmandSettings { @@ -2039,37 +2039,14 @@ private String getPagedWidgetIds(@NonNull List pages) { public final CommonPreference SEARCH_TRACKS_SORT_MODE = new EnumStringPreference<>(this, "search_tracks_sort_mode", TracksSortMode.getDefaultSortMode(), TracksSortMode.values()); public final ListStringPreference TRACKS_TABS_SORT_MODES = (ListStringPreference) new ListStringPreference(this, "tracks_tabs_sort_modes", null, ";;").makeGlobal().makeShared().cache(); - @NonNull - public Map getTrackSortModes() { - return getTrackSortModes(TRACKS_TABS_SORT_MODES.getStringsList()); - } - - public void saveTabsSortModes(@NonNull Map tabsSortModes) { - List sortModes = getPlainSortModes(tabsSortModes); - TRACKS_TABS_SORT_MODES.setStringsList(sortModes); - } - - @NonNull - private Map getTrackSortModes(@Nullable List modes) { - Map sortModes = new HashMap<>(); - if (!Algorithms.isEmpty(modes)) { - for (String sortMode : modes) { - String[] tabSortMode = sortMode.split(",,"); - if (tabSortMode.length == 2) { - sortModes.put(tabSortMode[0], tabSortMode[1]); - } - } - } - return sortModes; - } + private TrackSortModesCollection trackSortModesCollection = null; @NonNull - private List getPlainSortModes(@NonNull Map tabsSortModes) { - List sortTypes = new ArrayList<>(); - for (Entry entry : tabsSortModes.entrySet()) { - sortTypes.add(entry.getKey() + ",," + entry.getValue()); + public TrackSortModesCollection getTrackSortModes() { + if (trackSortModesCollection == null) { + trackSortModesCollection = new TrackSortModesCollection(this); } - return sortTypes; + return trackSortModesCollection; } public final OsmandPreference ANIMATE_MY_LOCATION = new BooleanPreference(this, "animate_my_location", true).makeProfile().cache(); diff --git a/OsmAnd/src/net/osmand/plus/utils/FileUtils.java b/OsmAnd/src/net/osmand/plus/utils/FileUtils.java index 12d5553aa48..6194fa220bc 100644 --- a/OsmAnd/src/net/osmand/plus/utils/FileUtils.java +++ b/OsmAnd/src/net/osmand/plus/utils/FileUtils.java @@ -24,7 +24,10 @@ import net.osmand.plus.track.helpers.SelectedGpxFile; import net.osmand.plus.track.helpers.save.SaveGpxHelper; import net.osmand.shared.gpx.GpxFile; +import net.osmand.shared.gpx.TrackItem; +import net.osmand.shared.gpx.data.TrackFolder; import net.osmand.shared.gpx.primitives.Metadata; +import net.osmand.shared.io.KFile; import net.osmand.util.Algorithms; import net.osmand.util.CollectionUtils; @@ -174,6 +177,18 @@ private static void updateGpxMetadata(@NonNull GpxFile gpxFile, @NonNull String } } + public static void updateMovedTrackFolder(@NonNull OsmandApplication app, @NonNull TrackFolder trackFolder, + @NonNull File srcDir, @NonNull File destDir) { + List files = new ArrayList<>(); + for (TrackItem trackItem : trackFolder.getFlattenedTrackItems()) { + KFile file = trackItem.getFile(); + if (file != null) { + files.add(SharedUtil.jFile(file)); + } + } + updateMovedGpxFiles(app, files, srcDir, destDir); + } + public static void updateMovedGpxFiles(@NonNull OsmandApplication app, @NonNull List files, @NonNull File srcDir, @NonNull File destDir) { for (File srcFile : files) { From dfaf225648384c5abab33b595e6f603e0b6c0931 Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Mon, 18 Nov 2024 18:26:34 +0200 Subject: [PATCH 05/18] Update track sort mode after folder was moved or renamed --- .../configmap/tracks/TrackSortModesCollection.java | 7 +++++++ OsmAnd/src/net/osmand/plus/utils/FileUtils.java | 12 ++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java index 9566eba08d5..82e8b900e81 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java @@ -50,6 +50,13 @@ public void setSortMode(@NonNull String id, @NonNull TracksSortMode sortMode) { cachedSortModes.put(id, sortMode); } + public void replaceKey(@NonNull String oldKey, @NonNull String newKey) { + TracksSortMode sortMode = cachedSortModes.remove(oldKey); + if (sortMode != null) { + cachedSortModes.put(newKey, sortMode); + } + } + public void clearSurplusKeys(@NonNull Collection validKeys) { Set keysToClear = new HashSet<>(); for (String key : cachedSortModes.keySet()) { diff --git a/OsmAnd/src/net/osmand/plus/utils/FileUtils.java b/OsmAnd/src/net/osmand/plus/utils/FileUtils.java index 6194fa220bc..49634bc212b 100644 --- a/OsmAnd/src/net/osmand/plus/utils/FileUtils.java +++ b/OsmAnd/src/net/osmand/plus/utils/FileUtils.java @@ -12,6 +12,8 @@ import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; +import net.osmand.plus.configmap.tracks.TrackPathUtil; +import net.osmand.plus.configmap.tracks.TrackSortModesCollection; import net.osmand.plus.shared.SharedUtil; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; @@ -179,6 +181,12 @@ private static void updateGpxMetadata(@NonNull GpxFile gpxFile, @NonNull String public static void updateMovedTrackFolder(@NonNull OsmandApplication app, @NonNull TrackFolder trackFolder, @NonNull File srcDir, @NonNull File destDir) { + TrackSortModesCollection sortModes = app.getSettings().getTrackSortModes(); + String oldKey = TrackPathUtil.getRelativePath(srcDir); + String newKey = TrackPathUtil.getRelativePath(destDir); + sortModes.replaceKey(oldKey, newKey); + sortModes.syncSettings(); + List files = new ArrayList<>(); for (TrackItem trackItem : trackFolder.getFlattenedTrackItems()) { KFile file = trackItem.getFile(); @@ -189,8 +197,8 @@ public static void updateMovedTrackFolder(@NonNull OsmandApplication app, @NonNu updateMovedGpxFiles(app, files, srcDir, destDir); } - public static void updateMovedGpxFiles(@NonNull OsmandApplication app, @NonNull List files, - @NonNull File srcDir, @NonNull File destDir) { + private static void updateMovedGpxFiles(@NonNull OsmandApplication app, @NonNull List files, + @NonNull File srcDir, @NonNull File destDir) { for (File srcFile : files) { String path = srcFile.getAbsolutePath(); String newPath = path.replace(srcDir.getAbsolutePath(), destDir.getAbsolutePath()); From 5e5d4cb2a4708e7692faece02667fdd3decd7fe5 Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Tue, 19 Nov 2024 18:19:15 +0200 Subject: [PATCH 06/18] Upgrade sort mode keys on flight --- .../net/osmand/shared/gpx/data/SmartFolder.kt | 5 +- .../net/osmand/shared/gpx/data/TrackFolder.kt | 17 ++++-- .../configmap/tracks/TrackFolderUtil.java | 51 +++++++++++++++++ .../plus/configmap/tracks/TrackPathUtil.java | 28 ---------- .../tracks/TrackSortModesCollection.java | 55 +++++++++++++++---- .../plus/configmap/tracks/TrackTab.java | 2 +- .../configmap/tracks/TrackTabsHelper.java | 7 ++- .../dialogs/BaseTrackFolderFragment.java | 20 +------ .../src/net/osmand/plus/utils/FileUtils.java | 6 +- 9 files changed, 117 insertions(+), 74 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/configmap/tracks/TrackFolderUtil.java delete mode 100644 OsmAnd/src/net/osmand/plus/configmap/tracks/TrackPathUtil.java diff --git a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/SmartFolder.kt b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/SmartFolder.kt index 2e7623e9ec2..832bb91cd61 100644 --- a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/SmartFolder.kt +++ b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/SmartFolder.kt @@ -8,10 +8,11 @@ import net.osmand.shared.gpx.filters.TrackFilterSerializer import net.osmand.shared.gpx.filters.TrackFolderAnalysis import net.osmand.shared.util.KCollectionUtils -const val ID_PREFIX = "SMART_FOLDER___" - @Serializable class SmartFolder(@Serializable var folderName: String) : TracksGroup, ComparableTracksGroup { + companion object { + const val ID_PREFIX = "SMART_FOLDER___" + } @Transient private var trackItems: List? = null diff --git a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt index f1238cf392a..a59f5c233e9 100644 --- a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt +++ b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt @@ -38,7 +38,7 @@ class TrackFolder(dirFile: KFile, parentFolder: TrackFolder?) : } override fun getId(): String { - return relativePath + return getRelativePath(true) } override fun getName(): String { @@ -54,15 +54,20 @@ class TrackFolder(dirFile: KFile, parentFolder: TrackFolder?) : } val relativePath: String - get() { - val dirName = getDirName() - val parentFolder = getParentFolder() - return if (parentFolder != null && !parentFolder.isRootFolder) parentFolder.relativePath + "/" + dirName else dirName - } + get() = getRelativePath(false) val isRootFolder: Boolean get() = getParentFolder() == null + private fun getRelativePath(includeRootFolder: Boolean): String { + val dirName = getDirName() + val parentFolder = getParentFolder() + return if (parentFolder != null && (!parentFolder.isRootFolder || includeRootFolder)) + parentFolder.relativePath + "/" + dirName else dirName + } + + fun getRootFolder(): TrackFolder = getParentFolder()?.getRootFolder() ?: this + fun getParentFolder(): TrackFolder? { return parentFolder } diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackFolderUtil.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackFolderUtil.java new file mode 100644 index 00000000000..f7c863fe2d6 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackFolderUtil.java @@ -0,0 +1,51 @@ +package net.osmand.plus.configmap.tracks; + +import androidx.annotation.NonNull; + +import net.osmand.IndexConstants; +import net.osmand.shared.gpx.data.SmartFolder; +import net.osmand.shared.io.KFile; +import net.osmand.util.CollectionUtils; + +import java.io.File; + +public class TrackFolderUtil { + + @NonNull + public static String getTrackFolderId(@NonNull KFile file) { + return getTrackFolderId(file.absolutePath()); + } + + @NonNull + public static String getTrackFolderId(@NonNull File file) { + return getTrackFolderId(file.getAbsolutePath()); + } + + @NonNull + public static String getTrackFolderId(@NonNull String absolutePath) { + int startIndex = absolutePath.indexOf(IndexConstants.GPX_INDEX_DIR); + return absolutePath.substring(startIndex); + } + + @NonNull + public static String getOutdatedStandardFolderId(@NonNull String id) { + int index = id.lastIndexOf(File.separator); + return index > 0 ? id.substring(index + 1) : id; + } + + public static boolean isOutdatedStandardFolderId(@NonNull String id) { + return !isPredefinedTabId(id) && !isSmartFolderId(id) && !isStandardFolderId(id); + } + + public static boolean isPredefinedTabId(@NonNull String id) { + return CollectionUtils.equalsToAny(id, TrackTabType.ON_MAP.name(), TrackTabType.ALL.name()); + } + + public static boolean isSmartFolderId(@NonNull String id) { + return id.startsWith(SmartFolder.ID_PREFIX); + } + + public static boolean isStandardFolderId(@NonNull String id) { + return id.startsWith(IndexConstants.GPX_INDEX_DIR); + } +} diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackPathUtil.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackPathUtil.java deleted file mode 100644 index e348116c281..00000000000 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackPathUtil.java +++ /dev/null @@ -1,28 +0,0 @@ -package net.osmand.plus.configmap.tracks; - -import androidx.annotation.NonNull; - -import net.osmand.IndexConstants; -import net.osmand.shared.io.KFile; - -import java.io.File; - -public class TrackPathUtil { - - @NonNull - public static String getRelativePath(@NonNull KFile file) { - return getRelativePath(file.absolutePath()); - } - - @NonNull - public static String getRelativePath(@NonNull File file) { - return getRelativePath(file.getAbsolutePath()); - } - - @NonNull - public static String getRelativePath(@NonNull String absolutePath) { - int startIndex = absolutePath.indexOf(IndexConstants.GPX_INDEX_DIR); - return absolutePath.substring(startIndex); - } - -} diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java index 82e8b900e81..ba72c158597 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java @@ -4,19 +4,20 @@ import androidx.annotation.Nullable; import net.osmand.IndexConstants; +import net.osmand.plus.OsmandApplication; import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.backend.preferences.ListStringPreference; import net.osmand.plus.settings.enums.TracksSortMode; +import net.osmand.shared.gpx.data.SmartFolder; +import net.osmand.shared.gpx.data.TrackFolder; import net.osmand.util.Algorithms; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Set; +import java.util.Objects; public class TrackSortModesCollection { @@ -43,7 +44,12 @@ public TracksSortMode requireSortMode(@Nullable String id) { @Nullable public TracksSortMode getSortMode(@Nullable String id) { - return id != null ? cachedSortModes.get(id) : null; + TracksSortMode sortMode = cachedSortModes.get(id); + if (sortMode == null && id != null && TrackFolderUtil.isStandardFolderId(id)) { + String oldId = TrackFolderUtil.getOutdatedStandardFolderId(id); + sortMode = cachedSortModes.get(oldId); + } + return sortMode; } public void setSortMode(@NonNull String id, @NonNull TracksSortMode sortMode) { @@ -57,15 +63,40 @@ public void replaceKey(@NonNull String oldKey, @NonNull String newKey) { } } - public void clearSurplusKeys(@NonNull Collection validKeys) { - Set keysToClear = new HashSet<>(); - for (String key : cachedSortModes.keySet()) { - if (!validKeys.contains(key)) { - keysToClear.add(key); - } + public void askSyncWithUpgrade(@NonNull OsmandApplication app, @NonNull TrackFolder trackFolder) { + if (askUpgradeStoredKeys(app, trackFolder.getRootFolder())) { + syncSettings(); + } + } + + /** + * Removes surplus keys and upgrades outdated ones. + * Then saves sort modes with upgraded keys to the preferences. + */ + public boolean askUpgradeStoredKeys(@NonNull OsmandApplication app, @NonNull TrackFolder rootFolder) { + if (!Objects.equals(rootFolder.getDirFile(), app.getAppPathKt(IndexConstants.GPX_INDEX_DIR))) { + // Execute only from tracks root folder to not lose valid entries + return false; + } + Map upgradedCache = new HashMap<>(); + putUpgradedKey(upgradedCache, TrackTabType.ON_MAP.name()); + putUpgradedKey(upgradedCache, TrackTabType.ALL.name()); + putUpgradedKey(upgradedCache, rootFolder.getId()); + for (TrackFolder folder : rootFolder.getFlattenedSubFolders()) { + putUpgradedKey(upgradedCache, folder.getId()); } - for (String key : keysToClear) { - cachedSortModes.remove(key); + for (SmartFolder folder : app.getSmartFolderHelper().getSmartFolders()) { + putUpgradedKey(upgradedCache, folder.getId()); + } + cachedSortModes.clear(); + cachedSortModes.putAll(upgradedCache); + return true; + } + + private void putUpgradedKey(@NonNull Map map, @NonNull String id) { + TracksSortMode sortMode = getSortMode(id); + if (sortMode != null) { + map.put(id, sortMode); } } diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java index dcd3846e90b..949f71f9662 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java @@ -55,7 +55,7 @@ public TrackTab(@NonNull TrackTabType type) { @Override public String getId() { return switch (type) { - case FOLDER -> directory != null ? TrackPathUtil.getRelativePath(directory) : ""; + case FOLDER -> directory != null ? TrackFolderUtil.getTrackFolderId(directory) : ""; case SMART_FOLDER -> smartFolder != null ? smartFolder.getId() : ""; default -> type.name(); }; diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java index f549237704b..736829e0a0c 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java @@ -250,12 +250,13 @@ private TrackTab addTrackItem(@NonNull Map trackTabs, @NonNull KFile file = item.getFile(); if (file != null && file.getParentFile() != null) { KFile dir = file.getParentFile(); - if(dir != null) { - TrackTab trackTab = trackTabs.get(TrackPathUtil.getRelativePath(dir)); + if (dir != null) { + String folderId = TrackFolderUtil.getTrackFolderId(dir); + TrackTab trackTab = trackTabs.get(folderId); if (trackTab == null) { trackTab = new TrackTab(SharedUtil.jFile(dir)); trackTab.items.add(TYPE_SORT_TRACKS); - trackTabs.put(trackTab.getId(), trackTab); + trackTabs.put(folderId, trackTab); } trackTab.items.add(item); return trackTab; diff --git a/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java index 71bd07a2e93..775821d564d 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java @@ -31,7 +31,6 @@ import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView.ViewHolder; -import net.osmand.IndexConstants; import net.osmand.plus.configmap.tracks.TrackSortModesCollection; import net.osmand.plus.shared.SharedUtil; import net.osmand.data.LatLon; @@ -81,7 +80,6 @@ import java.io.File; import java.util.ArrayList; import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Set; @@ -366,23 +364,7 @@ private void sortFolders(@NonNull TrackFolder trackFolder, @NonNull TrackSortMod } private void removeSurplusSortModes() { - if (!rootFolder.getDirFile().equals(app.getAppPathKt(IndexConstants.GPX_INDEX_DIR))) { - // Execute only from tracks root folder to not lose valid entries - return; - } - Set validKeys = new HashSet<>(); - validKeys.add(TrackTabType.ON_MAP.name()); - validKeys.add(TrackTabType.ALL.name()); - validKeys.add(rootFolder.getId()); - for (TrackFolder folder : rootFolder.getFlattenedSubFolders()) { - validKeys.add(folder.getId()); - } - for (SmartFolder folder : app.getSmartFolderHelper().getSmartFolders()) { - validKeys.add(folder.getId()); - } - TrackSortModesCollection sortModes = settings.getTrackSortModes(); - sortModes.clearSurplusKeys(validKeys); - sortModes.syncSettings(); + settings.getTrackSortModes().askSyncWithUpgrade(app, rootFolder); } @Override diff --git a/OsmAnd/src/net/osmand/plus/utils/FileUtils.java b/OsmAnd/src/net/osmand/plus/utils/FileUtils.java index 49634bc212b..bda74ef7375 100644 --- a/OsmAnd/src/net/osmand/plus/utils/FileUtils.java +++ b/OsmAnd/src/net/osmand/plus/utils/FileUtils.java @@ -12,7 +12,7 @@ import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; -import net.osmand.plus.configmap.tracks.TrackPathUtil; +import net.osmand.plus.configmap.tracks.TrackFolderUtil; import net.osmand.plus.configmap.tracks.TrackSortModesCollection; import net.osmand.plus.shared.SharedUtil; import net.osmand.plus.OsmandApplication; @@ -182,8 +182,8 @@ private static void updateGpxMetadata(@NonNull GpxFile gpxFile, @NonNull String public static void updateMovedTrackFolder(@NonNull OsmandApplication app, @NonNull TrackFolder trackFolder, @NonNull File srcDir, @NonNull File destDir) { TrackSortModesCollection sortModes = app.getSettings().getTrackSortModes(); - String oldKey = TrackPathUtil.getRelativePath(srcDir); - String newKey = TrackPathUtil.getRelativePath(destDir); + String oldKey = TrackFolderUtil.getTrackFolderId(srcDir); + String newKey = TrackFolderUtil.getTrackFolderId(destDir); sortModes.replaceKey(oldKey, newKey); sortModes.syncSettings(); From d4556270a35cf68166b99748aacdd37c2bd3960e Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:19:56 +0200 Subject: [PATCH 07/18] TrackSortModesCollection: small logic and code appearance fixes --- .../configmap/tracks/TrackFolderUtil.java | 51 ----------------- .../tracks/TrackSortModesCollection.java | 57 ++++++++++++++----- .../plus/configmap/tracks/TrackTab.java | 2 +- .../configmap/tracks/TrackTabsHelper.java | 2 +- .../src/net/osmand/plus/utils/FileUtils.java | 12 ++-- 5 files changed, 50 insertions(+), 74 deletions(-) delete mode 100644 OsmAnd/src/net/osmand/plus/configmap/tracks/TrackFolderUtil.java diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackFolderUtil.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackFolderUtil.java deleted file mode 100644 index f7c863fe2d6..00000000000 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackFolderUtil.java +++ /dev/null @@ -1,51 +0,0 @@ -package net.osmand.plus.configmap.tracks; - -import androidx.annotation.NonNull; - -import net.osmand.IndexConstants; -import net.osmand.shared.gpx.data.SmartFolder; -import net.osmand.shared.io.KFile; -import net.osmand.util.CollectionUtils; - -import java.io.File; - -public class TrackFolderUtil { - - @NonNull - public static String getTrackFolderId(@NonNull KFile file) { - return getTrackFolderId(file.absolutePath()); - } - - @NonNull - public static String getTrackFolderId(@NonNull File file) { - return getTrackFolderId(file.getAbsolutePath()); - } - - @NonNull - public static String getTrackFolderId(@NonNull String absolutePath) { - int startIndex = absolutePath.indexOf(IndexConstants.GPX_INDEX_DIR); - return absolutePath.substring(startIndex); - } - - @NonNull - public static String getOutdatedStandardFolderId(@NonNull String id) { - int index = id.lastIndexOf(File.separator); - return index > 0 ? id.substring(index + 1) : id; - } - - public static boolean isOutdatedStandardFolderId(@NonNull String id) { - return !isPredefinedTabId(id) && !isSmartFolderId(id) && !isStandardFolderId(id); - } - - public static boolean isPredefinedTabId(@NonNull String id) { - return CollectionUtils.equalsToAny(id, TrackTabType.ON_MAP.name(), TrackTabType.ALL.name()); - } - - public static boolean isSmartFolderId(@NonNull String id) { - return id.startsWith(SmartFolder.ID_PREFIX); - } - - public static boolean isStandardFolderId(@NonNull String id) { - return id.startsWith(IndexConstants.GPX_INDEX_DIR); - } -} diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java index ba72c158597..c88a677b5ee 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java @@ -12,6 +12,7 @@ import net.osmand.shared.gpx.data.TrackFolder; import net.osmand.util.Algorithms; +import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -21,6 +22,7 @@ public class TrackSortModesCollection { + private static final String ROOT_FOLDER = IndexConstants.GPX_INDEX_DIR; private static final String SEPARATOR = ",,"; private final Map cachedSortModes = new HashMap<>(); @@ -33,7 +35,7 @@ public TrackSortModesCollection(@NonNull OsmandSettings settings) { @NonNull public TracksSortMode getRootSortMode() { - return requireSortMode(IndexConstants.GPX_INDEX_DIR); + return requireSortMode(ROOT_FOLDER); } @NonNull @@ -45,9 +47,11 @@ public TracksSortMode requireSortMode(@Nullable String id) { @Nullable public TracksSortMode getSortMode(@Nullable String id) { TracksSortMode sortMode = cachedSortModes.get(id); - if (sortMode == null && id != null && TrackFolderUtil.isStandardFolderId(id)) { - String oldId = TrackFolderUtil.getOutdatedStandardFolderId(id); - sortMode = cachedSortModes.get(oldId); + if (sortMode == null && id != null && isFolderIdV2(id)) { + String idV1 = getFolderIdV1(id); + if (idV1 != null) { + sortMode = cachedSortModes.get(idV1); + } } return sortMode; } @@ -56,25 +60,24 @@ public void setSortMode(@NonNull String id, @NonNull TracksSortMode sortMode) { cachedSortModes.put(id, sortMode); } - public void replaceKey(@NonNull String oldKey, @NonNull String newKey) { - TracksSortMode sortMode = cachedSortModes.remove(oldKey); - if (sortMode != null) { - cachedSortModes.put(newKey, sortMode); - } + public void askSyncWithUpgrade(@NonNull OsmandApplication app, @NonNull TrackFolder trackFolder) { + askSyncWithUpgrade(app, trackFolder, false); } - public void askSyncWithUpgrade(@NonNull OsmandApplication app, @NonNull TrackFolder trackFolder) { - if (askUpgradeStoredKeys(app, trackFolder.getRootFolder())) { + public void askSyncWithUpgrade(@NonNull OsmandApplication app, @NonNull TrackFolder trackFolder, + boolean forceSync) { + if (askUpgradeCachedKeys(app, trackFolder) || forceSync) { syncSettings(); } } /** * Removes surplus keys and upgrades outdated ones. - * Then saves sort modes with upgraded keys to the preferences. + * Upgraded keys will use folder relative path as a key (V2) instead of folder name (V1). */ - public boolean askUpgradeStoredKeys(@NonNull OsmandApplication app, @NonNull TrackFolder rootFolder) { - if (!Objects.equals(rootFolder.getDirFile(), app.getAppPathKt(IndexConstants.GPX_INDEX_DIR))) { + private boolean askUpgradeCachedKeys(@NonNull OsmandApplication app, @NonNull TrackFolder trackFolder) { + TrackFolder rootFolder = trackFolder.getRootFolder(); + if (!Objects.equals(rootFolder.getDirFile(), app.getAppPathKt(ROOT_FOLDER))) { // Execute only from tracks root folder to not lose valid entries return false; } @@ -100,6 +103,16 @@ private void putUpgradedKey(@NonNull Map map, @NonNull S } } + public void updateMovedTrackFolder(@NonNull OsmandApplication app, + @NonNull TrackFolder trackFolder, @NonNull File oldDir) { + String previousId = getFolderId(oldDir.getAbsolutePath()); + TracksSortMode sortMode = getSortMode(previousId); + if (sortMode != null) { + setSortMode(trackFolder.getId(), sortMode); + askSyncWithUpgrade(app, trackFolder, true); + } + } + public void syncSettings() { saveToPreference(); } @@ -123,4 +136,20 @@ private void saveToPreference() { } preference.setStringsList(tokens); } + + @NonNull + public static String getFolderId(@NonNull String absolutePath) { + int index = absolutePath.indexOf(ROOT_FOLDER); + return index > 0 ? absolutePath.substring(index) : absolutePath; + } + + private static boolean isFolderIdV2(@NonNull String id) { + return id.startsWith(ROOT_FOLDER); + } + + @Nullable + private static String getFolderIdV1(@NonNull String idV2) { + int index = idV2.lastIndexOf(File.separator); + return index > 0 ? idV2.substring(index + 1) : null; + } } diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java index 949f71f9662..feaa234865a 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java @@ -55,7 +55,7 @@ public TrackTab(@NonNull TrackTabType type) { @Override public String getId() { return switch (type) { - case FOLDER -> directory != null ? TrackFolderUtil.getTrackFolderId(directory) : ""; + case FOLDER -> directory != null ? TrackSortModesCollection.getFolderId(directory.getAbsolutePath()) : ""; case SMART_FOLDER -> smartFolder != null ? smartFolder.getId() : ""; default -> type.name(); }; diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java index 736829e0a0c..b382c773e2e 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java @@ -251,7 +251,7 @@ private TrackTab addTrackItem(@NonNull Map trackTabs, @NonNull if (file != null && file.getParentFile() != null) { KFile dir = file.getParentFile(); if (dir != null) { - String folderId = TrackFolderUtil.getTrackFolderId(dir); + String folderId = TrackSortModesCollection.getFolderId(dir.absolutePath()); TrackTab trackTab = trackTabs.get(folderId); if (trackTab == null) { trackTab = new TrackTab(SharedUtil.jFile(dir)); diff --git a/OsmAnd/src/net/osmand/plus/utils/FileUtils.java b/OsmAnd/src/net/osmand/plus/utils/FileUtils.java index bda74ef7375..625da5ae1cc 100644 --- a/OsmAnd/src/net/osmand/plus/utils/FileUtils.java +++ b/OsmAnd/src/net/osmand/plus/utils/FileUtils.java @@ -12,8 +12,8 @@ import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; -import net.osmand.plus.configmap.tracks.TrackFolderUtil; import net.osmand.plus.configmap.tracks.TrackSortModesCollection; +import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.shared.SharedUtil; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; @@ -181,12 +181,6 @@ private static void updateGpxMetadata(@NonNull GpxFile gpxFile, @NonNull String public static void updateMovedTrackFolder(@NonNull OsmandApplication app, @NonNull TrackFolder trackFolder, @NonNull File srcDir, @NonNull File destDir) { - TrackSortModesCollection sortModes = app.getSettings().getTrackSortModes(); - String oldKey = TrackFolderUtil.getTrackFolderId(srcDir); - String newKey = TrackFolderUtil.getTrackFolderId(destDir); - sortModes.replaceKey(oldKey, newKey); - sortModes.syncSettings(); - List files = new ArrayList<>(); for (TrackItem trackItem : trackFolder.getFlattenedTrackItems()) { KFile file = trackItem.getFile(); @@ -195,6 +189,10 @@ public static void updateMovedTrackFolder(@NonNull OsmandApplication app, @NonNu } } updateMovedGpxFiles(app, files, srcDir, destDir); + + OsmandSettings settings = app.getSettings(); + TrackSortModesCollection sortModes = settings.getTrackSortModes(); + sortModes.updateMovedTrackFolder(app, trackFolder, srcDir); } private static void updateMovedGpxFiles(@NonNull OsmandApplication app, @NonNull List files, From de5e683fa52d1d6a4d307d8b3e196cceacd39c90 Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Wed, 20 Nov 2024 14:19:11 +0200 Subject: [PATCH 08/18] Upgrade sort modes collection keys in the proper places --- .../configmap/tracks/TrackSortModesCollection.java | 13 ++++++++++--- .../controller/TrackFolderOptionsController.java | 2 ++ .../tracks/dialogs/BaseTrackFolderFragment.java | 6 ------ OsmAnd/src/net/osmand/plus/utils/FileUtils.java | 8 +++++--- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java index c88a677b5ee..32891338ae0 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java @@ -19,13 +19,14 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; public class TrackSortModesCollection { private static final String ROOT_FOLDER = IndexConstants.GPX_INDEX_DIR; private static final String SEPARATOR = ",,"; - private final Map cachedSortModes = new HashMap<>(); + private final Map cachedSortModes = new ConcurrentHashMap<>(); private final ListStringPreference preference; public TrackSortModesCollection(@NonNull OsmandSettings settings) { @@ -103,8 +104,8 @@ private void putUpgradedKey(@NonNull Map map, @NonNull S } } - public void updateMovedTrackFolder(@NonNull OsmandApplication app, - @NonNull TrackFolder trackFolder, @NonNull File oldDir) { + public void updateAfterMoveTrackFolder(@NonNull OsmandApplication app, + @NonNull TrackFolder trackFolder, @NonNull File oldDir) { String previousId = getFolderId(oldDir.getAbsolutePath()); TracksSortMode sortMode = getSortMode(previousId); if (sortMode != null) { @@ -113,6 +114,12 @@ public void updateMovedTrackFolder(@NonNull OsmandApplication app, } } + public void updateAfterDeleteTrackFolder(@NonNull OsmandApplication app, + @NonNull TrackFolder trackFolder) { + cachedSortModes.remove(trackFolder.getId()); + askSyncWithUpgrade(app, trackFolder, true); + } + public void syncSettings() { saveToPreference(); } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/tracks/controller/TrackFolderOptionsController.java b/OsmAnd/src/net/osmand/plus/myplaces/tracks/controller/TrackFolderOptionsController.java index 48964b79007..0b9cf5a23d7 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/tracks/controller/TrackFolderOptionsController.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/tracks/controller/TrackFolderOptionsController.java @@ -223,6 +223,8 @@ private void showDeleteDialog() { @Override public void onFolderDeleted() { + FileUtils.updateAfterDeleteTrackFolder(app, trackFolder); + // Close options dialog after folder deleted dialogManager.askDismissDialog(PROCESS_ID); diff --git a/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java index 775821d564d..a10cadf5500 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java @@ -363,10 +363,6 @@ private void sortFolders(@NonNull TrackFolder trackFolder, @NonNull TrackSortMod } } - private void removeSurplusSortModes() { - settings.getTrackSortModes().askSyncWithUpgrade(app, rootFolder); - } - @Override public void importTracks() { Intent intent = ImportHelper.getImportFileIntent(); @@ -478,13 +474,11 @@ public ScreenPositionData getFirstSuitableItemScreenPosition() { @Override public void onFolderRenamed(@NonNull File newDir) { updateContent(); - removeSurplusSortModes(); } @Override public void onFolderDeleted() { reloadTracks(); - removeSurplusSortModes(); } @Override diff --git a/OsmAnd/src/net/osmand/plus/utils/FileUtils.java b/OsmAnd/src/net/osmand/plus/utils/FileUtils.java index 625da5ae1cc..f6674426d8d 100644 --- a/OsmAnd/src/net/osmand/plus/utils/FileUtils.java +++ b/OsmAnd/src/net/osmand/plus/utils/FileUtils.java @@ -190,9 +190,11 @@ public static void updateMovedTrackFolder(@NonNull OsmandApplication app, @NonNu } updateMovedGpxFiles(app, files, srcDir, destDir); - OsmandSettings settings = app.getSettings(); - TrackSortModesCollection sortModes = settings.getTrackSortModes(); - sortModes.updateMovedTrackFolder(app, trackFolder, srcDir); + app.getSettings().getTrackSortModes().updateAfterMoveTrackFolder(app, trackFolder, srcDir); + } + + public static void updateAfterDeleteTrackFolder(@NonNull OsmandApplication app, @NonNull TrackFolder trackFolder) { + app.getSettings().getTrackSortModes().updateAfterDeleteTrackFolder(app, trackFolder); } private static void updateMovedGpxFiles(@NonNull OsmandApplication app, @NonNull List files, From baf88d99fbe577415bda5459aea71bf66d7db7f7 Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Thu, 21 Nov 2024 10:53:40 +0200 Subject: [PATCH 09/18] Remove extra file separator to ensure consistency of folder keys --- .../tracks/TrackSortModesCollection.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java index 32891338ae0..3c784bf4947 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java @@ -47,6 +47,7 @@ public TracksSortMode requireSortMode(@Nullable String id) { @Nullable public TracksSortMode getSortMode(@Nullable String id) { + id = removeExtraFileSeparator(id); TracksSortMode sortMode = cachedSortModes.get(id); if (sortMode == null && id != null && isFolderIdV2(id)) { String idV1 = getFolderIdV1(id); @@ -58,15 +59,11 @@ public TracksSortMode getSortMode(@Nullable String id) { } public void setSortMode(@NonNull String id, @NonNull TracksSortMode sortMode) { + id = removeExtraFileSeparator(id); cachedSortModes.put(id, sortMode); } - public void askSyncWithUpgrade(@NonNull OsmandApplication app, @NonNull TrackFolder trackFolder) { - askSyncWithUpgrade(app, trackFolder, false); - } - - public void askSyncWithUpgrade(@NonNull OsmandApplication app, @NonNull TrackFolder trackFolder, - boolean forceSync) { + public void askSyncWithUpgrade(@NonNull OsmandApplication app, @NonNull TrackFolder trackFolder, boolean forceSync) { if (askUpgradeCachedKeys(app, trackFolder) || forceSync) { syncSettings(); } @@ -98,6 +95,7 @@ private boolean askUpgradeCachedKeys(@NonNull OsmandApplication app, @NonNull Tr } private void putUpgradedKey(@NonNull Map map, @NonNull String id) { + id = removeExtraFileSeparator(id); TracksSortMode sortMode = getSortMode(id); if (sortMode != null) { map.put(id, sortMode); @@ -159,4 +157,14 @@ private static String getFolderIdV1(@NonNull String idV2) { int index = idV2.lastIndexOf(File.separator); return index > 0 ? idV2.substring(index + 1) : null; } + + @Nullable + private static String removeExtraFileSeparator(@Nullable String id) { + // Ensure consistency by removing trailing File.separator from relative paths + // before querying or saving to settings to avoid key mismatches. + if (id != null && id.endsWith(File.separator)) { + return id.substring(0, id.length() - 1); + } + return id; + } } From eac8c9ea5ab68060fd69b99206021ffaf13658de Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Thu, 21 Nov 2024 15:10:38 +0200 Subject: [PATCH 10/18] Improve name retrieval code for TrackTab --- .../shared/gpx/TrackFolderLoaderTask.kt | 2 +- .../shared/gpx/data/ComparableTracksGroup.kt | 2 +- .../net/osmand/shared/gpx/data/SmartFolder.kt | 2 +- .../net/osmand/shared/gpx/data/TrackFolder.kt | 23 +++--- .../plus/auto/screens/TracksFoldersScreen.kt | 2 +- .../osmand/plus/auto/screens/TracksScreen.kt | 2 +- .../tracks/TrackFolderLoaderTask.java | 2 +- .../tracks/TrackGroupsBottomSheet.java | 2 +- .../plus/configmap/tracks/TrackTab.java | 71 +++++++++---------- .../configmap/tracks/TrackTabsHelper.java | 16 ++--- .../configmap/tracks/TracksComparator.java | 12 ++-- .../configmap/tracks/TracksTabAdapter.java | 2 +- .../TrackFolderOptionsController.java | 2 +- .../plus/track/BaseTracksTabsFragment.java | 2 +- .../plus/track/SelectTrackFolderFragment.java | 2 +- .../plus/track/helpers/GpxUiHelper.java | 28 ++++---- 16 files changed, 84 insertions(+), 88 deletions(-) diff --git a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/TrackFolderLoaderTask.kt b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/TrackFolderLoaderTask.kt index 5b1db67aa09..b4222663461 100644 --- a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/TrackFolderLoaderTask.kt +++ b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/TrackFolderLoaderTask.kt @@ -51,7 +51,7 @@ class TrackFolderLoaderTask( if (!shouldLoadFolder(cachedRootFolder)) return cachedRootFolder!! val start = currentTimeMillis() - log.info("Start loading tracks in ${folder.getDirName()}") + log.info("Start loading tracks in ${folder.getDirName(true)}") folder.clearData() loadingTime = currentTimeMillis() diff --git a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/ComparableTracksGroup.kt b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/ComparableTracksGroup.kt index ce44c96008c..b260ec28d03 100644 --- a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/ComparableTracksGroup.kt +++ b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/ComparableTracksGroup.kt @@ -4,6 +4,6 @@ import net.osmand.shared.gpx.filters.TrackFolderAnalysis interface ComparableTracksGroup { fun getFolderAnalysis(): TrackFolderAnalysis? - fun getDirName(): String + fun getDirName(useExtendedName: Boolean): String fun lastModified(): Long } \ No newline at end of file diff --git a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/SmartFolder.kt b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/SmartFolder.kt index 832bb91cd61..263d81d97fe 100644 --- a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/SmartFolder.kt +++ b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/SmartFolder.kt @@ -59,7 +59,7 @@ class SmartFolder(@Serializable var folderName: String) : TracksGroup, Comparabl return analysis } - override fun getDirName() = folderName + override fun getDirName(useExtendedName: Boolean) = folderName override fun lastModified() = creationTime diff --git a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt index a59f5c233e9..95c059eb025 100644 --- a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt +++ b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt @@ -5,7 +5,6 @@ import net.osmand.shared.gpx.TrackItem import net.osmand.shared.gpx.filters.TrackFolderAnalysis import net.osmand.shared.io.KFile import net.osmand.shared.util.KAlgorithms -import net.osmand.shared.util.KCollectionUtils import kotlin.math.max class TrackFolder(dirFile: KFile, parentFolder: TrackFolder?) : @@ -37,9 +36,7 @@ class TrackFolder(dirFile: KFile, parentFolder: TrackFolder?) : lastModified = folder.lastModified } - override fun getId(): String { - return getRelativePath(true) - } + override fun getId() = getRelativePath(true) override fun getName(): String { return GpxHelper.getFolderName(dirFile, false) @@ -59,13 +56,6 @@ class TrackFolder(dirFile: KFile, parentFolder: TrackFolder?) : val isRootFolder: Boolean get() = getParentFolder() == null - private fun getRelativePath(includeRootFolder: Boolean): String { - val dirName = getDirName() - val parentFolder = getParentFolder() - return if (parentFolder != null && (!parentFolder.isRootFolder || includeRootFolder)) - parentFolder.relativePath + "/" + dirName else dirName - } - fun getRootFolder(): TrackFolder = getParentFolder()?.getRootFolder() ?: this fun getParentFolder(): TrackFolder? { @@ -153,8 +143,15 @@ class TrackFolder(dirFile: KFile, parentFolder: TrackFolder?) : return analysis } - override fun getDirName(): String { - return dirFile.name() + override fun getDirName(useExtendedName: Boolean): String { + return if (useExtendedName) relativePath else dirFile.name() + } + + private fun getRelativePath(includeRootFolder: Boolean): String { + val dirName = dirFile.name() + val parent = getParentFolder() + val includeParent = parent != null && (!parent.isRootFolder || includeRootFolder) + return if (includeParent) parent!!.getRelativePath(includeRootFolder) + "/" + dirName else dirName } fun getLastModified(): Long { diff --git a/OsmAnd/src/net/osmand/plus/auto/screens/TracksFoldersScreen.kt b/OsmAnd/src/net/osmand/plus/auto/screens/TracksFoldersScreen.kt index 32e17c4e6e7..6d37d2e365b 100644 --- a/OsmAnd/src/net/osmand/plus/auto/screens/TracksFoldersScreen.kt +++ b/OsmAnd/src/net/osmand/plus/auto/screens/TracksFoldersScreen.kt @@ -100,7 +100,7 @@ class TracksFoldersScreen( if (itemsCount == contentLimit) { break } - val title = trackTab.getName(app) + val title = trackTab.getName() val iconColorId = ColorUtilities.getDefaultIconColorId(app.daynightHelper.isNightMode) val iconDrawable = app.uiUtilities.getIcon(trackTab.type.iconId, iconColorId) val icon = CarIcon.Builder( diff --git a/OsmAnd/src/net/osmand/plus/auto/screens/TracksScreen.kt b/OsmAnd/src/net/osmand/plus/auto/screens/TracksScreen.kt index d378c771795..0c6f1e7a383 100644 --- a/OsmAnd/src/net/osmand/plus/auto/screens/TracksScreen.kt +++ b/OsmAnd/src/net/osmand/plus/auto/screens/TracksScreen.kt @@ -88,7 +88,7 @@ class TracksScreen( val title = if (trackTab.type == TrackTabType.ALL) { app.getString(R.string.sort_last_modified) } else { - trackTab.getName(app) + trackTab.getName() } val isLoading = loadTracksTask.status != AsyncTask.Status.FINISHED templateBuilder.setLoading(isLoading) diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackFolderLoaderTask.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackFolderLoaderTask.java index b592fa8d017..3cd70776fa6 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackFolderLoaderTask.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackFolderLoaderTask.java @@ -66,7 +66,7 @@ protected void onProgressUpdate(TrackItem... items) { @Override protected Void doInBackground(Void... voids) { long start = System.currentTimeMillis(); - LOG.info("Start loading tracks in " + folder.getDirName()); + LOG.info("Start loading tracks in " + folder.getDirName(true)); folder.clearData(); loadingTime = System.currentTimeMillis(); diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackGroupsBottomSheet.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackGroupsBottomSheet.java index 071b0854f68..ae007df96ee 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackGroupsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackGroupsBottomSheet.java @@ -86,7 +86,7 @@ public TrackGroupViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int vi public void onBindViewHolder(@NonNull TrackGroupViewHolder holder, int position) { TrackTab trackTab = trackTabs.get(position); - holder.title.setText(trackTab.getName(app, true)); + holder.title.setText(trackTab.getDirName(true)); boolean selected = trackTab == selectedTab; int colorId = selected ? activeColorId : defaultColorId; diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java index feaa234865a..08e4397bec6 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java @@ -1,5 +1,6 @@ package net.osmand.plus.configmap.tracks; +import static net.osmand.plus.configmap.tracks.TrackTabType.FOLDER; import static net.osmand.plus.configmap.tracks.TrackTabType.SMART_FOLDER; import android.content.Context; @@ -30,25 +31,42 @@ public class TrackTab implements TracksGroup, ComparableTracksGroup { public final File directory; @Nullable public final SmartFolder smartFolder; + public final String initialName; private TracksSortMode sortMode = TracksSortMode.getDefaultSortMode(); - public TrackTab(@NonNull File directory) { - this.directory = directory; - this.smartFolder = null; - this.type = TrackTabType.FOLDER; + public TrackTab(@NonNull Context context, @NonNull File directory) { + this(context, directory, null, FOLDER); } - public TrackTab(@NonNull SmartFolder smartFolder) { - this.directory = null; - this.smartFolder = smartFolder; - this.type = SMART_FOLDER; + public TrackTab(@NonNull Context context, @NonNull SmartFolder smartFolder) { + this(context, null, smartFolder, SMART_FOLDER); } - public TrackTab(@NonNull TrackTabType type) { - this.directory = null; - this.smartFolder = null; + public TrackTab(@NonNull Context context, @NonNull TrackTabType type) { + this(context, null, null, type); + } + + private TrackTab(@NonNull Context context, @Nullable File directory, + @Nullable SmartFolder smartFolder, @NonNull TrackTabType type) { + this.directory = directory; + this.smartFolder = smartFolder; this.type = type; + this.initialName = createInitialName(context); + } + + @NonNull + private String createInitialName(@NonNull Context context) { + if (type.titleId != -1) { + return context.getString(type.titleId); + } + if (directory != null) { + return GpxUiHelper.getFolderName(context, directory); + } + if (smartFolder != null) { + return smartFolder.getFolderName(); + } + return ""; } @NonNull @@ -61,12 +79,6 @@ public String getId() { }; } - @NonNull - @Override - public String getName() { - return null; - } - @Nullable @Override public TrackFolderAnalysis getFolderAnalysis() { @@ -84,31 +96,18 @@ public void setSortMode(@NonNull TracksSortMode sortMode) { this.sortMode = sortMode; } - @NonNull - public String getName(@NonNull Context context) { - return getName(context, false); - } - - @NonNull - public String getName(@NonNull Context context, boolean includeParentDir) { - return type.titleId != -1 ? context.getString(type.titleId) : getDirName(includeParentDir); - } - @NonNull @Override - public String getDirName() { + public String getName() { return getDirName(false); } @NonNull - public String getDirName(boolean includeParentName) { - if (directory != null) { - return GpxUiHelper.getFolderName(directory, includeParentName); - } - if (smartFolder != null) { - return smartFolder.getDirName(); - } - return ""; + @Override + public String getDirName(boolean useExtendedName) { + return directory != null && useExtendedName + ? GpxUiHelper.getExtendedFolderName(directory, initialName) + : initialName; } @NonNull diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java index b382c773e2e..353c4dc42cf 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java @@ -58,9 +58,9 @@ public ItemsSelectionHelper getItemsSelectionHelper() { } @NonNull - public List getSortedTrackTabs(boolean checkParentName) { + public List getSortedTrackTabs(boolean useExtendedName) { List result = new ArrayList<>(trackTabs.values()); - result.sort(new TracksComparator(getRootSortMode(), getDefaultLocation(), checkParentName)); + result.sort(new TracksComparator(getRootSortMode(), getDefaultLocation(), useExtendedName)); return result; } @@ -94,7 +94,7 @@ public void updateTrackItems(@NonNull List trackItems) { updateTrackTabs(trackTabs); } - public void updateItems(@NonNull TrackFolder folder){ + public void updateItems(@NonNull TrackFolder folder) { List allTrackItems = new ArrayList<>(folder.getFlattenedTrackItems()); if (settings.SAVE_GLOBAL_TRACK_TO_GPX.get() || gpxSelectionHelper.getSelectedCurrentRecordingTrack() != null) { SelectedGpxFile selectedGpxFile = app.getSavingTrackHelper().getCurrentTrack(); @@ -134,7 +134,7 @@ private void updateSelectTrackTabs(@NonNull TrackFolder folder) { @NonNull private TrackTab getTracksOnMapTab() { - TrackTab trackTab = new TrackTab(TrackTabType.ON_MAP); + TrackTab trackTab = new TrackTab(app, TrackTabType.ON_MAP); trackTab.items.addAll(getOnMapTabItems()); return trackTab; } @@ -147,7 +147,7 @@ public void updateTracksOnMap() { @NonNull private TrackTab getAllTracksTab() { - TrackTab trackTab = new TrackTab(TrackTabType.ALL); + TrackTab trackTab = new TrackTab(app, TrackTabType.ALL); trackTab.items.addAll(getAllTabItems()); return trackTab; } @@ -177,7 +177,7 @@ private List getAllTabItems() { @NonNull private TrackTab getFoldersTab(@NonNull TrackFolder folder) { - TrackTab trackTab = new TrackTab(TrackTabType.FOLDERS); + TrackTab trackTab = new TrackTab(app, TrackTabType.FOLDERS); trackTab.items.add(TYPE_SORT_TRACKS); trackTab.items.addAll(folder.getSubFolders()); trackTab.items.addAll(folder.getTrackItems()); @@ -237,7 +237,7 @@ public void processVisibleTracks() { private List getAllSmartFoldersTabs() { List smartFoldersTabs = new ArrayList<>(); for (SmartFolder folder : app.getSmartFolderHelper().getSmartFolders()) { - TrackTab folderTab = new TrackTab(folder); + TrackTab folderTab = new TrackTab(app, folder); folderTab.items.add(TYPE_SORT_TRACKS); folderTab.items.addAll(folder.getTrackItems()); smartFoldersTabs.add(folderTab); @@ -254,7 +254,7 @@ private TrackTab addTrackItem(@NonNull Map trackTabs, @NonNull String folderId = TrackSortModesCollection.getFolderId(dir.absolutePath()); TrackTab trackTab = trackTabs.get(folderId); if (trackTab == null) { - trackTab = new TrackTab(SharedUtil.jFile(dir)); + trackTab = new TrackTab(app, SharedUtil.jFile(dir)); trackTab.items.add(TYPE_SORT_TRACKS); trackTabs.put(folderId, trackTab); } diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java index 24ebe56659b..f503bf4ab8a 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java @@ -36,7 +36,7 @@ public class TracksComparator implements Comparator { public final TrackTab trackTab; public final TracksSortMode sortMode; public final Collator collator = OsmAndCollator.primaryCollator(); - private boolean checkParentNameForTabs = false; + private boolean useExtendedName = false; public TracksComparator(@NonNull TrackTab trackTab, @NonNull LatLon latLon) { this.trackTab = trackTab; @@ -45,9 +45,9 @@ public TracksComparator(@NonNull TrackTab trackTab, @NonNull LatLon latLon) { } public TracksComparator(@NonNull TracksSortMode sortMode, - @NonNull LatLon latLon, boolean checkParentNameForTabs) { + @NonNull LatLon latLon, boolean useExtendedName) { this(sortMode, latLon); - this.checkParentNameForTabs = checkParentNameForTabs; + this.useExtendedName = useExtendedName; } public TracksComparator(@NonNull TracksSortMode sortMode, @NonNull LatLon latLon) { @@ -312,11 +312,7 @@ private int compareTrackItemNames(@NonNull TrackItem item1, @NonNull TrackItem i private int compareTrackFolderNames(@NonNull ComparableTracksGroup folder1, @NonNull ComparableTracksGroup folder2) { - if (folder1 instanceof TrackTab tab1 && folder2 instanceof TrackTab tab2) { - return compareNames(tab1.getDirName(checkParentNameForTabs), tab2.getDirName(checkParentNameForTabs)); - } else { - return compareNames(folder1.getDirName(), folder2.getDirName()); - } + return compareNames(folder1.getDirName(useExtendedName), folder2.getDirName(useExtendedName)); } private int compareNames(@NonNull String item1, @NonNull String item2) { diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabAdapter.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabAdapter.java index 63a83a0fcc9..d0306ba9280 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabAdapter.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabAdapter.java @@ -56,7 +56,7 @@ public Fragment getItem(int position) { @Override public CharSequence getPageTitle(int position) { - return trackTabs.get(position).getName(app); + return trackTabs.get(position).getName(); } @Override diff --git a/OsmAnd/src/net/osmand/plus/myplaces/tracks/controller/TrackFolderOptionsController.java b/OsmAnd/src/net/osmand/plus/myplaces/tracks/controller/TrackFolderOptionsController.java index 0b9cf5a23d7..2cdb74686ab 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/tracks/controller/TrackFolderOptionsController.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/tracks/controller/TrackFolderOptionsController.java @@ -162,7 +162,7 @@ private void showRenameDialog() { } }); String caption = activity.getString(R.string.enter_new_name); - CustomAlert.showInput(dialogData, activity, trackFolder.getDirName(), caption); + CustomAlert.showInput(dialogData, activity, trackFolder.getDirName(false), caption); } } diff --git a/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java b/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java index a52ca11ca1a..0c660223667 100644 --- a/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java +++ b/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java @@ -131,7 +131,7 @@ public View getCustomTabView(@NonNull ViewGroup parent, int position) { TextView textView = customView.findViewById(android.R.id.text1); textView.setPadding(sidePadding, textView.getPaddingTop(), sidePadding, textView.getPaddingBottom()); textView.setTextColor(AndroidUtils.createColorStateList(android.R.attr.state_selected, activeColor, textColor)); - textView.setText(trackTab.getName(app)); + textView.setText(trackTab.getName()); return customView; } diff --git a/OsmAnd/src/net/osmand/plus/track/SelectTrackFolderFragment.java b/OsmAnd/src/net/osmand/plus/track/SelectTrackFolderFragment.java index 3d562f2dc9e..52e6b57cd47 100644 --- a/OsmAnd/src/net/osmand/plus/track/SelectTrackFolderFragment.java +++ b/OsmAnd/src/net/osmand/plus/track/SelectTrackFolderFragment.java @@ -181,7 +181,7 @@ private void updateAdapter() { } private TrackTab getUpdatedTrackTab() { - TrackTab trackTab = new TrackTab(TrackTabType.FOLDERS); + TrackTab trackTab = new TrackTab(app, TrackTabType.FOLDERS); List subFolders = currentTrackFolder.getSubFolders(); List trackItems = currentTrackFolder.getTrackItems(); if (Algorithms.isEmpty(subFolders) && Algorithms.isEmpty(trackItems)) { diff --git a/OsmAnd/src/net/osmand/plus/track/helpers/GpxUiHelper.java b/OsmAnd/src/net/osmand/plus/track/helpers/GpxUiHelper.java index c4a059b1a75..fbe27285add 100644 --- a/OsmAnd/src/net/osmand/plus/track/helpers/GpxUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/track/helpers/GpxUiHelper.java @@ -67,6 +67,7 @@ import net.osmand.shared.gpx.primitives.WptPt; import net.osmand.shared.io.KFile; import net.osmand.util.Algorithms; +import net.osmand.util.CollectionUtils; import net.osmand.util.MapUtils; import org.apache.commons.logging.Log; @@ -189,26 +190,29 @@ public static void selectSingleGPXFile(FragmentActivity activity, boolean showCu } @NonNull - public static String getFolderName(@NonNull File dir, boolean includeParentDir) { - String name = dir.getName(); + public static String getFolderName(@NonNull Context context, @NonNull File directory) { + String name = directory.getName(); if (GPX_INDEX_DIR.equals(name + File.separator)) { - return Algorithms.capitalizeFirstLetter(name); + return context.getString(R.string.shared_string_tracks); } - String dirPath = dir.getPath() + File.separator; + String dirPath = directory.getPath() + File.separator; if (dirPath.endsWith(GPX_IMPORT_DIR) || dirPath.endsWith(GPX_RECORDED_INDEX_DIR)) { return Algorithms.capitalizeFirstLetter(name); } - if (includeParentDir) { - File parent = dir.getParentFile(); - String parentName = parent != null ? parent.getName() : ""; - if (!Algorithms.isEmpty(parentName) && !GPX_INDEX_DIR.equals(parentName + File.separator)) { - name = parentName + File.separator + name; - } - return name; - } return name; } + @NonNull + public static String getExtendedFolderName(@NonNull File directory, @NonNull String initialName) { + String name = directory.getName() + File.separator; + File parent = directory.getParentFile(); + String parentName = parent != null ? parent.getName() + File.separator : ""; + if (!CollectionUtils.equalsToAny(GPX_INDEX_DIR, name, parentName)) { + return parentName + initialName; + } + return initialName; + } + @NonNull public static String getFolderDescription(@NonNull OsmandApplication app, @NonNull TrackFolder folder) { long lastModified = folder.getLastModified(); From cb697dd08cad45ae0aa404c44df9044e632bbea1 Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Thu, 21 Nov 2024 15:48:49 +0200 Subject: [PATCH 11/18] Calculate TrackFolderAnalysis for TrackTab + refactoring --- .../shared/gpx/data/ComparableTracksGroup.kt | 3 +- .../plus/configmap/tracks/TrackTab.java | 18 ++++- .../configmap/tracks/TracksComparator.java | 73 ++++++++----------- 3 files changed, 48 insertions(+), 46 deletions(-) diff --git a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/ComparableTracksGroup.kt b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/ComparableTracksGroup.kt index b260ec28d03..e726ad3ef0c 100644 --- a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/ComparableTracksGroup.kt +++ b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/ComparableTracksGroup.kt @@ -3,7 +3,8 @@ package net.osmand.shared.gpx.data import net.osmand.shared.gpx.filters.TrackFolderAnalysis interface ComparableTracksGroup { - fun getFolderAnalysis(): TrackFolderAnalysis? + fun getFolderAnalysis(): TrackFolderAnalysis fun getDirName(useExtendedName: Boolean): String fun lastModified(): Long + fun getDefaultOrder(): Int = -1 } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java index 08e4397bec6..49e39acab6a 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java @@ -34,6 +34,7 @@ public class TrackTab implements TracksGroup, ComparableTracksGroup { public final String initialName; private TracksSortMode sortMode = TracksSortMode.getDefaultSortMode(); + private TrackFolderAnalysis analysis = null; public TrackTab(@NonNull Context context, @NonNull File directory) { this(context, directory, null, FOLDER); @@ -79,12 +80,16 @@ public String getId() { }; } - @Nullable + @NonNull @Override public TrackFolderAnalysis getFolderAnalysis() { - // Analysis should be prepared and accessible here. - // It's needed for proper sorting order on UI. - return null; + // Note: To avoid excessive calculations that could slow down the UI, + // analysis is not recalculated when folder or track parameters change. + // For example after file deletion or moving to another directory. + if (analysis == null) { + analysis = smartFolder != null ? smartFolder.getFolderAnalysis() : new TrackFolderAnalysis(this); + } + return analysis; } @NonNull @@ -110,6 +115,11 @@ public String getDirName(boolean useExtendedName) { : initialName; } + @Override + public int getDefaultOrder() { + return type.ordinal(); + } + @NonNull public List getTrackItems() { List trackItems = new ArrayList<>(); diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java index f503bf4ab8a..91edda5cd1c 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java @@ -19,7 +19,6 @@ import net.osmand.plus.myplaces.tracks.VisibleTracksGroup; import net.osmand.plus.settings.enums.TracksSortMode; import net.osmand.shared.gpx.data.ComparableTracksGroup; -import net.osmand.shared.gpx.filters.TrackFolderAnalysis; import net.osmand.shared.data.KLatLon; import net.osmand.shared.gpx.GpxDataItem; import net.osmand.shared.gpx.GpxTrackAnalysis; @@ -76,8 +75,16 @@ public int compare(Object o1, Object o2) { if (o2 instanceof VisibleTracksGroup) { return 1; } - if (o1 instanceof ComparableTracksGroup) { - return o2 instanceof ComparableTracksGroup ? compareTrackFolders((ComparableTracksGroup) o1, (ComparableTracksGroup) o2) : -1; + if (o1 instanceof ComparableTracksGroup folder1) { + if (o2 instanceof ComparableTracksGroup folder2) { + int order1 = folder1.getDefaultOrder(); + int order2 = folder2.getDefaultOrder(); + if (order1 != order2) { + return Integer.compare(order1, order2); + } + return compareTrackFolders(folder1, folder2); + } + return -1; } if (o2 instanceof ComparableTracksGroup) { return 1; @@ -89,49 +96,33 @@ public int compare(Object o1, Object o2) { } private int compareTrackFolders(@NonNull ComparableTracksGroup folder1, @NonNull ComparableTracksGroup folder2) { - if (folder1 instanceof TrackTab tab1 && folder2 instanceof TrackTab tab2) { - if (tab1.type != tab2.type) { - return Integer.compare(tab1.type.ordinal(), tab2.type.ordinal()); - } - } - TrackFolderAnalysis analysis1; - TrackFolderAnalysis analysis2; + int multiplier; switch (sortMode) { - case NAME_ASCENDING: - case NAME_DESCENDING: { - int sign = sortMode == NAME_ASCENDING ? 1 : -1; - return sign * compareTrackFolderNames(folder1, folder2); + case NAME_ASCENDING, NAME_DESCENDING: { + multiplier = sortMode == NAME_ASCENDING ? 1 : -1; + return multiplier * compareTrackFolderNames(folder1, folder2); } - case LAST_MODIFIED: - case DATE_ASCENDING: - case DATE_DESCENDING: { - int sign = sortMode == DATE_DESCENDING ? -1 : 1; - return sign * compareFolderFilesByLastModified(folder1, folder2); + + case LAST_MODIFIED, DATE_ASCENDING, DATE_DESCENDING: { + multiplier = sortMode == DATE_DESCENDING ? -1 : 1; + return multiplier * compareFolderFilesByLastModified(folder1, folder2); } - case DISTANCE_ASCENDING: - case DISTANCE_DESCENDING: { - analysis1 = folder1.getFolderAnalysis(); - analysis2 = folder2.getFolderAnalysis(); - if (analysis1 != null && analysis2 != null) { - float dist1 = analysis1.getTotalDistance(); - float dist2 = analysis2.getTotalDistance(); - if (Math.abs(dist1 - dist2) >= EQUIVALENT_TOLERANCE) { - int sign = sortMode == DISTANCE_ASCENDING ? 1 : -1; - return sign * Float.compare(dist1, dist2); - } + + case DISTANCE_ASCENDING, DISTANCE_DESCENDING: { + float dist1 = folder1.getFolderAnalysis().getTotalDistance(); + float dist2 = folder2.getFolderAnalysis().getTotalDistance(); + if (Math.abs(dist1 - dist2) >= EQUIVALENT_TOLERANCE) { + multiplier = sortMode == DISTANCE_ASCENDING ? 1 : -1; + return multiplier * Float.compare(dist1, dist2); } } - case DURATION_ASCENDING: - case DURATION_DESCENDING: { - analysis1 = folder1.getFolderAnalysis(); - analysis2 = folder2.getFolderAnalysis(); - if (analysis1 != null && analysis2 != null) { - int timeSpan1 = analysis1.getTimeSpan(); - int timeSpan2 = analysis2.getTimeSpan(); - if (timeSpan1 != timeSpan2) { - int sign = sortMode == DURATION_ASCENDING ? 1 : -1; - return sign * Long.compare(timeSpan1, timeSpan2); - } + + case DURATION_ASCENDING, DURATION_DESCENDING: { + int timeSpan1 = folder1.getFolderAnalysis().getTimeSpan(); + int timeSpan2 = folder2.getFolderAnalysis().getTimeSpan(); + if (timeSpan1 != timeSpan2) { + multiplier = sortMode == DURATION_ASCENDING ? 1 : -1; + return multiplier * Long.compare(timeSpan1, timeSpan2); } } } From 5d62318dfdd743261eee0b9978a145a627e94260 Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Thu, 21 Nov 2024 15:56:52 +0200 Subject: [PATCH 12/18] Fix problems for "FOLDERS" tab --- .../plus/configmap/tracks/TrackSortModesCollection.java | 1 + .../net/osmand/plus/configmap/tracks/TrackTabsHelper.java | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java index 3c784bf4947..4678e28ad97 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java @@ -82,6 +82,7 @@ private boolean askUpgradeCachedKeys(@NonNull OsmandApplication app, @NonNull Tr Map upgradedCache = new HashMap<>(); putUpgradedKey(upgradedCache, TrackTabType.ON_MAP.name()); putUpgradedKey(upgradedCache, TrackTabType.ALL.name()); + putUpgradedKey(upgradedCache, TrackTabType.FOLDERS.name()); putUpgradedKey(upgradedCache, rootFolder.getId()); for (TrackFolder folder : rootFolder.getFlattenedSubFolders()) { putUpgradedKey(upgradedCache, folder.getId()); diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java index 353c4dc42cf..1a5b7388f97 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java @@ -125,9 +125,9 @@ private void updateSelectTrackTabs(@NonNull TrackFolder folder) { processVisibleTracks(); processRecentlyVisibleTracks(); trackTabs.clear(); - trackTabs.put(app.getString(R.string.shared_string_visible), getTracksOnMapTab()); - trackTabs.put(app.getString(R.string.shared_string_all_tracks), getAllTracksTab()); - trackTabs.put(app.getString(R.string.shared_string_folders), getFoldersTab(folder)); + trackTabs.put(TrackTabType.ON_MAP.name(), getTracksOnMapTab()); + trackTabs.put(TrackTabType.ALL.name(), getAllTracksTab()); + trackTabs.put(TrackTabType.FOLDERS.name(), getFoldersTab(folder)); loadTabsSortModes(); sortTrackTabs(); } From dae43cbf34d74411795a06b9846aee6242d13491 Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Fri, 22 Nov 2024 14:20:59 +0200 Subject: [PATCH 13/18] Upgrade keys only once & try to do it when collection is requested --- .../plus/auto/screens/TracksFoldersScreen.kt | 2 +- .../tracks/SelectTrackTabsHelper.java | 21 +++++ .../tracks/TrackSortModesCollection.java | 20 ++--- .../configmap/tracks/TrackTabsHelper.java | 80 ++++++++----------- .../configmap/tracks/TracksTabsFragment.java | 2 +- .../plus/settings/backend/OsmandSettings.java | 15 +++- .../plus/track/SelectTrackTabsFragment.java | 11 ++- .../src/net/osmand/plus/utils/FileUtils.java | 8 +- 8 files changed, 96 insertions(+), 63 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/configmap/tracks/SelectTrackTabsHelper.java diff --git a/OsmAnd/src/net/osmand/plus/auto/screens/TracksFoldersScreen.kt b/OsmAnd/src/net/osmand/plus/auto/screens/TracksFoldersScreen.kt index 6d37d2e365b..526542d7aa1 100644 --- a/OsmAnd/src/net/osmand/plus/auto/screens/TracksFoldersScreen.kt +++ b/OsmAnd/src/net/osmand/plus/auto/screens/TracksFoldersScreen.kt @@ -131,7 +131,7 @@ class TracksFoldersScreen( } override fun loadTracksFinished(folder: TrackFolder) { - trackTabsHelper.updateTrackItems(folder.getFlattenedTrackItems()) + trackTabsHelper.updateTrackItems(folder) invalidate() } diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/SelectTrackTabsHelper.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/SelectTrackTabsHelper.java new file mode 100644 index 00000000000..27e707fb17d --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/SelectTrackTabsHelper.java @@ -0,0 +1,21 @@ +package net.osmand.plus.configmap.tracks; + +import androidx.annotation.NonNull; + +import net.osmand.plus.OsmandApplication; +import net.osmand.shared.gpx.data.TrackFolder; + +public class SelectTrackTabsHelper extends TrackTabsHelper { + + public SelectTrackTabsHelper(@NonNull OsmandApplication app) { + super(app); + } + + @Override + protected void updateTrackTabs(@NonNull TrackFolder rootFolder) { + trackTabs.clear(); + trackTabs.put(TrackTabType.ON_MAP.name(), getTracksOnMapTab()); + trackTabs.put(TrackTabType.ALL.name(), getAllTracksTab()); + trackTabs.put(TrackTabType.FOLDERS.name(), getFoldersTab(rootFolder)); + } +} diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java index 4678e28ad97..1dba084c2ca 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java @@ -28,6 +28,7 @@ public class TrackSortModesCollection { private final Map cachedSortModes = new ConcurrentHashMap<>(); private final ListStringPreference preference; + private boolean keysUpgraded = false; public TrackSortModesCollection(@NonNull OsmandSettings settings) { this.preference = settings.TRACKS_TABS_SORT_MODES; @@ -63,9 +64,12 @@ public void setSortMode(@NonNull String id, @NonNull TracksSortMode sortMode) { cachedSortModes.put(id, sortMode); } - public void askSyncWithUpgrade(@NonNull OsmandApplication app, @NonNull TrackFolder trackFolder, boolean forceSync) { - if (askUpgradeCachedKeys(app, trackFolder) || forceSync) { - syncSettings(); + public void askUpgradeKeysWithSync(@NonNull OsmandApplication app, @Nullable TrackFolder folder) { + if (folder != null && !keysUpgraded) { + if (askUpgradeCachedKeys(app, folder)) { + keysUpgraded = true; + syncSettings(); + } } } @@ -103,20 +107,18 @@ private void putUpgradedKey(@NonNull Map map, @NonNull S } } - public void updateAfterMoveTrackFolder(@NonNull OsmandApplication app, - @NonNull TrackFolder trackFolder, @NonNull File oldDir) { + public void updateAfterMoveTrackFolder(@NonNull TrackFolder trackFolder, @NonNull File oldDir) { String previousId = getFolderId(oldDir.getAbsolutePath()); TracksSortMode sortMode = getSortMode(previousId); if (sortMode != null) { setSortMode(trackFolder.getId(), sortMode); - askSyncWithUpgrade(app, trackFolder, true); + syncSettings(); } } - public void updateAfterDeleteTrackFolder(@NonNull OsmandApplication app, - @NonNull TrackFolder trackFolder) { + public void updateAfterDeleteTrackFolder(@NonNull TrackFolder trackFolder) { cachedSortModes.remove(trackFolder.getId()); - askSyncWithUpgrade(app, trackFolder, true); + syncSettings(); } public void syncSettings() { diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java index 1a5b7388f97..81623092321 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java @@ -12,7 +12,6 @@ import net.osmand.data.LatLon; import net.osmand.shared.gpx.GpxFile; import net.osmand.plus.OsmandApplication; -import net.osmand.plus.R; import net.osmand.plus.myplaces.tracks.ItemsSelectionHelper; import net.osmand.plus.plugins.PluginsHelper; import net.osmand.plus.plugins.monitoring.OsmandMonitoringPlugin; @@ -27,6 +26,7 @@ import net.osmand.util.Algorithms; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashMap; @@ -43,7 +43,7 @@ public class TrackTabsHelper { private final ItemsSelectionHelper itemsSelectionHelper; private final Set recentlyVisibleTrackItem = new HashSet<>(); - private final Map trackTabs = new LinkedHashMap<>(); + protected final Map trackTabs = new LinkedHashMap<>(); public TrackTabsHelper(@NonNull OsmandApplication app) { this.app = app; @@ -79,61 +79,51 @@ public Set getRecentlyVisibleTracks() { return new HashSet<>(recentlyVisibleTrackItem); } - public void updateTrackItems(@NonNull List trackItems) { - List allTrackItems = new ArrayList<>(trackItems); - if (settings.SAVE_GLOBAL_TRACK_TO_GPX.get() || gpxSelectionHelper.getSelectedCurrentRecordingTrack() != null) { - SelectedGpxFile selectedGpxFile = app.getSavingTrackHelper().getCurrentTrack(); - TrackItem trackItem = new TrackItem(selectedGpxFile.getGpxFile()); - allTrackItems.add(trackItem); - } - itemsSelectionHelper.setAllItems(allTrackItems); - Map trackTabs = new LinkedHashMap<>(); - for (TrackItem item : trackItems) { - addTrackItem(trackTabs, item); - } - updateTrackTabs(trackTabs); - } - - public void updateItems(@NonNull TrackFolder folder) { - List allTrackItems = new ArrayList<>(folder.getFlattenedTrackItems()); - if (settings.SAVE_GLOBAL_TRACK_TO_GPX.get() || gpxSelectionHelper.getSelectedCurrentRecordingTrack() != null) { - SelectedGpxFile selectedGpxFile = app.getSavingTrackHelper().getCurrentTrack(); - TrackItem trackItem = new TrackItem(selectedGpxFile.getGpxFile()); - allTrackItems.add(trackItem); - } + public void updateTrackItems(@NonNull TrackFolder rootFolder) { + List allTrackItems = new ArrayList<>(rootFolder.getFlattenedTrackItems()); + addCurrentTrackItemIfPresent(allTrackItems); itemsSelectionHelper.setAllItems(allTrackItems); - updateSelectTrackTabs(folder); - } - private void updateTrackTabs(@NonNull Map folderTabs) { processVisibleTracks(); processRecentlyVisibleTracks(); + + updateTrackTabs(rootFolder); + loadTabsSortModes(rootFolder); + sortTrackTabsContent(); + } + + protected void updateTrackTabs(@NonNull TrackFolder rootFolder) { trackTabs.clear(); trackTabs.put(TrackTabType.ON_MAP.name(), getTracksOnMapTab()); trackTabs.put(TrackTabType.ALL.name(), getAllTracksTab()); for (TrackTab tab : getAllSmartFoldersTabs()) { trackTabs.put(tab.getId(), tab); } - for (TrackTab tab : folderTabs.values()) { + for (TrackTab tab : getAllTrackFoldersTabs(rootFolder)) { trackTabs.put(tab.getId(), tab); } - loadTabsSortModes(); - sortTrackTabs(); } - private void updateSelectTrackTabs(@NonNull TrackFolder folder) { - processVisibleTracks(); - processRecentlyVisibleTracks(); - trackTabs.clear(); - trackTabs.put(TrackTabType.ON_MAP.name(), getTracksOnMapTab()); - trackTabs.put(TrackTabType.ALL.name(), getAllTracksTab()); - trackTabs.put(TrackTabType.FOLDERS.name(), getFoldersTab(folder)); - loadTabsSortModes(); - sortTrackTabs(); + private void addCurrentTrackItemIfPresent(@NonNull List trackItems) { + if (settings.SAVE_GLOBAL_TRACK_TO_GPX.get() || gpxSelectionHelper.getSelectedCurrentRecordingTrack() != null) { + SelectedGpxFile selectedGpxFile = app.getSavingTrackHelper().getCurrentTrack(); + TrackItem trackItem = new TrackItem(selectedGpxFile.getGpxFile()); + trackItems.add(trackItem); + } + } + + @NonNull + private Collection getAllTrackFoldersTabs(@NonNull TrackFolder rootFolder) { + List trackItems = rootFolder.getFlattenedTrackItems(); + Map trackFolderTabs = new LinkedHashMap<>(); + for (TrackItem item : trackItems) { + addTrackItem(trackFolderTabs, item); + } + return trackFolderTabs.values(); } @NonNull - private TrackTab getTracksOnMapTab() { + protected TrackTab getTracksOnMapTab() { TrackTab trackTab = new TrackTab(app, TrackTabType.ON_MAP); trackTab.items.addAll(getOnMapTabItems()); return trackTab; @@ -146,7 +136,7 @@ public void updateTracksOnMap() { } @NonNull - private TrackTab getAllTracksTab() { + protected TrackTab getAllTracksTab() { TrackTab trackTab = new TrackTab(app, TrackTabType.ALL); trackTab.items.addAll(getAllTabItems()); return trackTab; @@ -176,7 +166,7 @@ private List getAllTabItems() { } @NonNull - private TrackTab getFoldersTab(@NonNull TrackFolder folder) { + protected TrackTab getFoldersTab(@NonNull TrackFolder folder) { TrackTab trackTab = new TrackTab(app, TrackTabType.FOLDERS); trackTab.items.add(TYPE_SORT_TRACKS); trackTab.items.addAll(folder.getSubFolders()); @@ -290,7 +280,7 @@ public void saveTracksVisibility() { gpxSelectionHelper.saveTracksVisibility(itemsSelectionHelper.getSelectedItems()); } - private void sortTrackTabs() { + private void sortTrackTabsContent() { for (TrackTab trackTab : trackTabs.values()) { sortTrackTab(trackTab); } @@ -315,8 +305,8 @@ public void sortTrackTab(@NonNull TrackTab trackTab) { } } - public void loadTabsSortModes() { - TrackSortModesCollection sortModes = settings.getTrackSortModes(); + public void loadTabsSortModes(@NonNull TrackFolder folder) { + TrackSortModesCollection sortModes = settings.getTrackSortModes(folder); for (Entry entry : trackTabs.entrySet()) { TracksSortMode sortMode = sortModes.getSortMode(entry.getKey()); if (sortMode != null) { diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java index 99cfd43bd7d..0ce04d450cd 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java @@ -263,7 +263,7 @@ public void tracksLoaded(@NonNull TrackFolder folder) { @Override public void loadTracksFinished(@NonNull TrackFolder folder) { - trackTabsHelper.updateTrackItems(folder.getFlattenedTrackItems()); + trackTabsHelper.updateTrackItems(folder); AndroidUiHelper.updateVisibility(progressBar, false); updateTrackTabs(); applyPreselectedParams(); diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java index a7f586fa30c..e9468402b24 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java @@ -97,6 +97,7 @@ import net.osmand.plus.wikipedia.WikiArticleShowImages; import net.osmand.render.RenderingRulesStorage; import net.osmand.shared.gpx.ColoringPurpose; +import net.osmand.shared.gpx.data.TrackFolder; import net.osmand.shared.obd.OBDDataComputer; import net.osmand.shared.routing.ColoringType; import net.osmand.shared.settings.enums.MetricsConstants; @@ -2039,14 +2040,20 @@ private String getPagedWidgetIds(@NonNull List pages) { public final CommonPreference SEARCH_TRACKS_SORT_MODE = new EnumStringPreference<>(this, "search_tracks_sort_mode", TracksSortMode.getDefaultSortMode(), TracksSortMode.values()); public final ListStringPreference TRACKS_TABS_SORT_MODES = (ListStringPreference) new ListStringPreference(this, "tracks_tabs_sort_modes", null, ";;").makeGlobal().makeShared().cache(); - private TrackSortModesCollection trackSortModesCollection = null; + private TrackSortModesCollection trackSortModes = null; @NonNull public TrackSortModesCollection getTrackSortModes() { - if (trackSortModesCollection == null) { - trackSortModesCollection = new TrackSortModesCollection(this); + return getTrackSortModes(null); + } + + @NonNull + public TrackSortModesCollection getTrackSortModes(@Nullable TrackFolder trackFolder) { + if (trackSortModes == null) { + trackSortModes = new TrackSortModesCollection(this); } - return trackSortModesCollection; + trackSortModes.askUpgradeKeysWithSync(ctx, trackFolder); + return trackSortModes; } public final OsmandPreference ANIMATE_MY_LOCATION = new BooleanPreference(this, "animate_my_location", true).makeProfile().cache(); diff --git a/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java b/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java index 400a043f8d6..5f349b6c898 100644 --- a/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java +++ b/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java @@ -20,6 +20,9 @@ import net.osmand.CallbackWithObject; import net.osmand.IndexConstants; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.configmap.tracks.SelectTrackTabsHelper; +import net.osmand.plus.configmap.tracks.TrackTabsHelper; import net.osmand.plus.shared.SharedUtil; import net.osmand.shared.gpx.GpxFile; import net.osmand.plus.R; @@ -95,6 +98,12 @@ protected void setTabs(@NonNull List tabs) { viewPager.setCurrentItem(0); } + @NonNull + @Override + protected TrackTabsHelper createTrackTabsHelper(@NonNull OsmandApplication app) { + return new SelectTrackTabsHelper(app); + } + @Override public void loadTracksProgress(@NonNull TrackItem... items) { } @@ -105,7 +114,7 @@ public void tracksLoaded(@NonNull TrackFolder folder) { @Override public void loadTracksFinished(@NonNull TrackFolder folder) { - trackTabsHelper.updateItems(folder); + trackTabsHelper.updateTrackItems(folder); AndroidUiHelper.updateVisibility(progressBar, false); updateTrackTabs(); updateTabsContent(); diff --git a/OsmAnd/src/net/osmand/plus/utils/FileUtils.java b/OsmAnd/src/net/osmand/plus/utils/FileUtils.java index f6674426d8d..683262b8f94 100644 --- a/OsmAnd/src/net/osmand/plus/utils/FileUtils.java +++ b/OsmAnd/src/net/osmand/plus/utils/FileUtils.java @@ -190,11 +190,15 @@ public static void updateMovedTrackFolder(@NonNull OsmandApplication app, @NonNu } updateMovedGpxFiles(app, files, srcDir, destDir); - app.getSettings().getTrackSortModes().updateAfterMoveTrackFolder(app, trackFolder, srcDir); + OsmandSettings settings = app.getSettings(); + TrackSortModesCollection sortModes = settings.getTrackSortModes(trackFolder); + sortModes.updateAfterMoveTrackFolder(trackFolder, srcDir); } public static void updateAfterDeleteTrackFolder(@NonNull OsmandApplication app, @NonNull TrackFolder trackFolder) { - app.getSettings().getTrackSortModes().updateAfterDeleteTrackFolder(app, trackFolder); + OsmandSettings settings = app.getSettings(); + TrackSortModesCollection sortModes = settings.getTrackSortModes(trackFolder); + sortModes.updateAfterDeleteTrackFolder(trackFolder); } private static void updateMovedGpxFiles(@NonNull OsmandApplication app, @NonNull List files, From 1dbe52db6f821594b4164b32eebbeb24de8ac38c Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Fri, 22 Nov 2024 15:16:32 +0200 Subject: [PATCH 14/18] Never display root folder name in tracks folder's relative path and id --- .../net/osmand/shared/gpx/data/TrackFolder.kt | 19 ++++++++------- .../tracks/TrackSortModesCollection.java | 24 +++++++++---------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt index 95c059eb025..5b09bf61dfc 100644 --- a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt +++ b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt @@ -36,7 +36,7 @@ class TrackFolder(dirFile: KFile, parentFolder: TrackFolder?) : lastModified = folder.lastModified } - override fun getId() = getRelativePath(true) + override fun getId() = relativePath override fun getName(): String { return GpxHelper.getFolderName(dirFile, false) @@ -51,7 +51,15 @@ class TrackFolder(dirFile: KFile, parentFolder: TrackFolder?) : } val relativePath: String - get() = getRelativePath(false) + get() = + if (!isRootFolder) { + val dirName = dirFile.name() + val parent = getParentFolder() + if (parent?.isRootFolder == false) parent.relativePath + "/" + dirName else dirName + } else { + "" + } + val isRootFolder: Boolean get() = getParentFolder() == null @@ -147,13 +155,6 @@ class TrackFolder(dirFile: KFile, parentFolder: TrackFolder?) : return if (useExtendedName) relativePath else dirFile.name() } - private fun getRelativePath(includeRootFolder: Boolean): String { - val dirName = dirFile.name() - val parent = getParentFolder() - val includeParent = parent != null && (!parent.isRootFolder || includeRootFolder) - return if (includeParent) parent!!.getRelativePath(includeRootFolder) + "/" + dirName else dirName - } - fun getLastModified(): Long { if (lastModified < 0) { lastModified = dirFile.lastModified() diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java index 1dba084c2ca..1f772e3158f 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java @@ -37,7 +37,7 @@ public TrackSortModesCollection(@NonNull OsmandSettings settings) { @NonNull public TracksSortMode getRootSortMode() { - return requireSortMode(ROOT_FOLDER); + return requireSortMode(""); } @NonNull @@ -50,11 +50,9 @@ public TracksSortMode requireSortMode(@Nullable String id) { public TracksSortMode getSortMode(@Nullable String id) { id = removeExtraFileSeparator(id); TracksSortMode sortMode = cachedSortModes.get(id); - if (sortMode == null && id != null && isFolderIdV2(id)) { + if (sortMode == null) { String idV1 = getFolderIdV1(id); - if (idV1 != null) { - sortMode = cachedSortModes.get(idV1); - } + return idV1 != null ? cachedSortModes.get(idV1) : null; } return sortMode; } @@ -148,17 +146,19 @@ private void saveToPreference() { @NonNull public static String getFolderId(@NonNull String absolutePath) { int index = absolutePath.indexOf(ROOT_FOLDER); + if (index > 0) { + index += ROOT_FOLDER.length(); + } return index > 0 ? absolutePath.substring(index) : absolutePath; } - private static boolean isFolderIdV2(@NonNull String id) { - return id.startsWith(ROOT_FOLDER); - } - @Nullable - private static String getFolderIdV1(@NonNull String idV2) { - int index = idV2.lastIndexOf(File.separator); - return index > 0 ? idV2.substring(index + 1) : null; + private static String getFolderIdV1(@Nullable String id) { + if (id != null && id.isEmpty()) { + return removeExtraFileSeparator(ROOT_FOLDER); + } + int index = id != null ? id.lastIndexOf(File.separator) : -1; + return index > 0 ? id.substring(index + 1) : null; } @Nullable From 3123ae05714c81253e1d51b8ca1fb60cfcd4690c Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Fri, 22 Nov 2024 17:11:50 +0200 Subject: [PATCH 15/18] Small fixes --- .../shared/gpx/filters/TrackFolderAnalysis.kt | 2 +- .../configmap/tracks/TrackTabsHelper.java | 12 +++++----- .../configmap/tracks/TracksComparator.java | 8 +++---- .../configmap/tracks/TracksTabAdapter.java | 11 ++++----- .../configmap/tracks/TracksTabsFragment.java | 2 +- .../dialogs/BaseTrackFolderFragment.java | 10 ++++---- .../plus/track/BaseTracksTabsFragment.java | 23 ++++++++++++++----- .../plus/track/SelectTrackTabsFragment.java | 4 ++-- 8 files changed, 41 insertions(+), 31 deletions(-) diff --git a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/filters/TrackFolderAnalysis.kt b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/filters/TrackFolderAnalysis.kt index ec4aa8ee39c..3cfc13033a2 100644 --- a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/filters/TrackFolderAnalysis.kt +++ b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/filters/TrackFolderAnalysis.kt @@ -52,7 +52,7 @@ class TrackFolderAnalysis(folder: TracksGroup) { timeSpan = timeSpanSum.toInt() tracksCount = items.size - log.info(">>>> ${folder.getName()} = (tracks: $tracksCount, totalDistance: ${"%.2f".format(totalDistance)}, " + + log.info(">>>> ${folder.getId()} = (tracks: $tracksCount, totalDistance: ${"%.2f".format(totalDistance)}, " + "timeSpan: $timeSpan, fileSize: $fileSize, diffElevationUp: ${"%.2f".format(diffElevationUp)}, diffElevationDown: ${"%.2f".format(diffElevationDown)}") } } diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java index 81623092321..7c56832c71f 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java @@ -59,21 +59,21 @@ public ItemsSelectionHelper getItemsSelectionHelper() { @NonNull public List getSortedTrackTabs(boolean useExtendedName) { - List result = new ArrayList<>(trackTabs.values()); + List result = getTrackTabs(); result.sort(new TracksComparator(getRootSortMode(), getDefaultLocation(), useExtendedName)); return result; } + @NonNull + public List getTrackTabs() { + return new ArrayList<>(trackTabs.values()); + } + @Nullable public TrackTab getTrackTab(@NonNull String id) { return trackTabs.get(id); } - @NonNull - public Map getTrackTabs() { - return trackTabs; - } - @NonNull public Set getRecentlyVisibleTracks() { return new HashSet<>(recentlyVisibleTrackItem); diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java index 91edda5cd1c..e44bb2d2092 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java @@ -77,10 +77,10 @@ public int compare(Object o1, Object o2) { } if (o1 instanceof ComparableTracksGroup folder1) { if (o2 instanceof ComparableTracksGroup folder2) { - int order1 = folder1.getDefaultOrder(); - int order2 = folder2.getDefaultOrder(); - if (order1 != order2) { - return Integer.compare(order1, order2); + int predefinedOrder1 = folder1.getDefaultOrder(); + int predefinedOrder2 = folder2.getDefaultOrder(); + if (predefinedOrder1 != predefinedOrder2) { + return Integer.compare(predefinedOrder1, predefinedOrder2); } return compareTrackFolders(folder1, folder2); } diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabAdapter.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabAdapter.java index d0306ba9280..8ac7fd76113 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabAdapter.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabAdapter.java @@ -15,25 +15,22 @@ public class TracksTabAdapter extends FragmentStatePagerAdapter { - private final OsmandApplication app; private final List trackTabs = new ArrayList<>(); - public TracksTabAdapter(@NonNull OsmandApplication app, @NonNull FragmentManager manager, @NonNull List tabs) { + public TracksTabAdapter(@NonNull FragmentManager manager, @NonNull List tabs) { super(manager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); - this.app = app; trackTabs.addAll(tabs); } - public void setTrackTabs(@NonNull Map tabs) { + public void setTrackTabs(@NonNull List tabs) { trackTabs.clear(); - trackTabs.addAll(tabs.values()); + trackTabs.addAll(tabs); notifyDataSetChanged(); } @Override public int getItemPosition(@NonNull Object object) { - if (object instanceof TrackItemsFragment) { - TrackItemsFragment fragment = (TrackItemsFragment) object; + if (object instanceof TrackItemsFragment fragment) { int index = trackTabs.indexOf(fragment.getTrackTab()); return index >= 0 ? index : POSITION_NONE; } diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java index 0ce04d450cd..98fcf8d0d6c 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksTabsFragment.java @@ -306,7 +306,7 @@ public void changeAppearance() { protected void addTrackItem(@NonNull TrackItem item) { trackTabsHelper.addTrackItem(item); updateTrackTabs(); - setSelectedTab(IndexConstants.GPX_IMPORT_DIR); + setSelectedTab("import"); updateTabsContent(); updateButtonsState(); } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java index a10cadf5500..9e1abce7489 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java @@ -326,7 +326,8 @@ public void showSortByDialog() { @NonNull @Override public TracksSortMode getTracksSortMode() { - return settings.getTrackSortModes().requireSortMode(getSortEntryId()); + TrackSortModesCollection sortModes = settings.getTrackSortModes(rootFolder); + return sortModes.requireSortMode(getSortEntryId()); } @Nullable @@ -341,7 +342,7 @@ public void setTracksSortMode(@NonNull TracksSortMode sortMode, boolean sortSubF sortSubFolder(sortMode); } else { TracksGroup folder = smartFolder != null ? smartFolder : selectedFolder; - TrackSortModesCollection sortModes = settings.getTrackSortModes(); + TrackSortModesCollection sortModes = settings.getTrackSortModes(rootFolder); sortModes.setSortMode(folder.getId(), sortMode); sortModes.syncSettings(); updateContent(); @@ -349,14 +350,15 @@ public void setTracksSortMode(@NonNull TracksSortMode sortMode, boolean sortSubF } private void sortSubFolder(@NonNull TracksSortMode sortMode) { - TrackSortModesCollection sortModes = settings.getTrackSortModes(); + TrackSortModesCollection sortModes = settings.getTrackSortModes(rootFolder); sortFolders(selectedFolder, sortModes, sortMode); sortModes.syncSettings(); app.showToastMessage(app.getString(R.string.sorted_sufolders_toast, selectedFolder.getName(), app.getString(sortMode.getNameId()))); } - private void sortFolders(@NonNull TrackFolder trackFolder, @NonNull TrackSortModesCollection sortModes, + private void sortFolders(@NonNull TrackFolder trackFolder, + @NonNull TrackSortModesCollection sortModes, @NonNull TracksSortMode sortMode) { for (TrackFolder folder : trackFolder.getFlattenedSubFolders()) { sortModes.setSortMode(folder.getId(), sortMode); diff --git a/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java b/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java index 0c660223667..15a5a5ae799 100644 --- a/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java +++ b/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java @@ -22,6 +22,7 @@ import androidx.fragment.app.FragmentManager; import androidx.viewpager.widget.ViewPager; +import net.osmand.plus.OsmandApplication; import net.osmand.plus.shared.SharedUtil; import net.osmand.plus.R; import net.osmand.plus.base.BaseOsmAndDialogFragment; @@ -107,7 +108,7 @@ public ItemsSelectionHelper getSelectionHelper() { public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); importHelper = app.getImportHelper(); - trackTabsHelper = new TrackTabsHelper(app); + trackTabsHelper = createTrackTabsHelper(app); gpxSelectionHelper = app.getSelectedGpxHelper(); itemsSelectionHelper = trackTabsHelper.getItemsSelectionHelper(); } @@ -153,18 +154,28 @@ public void tabStylesUpdated(View tabsContainer, int currentPosition) { setTabs(tabs); } + @NonNull + protected TrackTabsHelper createTrackTabsHelper(@NonNull OsmandApplication app) { + return new TrackTabsHelper(app); + } + + @NonNull + public List getTrackTabs() { + return trackTabsHelper.getTrackTabs(); + } + @NonNull public List getSortedTrackTabs() { return getSortedTrackTabs(false); } @NonNull - public List getSortedTrackTabs(boolean checkParentName) { - return trackTabsHelper.getSortedTrackTabs(checkParentName); + public List getSortedTrackTabs(boolean useExtendedName) { + return trackTabsHelper.getSortedTrackTabs(useExtendedName); } protected void setViewPagerAdapter(@NonNull ViewPager pager, List items) { - adapter = new TracksTabAdapter(app, getChildFragmentManager(), items); + adapter = new TracksTabAdapter(getChildFragmentManager(), items); pager.setAdapter(adapter); } @@ -187,7 +198,7 @@ public void setSelectedTab(@NonNull String id) { @Nullable public TrackTab getTab(@NonNull String id) { - for (TrackTab trackTab : getSortedTrackTabs()) { + for (TrackTab trackTab : getTrackTabs()) { if (Algorithms.stringsEqual(id, trackTab.getId())) { return trackTab; } @@ -242,7 +253,7 @@ public void loadTracksStarted() { } protected void updateTrackTabs() { - adapter.setTrackTabs(trackTabsHelper.getTrackTabs()); + adapter.setTrackTabs(trackTabsHelper.getSortedTrackTabs(false)); } @Override diff --git a/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java b/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java index 5f349b6c898..c956cac4345 100644 --- a/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java +++ b/OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java @@ -136,7 +136,7 @@ public void onTrackFolderSelected(@NonNull TrackFolder trackFolder) { protected void addTrackItem(@NonNull TrackItem item) { trackTabsHelper.addTrackItem(item); updateTrackTabs(); - setSelectedTab(IndexConstants.GPX_IMPORT_DIR); + setSelectedTab("import"); updateTabsContent(); } @@ -170,7 +170,7 @@ public void onTrackItemsSelected(@NonNull Set trackItems, boolean sel @Nullable @Override public TrackTab getTab(@NonNull String id) { - for (TrackTab trackTab : getSortedTrackTabs()) { + for (TrackTab trackTab : getTrackTabs()) { if (Algorithms.stringsEqual(id, trackTab.getId())) { updateTrackItemsVisibility(trackTab); return trackTab; From b55917b996b13588799bba14af75d5f20e9b747e Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Fri, 22 Nov 2024 17:20:28 +0200 Subject: [PATCH 16/18] Small fixes after merge r4.9 --- .../plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java | 1 - 1 file changed, 1 deletion(-) diff --git a/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java index 567e7c46f33..20dd7136fe7 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java @@ -32,7 +32,6 @@ import androidx.recyclerview.widget.RecyclerView.ViewHolder; import net.osmand.plus.configmap.tracks.TrackSortModesCollection; -import net.osmand.IndexConstants; import net.osmand.plus.myplaces.tracks.DialogClosedListener; import net.osmand.plus.shared.SharedUtil; import net.osmand.data.LatLon; From adb6990a9b6c32779079ac60ecdb7026828e2851 Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Sun, 24 Nov 2024 10:14:43 +0200 Subject: [PATCH 17/18] Fixes after Code Review --- .../shared/gpx/data/ComparableTracksGroup.kt | 2 +- .../net/osmand/shared/gpx/data/SmartFolder.kt | 2 +- .../net/osmand/shared/gpx/data/TrackFolder.kt | 4 +- .../src/net/osmand/plus/AppInitializer.java | 2 + .../net/osmand/plus/OsmandApplication.java | 7 ++ ...lection.java => TrackSortModesHelper.java} | 76 ++----------- .../plus/configmap/tracks/TrackTab.java | 8 +- .../configmap/tracks/TrackTabsHelper.java | 20 ++-- .../configmap/tracks/TracksComparator.java | 8 +- .../UpgradeTrackSortModeKeysAlgorithm.java | 104 ++++++++++++++++++ .../dialogs/BaseTrackFolderFragment.java | 22 ++-- .../plus/settings/backend/OsmandSettings.java | 18 --- .../plus/track/BaseTracksTabsFragment.java | 4 +- .../plus/track/helpers/GpxUiHelper.java | 2 +- .../src/net/osmand/plus/utils/FileUtils.java | 13 +-- 15 files changed, 165 insertions(+), 127 deletions(-) rename OsmAnd/src/net/osmand/plus/configmap/tracks/{TrackSortModesCollection.java => TrackSortModesHelper.java} (56%) create mode 100644 OsmAnd/src/net/osmand/plus/configmap/tracks/UpgradeTrackSortModeKeysAlgorithm.java diff --git a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/ComparableTracksGroup.kt b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/ComparableTracksGroup.kt index e726ad3ef0c..317b45a2648 100644 --- a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/ComparableTracksGroup.kt +++ b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/ComparableTracksGroup.kt @@ -4,7 +4,7 @@ import net.osmand.shared.gpx.filters.TrackFolderAnalysis interface ComparableTracksGroup { fun getFolderAnalysis(): TrackFolderAnalysis - fun getDirName(useExtendedName: Boolean): String + fun getDirName(includingSubdirs: Boolean): String fun lastModified(): Long fun getDefaultOrder(): Int = -1 } \ No newline at end of file diff --git a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/SmartFolder.kt b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/SmartFolder.kt index 263d81d97fe..f45bd199247 100644 --- a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/SmartFolder.kt +++ b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/SmartFolder.kt @@ -59,7 +59,7 @@ class SmartFolder(@Serializable var folderName: String) : TracksGroup, Comparabl return analysis } - override fun getDirName(useExtendedName: Boolean) = folderName + override fun getDirName(includingSubdirs: Boolean) = folderName override fun lastModified() = creationTime diff --git a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt index 5b09bf61dfc..2cbe0ee006b 100644 --- a/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt +++ b/OsmAnd-shared/src/commonMain/kotlin/net/osmand/shared/gpx/data/TrackFolder.kt @@ -151,8 +151,8 @@ class TrackFolder(dirFile: KFile, parentFolder: TrackFolder?) : return analysis } - override fun getDirName(useExtendedName: Boolean): String { - return if (useExtendedName) relativePath else dirFile.name() + override fun getDirName(includingSubdirs: Boolean): String { + return if (includingSubdirs) relativePath else dirFile.name() } fun getLastModified(): Long { diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index 6d3e9066b79..2964da2aa0f 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -32,6 +32,7 @@ import net.osmand.plus.base.MapViewTrackingUtilities; import net.osmand.plus.base.dialog.DialogManager; import net.osmand.plus.configmap.routes.RouteLayersHelper; +import net.osmand.plus.configmap.tracks.TrackSortModesHelper; import net.osmand.plus.download.local.LocalIndexHelper; import net.osmand.plus.download.local.LocalItem; import net.osmand.plus.feedback.AnalyticsHelper; @@ -332,6 +333,7 @@ public void onCreateApplication() { app.dialogManager = startupInit(new DialogManager(), DialogManager.class); app.routeLayersHelper = startupInit(new RouteLayersHelper(app), RouteLayersHelper.class); app.model3dHelper = startupInit(new Model3dHelper(app), Model3dHelper.class); + app.trackSortModesHelper = startupInit(new TrackSortModesHelper(app), TrackSortModesHelper.class); initOpeningHoursParser(); } diff --git a/OsmAnd/src/net/osmand/plus/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/OsmandApplication.java index c440c6830b8..1bba4177886 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandApplication.java +++ b/OsmAnd/src/net/osmand/plus/OsmandApplication.java @@ -31,6 +31,7 @@ import androidx.multidex.MultiDex; import androidx.multidex.MultiDexApplication; +import net.osmand.plus.configmap.tracks.TrackSortModesHelper; import net.osmand.plus.plugins.OsmandPlugin; import net.osmand.plus.shared.OsmAndContextImpl; import net.osmand.PlatformUtil; @@ -221,6 +222,7 @@ public class OsmandApplication extends MultiDexApplication { DialogManager dialogManager; RouteLayersHelper routeLayersHelper; Model3dHelper model3dHelper; + TrackSortModesHelper trackSortModesHelper; private final Map customRoutingConfigs = new ConcurrentHashMap<>(); private File externalStorageDirectory; @@ -661,6 +663,11 @@ public Model3dHelper getModel3dHelper() { return model3dHelper; } + @NonNull + public TrackSortModesHelper getTrackSortModesHelper() { + return trackSortModesHelper; + } + public CommandPlayer getPlayer() { return player; } diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesHelper.java similarity index 56% rename from OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java rename to OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesHelper.java index 1f772e3158f..03d67f37644 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesCollection.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesHelper.java @@ -8,35 +8,32 @@ import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.backend.preferences.ListStringPreference; import net.osmand.plus.settings.enums.TracksSortMode; -import net.osmand.shared.gpx.data.SmartFolder; import net.osmand.shared.gpx.data.TrackFolder; import net.osmand.util.Algorithms; import java.io.File; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; -public class TrackSortModesCollection { +public class TrackSortModesHelper { private static final String ROOT_FOLDER = IndexConstants.GPX_INDEX_DIR; private static final String SEPARATOR = ",,"; private final Map cachedSortModes = new ConcurrentHashMap<>(); private final ListStringPreference preference; - private boolean keysUpgraded = false; - public TrackSortModesCollection(@NonNull OsmandSettings settings) { - this.preference = settings.TRACKS_TABS_SORT_MODES; - loadFromPreference(); + public TrackSortModesHelper(@NonNull OsmandApplication app) { + OsmandSettings settings = app.getSettings(); + preference = settings.TRACKS_TABS_SORT_MODES; + loadFromPreference(app); } @NonNull - public TracksSortMode getRootSortMode() { + public TracksSortMode getRootFolderSortMode() { return requireSortMode(""); } @@ -49,12 +46,7 @@ public TracksSortMode requireSortMode(@Nullable String id) { @Nullable public TracksSortMode getSortMode(@Nullable String id) { id = removeExtraFileSeparator(id); - TracksSortMode sortMode = cachedSortModes.get(id); - if (sortMode == null) { - String idV1 = getFolderIdV1(id); - return idV1 != null ? cachedSortModes.get(idV1) : null; - } - return sortMode; + return cachedSortModes.get(id); } public void setSortMode(@NonNull String id, @NonNull TracksSortMode sortMode) { @@ -62,47 +54,9 @@ public void setSortMode(@NonNull String id, @NonNull TracksSortMode sortMode) { cachedSortModes.put(id, sortMode); } - public void askUpgradeKeysWithSync(@NonNull OsmandApplication app, @Nullable TrackFolder folder) { - if (folder != null && !keysUpgraded) { - if (askUpgradeCachedKeys(app, folder)) { - keysUpgraded = true; - syncSettings(); - } - } - } - - /** - * Removes surplus keys and upgrades outdated ones. - * Upgraded keys will use folder relative path as a key (V2) instead of folder name (V1). - */ - private boolean askUpgradeCachedKeys(@NonNull OsmandApplication app, @NonNull TrackFolder trackFolder) { - TrackFolder rootFolder = trackFolder.getRootFolder(); - if (!Objects.equals(rootFolder.getDirFile(), app.getAppPathKt(ROOT_FOLDER))) { - // Execute only from tracks root folder to not lose valid entries - return false; - } - Map upgradedCache = new HashMap<>(); - putUpgradedKey(upgradedCache, TrackTabType.ON_MAP.name()); - putUpgradedKey(upgradedCache, TrackTabType.ALL.name()); - putUpgradedKey(upgradedCache, TrackTabType.FOLDERS.name()); - putUpgradedKey(upgradedCache, rootFolder.getId()); - for (TrackFolder folder : rootFolder.getFlattenedSubFolders()) { - putUpgradedKey(upgradedCache, folder.getId()); - } - for (SmartFolder folder : app.getSmartFolderHelper().getSmartFolders()) { - putUpgradedKey(upgradedCache, folder.getId()); - } + public void setSortModes(@NonNull Map sortModes) { cachedSortModes.clear(); - cachedSortModes.putAll(upgradedCache); - return true; - } - - private void putUpgradedKey(@NonNull Map map, @NonNull String id) { - id = removeExtraFileSeparator(id); - TracksSortMode sortMode = getSortMode(id); - if (sortMode != null) { - map.put(id, sortMode); - } + cachedSortModes.putAll(sortModes); } public void updateAfterMoveTrackFolder(@NonNull TrackFolder trackFolder, @NonNull File oldDir) { @@ -123,7 +77,7 @@ public void syncSettings() { saveToPreference(); } - private void loadFromPreference() { + private void loadFromPreference(@NonNull OsmandApplication app) { List tokens = preference.getStringsList(); if (!Algorithms.isEmpty(tokens)) { for (String token : tokens) { @@ -132,6 +86,7 @@ private void loadFromPreference() { cachedSortModes.put(tokenParts[0], TracksSortMode.getByValue(tokenParts[1])); } } + UpgradeTrackSortModeKeysAlgorithm.execute(app, this); } } @@ -152,15 +107,6 @@ public static String getFolderId(@NonNull String absolutePath) { return index > 0 ? absolutePath.substring(index) : absolutePath; } - @Nullable - private static String getFolderIdV1(@Nullable String id) { - if (id != null && id.isEmpty()) { - return removeExtraFileSeparator(ROOT_FOLDER); - } - int index = id != null ? id.lastIndexOf(File.separator) : -1; - return index > 0 ? id.substring(index + 1) : null; - } - @Nullable private static String removeExtraFileSeparator(@Nullable String id) { // Ensure consistency by removing trailing File.separator from relative paths diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java index 49e39acab6a..fbc66a5c198 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTab.java @@ -74,7 +74,7 @@ private String createInitialName(@NonNull Context context) { @Override public String getId() { return switch (type) { - case FOLDER -> directory != null ? TrackSortModesCollection.getFolderId(directory.getAbsolutePath()) : ""; + case FOLDER -> directory != null ? TrackSortModesHelper.getFolderId(directory.getAbsolutePath()) : ""; case SMART_FOLDER -> smartFolder != null ? smartFolder.getId() : ""; default -> type.name(); }; @@ -109,9 +109,9 @@ public String getName() { @NonNull @Override - public String getDirName(boolean useExtendedName) { - return directory != null && useExtendedName - ? GpxUiHelper.getExtendedFolderName(directory, initialName) + public String getDirName(boolean includingSubdirs) { + return directory != null && includingSubdirs + ? GpxUiHelper.getFolderPath(directory, initialName) : initialName; } diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java index 7c56832c71f..a4531733252 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java @@ -58,9 +58,9 @@ public ItemsSelectionHelper getItemsSelectionHelper() { } @NonNull - public List getSortedTrackTabs(boolean useExtendedName) { + public List getSortedTrackTabs(boolean useSubdirs) { List result = getTrackTabs(); - result.sort(new TracksComparator(getRootSortMode(), getDefaultLocation(), useExtendedName)); + result.sort(new TracksComparator(getRootSortMode(), getDefaultLocation(), useSubdirs)); return result; } @@ -88,7 +88,7 @@ public void updateTrackItems(@NonNull TrackFolder rootFolder) { processRecentlyVisibleTracks(); updateTrackTabs(rootFolder); - loadTabsSortModes(rootFolder); + loadTabsSortModes(); sortTrackTabsContent(); } @@ -241,7 +241,7 @@ private TrackTab addTrackItem(@NonNull Map trackTabs, @NonNull if (file != null && file.getParentFile() != null) { KFile dir = file.getParentFile(); if (dir != null) { - String folderId = TrackSortModesCollection.getFolderId(dir.absolutePath()); + String folderId = TrackSortModesHelper.getFolderId(dir.absolutePath()); TrackTab trackTab = trackTabs.get(folderId); if (trackTab == null) { trackTab = new TrackTab(app, SharedUtil.jFile(dir)); @@ -305,8 +305,8 @@ public void sortTrackTab(@NonNull TrackTab trackTab) { } } - public void loadTabsSortModes(@NonNull TrackFolder folder) { - TrackSortModesCollection sortModes = settings.getTrackSortModes(folder); + public void loadTabsSortModes() { + TrackSortModesHelper sortModes = app.getTrackSortModesHelper(); for (Entry entry : trackTabs.entrySet()) { TracksSortMode sortMode = sortModes.getSortMode(entry.getKey()); if (sortMode != null) { @@ -317,16 +317,16 @@ public void loadTabsSortModes(@NonNull TrackFolder folder) { } public void saveTabsSortModes() { - TrackSortModesCollection sortModes = settings.getTrackSortModes(); + TrackSortModesHelper sortModesHelper = app.getTrackSortModesHelper(); for (TrackTab trackTab : trackTabs.values()) { - sortModes.setSortMode(trackTab.getId(), trackTab.getSortMode()); + sortModesHelper.setSortMode(trackTab.getId(), trackTab.getSortMode()); } - sortModes.syncSettings(); + sortModesHelper.syncSettings(); } @NonNull private TracksSortMode getRootSortMode() { - return settings.getTrackSortModes().getRootSortMode(); + return app.getTrackSortModesHelper().getRootFolderSortMode(); } @NonNull diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java index e44bb2d2092..6907a10fd6e 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TracksComparator.java @@ -35,7 +35,7 @@ public class TracksComparator implements Comparator { public final TrackTab trackTab; public final TracksSortMode sortMode; public final Collator collator = OsmAndCollator.primaryCollator(); - private boolean useExtendedName = false; + private boolean useSubdirs = false; public TracksComparator(@NonNull TrackTab trackTab, @NonNull LatLon latLon) { this.trackTab = trackTab; @@ -44,9 +44,9 @@ public TracksComparator(@NonNull TrackTab trackTab, @NonNull LatLon latLon) { } public TracksComparator(@NonNull TracksSortMode sortMode, - @NonNull LatLon latLon, boolean useExtendedName) { + @NonNull LatLon latLon, boolean useSubdirs) { this(sortMode, latLon); - this.useExtendedName = useExtendedName; + this.useSubdirs = useSubdirs; } public TracksComparator(@NonNull TracksSortMode sortMode, @NonNull LatLon latLon) { @@ -303,7 +303,7 @@ private int compareTrackItemNames(@NonNull TrackItem item1, @NonNull TrackItem i private int compareTrackFolderNames(@NonNull ComparableTracksGroup folder1, @NonNull ComparableTracksGroup folder2) { - return compareNames(folder1.getDirName(useExtendedName), folder2.getDirName(useExtendedName)); + return compareNames(folder1.getDirName(useSubdirs), folder2.getDirName(useSubdirs)); } private int compareNames(@NonNull String item1, @NonNull String item2) { diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/UpgradeTrackSortModeKeysAlgorithm.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/UpgradeTrackSortModeKeysAlgorithm.java new file mode 100644 index 00000000000..1ec7f569f25 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/UpgradeTrackSortModeKeysAlgorithm.java @@ -0,0 +1,104 @@ +package net.osmand.plus.configmap.tracks; + +import static net.osmand.IndexConstants.GPX_INDEX_DIR; +import static net.osmand.plus.AppInitEvents.GPX_DB_INITIALIZED; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.settings.enums.TracksSortMode; +import net.osmand.shared.gpx.GpxDbHelper; +import net.osmand.shared.gpx.GpxDirItem; +import net.osmand.shared.gpx.SmartFolderHelper; +import net.osmand.shared.gpx.data.SmartFolder; +import net.osmand.shared.io.KFile; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Removes surplus keys and upgrades outdated ones. + * Upgraded keys will use folder relative path as a key (V2) instead of folder name (V1). + */ +public class UpgradeTrackSortModeKeysAlgorithm { + + private final OsmandApplication app; + private final GpxDbHelper gpxDbHelper; + private final SmartFolderHelper smartFolderHelper; + private final TrackSortModesHelper sortModesHelper; + + private UpgradeTrackSortModeKeysAlgorithm(@NonNull OsmandApplication app, + @NonNull TrackSortModesHelper sortModesHelper) { + this.app = app; + this.sortModesHelper = sortModesHelper; + this.gpxDbHelper = app.getGpxDbHelper(); + this.smartFolderHelper = app.getSmartFolderHelper(); + } + + public static void execute(@NonNull OsmandApplication app, + @NonNull TrackSortModesHelper sortModesHelper) { + new UpgradeTrackSortModeKeysAlgorithm(app, sortModesHelper).execute(); + } + + private void execute() { + if (gpxDbHelper.isInitialized()) { + executeImpl(); + } else { + app.getAppInitializer().addOnProgressListener(GPX_DB_INITIALIZED, init -> executeImpl()); + } + } + + private void executeImpl() { + Map upgradedCache = new HashMap<>(); + putUpgradedKey(upgradedCache, TrackTabType.ON_MAP.name()); + putUpgradedKey(upgradedCache, TrackTabType.ALL.name()); + putUpgradedKey(upgradedCache, TrackTabType.FOLDERS.name()); + List gpxDirs = gpxDbHelper.getDirItems(); + for (GpxDirItem gpxDir : gpxDirs) { + KFile directory = gpxDir.getFile(); + String absolutePath = directory.absolutePath(); + if (!absolutePath.endsWith(File.separator)) { + absolutePath += File.separator; + } + putUpgradedKey(upgradedCache, getFolderIdV2(absolutePath)); + } + for (SmartFolder folder : smartFolderHelper.getSmartFolders()) { + putUpgradedKey(upgradedCache, folder.getId()); + } + sortModesHelper.setSortModes(upgradedCache); + sortModesHelper.syncSettings(); + } + + private void putUpgradedKey(@NonNull Map map, @NonNull String id) { + TracksSortMode sortMode = getSortMode(id); + if (sortMode != null) { + map.put(id, sortMode); + } + } + + private TracksSortMode getSortMode(@NonNull String id) { + TracksSortMode sortMode = sortModesHelper.getSortMode(id); + if (sortMode == null) { + String idV1 = getFolderIdV1(id); + return idV1 != null ? sortModesHelper.getSortMode(idV1) : null; + } + return sortMode; + } + + @NonNull + private static String getFolderIdV2(@NonNull String absolutePath) { + return TrackSortModesHelper.getFolderId(absolutePath); + } + + @Nullable + private static String getFolderIdV1(@Nullable String id) { + if (id != null && id.isEmpty()) { + return GPX_INDEX_DIR; + } + int index = id != null ? id.lastIndexOf(File.separator) : -1; + return index > 0 ? id.substring(index + 1) : null; + } +} diff --git a/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java index 20dd7136fe7..e13f10869c2 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/tracks/dialogs/BaseTrackFolderFragment.java @@ -31,7 +31,7 @@ import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView.ViewHolder; -import net.osmand.plus.configmap.tracks.TrackSortModesCollection; +import net.osmand.plus.configmap.tracks.TrackSortModesHelper; import net.osmand.plus.myplaces.tracks.DialogClosedListener; import net.osmand.plus.shared.SharedUtil; import net.osmand.data.LatLon; @@ -327,8 +327,8 @@ public void showSortByDialog() { @NonNull @Override public TracksSortMode getTracksSortMode() { - TrackSortModesCollection sortModes = settings.getTrackSortModes(rootFolder); - return sortModes.requireSortMode(getSortEntryId()); + TrackSortModesHelper sortModesHelper = app.getTrackSortModesHelper(); + return sortModesHelper.requireSortMode(getSortEntryId()); } @Nullable @@ -343,26 +343,26 @@ public void setTracksSortMode(@NonNull TracksSortMode sortMode, boolean sortSubF sortSubFolder(sortMode); } else { TracksGroup folder = smartFolder != null ? smartFolder : selectedFolder; - TrackSortModesCollection sortModes = settings.getTrackSortModes(rootFolder); - sortModes.setSortMode(folder.getId(), sortMode); - sortModes.syncSettings(); + TrackSortModesHelper sortModesHelper = app.getTrackSortModesHelper(); + sortModesHelper.setSortMode(folder.getId(), sortMode); + sortModesHelper.syncSettings(); updateContent(); } } private void sortSubFolder(@NonNull TracksSortMode sortMode) { - TrackSortModesCollection sortModes = settings.getTrackSortModes(rootFolder); - sortFolders(selectedFolder, sortModes, sortMode); - sortModes.syncSettings(); + TrackSortModesHelper sortModesHelper = app.getTrackSortModesHelper(); + sortFolders(selectedFolder, sortModesHelper, sortMode); + sortModesHelper.syncSettings(); app.showToastMessage(app.getString(R.string.sorted_sufolders_toast, selectedFolder.getName(), app.getString(sortMode.getNameId()))); } private void sortFolders(@NonNull TrackFolder trackFolder, - @NonNull TrackSortModesCollection sortModes, + @NonNull TrackSortModesHelper sortModesHelper, @NonNull TracksSortMode sortMode) { for (TrackFolder folder : trackFolder.getFlattenedSubFolders()) { - sortModes.setSortMode(folder.getId(), sortMode); + sortModesHelper.setSortMode(folder.getId(), sortMode); } } diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java index 8dea33efc71..11315cdcd03 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java @@ -65,7 +65,6 @@ import net.osmand.plus.charts.GPXDataSetAxisType; import net.osmand.plus.charts.GPXDataSetType; import net.osmand.plus.configmap.routes.MtbClassification; -import net.osmand.plus.configmap.tracks.TrackSortModesCollection; import net.osmand.plus.download.IndexItem; import net.osmand.plus.feedback.RateUsState; import net.osmand.plus.helpers.OsmandBackupAgent; @@ -97,7 +96,6 @@ import net.osmand.plus.wikipedia.WikiArticleShowImages; import net.osmand.render.RenderingRulesStorage; import net.osmand.shared.gpx.ColoringPurpose; -import net.osmand.shared.gpx.data.TrackFolder; import net.osmand.shared.obd.OBDDataComputer; import net.osmand.shared.routing.ColoringType; import net.osmand.shared.settings.enums.MetricsConstants; @@ -2055,22 +2053,6 @@ private String getPagedWidgetIds(@NonNull List pages) { public final CommonPreference SEARCH_TRACKS_SORT_MODE = new EnumStringPreference<>(this, "search_tracks_sort_mode", TracksSortMode.getDefaultSortMode(), TracksSortMode.values()); public final ListStringPreference TRACKS_TABS_SORT_MODES = (ListStringPreference) new ListStringPreference(this, "tracks_tabs_sort_modes", null, ";;").makeGlobal().makeShared().cache(); - private TrackSortModesCollection trackSortModes = null; - - @NonNull - public TrackSortModesCollection getTrackSortModes() { - return getTrackSortModes(null); - } - - @NonNull - public TrackSortModesCollection getTrackSortModes(@Nullable TrackFolder trackFolder) { - if (trackSortModes == null) { - trackSortModes = new TrackSortModesCollection(this); - } - trackSortModes.askUpgradeKeysWithSync(ctx, trackFolder); - return trackSortModes; - } - public final OsmandPreference ANIMATE_MY_LOCATION = new BooleanPreference(this, "animate_my_location", true).makeProfile().cache(); public final OsmandPreference EXTERNAL_INPUT_DEVICE = new StringPreference(this, "selected_external_input_device", KeyboardDeviceProfile.ID).makeProfile(); diff --git a/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java b/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java index 15a5a5ae799..572aeed17f9 100644 --- a/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java +++ b/OsmAnd/src/net/osmand/plus/track/BaseTracksTabsFragment.java @@ -170,8 +170,8 @@ public List getSortedTrackTabs() { } @NonNull - public List getSortedTrackTabs(boolean useExtendedName) { - return trackTabsHelper.getSortedTrackTabs(useExtendedName); + public List getSortedTrackTabs(boolean useSubdirs) { + return trackTabsHelper.getSortedTrackTabs(useSubdirs); } protected void setViewPagerAdapter(@NonNull ViewPager pager, List items) { diff --git a/OsmAnd/src/net/osmand/plus/track/helpers/GpxUiHelper.java b/OsmAnd/src/net/osmand/plus/track/helpers/GpxUiHelper.java index fbe27285add..12877fab62b 100644 --- a/OsmAnd/src/net/osmand/plus/track/helpers/GpxUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/track/helpers/GpxUiHelper.java @@ -203,7 +203,7 @@ public static String getFolderName(@NonNull Context context, @NonNull File direc } @NonNull - public static String getExtendedFolderName(@NonNull File directory, @NonNull String initialName) { + public static String getFolderPath(@NonNull File directory, @NonNull String initialName) { String name = directory.getName() + File.separator; File parent = directory.getParentFile(); String parentName = parent != null ? parent.getName() + File.separator : ""; diff --git a/OsmAnd/src/net/osmand/plus/utils/FileUtils.java b/OsmAnd/src/net/osmand/plus/utils/FileUtils.java index 683262b8f94..50d59589bde 100644 --- a/OsmAnd/src/net/osmand/plus/utils/FileUtils.java +++ b/OsmAnd/src/net/osmand/plus/utils/FileUtils.java @@ -12,8 +12,7 @@ import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; -import net.osmand.plus.configmap.tracks.TrackSortModesCollection; -import net.osmand.plus.settings.backend.OsmandSettings; +import net.osmand.plus.configmap.tracks.TrackSortModesHelper; import net.osmand.plus.shared.SharedUtil; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; @@ -190,15 +189,13 @@ public static void updateMovedTrackFolder(@NonNull OsmandApplication app, @NonNu } updateMovedGpxFiles(app, files, srcDir, destDir); - OsmandSettings settings = app.getSettings(); - TrackSortModesCollection sortModes = settings.getTrackSortModes(trackFolder); - sortModes.updateAfterMoveTrackFolder(trackFolder, srcDir); + TrackSortModesHelper sortModesHelper = app.getTrackSortModesHelper(); + sortModesHelper.updateAfterMoveTrackFolder(trackFolder, srcDir); } public static void updateAfterDeleteTrackFolder(@NonNull OsmandApplication app, @NonNull TrackFolder trackFolder) { - OsmandSettings settings = app.getSettings(); - TrackSortModesCollection sortModes = settings.getTrackSortModes(trackFolder); - sortModes.updateAfterDeleteTrackFolder(trackFolder); + TrackSortModesHelper sortModesHelper = app.getTrackSortModesHelper(); + sortModesHelper.updateAfterDeleteTrackFolder(trackFolder); } private static void updateMovedGpxFiles(@NonNull OsmandApplication app, @NonNull List files, From 72d6dbca85474b1a77fceeee42a8c140f32a875a Mon Sep 17 00:00:00 2001 From: chumv Date: Thu, 28 Nov 2024 16:25:52 +0200 Subject: [PATCH 18/18] Fix subfolders support for UpgradeTrackSortModeKeysAlgorithm --- .../UpgradeTrackSortModeKeysAlgorithm.java | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/UpgradeTrackSortModeKeysAlgorithm.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/UpgradeTrackSortModeKeysAlgorithm.java index 1ec7f569f25..c12527091fa 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/UpgradeTrackSortModeKeysAlgorithm.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/UpgradeTrackSortModeKeysAlgorithm.java @@ -8,16 +8,17 @@ import net.osmand.plus.OsmandApplication; import net.osmand.plus.settings.enums.TracksSortMode; +import net.osmand.shared.gpx.GpxDataItem; import net.osmand.shared.gpx.GpxDbHelper; -import net.osmand.shared.gpx.GpxDirItem; import net.osmand.shared.gpx.SmartFolderHelper; import net.osmand.shared.gpx.data.SmartFolder; import net.osmand.shared.io.KFile; import java.io.File; import java.util.HashMap; -import java.util.List; +import java.util.HashSet; import java.util.Map; +import java.util.Set; /** * Removes surplus keys and upgrades outdated ones. @@ -31,7 +32,7 @@ public class UpgradeTrackSortModeKeysAlgorithm { private final TrackSortModesHelper sortModesHelper; private UpgradeTrackSortModeKeysAlgorithm(@NonNull OsmandApplication app, - @NonNull TrackSortModesHelper sortModesHelper) { + @NonNull TrackSortModesHelper sortModesHelper) { this.app = app; this.sortModesHelper = sortModesHelper; this.gpxDbHelper = app.getGpxDbHelper(); @@ -39,7 +40,7 @@ private UpgradeTrackSortModeKeysAlgorithm(@NonNull OsmandApplication app, } public static void execute(@NonNull OsmandApplication app, - @NonNull TrackSortModesHelper sortModesHelper) { + @NonNull TrackSortModesHelper sortModesHelper) { new UpgradeTrackSortModeKeysAlgorithm(app, sortModesHelper).execute(); } @@ -56,14 +57,8 @@ private void executeImpl() { putUpgradedKey(upgradedCache, TrackTabType.ON_MAP.name()); putUpgradedKey(upgradedCache, TrackTabType.ALL.name()); putUpgradedKey(upgradedCache, TrackTabType.FOLDERS.name()); - List gpxDirs = gpxDbHelper.getDirItems(); - for (GpxDirItem gpxDir : gpxDirs) { - KFile directory = gpxDir.getFile(); - String absolutePath = directory.absolutePath(); - if (!absolutePath.endsWith(File.separator)) { - absolutePath += File.separator; - } - putUpgradedKey(upgradedCache, getFolderIdV2(absolutePath)); + for (String id : getDirIds()) { + putUpgradedKey(upgradedCache, id); } for (SmartFolder folder : smartFolderHelper.getSmartFolders()) { putUpgradedKey(upgradedCache, folder.getId()); @@ -72,6 +67,23 @@ private void executeImpl() { sortModesHelper.syncSettings(); } + @NonNull + private Set getDirIds() { + Set ids = new HashSet<>(); + for (GpxDataItem item : gpxDbHelper.getItems()) { + KFile file = item.getFile(); + KFile dir = file.getParentFile(); + if (dir != null) { + String path = dir.absolutePath(); + if (!path.endsWith(File.separator)) { + path += File.separator; + } + ids.add(getFolderIdV2(path)); + } + } + return ids; + } + private void putUpgradedKey(@NonNull Map map, @NonNull String id) { TracksSortMode sortMode = getSortMode(id); if (sortMode != null) {