summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/adapter/ChaptersListAdapter.java
blob: cb72a9150f96762ac6e6c0f2059b4c4f2ec519f4 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package de.danoeh.antennapod.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.FitCenter;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.feed.Chapter;
import de.danoeh.antennapod.core.glide.ApGlideSettings;
import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.EmbeddedChapterImage;
import de.danoeh.antennapod.core.util.IntentUtils;
import de.danoeh.antennapod.core.util.ThemeUtils;
import de.danoeh.antennapod.core.util.playback.Playable;

public class ChaptersListAdapter extends RecyclerView.Adapter<ChaptersListAdapter.ChapterHolder> {
    private Playable media;
    private final Callback callback;
    private final Context context;
    private int currentChapterIndex = -1;
    private boolean hasImages = false;

    public ChaptersListAdapter(Context context, Callback callback) {
        this.callback = callback;
        this.context = context;
    }

    public void setMedia(Playable media) {
        this.media = media;
        hasImages = false;
        if (media.getChapters() != null) {
            for (Chapter chapter : media.getChapters()) {
                if (!ignoreChapter(chapter) && !TextUtils.isEmpty(chapter.getImageUrl())) {
                    hasImages = true;
                }
            }
        }
        notifyDataSetChanged();
    }


    @Override
    public void onBindViewHolder(@NonNull ChapterHolder holder, int position) {
        Chapter sc = getItem(position);
        holder.title.setText(sc.getTitle());
        holder.start.setText(Converter.getDurationStringLong((int) sc
                .getStart()));

        long duration;
        if (position + 1 < media.getChapters().size()) {
            duration = media.getChapters().get(position + 1).getStart() - sc.getStart();
        } else {
            duration = media.getDuration() - sc.getStart();
        }
        holder.duration.setText(context.getString(R.string.chapter_duration,
                Converter.getDurationStringLong((int) duration)));

        if (sc.getLink() == null) {
            holder.link.setVisibility(View.GONE);
        } else {
            holder.link.setVisibility(View.VISIBLE);
            holder.link.setText(sc.getLink());
            holder.link.setOnClickListener(v -> IntentUtils.openInBrowser(context, sc.getLink()));
        }
        holder.secondaryActionIcon.setImageResource(ThemeUtils.getDrawableFromAttr(context, R.attr.av_play));
        holder.secondaryActionButton.setOnClickListener(v -> {
            if (callback != null) {
                callback.onPlayChapterButtonClicked(position);
            }
        });

        if (position == currentChapterIndex) {
            int playingBackGroundColor = ThemeUtils.getColorFromAttr(context, R.attr.currently_playing_background);
            holder.itemView.setBackgroundColor(playingBackGroundColor);
        } else {
            holder.itemView.setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));
        }

        if (hasImages) {
            holder.image.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(sc.getImageUrl())) {
                Glide.with(context).clear(holder.image);
            } else {
                Glide.with(context)
                        .load(EmbeddedChapterImage.getModelFor(media, position))
                        .apply(new RequestOptions()
                                .diskCacheStrategy(ApGlideSettings.AP_DISK_CACHE_STRATEGY)
                                .dontAnimate()
                                .transforms(new FitCenter(), new RoundedCorners((int)
                                        (4 * context.getResources().getDisplayMetrics().density))))
                        .into(holder.image);
            }
        } else {
            holder.image.setVisibility(View.GONE);
        }
    }

    @NonNull
    @Override
    public ChapterHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        return new ChapterHolder(inflater.inflate(R.layout.simplechapter_item, parent, false));
    }

    @Override
    public int getItemCount() {
        if (media == null || media.getChapters() == null) {
            return 0;
        }
        // ignore invalid chapters
        int counter = 0;
        for (Chapter chapter : media.getChapters()) {
            if (!ignoreChapter(chapter)) {
                counter++;
            }
        }
        return counter;
    }

    static class ChapterHolder extends RecyclerView.ViewHolder {
        final TextView title;
        final TextView start;
        final TextView link;
        final TextView duration;
        final ImageView image;
        final View secondaryActionButton;
        final ImageView secondaryActionIcon;

        public ChapterHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.txtvTitle);
            start = itemView.findViewById(R.id.txtvStart);
            link = itemView.findViewById(R.id.txtvLink);
            image = itemView.findViewById(R.id.imgvCover);
            duration = itemView.findViewById(R.id.txtvDuration);
            secondaryActionButton = itemView.findViewById(R.id.secondaryActionButton);
            secondaryActionIcon = itemView.findViewById(R.id.secondaryActionIcon);
        }
    }

    public void notifyChapterChanged(int newChapterIndex) {
        currentChapterIndex = newChapterIndex;
        notifyDataSetChanged();
    }

    private boolean ignoreChapter(Chapter c) {
        return media.getDuration() > 0 && media.getDuration() < c.getStart();
    }

    public Chapter getItem(int position) {
        int i = 0;
        for (Chapter chapter : media.getChapters()) {
            if (!ignoreChapter(chapter)) {
                if (i == position) {
                    return chapter;
                } else {
                    i++;
                }
            }
        }
        return null;
    }

    public interface Callback {
        void onPlayChapterButtonClicked(int position);
    }

}