Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve sorting functionality of Track folders / Tracks #21310

Merged
merged 20 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
7d48fc8
Use TracksComparator to sort TrackTab instances
nazar-kutz Nov 12, 2024
e0083ff
Use relative path to identify tracks folder (refactoring)
nazar-kutz Nov 18, 2024
6939e0e
Use relative path for "import" tracks folder as well
nazar-kutz Nov 18, 2024
b3d5c33
Create wrapper class to simplify interaction with track sort modes
nazar-kutz Nov 18, 2024
dfaf225
Update track sort mode after folder was moved or renamed
nazar-kutz Nov 18, 2024
5e5d4cb
Upgrade sort mode keys on flight
nazar-kutz Nov 19, 2024
d455627
TrackSortModesCollection: small logic and code appearance fixes
nazar-kutz Nov 20, 2024
de5e683
Upgrade sort modes collection keys in the proper places
nazar-kutz Nov 20, 2024
baf88d9
Remove extra file separator to ensure consistency of folder keys
nazar-kutz Nov 21, 2024
eac8c9e
Improve name retrieval code for TrackTab
nazar-kutz Nov 21, 2024
cb697dd
Calculate TrackFolderAnalysis for TrackTab + refactoring
nazar-kutz Nov 21, 2024
5d62318
Fix problems for "FOLDERS" tab
nazar-kutz Nov 21, 2024
dae43cb
Upgrade keys only once & try to do it when collection is requested
nazar-kutz Nov 22, 2024
1dbe52d
Never display root folder name in tracks folder's relative path and id
nazar-kutz Nov 22, 2024
3123ae0
Small fixes
nazar-kutz Nov 22, 2024
9d818fe
Merge branch 'r4.9' into fix_19634_reopen_v2
nazar-kutz Nov 22, 2024
b55917b
Small fixes after merge r4.9
nazar-kutz Nov 22, 2024
adb6990
Fixes after Code Review
nazar-kutz Nov 24, 2024
a513b75
Merge branch 'r4.9' into fix_19634_reopen_v2
Chumva Nov 28, 2024
72d6dbc
Fix subfolders support for UpgradeTrackSortModeKeysAlgorithm
Chumva Nov 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import net.osmand.shared.gpx.filters.TrackFolderAnalysis

interface ComparableTracksGroup {
fun getFolderAnalysis(): TrackFolderAnalysis
fun getDirName(): String
fun getDirName(useExtendedName: Boolean): String
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useExtendedName > includingSubdirs

fun lastModified(): Long
fun getDefaultOrder(): Int = -1
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import net.osmand.shared.util.KCollectionUtils

@Serializable
class SmartFolder(@Serializable var folderName: String) : TracksGroup, ComparableTracksGroup {
companion object {
const val ID_PREFIX = "SMART_FOLDER___"
}

@Transient
private var trackItems: List<TrackItem>? = null
Expand All @@ -25,6 +28,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<TrackItem> {
Expand Down Expand Up @@ -52,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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?) :
Expand Down Expand Up @@ -37,6 +36,8 @@ class TrackFolder(dirFile: KFile, parentFolder: TrackFolder?) :
lastModified = folder.lastModified
}

override fun getId() = relativePath

override fun getName(): String {
return GpxHelper.getFolderName(dirFile, false)
}
Expand All @@ -50,15 +51,21 @@ 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() =
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

fun getRootFolder(): TrackFolder = getParentFolder()?.getRootFolder() ?: this

fun getParentFolder(): TrackFolder? {
return parentFolder
}
Expand Down Expand Up @@ -144,8 +151,8 @@ 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()
}

fun getLastModified(): Long {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TrackItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -93,14 +93,14 @@ 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
}
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(
Expand Down Expand Up @@ -131,7 +131,7 @@ class TracksFoldersScreen(
}

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

Expand Down
2 changes: 1 addition & 1 deletion OsmAnd/src/net/osmand/plus/auto/screens/TracksScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -36,16 +22,7 @@ public boolean shouldSelectAll() {
}

@NonNull
public String getPreselectedTabName(@NonNull Context context, @NonNull List<TrackTab> 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;
}
}
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 @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -87,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;
Expand All @@ -101,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();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading