forked from upstream-mirrors/OpenTracks
Intervals refactoring: added stack mode to the intervals adapter with two possibilities: stack from top and stack from bottom.
Stack from top for recorded intervals (the first lap in the top). Stack from bottom for recording intervals (the last lap in the top).
This commit is contained in:
@@ -20,18 +20,21 @@ import de.dennisguse.opentracks.viewmodels.IntervalStatistics;
|
||||
|
||||
public class IntervalStatisticsAdapter extends ArrayAdapter<IntervalStatistics.Interval> {
|
||||
|
||||
private StackMode stackMode;
|
||||
private boolean metricUnits;
|
||||
private float sumDistance_m = 0f;
|
||||
private float sumDistance_m;
|
||||
|
||||
public IntervalStatisticsAdapter(Context context, List<IntervalStatistics.Interval> intervalList) {
|
||||
public IntervalStatisticsAdapter(Context context, List<IntervalStatistics.Interval> intervalList, StackMode stackMode) {
|
||||
super(context, R.layout.interval_stats_list_item, intervalList);
|
||||
metricUnits = PreferencesUtils.isMetricUnits(context);
|
||||
this.stackMode = stackMode;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public View getView(int position, @Nullable View intervalView, @NonNull ViewGroup parent) {
|
||||
IntervalStatistics.Interval interval = getItem(position);
|
||||
int actualPosition = stackMode == StackMode.STACK_FROM_TOP ? position : getCount() - 1 - position;
|
||||
IntervalStatistics.Interval interval = getItem(actualPosition);
|
||||
ViewHolder viewHolder;
|
||||
|
||||
if (intervalView == null) {
|
||||
@@ -48,7 +51,11 @@ public class IntervalStatisticsAdapter extends ArrayAdapter<IntervalStatistics.I
|
||||
viewHolder = (ViewHolder) intervalView.getTag();
|
||||
}
|
||||
|
||||
sumDistance_m = position + 1 == getCount() && position > 0 ? (position * getItem(position - 1).getDistance_m()) + interval.getDistance_m() : (position + 1) * interval.getDistance_m();
|
||||
if (actualPosition + 1 == getCount() && actualPosition > 0) {
|
||||
sumDistance_m = actualPosition * getItem(actualPosition - 1).getDistance_m() + interval.getDistance_m();
|
||||
} else {
|
||||
sumDistance_m = (actualPosition + 1) * interval.getDistance_m();
|
||||
}
|
||||
viewHolder.distance.setText(StringUtils.formatDistance(getContext(), sumDistance_m, metricUnits));
|
||||
|
||||
Pair<String, String> speedParts = StringUtils.getSpeedParts(getContext(), interval.getSpeed_ms(), metricUnits, true);
|
||||
@@ -60,6 +67,14 @@ public class IntervalStatisticsAdapter extends ArrayAdapter<IntervalStatistics.I
|
||||
return intervalView;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the two modes of list items stacking: from top or from bottom.
|
||||
*/
|
||||
public enum StackMode {
|
||||
STACK_FROM_BOTTOM,
|
||||
STACK_FROM_TOP;
|
||||
}
|
||||
|
||||
private static class ViewHolder {
|
||||
private TextView distance;
|
||||
private TextView speed;
|
||||
|
||||
@@ -40,6 +40,7 @@ public class IntervalsFragment extends Fragment {
|
||||
|
||||
private IntervalStatisticsModel viewModel;
|
||||
private ListView intervalListView;
|
||||
protected IntervalStatisticsAdapter.StackMode stackModeListView;
|
||||
private IntervalStatisticsModel.IntervalOption selectedInterval;
|
||||
private IntervalStatisticsAdapter adapter;
|
||||
|
||||
@@ -86,6 +87,8 @@ public class IntervalsFragment extends Fragment {
|
||||
intervalListView = view.findViewById(R.id.interval_list);
|
||||
intervalListView.setEmptyView(view.findViewById(R.id.interval_list_empty_view));
|
||||
|
||||
stackModeListView = IntervalStatisticsAdapter.StackMode.STACK_FROM_TOP;
|
||||
|
||||
viewModel = new ViewModelProvider(this).get(IntervalStatisticsModel.class);
|
||||
|
||||
spinnerIntervals = view.findViewById(R.id.spinner_intervals);
|
||||
@@ -142,7 +145,7 @@ public class IntervalsFragment extends Fragment {
|
||||
LiveData<IntervalStatistics> liveData = viewModel.getIntervalStats(trackId, selectedInterval);
|
||||
liveData.observe(getActivity(), intervalStatistics -> {
|
||||
if (intervalStatistics != null) {
|
||||
adapter = new IntervalStatisticsAdapter(getContext(), intervalStatistics.getIntervalList());
|
||||
adapter = new IntervalStatisticsAdapter(getContext(), intervalStatistics.getIntervalList(), stackModeListView);
|
||||
intervalListView.setAdapter(adapter);
|
||||
}
|
||||
});
|
||||
@@ -179,10 +182,10 @@ public class IntervalsFragment extends Fragment {
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
((TrackRecordingActivity) getActivity()).setTrackIdListener(this);
|
||||
intervalHandler = new Handler();
|
||||
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
stackModeListView = IntervalStatisticsAdapter.StackMode.STACK_FROM_BOTTOM;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -89,7 +89,7 @@
|
||||
|
||||
<TextView
|
||||
android:id="@+id/interval_list_empty_view"
|
||||
style="@style/TextLarge"
|
||||
style="@style/TextMedium"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center_horizontal|center_vertical"
|
||||
|
||||
Reference in New Issue
Block a user