Aggregated stats for only selected tracks.

This commit is contained in:
Román Martínez
2021-12-04 16:31:44 +01:00
parent 14b8ab21db
commit 19382f19c4
5 changed files with 49 additions and 5 deletions
@@ -5,12 +5,17 @@ import android.view.View;
import androidx.lifecycle.ViewModelProvider;
import java.util.List;
import de.dennisguse.opentracks.adapters.AggregatedStatisticsAdapter;
import de.dennisguse.opentracks.content.data.Track;
import de.dennisguse.opentracks.databinding.AggregatedStatsBinding;
import de.dennisguse.opentracks.viewmodels.AggregatedStatisticsModel;
public class AggregatedStatisticsActivity extends AbstractActivity {
public static final String EXTRA_TRACK_IDS = "track_ids";
private AggregatedStatsBinding viewBinding;
private AggregatedStatisticsAdapter adapter;
@@ -21,8 +26,10 @@ public class AggregatedStatisticsActivity extends AbstractActivity {
viewBinding.aggregatedStatsList.setEmptyView(viewBinding.aggregatedStatsEmptyView);
List<Track.Id> trackIds = getIntent().getParcelableArrayListExtra(EXTRA_TRACK_IDS);
final AggregatedStatisticsModel viewModel = new ViewModelProvider(this).get(AggregatedStatisticsModel.class);
viewModel.getAggregatedStats().observe(this, aggregatedStatistics -> {
viewModel.getAggregatedStats(trackIds).observe(this, aggregatedStatistics -> {
if (aggregatedStatistics != null) {
adapter = new AggregatedStatisticsAdapter(this, aggregatedStatistics);
viewBinding.aggregatedStatsList.setAdapter(adapter);
@@ -49,6 +49,10 @@ import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import de.dennisguse.opentracks.content.data.Distance;
import de.dennisguse.opentracks.content.data.Track;
@@ -470,6 +474,13 @@ public class TrackListActivity extends AbstractTrackDeleteActivity implements Co
return true;
}
if (itemId == R.id.list_context_menu_aggregated_stats) {
Intent intent = IntentUtils.newIntent(this, AggregatedStatisticsActivity.class)
.putParcelableArrayListExtra(AggregatedStatisticsActivity.EXTRA_TRACK_IDS, new ArrayList<>(Arrays.asList(trackIds)));
startActivity(intent);
return true;
}
if (itemId == R.id.list_context_menu_select_all) {
for (int i = 0; i < viewBinding.trackList.getCount(); i++) {
viewBinding.trackList.setItemChecked(i, true);
@@ -205,6 +205,26 @@ public class ContentProviderUtils {
return tracks;
}
public List<Track> getTracks(List<Track.Id> trackIds) {
if (trackIds == null || trackIds.isEmpty()) {
return getTracks();
}
String selection = String.format(TracksColumns._ID + " IN (%s)", TextUtils.join(",", Collections.nCopies(trackIds.size(), "?")));
String[] selectionArgs = trackIds.stream().map(id -> Long.toString(id.getId())).toArray(String[]::new);
ArrayList<Track> tracks = new ArrayList<>();
try (Cursor cursor = getTrackCursor(selection, selectionArgs, TracksColumns._ID)) {
if (cursor != null && cursor.moveToFirst()) {
tracks.ensureCapacity(cursor.getCount());
do {
tracks.add(createTrack(cursor));
} while (cursor.moveToNext());
}
}
return tracks;
}
public Track getTrack(@NonNull Track.Id trackId) {
try (Cursor cursor = getTrackCursor(TracksColumns._ID + "=?", new String[]{Long.toString(trackId.getId())}, null)) {
if (cursor != null && cursor.moveToNext()) {
@@ -3,6 +3,7 @@ package de.dennisguse.opentracks.viewmodels;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
@@ -20,18 +21,18 @@ public class AggregatedStatisticsModel extends AndroidViewModel {
super(application);
}
public LiveData<AggregatedStatistics> getAggregatedStats() {
public LiveData<AggregatedStatistics> getAggregatedStats(@Nullable List<Track.Id> trackIds) {
if (aggregatedStats == null) {
aggregatedStats = new MutableLiveData<>();
loadAggregatedStats();
loadAggregatedStats(trackIds);
}
return aggregatedStats;
}
private void loadAggregatedStats() {
private void loadAggregatedStats(@Nullable List<Track.Id> trackIds) {
new Thread(() -> {
ContentProviderUtils contentProviderUtils = new ContentProviderUtils(getApplication().getApplicationContext());
List<Track> tracks = contentProviderUtils.getTracks();
List<Track> tracks = contentProviderUtils.getTracks(trackIds);
AggregatedStatistics aggregatedStatistics = new AggregatedStatistics(tracks);