Interval statistics: listen preferred units change (metric/imperial) and recreate intervals.

This commit is contained in:
Román
2020-08-12 16:20:07 +02:00
committed by Román Martínez
parent cbc803d038
commit a6c3cf1c28
4 changed files with 33 additions and 8 deletions
@@ -63,6 +63,9 @@ public class StatisticsRecordedFragment extends Fragment implements IntervalList
private final SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener = (preferences, key) -> { private final SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener = (preferences, key) -> {
if (PreferencesUtils.isKey(getContext(), R.string.stats_units_key, key) || PreferencesUtils.isKey(getContext(), R.string.stats_rate_key, key)) { if (PreferencesUtils.isKey(getContext(), R.string.stats_units_key, key) || PreferencesUtils.isKey(getContext(), R.string.stats_rate_key, key)) {
if (viewModel != null) {
viewModel.invalidate();
}
if (isResumed()) { if (isResumed()) {
getActivity().runOnUiThread(() -> { getActivity().runOnUiThread(() -> {
if (isResumed()) { if (isResumed()) {
@@ -187,16 +190,12 @@ public class StatisticsRecordedFragment extends Fragment implements IntervalList
loadStatistics(); loadStatistics();
} }
@Override
public void onPause() {
super.onPause();
PreferencesUtils.unregister(getContext(), sharedPreferenceChangeListener);
}
@Override @Override
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();
PreferencesUtils.unregister(getContext(), sharedPreferenceChangeListener);
totalTimeValueView = null; totalTimeValueView = null;
distanceValue = null; distanceValue = null;
@@ -218,6 +217,10 @@ public class StatisticsRecordedFragment extends Fragment implements IntervalList
speedMovingLabel = null; speedMovingLabel = null;
speedMovingValue = null; speedMovingValue = null;
speedMovingUnit = null; speedMovingUnit = null;
intervalListView.destroy();
intervalListView = null;
viewModel = null;
} }
public void loadStatistics() { public void loadStatistics() {
@@ -1,7 +1,5 @@
package de.dennisguse.opentracks.viewmodels; package de.dennisguse.opentracks.viewmodels;
import android.util.Log;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -38,6 +38,10 @@ public class IntervalStatisticsModel extends AndroidViewModel {
return getIntervalStats(trackId, interval); return getIntervalStats(trackId, interval);
} }
public void invalidate() {
intervalStats = null;
}
private void loadIntervalStats(long trackId) { private void loadIntervalStats(long trackId) {
new Thread(() -> { new Thread(() -> {
Context context = getApplication().getApplicationContext(); Context context = getApplication().getApplicationContext();
@@ -1,6 +1,7 @@
package de.dennisguse.opentracks.views; package de.dennisguse.opentracks.views;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
@@ -26,10 +27,19 @@ public class IntervalListView extends LinearLayout {
private Context context; private Context context;
private IntervalListListener listener; private IntervalListListener listener;
private final SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener = (preferences, key) -> {
if (PreferencesUtils.isKey(getContext(), R.string.stats_units_key, key) || PreferencesUtils.isKey(getContext(), R.string.stats_rate_key, key)) {
if (spinnerIntervalsUnit != null) {
spinnerIntervalsUnit.setText(PreferencesUtils.isMetricUnits(context) ? context.getString(R.string.unit_kilometer) : context.getString(R.string.unit_mile));
}
}
};
public IntervalListView(Context context, IntervalListListener listener) { public IntervalListView(Context context, IntervalListListener listener) {
super(context); super(context);
this.context = context; this.context = context;
this.listener = listener; this.listener = listener;
PreferencesUtils.register(getContext(), sharedPreferenceChangeListener);
init(); init();
} }
@@ -55,6 +65,16 @@ public class IntervalListView extends LinearLayout {
}); });
} }
public void destroy() {
PreferencesUtils.unregister(getContext(), sharedPreferenceChangeListener);
adapter = null;
linearLayoutIntervals = null;
spinnerIntervals = null;
spinnerIntervalsUnit = null;
context = null;
listener = null;
}
public void display(List<IntervalStatistics.Interval> intervalList) { public void display(List<IntervalStatistics.Interval> intervalList) {
if (intervalList != null) { if (intervalList != null) {
adapter = new IntervalStatisticsAdapter(getContext(), intervalList); adapter = new IntervalStatisticsAdapter(getContext(), intervalList);