-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathChannelAdapter.java
More file actions
125 lines (105 loc) · 4.41 KB
/
ChannelAdapter.java
File metadata and controls
125 lines (105 loc) · 4.41 KB
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
package com.pitv.player.adapter;
import android.content.Context;
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.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.pitv.player.R;
import com.pitv.player.model.Channel;
import com.pitv.player.service.PlaylistManager;
import java.util.List;
/**
* Adapter para exibição de canais em um RecyclerView
*/
public class ChannelAdapter extends RecyclerView.Adapter<ChannelAdapter.ChannelViewHolder> {
private final Context context;
private final List<Channel> channels;
private final OnChannelClickListener listener;
private final PlaylistManager playlistManager;
public interface OnChannelClickListener {
void onChannelClick(Channel channel);
}
public ChannelAdapter(Context context, List<Channel> channels, OnChannelClickListener listener) {
this.context = context;
this.channels = channels;
this.listener = listener;
this.playlistManager = PlaylistManager.getInstance(context);
}
@NonNull
@Override
public ChannelViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_channel, parent, false);
return new ChannelViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ChannelViewHolder holder, int position) {
Channel channel = channels.get(position);
holder.bind(channel);
}
@Override
public int getItemCount() {
return channels.size();
}
class ChannelViewHolder extends RecyclerView.ViewHolder {
private final ImageView logoImageView;
private final TextView nameTextView;
private final TextView groupTextView;
private final ImageView favoriteImageView;
public ChannelViewHolder(@NonNull View itemView) {
super(itemView);
logoImageView = itemView.findViewById(R.id.channel_logo);
nameTextView = itemView.findViewById(R.id.channel_name);
groupTextView = itemView.findViewById(R.id.channel_group);
favoriteImageView = itemView.findViewById(R.id.favorite_icon);
itemView.setOnClickListener(v -> {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION && listener != null) {
listener.onChannelClick(channels.get(position));
}
});
favoriteImageView.setOnClickListener(v -> {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
Channel channel = channels.get(position);
toggleFavorite(channel);
notifyItemChanged(position);
}
});
}
public void bind(Channel channel) {
nameTextView.setText(channel.getName());
groupTextView.setText(channel.getGroup());
// Carrega o logo do canal
if (channel.getLogoUrl() != null && !channel.getLogoUrl().isEmpty()) {
Glide.with(context)
.load(channel.getLogoUrl())
.placeholder(R.drawable.default_channel_logo)
.error(R.drawable.default_channel_logo)
.into(logoImageView);
} else {
logoImageView.setImageResource(R.drawable.default_channel_logo);
}
// Atualiza o ícone de favorito
updateFavoriteIcon(channel);
}
private void updateFavoriteIcon(Channel channel) {
if (playlistManager.isFavorite(channel)) {
favoriteImageView.setImageResource(R.drawable.ic_favorite);
} else {
favoriteImageView.setImageResource(R.drawable.ic_favorite_border);
}
}
private void toggleFavorite(Channel channel) {
if (playlistManager.isFavorite(channel)) {
playlistManager.removeFromFavorites(channel);
} else {
playlistManager.addToFavorites(channel);
}
updateFavoriteIcon(channel);
}
}
}