forked from upstream-mirrors/OpenTracks
Interval Stats: use a tab for recorded tracks as well.
This commit is contained in:
committed by
Román Martínez
parent
efa6caca66
commit
25ef0ecad1
@@ -0,0 +1,103 @@
|
||||
package de.dennisguse.opentracks.fragments;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import de.dennisguse.opentracks.R;
|
||||
import de.dennisguse.opentracks.content.data.Track;
|
||||
import de.dennisguse.opentracks.viewmodels.IntervalStatistics;
|
||||
import de.dennisguse.opentracks.viewmodels.IntervalStatisticsModel;
|
||||
import de.dennisguse.opentracks.views.IntervalListView;
|
||||
|
||||
/**
|
||||
* A fragment to display the intervals from recorded track.
|
||||
*/
|
||||
public class IntervalsRecordedFragment extends Fragment implements IntervalListView.IntervalListListener {
|
||||
|
||||
private static final String TAG = IntervalsRecordedFragment.class.getSimpleName();
|
||||
|
||||
private static final String TRACK_ID_KEY = "trackId";
|
||||
|
||||
private IntervalStatisticsModel viewModel;
|
||||
private IntervalListView.IntervalReverseListView intervalListView;
|
||||
|
||||
private Track.Id trackId;
|
||||
|
||||
public static Fragment newInstance(Track.Id trackId) {
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putParcelable(TRACK_ID_KEY, trackId);
|
||||
|
||||
Fragment fragment = new IntervalsRecordedFragment();
|
||||
fragment.setArguments(bundle);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.intervals_recording, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
trackId = getArguments().getParcelable(TRACK_ID_KEY);
|
||||
|
||||
intervalListView = new IntervalListView.IntervalReverseListView(getActivity(), this);
|
||||
intervalListView.setId(View.generateViewId());
|
||||
LinearLayout linearLayout = view.findViewById(R.id.root_view);
|
||||
linearLayout.removeAllViews();
|
||||
linearLayout.addView(intervalListView);
|
||||
|
||||
viewModel = new ViewModelProvider(this).get(IntervalStatisticsModel.class);
|
||||
|
||||
updateIntervals(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
intervalListView.destroy();
|
||||
intervalListView = null;
|
||||
viewModel = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update intervals through {@link IntervalStatisticsModel} view model.
|
||||
*
|
||||
* @param interval intervals will split in this interval if not null. If it's null then view model will use the default one.
|
||||
*/
|
||||
private void updateIntervals(@Nullable IntervalStatisticsModel.IntervalOption interval) {
|
||||
if (viewModel == null || intervalListView == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
viewModel.invalidate();
|
||||
LiveData<IntervalStatistics> liveData;
|
||||
if (interval == null) {
|
||||
liveData = viewModel.getIntervalStats(trackId);
|
||||
} else {
|
||||
liveData = viewModel.getIntervalStats(trackId, interval);
|
||||
}
|
||||
liveData.observe(getActivity(), intervalStatistics -> {
|
||||
if (intervalStatistics != null) {
|
||||
intervalListView.display(intervalStatistics.getIntervalList());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void intervalChanged(IntervalStatisticsModel.IntervalOption interval) {
|
||||
updateIntervals(interval);
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,6 @@ import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import de.dennisguse.opentracks.R;
|
||||
import de.dennisguse.opentracks.content.data.Track;
|
||||
import de.dennisguse.opentracks.util.PreferencesUtils;
|
||||
import de.dennisguse.opentracks.util.UnitConversions;
|
||||
import de.dennisguse.opentracks.viewmodels.IntervalStatistics;
|
||||
import de.dennisguse.opentracks.viewmodels.IntervalStatisticsModel;
|
||||
@@ -28,14 +27,23 @@ public class IntervalsRecordingFragment extends Fragment implements IntervalList
|
||||
|
||||
private static final String TAG = IntervalsRecordingFragment.class.getSimpleName();
|
||||
|
||||
private static final String TRACK_ID_KEY = "trackId";
|
||||
|
||||
// Refreshing intervals stats it's not so demanding so 5 seconds is enough to balance performance and user experience.
|
||||
private static final long UI_UPDATE_INTERVAL = 5 * UnitConversions.ONE_SECOND_MS;
|
||||
|
||||
private IntervalStatisticsModel viewModel;
|
||||
private IntervalListView.IntervalReverseListView intervalListView;
|
||||
|
||||
public static Fragment newInstance() {
|
||||
return new IntervalsRecordingFragment();
|
||||
private Track.Id trackId;
|
||||
|
||||
public static Fragment newInstance(Track.Id trackId) {
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putParcelable(TRACK_ID_KEY, trackId);
|
||||
|
||||
Fragment fragment = new IntervalsRecordingFragment();
|
||||
fragment.setArguments(bundle);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
private final Runnable intervalRunner = new Runnable() {
|
||||
@@ -59,11 +67,12 @@ public class IntervalsRecordingFragment extends Fragment implements IntervalList
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
trackId = getArguments().getParcelable(TRACK_ID_KEY);
|
||||
|
||||
intervalHandler = new Handler();
|
||||
|
||||
intervalListView = new IntervalListView.IntervalReverseListView(getActivity(), this);
|
||||
intervalListView.setId(View.generateViewId());
|
||||
intervalListView.findViewById(R.id.interval_title_label).setVisibility(View.GONE);
|
||||
LinearLayout linearLayout = view.findViewById(R.id.root_view);
|
||||
linearLayout.removeAllViews();
|
||||
linearLayout.addView(intervalListView);
|
||||
@@ -102,7 +111,6 @@ public class IntervalsRecordingFragment extends Fragment implements IntervalList
|
||||
return;
|
||||
}
|
||||
|
||||
Track.Id trackId = PreferencesUtils.getRecordingTrackId(getContext());
|
||||
viewModel.invalidate();
|
||||
LiveData<IntervalStatistics> liveData;
|
||||
if (interval == null) {
|
||||
|
||||
@@ -24,14 +24,12 @@ import android.view.LayoutInflater;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import de.dennisguse.opentracks.R;
|
||||
import de.dennisguse.opentracks.TrackRecordedActivity;
|
||||
@@ -41,8 +39,6 @@ import de.dennisguse.opentracks.stats.TrackStatistics;
|
||||
import de.dennisguse.opentracks.util.PreferencesUtils;
|
||||
import de.dennisguse.opentracks.util.StringUtils;
|
||||
import de.dennisguse.opentracks.util.TrackIconUtils;
|
||||
import de.dennisguse.opentracks.viewmodels.IntervalStatisticsModel;
|
||||
import de.dennisguse.opentracks.views.IntervalListView;
|
||||
|
||||
/**
|
||||
* A fragment to display track statistics to the user.
|
||||
@@ -50,7 +46,7 @@ import de.dennisguse.opentracks.views.IntervalListView;
|
||||
* @author Sandor Dornbush
|
||||
* @author Rodrigo Damazio
|
||||
*/
|
||||
public class StatisticsRecordedFragment extends Fragment implements IntervalListView.IntervalListListener {
|
||||
public class StatisticsRecordedFragment extends Fragment {
|
||||
|
||||
private static final String TRACK_ID_KEY = "trackId";
|
||||
|
||||
@@ -58,14 +54,8 @@ public class StatisticsRecordedFragment extends Fragment implements IntervalList
|
||||
private String category = "";
|
||||
private Track track;
|
||||
|
||||
private IntervalStatisticsModel viewModel;
|
||||
private IntervalListView intervalListView;
|
||||
|
||||
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 (viewModel != null) {
|
||||
viewModel.invalidate();
|
||||
}
|
||||
if (isResumed()) {
|
||||
getActivity().runOnUiThread(() -> {
|
||||
if (isResumed()) {
|
||||
@@ -81,8 +71,6 @@ public class StatisticsRecordedFragment extends Fragment implements IntervalList
|
||||
return inflater.inflate(R.layout.statistics_recorded, container, false);
|
||||
}
|
||||
|
||||
/* Views */
|
||||
private ViewGroup rootView;
|
||||
private TextView totalTimeValueView;
|
||||
private TextView distanceValue;
|
||||
private TextView distanceUnit;
|
||||
@@ -121,7 +109,7 @@ public class StatisticsRecordedFragment extends Fragment implements IntervalList
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
rootView = view.findViewById(R.id.root_view);
|
||||
ViewGroup rootView = view.findViewById(R.id.root_view);
|
||||
|
||||
totalTimeValueView = view.findViewById(R.id.stats_total_time_value);
|
||||
|
||||
@@ -144,14 +132,6 @@ public class StatisticsRecordedFragment extends Fragment implements IntervalList
|
||||
speedMovingLabel = view.findViewById(R.id.stats_moving_speed_label);
|
||||
speedMovingValue = view.findViewById(R.id.stats_moving_speed_value);
|
||||
speedMovingUnit = view.findViewById(R.id.stats_moving_speed_unit);
|
||||
|
||||
intervalListView = new IntervalListView(getActivity(), this);
|
||||
intervalListView.setId(View.generateViewId());
|
||||
LinearLayout linearLayoutExtra = rootView.findViewById(R.id.linear_layout_extra);
|
||||
linearLayoutExtra.removeAllViews();
|
||||
linearLayoutExtra.addView(intervalListView);
|
||||
|
||||
viewModel = new ViewModelProvider(this).get(IntervalStatisticsModel.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -174,14 +154,6 @@ public class StatisticsRecordedFragment extends Fragment implements IntervalList
|
||||
});
|
||||
}
|
||||
|
||||
private void addIntervals() {
|
||||
viewModel.getIntervalStats(track.getId()).observe(getActivity(), intervalStatistics -> {
|
||||
if (intervalStatistics != null) {
|
||||
intervalListView.display(intervalStatistics.getIntervalList());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
@@ -217,10 +189,6 @@ public class StatisticsRecordedFragment extends Fragment implements IntervalList
|
||||
speedMovingLabel = null;
|
||||
speedMovingValue = null;
|
||||
speedMovingUnit = null;
|
||||
|
||||
intervalListView.destroy();
|
||||
intervalListView = null;
|
||||
viewModel = null;
|
||||
}
|
||||
|
||||
public void loadStatistics() {
|
||||
@@ -298,18 +266,5 @@ public class StatisticsRecordedFragment extends Fragment implements IntervalList
|
||||
speedMovingValue.setText(parts.first);
|
||||
speedMovingUnit.setText(parts.second);
|
||||
}
|
||||
|
||||
addIntervals();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void intervalChanged(IntervalStatisticsModel.IntervalOption interval) {
|
||||
if (viewModel != null && intervalListView != null) {
|
||||
viewModel.getIntervalStats(track.getId(), interval).observe(getActivity(), intervalStatistics -> {
|
||||
if (intervalStatistics != null) {
|
||||
intervalListView.display(intervalStatistics.getIntervalList());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +48,8 @@ public class StatisticsRecordingFragment extends Fragment implements TrackDataLi
|
||||
|
||||
private static final String TAG = StatisticsRecordingFragment.class.getSimpleName();
|
||||
|
||||
private static final String TRACK_ID_KEY = "trackId";
|
||||
|
||||
private static final long UI_UPDATE_INTERVAL = UnitConversions.ONE_SECOND_MS;
|
||||
|
||||
private TrackDataHub trackDataHub;
|
||||
|
||||
Reference in New Issue
Block a user