Intervals refactoring: ListView instead of LinearLayout with a list of views.

This commit is contained in:
Román
2020-09-09 11:54:51 +02:00
committed by Román Martínez
parent 5a44da3fb0
commit 684762db58
8 changed files with 148 additions and 203 deletions
@@ -1,6 +1,7 @@
package de.dennisguse.opentracks.adapters;
import android.content.Context;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
@@ -48,7 +49,7 @@ public class IntervalStatisticsAdapter extends ArrayAdapter<IntervalStatistics.I
viewHolder = (ViewHolder) intervalView.getTag();
}
sumDistance_m += interval.getDistance_m();
sumDistance_m = position + 1 == getCount() ? interval.getDistance_m() : (position + 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);
@@ -1,10 +1,15 @@
package de.dennisguse.opentracks.fragments;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -12,26 +17,43 @@ import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModelProvider;
import java.util.Arrays;
import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.adapters.IntervalStatisticsAdapter;
import de.dennisguse.opentracks.content.data.Track;
import de.dennisguse.opentracks.util.PreferencesUtils;
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 {
public class IntervalsRecordedFragment extends Fragment {
private static final String TAG = IntervalsRecordedFragment.class.getSimpleName();
private static final String TRACK_ID_KEY = "trackId";
private IntervalStatisticsModel viewModel;
private IntervalListView intervalListView;
private ListView intervalListView;
private IntervalStatisticsModel.IntervalOption selectedInterval;
private IntervalStatisticsAdapter adapter;
protected Spinner spinnerIntervals;
protected TextView spinnerIntervalsUnit;
private Track.Id trackId;
protected 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(getContext()) ? getContext().getString(R.string.unit_kilometer) : getContext().getString(R.string.unit_mile));
intervalChanged();
}
}
};
public static Fragment newInstance(Track.Id trackId) {
Bundle bundle = new Bundle();
bundle.putParcelable(TRACK_ID_KEY, trackId);
@@ -43,63 +65,73 @@ public class IntervalsRecordedFragment extends Fragment implements IntervalListV
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.intervals_recording, container, false);
return inflater.inflate(R.layout.interval_list_view, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
PreferencesUtils.register(getContext(), sharedPreferenceChangeListener);
trackId = getArguments().getParcelable(TRACK_ID_KEY);
intervalListView = new IntervalListView(getActivity(), this);
intervalListView.setId(View.generateViewId());
LinearLayout linearLayout = view.findViewById(R.id.root_view);
linearLayout.removeAllViews();
linearLayout.addView(intervalListView);
intervalListView = view.findViewById(R.id.interval_list);
intervalListView.setEmptyView(view.findViewById(R.id.interval_list_empty_view));
viewModel = new ViewModelProvider(this).get(IntervalStatisticsModel.class);
intervalChanged(null);
spinnerIntervals = view.findViewById(R.id.spinner_intervals);
int[] intValues = Arrays.stream(IntervalStatisticsModel.IntervalOption.values()).mapToInt(i -> i.getValue()).toArray();
spinnerIntervals.setAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, Arrays.stream(intValues).mapToObj(String::valueOf).toArray(String[]::new)));
spinnerIntervalsUnit = view.findViewById(R.id.spinner_intervals_unit);
spinnerIntervalsUnit.setText(PreferencesUtils.isMetricUnits(getContext()) ? getContext().getString(R.string.unit_kilometer) : getContext().getString(R.string.unit_mile));
spinnerIntervals.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
selectedInterval = IntervalStatisticsModel.IntervalOption.values()[i];
intervalChanged();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
intervalChanged();
}
@Override
public void onDestroy() {
super.onDestroy();
intervalListView.destroy();
PreferencesUtils.unregister(getContext(), sharedPreferenceChangeListener);
intervalListView = null;
adapter = null;
spinnerIntervals = null;
spinnerIntervalsUnit = 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.
*/
@Override
public void intervalChanged(@Nullable IntervalStatisticsModel.IntervalOption interval) {
public void intervalChanged() {
if (viewModel == null || intervalListView == null) {
return;
}
LiveData<IntervalStatistics> liveData = viewModel.getIntervalStats(trackId, interval);
LiveData<IntervalStatistics> liveData = viewModel.getIntervalStats(trackId, selectedInterval);
liveData.observe(getActivity(), intervalStatistics -> {
if (intervalStatistics != null) {
intervalListView.display(intervalStatistics.getIntervalList());
adapter = new IntervalStatisticsAdapter(getContext(), intervalStatistics.getIntervalList());
intervalListView.setAdapter(adapter);
}
});
}
@Override
public void unitChanged() {
if (viewModel != null) {
LiveData<IntervalStatistics> liveData = viewModel.getIntervalStats(trackId, null);
liveData.observe(getActivity(), intervalStatistics -> {
if (intervalStatistics != null) {
intervalListView.display(intervalStatistics.getIntervalList());
}
});
}
}
}
@@ -1,11 +1,16 @@
package de.dennisguse.opentracks.fragments;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -13,18 +18,21 @@ import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModelProvider;
import java.util.Arrays;
import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.TrackRecordingActivity;
import de.dennisguse.opentracks.adapters.IntervalStatisticsAdapter;
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;
import de.dennisguse.opentracks.views.IntervalListView;
/**
* A fragment to display the intervals from recording track.
*/
public class IntervalsRecordingFragment extends Fragment implements IntervalListView.IntervalListListener, TrackRecordingActivity.OnTrackIdListener {
public class IntervalsRecordingFragment extends Fragment implements TrackRecordingActivity.OnTrackIdListener {
private static final String TAG = IntervalsRecordingFragment.class.getSimpleName();
@@ -34,11 +42,24 @@ public class IntervalsRecordingFragment extends Fragment implements IntervalList
private static final long UI_UPDATE_INTERVAL = 5 * UnitConversions.ONE_SECOND_MS;
private IntervalStatisticsModel viewModel;
private IntervalListView.IntervalReverseListView intervalListView;
private ListView intervalListView;
private IntervalStatisticsModel.IntervalOption selectedInterval;
private IntervalStatisticsAdapter adapter;
protected Spinner spinnerIntervals;
protected TextView spinnerIntervalsUnit;
private Track.Id trackId;
protected 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(getContext()) ? getContext().getString(R.string.unit_kilometer) : getContext().getString(R.string.unit_mile));
intervalChanged();
}
}
};
public static Fragment newInstance(Track.Id trackId) {
Bundle bundle = new Bundle();
bundle.putParcelable(TRACK_ID_KEY, trackId);
@@ -62,25 +83,45 @@ public class IntervalsRecordingFragment extends Fragment implements IntervalList
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.intervals_recording, container, false);
return inflater.inflate(R.layout.interval_list_view, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
PreferencesUtils.register(getContext(), sharedPreferenceChangeListener);
trackId = getArguments().getParcelable(TRACK_ID_KEY);
((TrackRecordingActivity) getActivity()).setTrackIdListener(this);
intervalHandler = new Handler();
intervalListView = new IntervalListView.IntervalReverseListView(getActivity(), this);
intervalListView.setId(View.generateViewId());
LinearLayout linearLayout = view.findViewById(R.id.root_view);
linearLayout.removeAllViews();
linearLayout.addView(intervalListView);
intervalListView = view.findViewById(R.id.interval_list);
intervalListView.setEmptyView(view.findViewById(R.id.interval_list_empty_view));
viewModel = new ViewModelProvider(this).get(IntervalStatisticsModel.class);
spinnerIntervals = view.findViewById(R.id.spinner_intervals);
int[] intValues = Arrays.stream(IntervalStatisticsModel.IntervalOption.values()).mapToInt(i -> i.getValue()).toArray();
spinnerIntervals.setAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, Arrays.stream(intValues).mapToObj(String::valueOf).toArray(String[]::new)));
spinnerIntervalsUnit = view.findViewById(R.id.spinner_intervals_unit);
spinnerIntervalsUnit.setText(PreferencesUtils.isMetricUnits(getContext()) ? getContext().getString(R.string.unit_kilometer) : getContext().getString(R.string.unit_mile));
spinnerIntervals.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
selectedInterval = IntervalStatisticsModel.IntervalOption.values()[i];
intervalChanged();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
@Override
@@ -99,45 +140,34 @@ public class IntervalsRecordingFragment extends Fragment implements IntervalList
public void onDestroy() {
super.onDestroy();
intervalListView.destroy();
PreferencesUtils.unregister(getContext(), sharedPreferenceChangeListener);
intervalListView = null;
adapter = null;
spinnerIntervals = null;
spinnerIntervalsUnit = 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.
*/
@Override
public void intervalChanged(@Nullable IntervalStatisticsModel.IntervalOption interval) {
public void intervalChanged() {
if (viewModel == null || intervalListView == null) {
return;
}
selectedInterval = interval;
LiveData<IntervalStatistics> liveData = viewModel.getIntervalStats(trackId, interval);
LiveData<IntervalStatistics> liveData = viewModel.getIntervalStats(trackId, selectedInterval);
liveData.observe(getActivity(), intervalStatistics -> {
if (intervalStatistics != null) {
intervalListView.display(intervalStatistics.getIntervalList());
adapter = new IntervalStatisticsAdapter(getContext(), intervalStatistics.getIntervalList());
intervalListView.setAdapter(adapter);
}
});
}
private void updateIntervals() {
intervalChanged(selectedInterval);
}
@Override
public void unitChanged() {
if (viewModel != null) {
LiveData<IntervalStatistics> liveData = viewModel.getIntervalStats(trackId, selectedInterval);
liveData.observe(getActivity(), intervalStatistics -> {
if (intervalStatistics != null) {
intervalListView.display(intervalStatistics.getIntervalList());
}
});
}
intervalChanged();
}
@Override
@@ -1,122 +0,0 @@
package de.dennisguse.opentracks.views;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.Arrays;
import java.util.List;
import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.adapters.IntervalStatisticsAdapter;
import de.dennisguse.opentracks.util.PreferencesUtils;
import de.dennisguse.opentracks.viewmodels.IntervalStatistics;
import de.dennisguse.opentracks.viewmodels.IntervalStatisticsModel;
/**
* LinearLayout view used to build a list of intervals.
* See {@link IntervalStatisticsAdapter}.
*/
public class IntervalListView extends LinearLayout {
protected IntervalStatisticsAdapter adapter;
protected LinearLayout linearLayoutIntervals;
protected Spinner spinnerIntervals;
protected TextView spinnerIntervalsUnit;
protected IntervalListListener listener;
protected 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(getContext()) ? getContext().getString(R.string.unit_kilometer) : getContext().getString(R.string.unit_mile));
listener.unitChanged();
}
}
};
public IntervalListView(Context context, IntervalListListener listener) {
super(context);
this.listener = listener;
PreferencesUtils.register(getContext(), sharedPreferenceChangeListener);
inflate(getContext(), R.layout.interval_list_view, this);
linearLayoutIntervals = findViewById(R.id.interval_list);
spinnerIntervals = findViewById(R.id.spinner_intervals);
int[] intValues = Arrays.stream(IntervalStatisticsModel.IntervalOption.values()).mapToInt(i -> i.getValue()).toArray();
spinnerIntervals.setAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, Arrays.stream(intValues).mapToObj(String::valueOf).toArray(String[]::new)));
spinnerIntervalsUnit = findViewById(R.id.spinner_intervals_unit);
spinnerIntervalsUnit.setText(PreferencesUtils.isMetricUnits(getContext()) ? getContext().getString(R.string.unit_kilometer) : getContext().getString(R.string.unit_mile));
spinnerIntervals.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
IntervalStatisticsModel.IntervalOption interval = IntervalStatisticsModel.IntervalOption.values()[i];
listener.intervalChanged(interval);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public void destroy() {
PreferencesUtils.unregister(getContext(), sharedPreferenceChangeListener);
adapter = null;
linearLayoutIntervals = null;
spinnerIntervals = null;
spinnerIntervalsUnit = null;
listener = null;
}
public void display(List<IntervalStatistics.Interval> intervalList) {
if (intervalList == null) {
return;
}
adapter = new IntervalStatisticsAdapter(getContext(), intervalList);
linearLayoutIntervals.removeAllViews();
for (int i = 0; i < adapter.getCount(); i++) {
View intervalView = adapter.getView(i, null, linearLayoutIntervals);
linearLayoutIntervals.addView(intervalView);
}
}
public interface IntervalListListener {
void intervalChanged(IntervalStatisticsModel.IntervalOption interval);
void unitChanged();
}
/**
* LinearLayout view used to build a list of intervals in a reverse mode, the last one will appear in the first position on the LinearLayout.
* This class is an specialization of {@link IntervalListView} that display the views contained in the LinearLayout in a reverse mode.
*/
public static class IntervalReverseListView extends IntervalListView {
public IntervalReverseListView(Context context, IntervalListListener listener) {
super(context, listener);
}
public void display(List<IntervalStatistics.Interval> intervalList) {
if (intervalList == null) {
return;
}
adapter = new IntervalStatisticsAdapter(getContext(), intervalList);
linearLayoutIntervals.removeAllViews();
for (int i = 0; i < adapter.getCount(); i++) {
View intervalView = adapter.getView(i, null, linearLayoutIntervals);
linearLayoutIntervals.addView(intervalView, 0);
}
}
}
}
+1 -1
View File
@@ -97,4 +97,4 @@
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
+20 -4
View File
@@ -9,7 +9,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="8dp">
android:layout_margin="8dp">
<TextView
android:layout_width="wrap_content"
@@ -37,7 +37,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="8dp">
android:layout_margin="8dp">
<TextView
android:id="@+id/interval_distance"
@@ -74,10 +74,26 @@
</LinearLayout>
<LinearLayout
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@color/stats_separator"/>
<ListView
android:id="@+id/interval_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
android:layout_margin="8dp"/>
<TextView
android:id="@+id/interval_list_empty_view"
style="@style/TextLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:text="@string/interval_list_empty_message"
android:textColor="@color/list_empty_text" />
</LinearLayout>
@@ -1,14 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/StatsScrollView">
<LinearLayout
android:id="@+id/root_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="16dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp" />
</ScrollView>
+2
View File
@@ -443,6 +443,8 @@ limitations under the License.
<string name="track_list_empty_message">Start recording your next adventure here</string>
<!-- Aggregated Statistics -->
<string name="aggregated_stats_empty_message">Record your first track to see aggregated statistics</string>
<!-- Interval List -->
<string name="interval_list_empty_message">There are not any interval yet</string>
<!-- Track Widget -->
<!-- Tracks -->
<string name="tracks_few">%1$d tracks</string>