Skip to content

Commit

Permalink
Fix "tracks folder sorting lost after app restart"
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-kutz committed Dec 2, 2024
1 parent caf1236 commit 69362f3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

public class TrackSortModesHelper {

private static final String ROOT_FOLDER = IndexConstants.GPX_INDEX_DIR;
private static final String ROOT_FOLDER_ID = "";
private static final String SEPARATOR = ",,";

private final Map<String, TracksSortMode> cachedSortModes = new ConcurrentHashMap<>();
Expand All @@ -34,27 +34,29 @@ public TrackSortModesHelper(@NonNull OsmandApplication app) {

@NonNull
public TracksSortMode getRootFolderSortMode() {
return requireSortMode("");
return requireSortMode(ROOT_FOLDER_ID);
}

@NonNull
public TracksSortMode requireSortMode(@Nullable String id) {
TracksSortMode sortMode = getSortMode(id);
TracksSortMode sortMode = id != null ? getSortMode(id) : null;
return sortMode != null ? sortMode : TracksSortMode.getDefaultSortMode();
}

@Nullable
public TracksSortMode getSortMode(@Nullable String id) {
public TracksSortMode getSortMode(@NonNull String id) {
id = removeExtraFileSeparator(id);
return cachedSortModes.get(id);
}

public void setSortMode(@NonNull String id, @NonNull TracksSortMode sortMode) {
public void setSortMode(@NonNull String id, @Nullable TracksSortMode sortMode) {
id = removeExtraFileSeparator(id);
cachedSortModes.put(id, sortMode);
}

public void setSortModes(@NonNull Map<String, TracksSortMode> sortModes) {
// The extra file separator is not checked here to avoid redundant validations.
// It should be handled in the methods that call this one.
cachedSortModes.clear();
cachedSortModes.putAll(sortModes);
}
Expand All @@ -69,7 +71,7 @@ public void updateAfterMoveTrackFolder(@NonNull TrackFolder trackFolder, @NonNul
}

public void updateAfterDeleteTrackFolder(@NonNull TrackFolder trackFolder) {
cachedSortModes.remove(trackFolder.getId());
setSortMode(trackFolder.getId(), null);
syncSettings();
}

Expand All @@ -83,7 +85,8 @@ private void loadFromPreference(@NonNull OsmandApplication app) {
for (String token : tokens) {
String[] tokenParts = token.split(SEPARATOR);
if (tokenParts.length == 2) {
cachedSortModes.put(tokenParts[0], TracksSortMode.getByValue(tokenParts[1]));
String id = removeExtraFileSeparator(tokenParts[0]);
cachedSortModes.put(id, TracksSortMode.getByValue(tokenParts[1]));
}
}
UpgradeTrackSortModeKeysAlgorithm.execute(app, this);
Expand All @@ -93,25 +96,33 @@ private void loadFromPreference(@NonNull OsmandApplication app) {
private void saveToPreference() {
List<String> tokens = new ArrayList<>();
for (Entry<String, TracksSortMode> entry : cachedSortModes.entrySet()) {
tokens.add(entry.getKey() + SEPARATOR + entry.getValue().name());
TracksSortMode value = entry.getValue();
if (value != null) {
tokens.add(entry.getKey() + SEPARATOR + value.name());
}
}
preference.setStringsList(tokens);
}

@NonNull
public static String getFolderId(@NonNull String absolutePath) {
int index = absolutePath.indexOf(ROOT_FOLDER);
String basePath = IndexConstants.GPX_INDEX_DIR;
int index = absolutePath.indexOf(basePath);
if (index > 0) {
index += ROOT_FOLDER.length();
index += basePath.length();
String relativePath = absolutePath.substring(index);
return removeExtraFileSeparator(relativePath);
} else if (absolutePath.endsWith(removeExtraFileSeparator(basePath))) {
return ROOT_FOLDER_ID;
}
return index > 0 ? absolutePath.substring(index) : absolutePath;
return absolutePath;
}

@Nullable
private static String removeExtraFileSeparator(@Nullable String id) {
@NonNull
private static String removeExtraFileSeparator(@NonNull 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)) {
if (id.endsWith(File.separator)) {
return id.substring(0, id.length() - 1);
}
return id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void executeImpl() {
putUpgradedKey(upgradedCache, TrackTabType.ON_MAP.name());
putUpgradedKey(upgradedCache, TrackTabType.ALL.name());
putUpgradedKey(upgradedCache, TrackTabType.FOLDERS.name());
for (String id : getDirIds()) {
for (String id : collectStandardDirectoriesIds()) {
putUpgradedKey(upgradedCache, id);
}
for (SmartFolder folder : smartFolderHelper.getSmartFolders()) {
Expand All @@ -68,20 +68,20 @@ private void executeImpl() {
}

@NonNull
private Set<String> getDirIds() {
Set<String> ids = new HashSet<>();
private Set<String> collectStandardDirectoriesIds() {
Set<String> result = new HashSet<>();
// add root directory
result.add("");
// collect all subdirectories which contains at least one gpx track
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));
KFile directory = file.getParentFile();
if (directory != null) {
String path = directory.absolutePath();
result.add(getFolderIdV2(path));
}
}
return ids;
return result;
}

private void putUpgradedKey(@NonNull Map<String, TracksSortMode> map, @NonNull String id) {
Expand All @@ -91,6 +91,7 @@ private void putUpgradedKey(@NonNull Map<String, TracksSortMode> map, @NonNull S
}
}

@Nullable
private TracksSortMode getSortMode(@NonNull String id) {
TracksSortMode sortMode = sortModesHelper.getSortMode(id);
if (sortMode == null) {
Expand Down

0 comments on commit 69362f3

Please sign in to comment.