Skip to content

Commit

Permalink
Upgrade keys only once & try to do it when collection is requested
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-kutz committed Nov 22, 2024
1 parent 5d62318 commit dae43cb
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class TracksFoldersScreen(
}

override fun loadTracksFinished(folder: TrackFolder) {
trackTabsHelper.updateTrackItems(folder.getFlattenedTrackItems())
trackTabsHelper.updateTrackItems(folder)
invalidate()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class TrackSortModesCollection {

private final Map<String, TracksSortMode> cachedSortModes = new ConcurrentHashMap<>();
private final ListStringPreference preference;
private boolean keysUpgraded = false;

public TrackSortModesCollection(@NonNull OsmandSettings settings) {
this.preference = settings.TRACKS_TABS_SORT_MODES;
Expand Down Expand Up @@ -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();
}
}
}

Expand Down Expand Up @@ -103,20 +107,18 @@ private void putUpgradedKey(@NonNull Map<String, TracksSortMode> 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() {
Expand Down
80 changes: 35 additions & 45 deletions OsmAnd/src/net/osmand/plus/configmap/tracks/TrackTabsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -43,7 +43,7 @@ public class TrackTabsHelper {
private final ItemsSelectionHelper<TrackItem> itemsSelectionHelper;

private final Set<TrackItem> recentlyVisibleTrackItem = new HashSet<>();
private final Map<String, TrackTab> trackTabs = new LinkedHashMap<>();
protected final Map<String, TrackTab> trackTabs = new LinkedHashMap<>();

public TrackTabsHelper(@NonNull OsmandApplication app) {
this.app = app;
Expand Down Expand Up @@ -79,61 +79,51 @@ public Set<TrackItem> getRecentlyVisibleTracks() {
return new HashSet<>(recentlyVisibleTrackItem);
}

public void updateTrackItems(@NonNull List<TrackItem> trackItems) {
List<TrackItem> 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<String, TrackTab> trackTabs = new LinkedHashMap<>();
for (TrackItem item : trackItems) {
addTrackItem(trackTabs, item);
}
updateTrackTabs(trackTabs);
}

public void updateItems(@NonNull TrackFolder folder) {
List<TrackItem> 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<TrackItem> allTrackItems = new ArrayList<>(rootFolder.getFlattenedTrackItems());
addCurrentTrackItemIfPresent(allTrackItems);
itemsSelectionHelper.setAllItems(allTrackItems);
updateSelectTrackTabs(folder);
}

private void updateTrackTabs(@NonNull Map<String, TrackTab> 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<TrackItem> 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<TrackTab> getAllTrackFoldersTabs(@NonNull TrackFolder rootFolder) {
List<TrackItem> trackItems = rootFolder.getFlattenedTrackItems();
Map<String, TrackTab> 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;
Expand All @@ -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;
Expand Down Expand Up @@ -176,7 +166,7 @@ private List<Object> 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());
Expand Down Expand Up @@ -290,7 +280,7 @@ public void saveTracksVisibility() {
gpxSelectionHelper.saveTracksVisibility(itemsSelectionHelper.getSelectedItems());
}

private void sortTrackTabs() {
private void sortTrackTabsContent() {
for (TrackTab trackTab : trackTabs.values()) {
sortTrackTab(trackTab);
}
Expand All @@ -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<String, TrackTab> entry : trackTabs.entrySet()) {
TracksSortMode sortMode = sortModes.getSortMode(entry.getKey());
if (sortMode != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 11 additions & 4 deletions OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2039,14 +2040,20 @@ private String getPagedWidgetIds(@NonNull List<String> pages) {
public final CommonPreference<TracksSortMode> 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<Boolean> ANIMATE_MY_LOCATION = new BooleanPreference(this, "animate_my_location", true).makeProfile().cache();
Expand Down
11 changes: 10 additions & 1 deletion OsmAnd/src/net/osmand/plus/track/SelectTrackTabsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,6 +98,12 @@ protected void setTabs(@NonNull List<TrackTab> tabs) {
viewPager.setCurrentItem(0);
}

@NonNull
@Override
protected TrackTabsHelper createTrackTabsHelper(@NonNull OsmandApplication app) {
return new SelectTrackTabsHelper(app);
}

@Override
public void loadTracksProgress(@NonNull TrackItem... items) {
}
Expand All @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions OsmAnd/src/net/osmand/plus/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<File> files,
Expand Down

0 comments on commit dae43cb

Please sign in to comment.