forked from upstream-mirrors/OpenTracks
IntervalsFragment/IntervalStatisticsModel refactoring: better performance with AndroidViewModel (it doesn't recreate the model every track point done anymore).
Also, it doesn't jump anymore. Fixes #669.
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
package de.dennisguse.opentracks.viewmodels;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.lifecycle.AndroidViewModel;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -12,33 +17,67 @@ import de.dennisguse.opentracks.util.UnitConversions;
|
||||
* This model is used to load intervals for a track.
|
||||
* It uses a default interval but it can be set from outside to manage the interval length.
|
||||
*/
|
||||
public class IntervalStatisticsModel {
|
||||
public class IntervalStatisticsModel extends AndroidViewModel {
|
||||
|
||||
private final List<TrackPoint> trackPoints = new ArrayList<>();
|
||||
private MutableLiveData<List<IntervalStatistics.Interval>> intervalsLiveData;
|
||||
private float distanceInterval;
|
||||
|
||||
public IntervalStatistics getIntervalStats(boolean metricUnits, @Nullable IntervalOption interval) {
|
||||
public IntervalStatisticsModel(@NonNull Application application) {
|
||||
super(application);
|
||||
}
|
||||
|
||||
public MutableLiveData<List<IntervalStatistics.Interval>> getIntervalStats(boolean metricUnits, @Nullable IntervalOption interval) {
|
||||
synchronized (trackPoints) {
|
||||
if (interval == null) {
|
||||
interval = IntervalOption.OPTION_1;
|
||||
}
|
||||
if (intervalsLiveData == null) {
|
||||
if (interval == null) {
|
||||
interval = IntervalOption.OPTION_1;
|
||||
}
|
||||
|
||||
float distanceInterval = metricUnits ? (float) (interval.getValue() * UnitConversions.KM_TO_M) : (float) (interval.getValue() * UnitConversions.MI_TO_M);
|
||||
return new IntervalStatistics(trackPoints, distanceInterval);
|
||||
intervalsLiveData = new MutableLiveData<>();
|
||||
distanceInterval = metricUnits ? (float) (interval.getValue() * UnitConversions.KM_TO_M) : (float) (interval.getValue() * UnitConversions.MI_TO_M);
|
||||
loadIntervalStatistics();
|
||||
}
|
||||
return intervalsLiveData;
|
||||
}
|
||||
}
|
||||
|
||||
private void loadIntervalStatistics() {
|
||||
IntervalStatistics intervalStatistics = new IntervalStatistics(trackPoints, distanceInterval);
|
||||
intervalsLiveData.postValue(intervalStatistics.getIntervalList());
|
||||
}
|
||||
|
||||
public void add(TrackPoint trackPoint) {
|
||||
synchronized (trackPoints) {
|
||||
trackPoints.add(trackPoint);
|
||||
}
|
||||
}
|
||||
|
||||
public void onNewTrackPoints() {
|
||||
synchronized (trackPoints) {
|
||||
if (intervalsLiveData != null) {
|
||||
loadIntervalStatistics();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
synchronized (trackPoints) {
|
||||
trackPoints.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public void upload(boolean metricUnits, @Nullable IntervalOption interval) {
|
||||
synchronized (trackPoints) {
|
||||
if (interval == null) {
|
||||
interval = IntervalOption.OPTION_1;
|
||||
}
|
||||
|
||||
distanceInterval = metricUnits ? (float) (interval.getValue() * UnitConversions.KM_TO_M) : (float) (interval.getValue() * UnitConversions.MI_TO_M);
|
||||
loadIntervalStatistics();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Intervals length this view model support.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user