summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/adapter/StatisticsListAdapter.java
blob: 31e82dbe05e9dc6faeac57798e8f625771aa3dff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package de.danoeh.antennapod.adapter;

import android.content.Context;
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 com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import com.bumptech.glide.request.RequestOptions;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.glide.ApGlideSettings;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.util.Converter;

/**
 * Adapter for the statistics list
 */
public class StatisticsListAdapter extends BaseAdapter {
    private final Context context;
    private List<DBReader.StatisticsItem> feedTime = new ArrayList<>();
    private boolean countAll = true;

    public StatisticsListAdapter(Context context) {
        this.context = context;
    }

    public void setCountAll(boolean countAll) {
        this.countAll = countAll;
    }

    @Override
    public int getCount() {
        return feedTime.size();
    }

    @Override
    public DBReader.StatisticsItem getItem(int position) {
        return feedTime.get(position);
    }

    @Override
    public long getItemId(int position) {
        return feedTime.get(position).feed.getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        StatisticsHolder holder;
        Feed feed = feedTime.get(position).feed;

        if (convertView == null) {
            holder = new StatisticsHolder();
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater.inflate(R.layout.statistics_listitem, parent, false);

            holder.image = convertView.findViewById(R.id.imgvCover);
            holder.title = convertView.findViewById(R.id.txtvTitle);
            holder.time = convertView.findViewById(R.id.txtvTime);
            convertView.setTag(holder);
        } else {
            holder = (StatisticsHolder) convertView.getTag();
        }

        Glide.with(context)
                .load(feed.getImageLocation())
                .apply(new RequestOptions()
                    .placeholder(R.color.light_gray)
                    .error(R.color.light_gray)
                    .diskCacheStrategy(ApGlideSettings.AP_DISK_CACHE_STRATEGY)
                    .fitCenter()
                    .dontAnimate())
                .into(holder.image);

        holder.title.setText(feed.getTitle());
        holder.time.setText(Converter.shortLocalizedDuration(context,
                countAll ? feedTime.get(position).timePlayedCountAll
                        : feedTime.get(position).timePlayed));
        return convertView;
    }

    public void update(List<DBReader.StatisticsItem> feedTime) {
        this.feedTime = feedTime;
        notifyDataSetChanged();
    }

    static class StatisticsHolder {
        ImageView image;
        TextView title;
        TextView time;
    }

}