Cleanup: inline SelectionData.

This commit is contained in:
Dennis Guse
2025-12-06 11:41:47 +01:00
parent 8db381c448
commit e6367b1dfa
4 changed files with 41 additions and 70 deletions
@@ -757,7 +757,7 @@ public class ContentProviderUtils {
@NonNull
public List<AggregatedStatistic> getAggregatedStatisticsForTracks(ContentProviderSelectionInterface selection) {
SelectionData data = selection.buildSelection();
try (Cursor cursor = contentResolver.query(TracksColumns.CONTENT_URI_AGGREGATED_STATISTICS, null, data.selection(), data.selectionArgs(), null)) {
try (Cursor cursor = contentResolver.query(TracksColumns.CONTENT_URI_AGGREGATED_STATISTICS, null, data.selection(), data.getSelectionArgs(), null)) {
ArrayList<AggregatedStatistic> aggregatedStatistics = new ArrayList<>(cursor.getCount());
while (cursor.moveToNext()) {
aggregatedStatistics.add(new AggregatedStatistic(
@@ -771,4 +771,15 @@ public class ContentProviderUtils {
return aggregatedStatistics;
}
}
public record SelectionData(
String selection,
List<String> selectionArgs
) {
public String[] getSelectionArgs() {
if (selectionArgs == null) return null;
return selectionArgs.toArray(String[]::new);
}
}
}
@@ -1,11 +0,0 @@
package de.dennisguse.opentracks.data;
public record SelectionData(
String selection,
String[] selectionArgs //TODO Refactor to List<String>
) {
public SelectionData() {
this(null, null);
}
}
@@ -41,37 +41,32 @@ public class TrackSelection implements ContentProviderUtils.ContentProviderSelec
}
@Override
public SelectionData buildSelection() {
public ContentProviderUtils.SelectionData buildSelection() {
String selection = "";
String[] selectionArgs;
ArrayList<String> fromToArgs = new ArrayList<>();
List<String> selectionArgs = new ArrayList<>();
// Builds selection.
if (!trackIds.isEmpty()) {
selection = String.format(TracksColumns._ID + " IN (%s)", TextUtils.join(",", Collections.nCopies(trackIds.size(), "?")));
selectionArgs.addAll(trackIds.stream().map(id -> Long.toString(id.id())).toList());
}
if (!categories.isEmpty()) {
selection += selection.isEmpty() ? "" : " AND ";
selection += String.format(TracksColumns.ACTIVITY_TYPE_LOCALIZED + " IN (%s)", TextUtils.join(",", Collections.nCopies(categories.size(), "?")));
selectionArgs.addAll(categories);
}
if (from != null && to != null) {
selection += selection.isEmpty() ? "" : " AND ";
selection += TracksColumns.STARTTIME + " BETWEEN ? AND ?";
fromToArgs.add(Long.toString(from.toEpochMilli()));
fromToArgs.add(Long.toString(to.toEpochMilli()));
selectionArgs.add(Long.toString(from.toEpochMilli()));
selectionArgs.add(Long.toString(to.toEpochMilli()));
}
if (selection.isEmpty()) {
return new SelectionData();
return new ContentProviderUtils.SelectionData(null, null);
}
// Builds selection arguments.
ArrayList<String> args = new ArrayList<>();
args.addAll(trackIds.stream().map(id -> Long.toString(id.id())).toList());
args.addAll(categories);
args.addAll(fromToArgs);
selectionArgs = args.toArray(String[]::new);
return new SelectionData(selection, selectionArgs);
return new ContentProviderUtils.SelectionData(selection, selectionArgs);
}
}