forked from yuliskov/SmartTube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardPresenter.java
156 lines (134 loc) · 6.51 KB
/
CardPresenter.java
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
package com.liskovsoft.smartyoutubetv2.tv.presenter;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.leanback.widget.Presenter;
import androidx.core.content.ContextCompat;
import android.view.ViewGroup;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.liskovsoft.sharedutils.helpers.Helpers;
import com.liskovsoft.sharedutils.mylogger.Log;
import com.liskovsoft.smartyoutubetv2.common.prefs.MainUIData;
import com.liskovsoft.smartyoutubetv2.tv.R;
import com.liskovsoft.smartyoutubetv2.common.app.models.data.Video;
import com.liskovsoft.smartyoutubetv2.tv.ui.widgets.complexcardview.ComplexImageCardView;
/*
* A CardPresenter is used to generate Views and bind Objects to them on demand.
* It contains an Image CardView
*/
public class CardPresenter extends Presenter {
private static final String TAG = CardPresenter.class.getSimpleName();
private int mDefaultBackgroundColor = -1;
private int mDefaultTextColor = -1;
private int mSelectedBackgroundColor = -1;
private int mSelectedTextColor = -1;
private Drawable mDefaultCardImage;
private boolean mIsAnimatedPreviewsEnabled;
private boolean mIsMultilineTitlesEnabled;
private float mVideoGridScale;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
mDefaultBackgroundColor =
ContextCompat.getColor(parent.getContext(), Helpers.getThemeAttr(parent.getContext(), R.attr.cardDefaultBackground));
mDefaultTextColor =
ContextCompat.getColor(parent.getContext(), R.color.card_default_text);
mSelectedBackgroundColor =
ContextCompat.getColor(parent.getContext(), Helpers.getThemeAttr(parent.getContext(), R.attr.cardSelectedBackground));
mSelectedTextColor =
ContextCompat.getColor(parent.getContext(), R.color.card_selected_text_grey);
mDefaultCardImage = ContextCompat.getDrawable(parent.getContext(), R.drawable.movie);
MainUIData mainUIData = MainUIData.instance(parent.getContext());
mIsAnimatedPreviewsEnabled = mainUIData.isAnimatedPreviewsEnabled();
mVideoGridScale = mainUIData.getVideoGridScale();
mIsMultilineTitlesEnabled = mainUIData.isMultilineTitlesEnabled();
ComplexImageCardView cardView = new ComplexImageCardView(parent.getContext()) {
@Override
public void setSelected(boolean selected) {
updateCardBackgroundColor(this, selected);
super.setSelected(selected);
}
};
cardView.enableMultilineTitles(mIsMultilineTitlesEnabled);
cardView.setFocusable(true);
cardView.setFocusableInTouchMode(true);
updateCardBackgroundColor(cardView, false);
return new ViewHolder(cardView);
}
private void updateCardBackgroundColor(ComplexImageCardView view, boolean selected) {
int backgroundColor = selected ? mSelectedBackgroundColor : mDefaultBackgroundColor;
int textColor = selected ? mSelectedTextColor : mDefaultTextColor;
// Both background colors should be set because the view's
// background is temporarily visible during animations.
view.setBackgroundColor(backgroundColor);
View infoField = view.findViewById(R.id.info_field);
if (infoField != null) {
infoField.setBackgroundColor(backgroundColor);
}
TextView titleText = view.findViewById(R.id.title_text);
if (titleText != null) {
titleText.setTextColor(textColor);
}
TextView contentText = view.findViewById(R.id.content_text);
if (contentText != null) {
contentText.setTextColor(textColor);
}
}
@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
Video video = (Video) item;
ComplexImageCardView cardView = (ComplexImageCardView) viewHolder.view;
Context context = cardView.getContext();
Resources res = cardView.getResources();
cardView.setTitleText(video.title);
cardView.setContentText(video.description);
cardView.setProgress(video.percentWatched);
cardView.setBadgeText(video.hasNewContent ?
context.getString(R.string.badge_new_content) : video.isLive ? context.getString(R.string.badge_live) : video.badge);
cardView.setBadgeColor(video.hasNewContent || video.isLive || video.isUpcoming ?
ContextCompat.getColor(context, R.color.dark_red) : ContextCompat.getColor(context, R.color.black));
if (mIsAnimatedPreviewsEnabled) {
cardView.setPreviewUrl(video.previewUrl);
}
if (video.cardImageUrl != null) {
// Set card size from dimension resources.
int width = res.getDimensionPixelSize(R.dimen.card_width);
int height = res.getDimensionPixelSize(R.dimen.card_height);
if (mVideoGridScale > 1.0f) {
width *= mVideoGridScale;
height *= mVideoGridScale;
}
cardView.setMainImageDimensions(width, height);
Glide.with(context)
.load(video.cardImageUrl)
.apply(RequestOptions.errorOf(mDefaultCardImage))
.listener(mErrorListener)
.into(cardView.getMainImageView());
}
}
@Override
public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
ComplexImageCardView cardView = (ComplexImageCardView) viewHolder.view;
// Remove references to images so that the garbage collector can free up memory.
cardView.setBadgeImage(null);
cardView.setMainImage(null);
}
private final RequestListener<Drawable> mErrorListener = new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
Log.e(TAG, "Glide load failed: " + e);
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
};
}