diff --git a/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatisticsActivity.java b/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatisticsActivity.java index 26ce1404b..04c0a23f3 100644 --- a/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatisticsActivity.java +++ b/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatisticsActivity.java @@ -7,6 +7,7 @@ import android.view.View; import androidx.annotation.NonNull; import androidx.lifecycle.ViewModelProvider; +import androidx.recyclerview.widget.LinearLayoutManager; import java.time.LocalDateTime; import java.time.ZoneId; @@ -40,8 +41,6 @@ public class AggregatedStatisticsActivity extends AbstractActivity implements Fi protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - viewBinding.aggregatedStatsList.setEmptyView(viewBinding.aggregatedStatsEmptyView); - areFiltersApplied = savedInstanceState != null && savedInstanceState.getBoolean(STATE_ARE_FILTERS_APPLIED); List trackIds = getIntent().getParcelableArrayListExtra(EXTRA_TRACK_IDS); @@ -49,21 +48,35 @@ public class AggregatedStatisticsActivity extends AbstractActivity implements Fi trackIds.stream().forEach(selection::addTrackId); } + LinearLayoutManager layoutManager = new LinearLayoutManager(this); + adapter = new AggregatedStatisticsAdapter(this, null); + viewBinding.aggregatedStatsList.setLayoutManager(layoutManager); + viewBinding.aggregatedStatsList.setAdapter(adapter); + viewModel = new ViewModelProvider(this).get(AggregatedStatisticsModel.class); viewModel.getAggregatedStats(selection).observe(this, aggregatedStatistics -> { if ((aggregatedStatistics == null || aggregatedStatistics.getCount() == 0) && !selection.isEmpty()) { viewBinding.aggregatedStatsEmptyView.setText(getString(R.string.aggregated_stats_filter_no_results)); } if (aggregatedStatistics != null) { - adapter = new AggregatedStatisticsAdapter(this, aggregatedStatistics); - viewBinding.aggregatedStatsList.setAdapter(adapter); + adapter.swapData(aggregatedStatistics); } - adapter.notifyDataSetChanged(); + checkListEmpty(); }); setSupportActionBar(viewBinding.bottomAppBarLayout.bottomAppBar); } + private void checkListEmpty() { + if (adapter.getItemCount() == 0) { + viewBinding.aggregatedStatsList.setVisibility(View.GONE); + viewBinding.aggregatedStatsEmptyView.setVisibility(View.VISIBLE); + } else { + viewBinding.aggregatedStatsList.setVisibility(View.VISIBLE); + viewBinding.aggregatedStatsEmptyView.setVisibility(View.GONE); + } + } + @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); diff --git a/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatisticsAdapter.java b/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatisticsAdapter.java index 38fa84cd0..9cb2e7c8f 100644 --- a/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatisticsAdapter.java +++ b/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatisticsAdapter.java @@ -1,15 +1,16 @@ package de.dennisguse.opentracks.ui.aggregatedStatistics; import android.content.Context; -import android.content.res.Resources; import android.util.Pair; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + import java.util.ArrayList; import java.util.List; @@ -21,7 +22,7 @@ import de.dennisguse.opentracks.settings.PreferencesUtils; import de.dennisguse.opentracks.settings.UnitSystem; import de.dennisguse.opentracks.util.StringUtils; -public class AggregatedStatisticsAdapter extends BaseAdapter { +public class AggregatedStatisticsAdapter extends RecyclerView.Adapter { private AggregatedStatistics aggregatedStatistics; private final Context context; @@ -31,54 +32,38 @@ public class AggregatedStatisticsAdapter extends BaseAdapter { this.aggregatedStatistics = aggregatedStatistics; } + @NonNull @Override - public int getCount() { - if (aggregatedStatistics != null) { - return aggregatedStatistics.getCount(); - } - return 0; + public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.aggregated_stats_list_item, parent, false); + return new ViewHolder(view); } @Override - public AggregatedStatistics.AggregatedStatistic getItem(int position) { - return aggregatedStatistics.getItem(position); - } + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { + ViewHolder viewHolder = (ViewHolder) holder; - @Override - public long getItemId(int position) { - // Not important because data are not database register but cooked data. - return position; - } + AggregatedStatistics.AggregatedStatistic aggregatedStatistic = aggregatedStatistics.getItem(position); - @Override - public View getView(int position, View convertView, ViewGroup parent) { - AggregatedStatistics.AggregatedStatistic aggregatedStatistic = getItem(position); - - ViewHolder viewHolder; - if (convertView == null) { - convertView = LayoutInflater.from(context).inflate(R.layout.aggregated_stats_list_item, parent, false); - viewHolder = new ViewHolder(convertView); - convertView.setTag(viewHolder); - } else { - viewHolder = (ViewHolder) convertView.getTag(); - } - - Resources resources = context.getResources(); - String localizedActivityType = aggregatedStatistic.getActivityTypeLocalized(); - if (ActivityType.findByLocalizedString(resources, localizedActivityType) - .isShowSpeedPreferred()) { + String type = aggregatedStatistic.getActivityTypeLocalized(); + if (ActivityType.findByLocalizedString(context, type).isShowSpeedPreferred()) { viewHolder.setSpeed(aggregatedStatistic); } else { viewHolder.setPace(aggregatedStatistic); } - - return convertView; } - public AggregatedStatistics swapData(AggregatedStatistics aggregatedStatistics) { + @Override + public int getItemCount() { + if (aggregatedStatistics == null) { + return 0; + } + return aggregatedStatistics.getCount(); + } + + public void swapData(AggregatedStatistics aggregatedStatistics) { this.aggregatedStatistics = aggregatedStatistics; this.notifyDataSetChanged(); - return aggregatedStatistics; } public List getCategories() { @@ -89,7 +74,7 @@ public class AggregatedStatisticsAdapter extends BaseAdapter { return categories; } - private class ViewHolder { + private class ViewHolder extends RecyclerView.ViewHolder { private final ImageView sportIcon; private final TextView typeLabel; private final TextView numTracks; @@ -108,6 +93,7 @@ public class AggregatedStatisticsAdapter extends BaseAdapter { private boolean reportSpeed; public ViewHolder(View view) { + super(view); sportIcon = view.findViewById(R.id.aggregated_stats_sport_icon); typeLabel = view.findViewById(R.id.aggregated_stats_type_label); numTracks = view.findViewById(R.id.aggregated_stats_num_tracks); @@ -183,9 +169,7 @@ public class AggregatedStatisticsAdapter extends BaseAdapter { private int getIcon(AggregatedStatistics.AggregatedStatistic aggregatedStatistic) { String localizedActivityType = aggregatedStatistic.getActivityTypeLocalized(); - String iconValue = ActivityType.findByLocalizedString(context, localizedActivityType) - .getIconId(); - return ActivityType.findBy(iconValue) + return ActivityType.findByLocalizedString(context, localizedActivityType) .getIconDrawableId(); } } diff --git a/src/main/res/layout/aggregated_stats.xml b/src/main/res/layout/aggregated_stats.xml index d28feb6f1..9cc6d24f3 100644 --- a/src/main/res/layout/aggregated_stats.xml +++ b/src/main/res/layout/aggregated_stats.xml @@ -23,10 +23,11 @@ android:paddingBottom="?attr/actionBarSize" app:layout_behavior="@string/appbar_scrolling_view_behavior"> - + android:layout_height="match_parent" + android:visibility="gone" /> + android:paddingBottom="8dp"> + + \ No newline at end of file