Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PodcastArrayAdapter.java « ListView « owncloudnewsreader « luhmer « de « java « main « src « News-Android-App - github.com/nextcloud/news-android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5e9b45b781ccc7c89bfd11f21fac4b60a0a4cfb0 (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
package de.luhmer.owncloudnewsreader.ListView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import androidx.annotation.NonNull;

import org.greenrobot.eventbus.EventBus;

import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.databinding.PodcastRowBinding;
import de.luhmer.owncloudnewsreader.events.podcast.AudioPodcastClicked;
import de.luhmer.owncloudnewsreader.events.podcast.StartDownloadPodcast;
import de.luhmer.owncloudnewsreader.helper.NewsFileUtils;
import de.luhmer.owncloudnewsreader.model.PodcastItem;

public class PodcastArrayAdapter extends ArrayAdapter<PodcastItem> {

    private LayoutInflater inflater;
    private EventBus eventBus;

    public PodcastArrayAdapter(Context context, PodcastItem[] values) {
        super(context, R.layout.podcast_row, values);
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        eventBus = EventBus.getDefault();
        //eventBus.register(this);
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            PodcastRowBinding binding = PodcastRowBinding.inflate(inflater, parent, false);
            view = binding.getRoot();
            holder = new ViewHolder(binding);
            view.setTag(holder);
        }

        final PodcastItem podcastItem = getItem(position);

        holder.binding.tvTitle.setText(podcastItem.title);
        holder.binding.tvBody.setText(podcastItem.mimeType);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                playPodcast(position);
            }
        });


        holder.binding.flDownloadPodcastWrapper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.binding.flDownloadPodcastWrapper.setVisibility(View.GONE);

                Toast.makeText(getContext(), "Starting download.. Please wait", Toast.LENGTH_SHORT).show();

                eventBus.post(new StartDownloadPodcast() {{ podcast = podcastItem; }});
            }
        });

        holder.binding.flPlayPodcastWrapper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                playPodcast(position);
            }
        });

        holder.binding.flDeletePodcastWrapper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(NewsFileUtils.deletePodcastFile(getContext(), podcastItem.link)) {
                    podcastItem.offlineCached = false;
                    podcastItem.downloadProgress = PodcastItem.DOWNLOAD_NOT_STARTED;
                    notifyDataSetChanged();
                }
            }
        });


        holder.binding.pbDownloadPodcast.setProgress(podcastItem.downloadProgress);
        if(podcastItem.downloadProgress >= 0) {
            holder.binding.tvDownloadPodcastProgress.setVisibility(View.VISIBLE);
            holder.binding.pbDownloadPodcast.setVisibility(View.VISIBLE);
            holder.binding.tvDownloadPodcastProgress.setText(podcastItem.downloadProgress + "%");
        }
        else {
            holder.binding.tvDownloadPodcastProgress.setVisibility(View.GONE);
            holder.binding.pbDownloadPodcast.setVisibility(View.GONE);
        }


        if(podcastItem.downloadProgress.equals(PodcastItem.DOWNLOAD_NOT_STARTED)) {
            holder.binding.flDownloadPodcastWrapper.setVisibility(View.VISIBLE);
        } else {
            holder.binding.flDownloadPodcastWrapper.setVisibility(View.GONE);
        }

        holder.binding.flDeletePodcastWrapper.setVisibility((podcastItem.downloadProgress.equals(PodcastItem.DOWNLOAD_COMPLETED)) ? View.VISIBLE : View.GONE );

        /*
        File podcastFile = new File(PodcastDownloadService.getUrlToPodcastFile(getContext(), podcastItem.link, true));
        File podcastFileCache = new File(PodcastDownloadService.getUrlToPodcastFile(getContext(), podcastItem.link, true) + ".download");
        if(podcastFile.exists()) {
            holder.flDownloadPodcast.setVisibility(View.GONE);
        }
        else if(podcastFileCache.exists()) {
            holder.flDownloadPodcast.setVisibility(View.GONE);
        }
        else
            holder.flDownloadPodcast.setVisibility(View.VISIBLE);
        */

        return view;
    }


    private void playPodcast(int position) {
        AudioPodcastClicked audioPodcastClicked = new AudioPodcastClicked();
        audioPodcastClicked.position = position;
        eventBus.post(audioPodcastClicked);
    }



    static class ViewHolder {
        @NonNull final PodcastRowBinding binding;

        public ViewHolder(@NonNull PodcastRowBinding binding) {
            this.binding = binding;
        }
    }
}