forked from hitherejoe/Vineyard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostGridFragment.java
299 lines (265 loc) · 11.9 KB
/
PostGridFragment.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package com.hitherejoe.vineyard.ui.fragment;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v17.leanback.app.BackgroundManager;
import android.support.v17.leanback.app.VerticalGridFragment;
import android.support.v17.leanback.widget.OnItemViewClickedListener;
import android.support.v17.leanback.widget.OnItemViewSelectedListener;
import android.support.v17.leanback.widget.Presenter;
import android.support.v17.leanback.widget.Row;
import android.support.v17.leanback.widget.RowPresenter;
import android.support.v17.leanback.widget.VerticalGridPresenter;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.hitherejoe.vineyard.R;
import com.hitherejoe.vineyard.data.DataManager;
import com.hitherejoe.vineyard.data.model.Option;
import com.hitherejoe.vineyard.data.model.Post;
import com.hitherejoe.vineyard.data.model.Tag;
import com.hitherejoe.vineyard.data.model.User;
import com.hitherejoe.vineyard.data.remote.VineyardService;
import com.hitherejoe.vineyard.ui.activity.BaseActivity;
import com.hitherejoe.vineyard.ui.activity.PlaybackActivity;
import com.hitherejoe.vineyard.ui.activity.SearchActivity;
import com.hitherejoe.vineyard.ui.adapter.PaginationAdapter;
import com.hitherejoe.vineyard.ui.adapter.PostAdapter;
import com.hitherejoe.vineyard.util.NetworkUtil;
import com.hitherejoe.vineyard.util.ToastFactory;
import java.net.URI;
import java.util.ArrayList;
import java.util.Map;
import javax.inject.Inject;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;
import timber.log.Timber;
public class PostGridFragment extends VerticalGridFragment {
public static final String ARG_ITEM = "arg_item";
public static final String TYPE_USER = "user";
public static final String TYPE_TAG = "tag";
@Inject CompositeSubscription mCompositeSubscription;
@Inject DataManager mDataManager;
private static final int NUM_COLUMNS = 5;
private static final int BACKGROUND_UPDATE_DELAY = 300;
private BackgroundManager mBackgroundManager;
private DisplayMetrics mMetrics;
private Drawable mDefaultBackground;
private Handler mHandler;
private PostAdapter mPostAdapter;
private Runnable mBackgroundRunnable;
private String mSelectedType;
private boolean mIsStopping;
public static PostGridFragment newInstance(Object selectedItem) {
PostGridFragment postGridFragment = new PostGridFragment();
Bundle args = new Bundle();
if (selectedItem instanceof User) {
args.putParcelable(ARG_ITEM, (User) selectedItem);
} else if (selectedItem instanceof Tag) {
args.putParcelable(ARG_ITEM, (Tag) selectedItem);
}
postGridFragment.setArguments(args);
return postGridFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((BaseActivity) getActivity()).getActivityComponent().inject(this);
Bundle args = getArguments();
Object item = args.getParcelable(ARG_ITEM);
if (item == null) {
throw new IllegalArgumentException("PostGridFragment requires an item arguement!");
}
setupFragment();
prepareBackgroundManager();
setTag(item);
setSearchAffordanceColor(ContextCompat.getColor(getActivity(), R.color.accent));
}
@Override
public void onDestroy() {
super.onDestroy();
if (mBackgroundRunnable != null) {
mHandler.removeCallbacks(mBackgroundRunnable);
mBackgroundRunnable = null;
}
mBackgroundManager = null;
mCompositeSubscription.unsubscribe();
}
@Override
public void onStart() {
super.onStart();
mIsStopping = false;
}
@Override
public void onStop() {
super.onStop();
mBackgroundManager.release();
mIsStopping = true;
}
public boolean isStopping() {
return mIsStopping;
}
private void prepareBackgroundManager() {
mBackgroundManager = BackgroundManager.getInstance(getActivity());
mBackgroundManager.attach(getActivity().getWindow());
mDefaultBackground =
new ColorDrawable(ContextCompat.getColor(getActivity(), R.color.bg_light_grey));
mMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(mMetrics);
}
public void setTag(Object selectedItem) {
String tag = null;
if (selectedItem instanceof User) {
mSelectedType = TYPE_USER;
tag = ((User) selectedItem).userId;
setTitle(((User) selectedItem).username);
} else if (selectedItem instanceof Tag) {
mSelectedType = TYPE_TAG;
tag = ((Tag) selectedItem).tag;
setTitle(String.format("#%s", tag));
}
mPostAdapter = new PostAdapter(getActivity(), tag);
setAdapter(mPostAdapter);
addPageLoadSubscription();
}
private void setupFragment() {
VerticalGridPresenter gridPresenter = new VerticalGridPresenter();
gridPresenter.setNumberOfColumns(NUM_COLUMNS);
setGridPresenter(gridPresenter);
mHandler = new Handler();
setOnSearchClickedListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(SearchActivity.getStartIntent(getActivity()));
}
});
setOnItemViewClickedListener(mOnItemViewClickedListener);
setOnItemViewSelectedListener(mOnItemViewSelectedListener);
}
private void startBackgroundTimer(final URI backgroundURI) {
if (mBackgroundRunnable != null) mHandler.removeCallbacks(mBackgroundRunnable);
mBackgroundRunnable = new Runnable() {
@Override
public void run() {
if (backgroundURI != null) updateBackground(backgroundURI.toString());
}
};
mHandler.postDelayed(mBackgroundRunnable, BACKGROUND_UPDATE_DELAY);
}
protected void updateBackground(String uri) {
int width = mMetrics.widthPixels;
int height = mMetrics.heightPixels;
Glide.with(getActivity())
.load(uri)
.asBitmap()
.centerCrop()
.error(mDefaultBackground)
.into(new SimpleTarget<Bitmap>(width, height) {
@Override
public void onResourceReady(Bitmap resource,
GlideAnimation<? super Bitmap>
glideAnimation) {
mBackgroundManager.setBitmap(resource);
}
});
if (mBackgroundRunnable != null) mHandler.removeCallbacks(mBackgroundRunnable);
}
private void addPageLoadSubscription() {
if (mPostAdapter.shouldShowLoadingIndicator()) mPostAdapter.showLoadingIndicator();
Map<String, String> options = mPostAdapter.getAdapterOptions();
String tag = options.get(PaginationAdapter.KEY_TAG);
final String anchor = options.get(PaginationAdapter.KEY_ANCHOR);
String nextPage = options.get(PaginationAdapter.KEY_NEXT_PAGE);
Observable<VineyardService.PostResponse> observable = null;
if (mSelectedType.equals(TYPE_TAG)) {
observable = mDataManager.getPostsByTag(tag, nextPage, anchor);
} else if (mSelectedType.equals(TYPE_USER)) {
observable = mDataManager.getPostsByUser(tag, nextPage, anchor);
}
if (observable != null) {
mCompositeSubscription.add(observable
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.subscribe(new Subscriber<VineyardService.PostResponse>() {
@Override
public void onCompleted() { }
@Override
public void onError(Throwable e) {
mPostAdapter.removeLoadingIndicator();
if (mPostAdapter.size() == 0) {
mPostAdapter.showTryAgainCard();
} else {
Toast.makeText(
getActivity(),
getString(R.string.error_message_loading_more_posts),
Toast.LENGTH_SHORT
).show();
}
Timber.e("There was an error loading the posts", e);
}
@Override
public void onNext(VineyardService.PostResponse postResponse) {
mPostAdapter.removeLoadingIndicator();
if (mPostAdapter.size() == 0 && postResponse.data.records.isEmpty()) {
mPostAdapter.showReloadCard();
} else {
if (anchor == null) {
mPostAdapter.setAnchor(postResponse.data.anchorStr);
}
mPostAdapter.setNextPage(postResponse.data.nextPage);
mPostAdapter.addAllItems(postResponse.data.records);
}
}
}));
}
}
private OnItemViewClickedListener mOnItemViewClickedListener = new OnItemViewClickedListener() {
@Override
public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
RowPresenter.ViewHolder rowViewHolder, Row row) {
if (item instanceof Post) {
if (NetworkUtil.isWifiConnected(getActivity())) {
Post post = (Post) item;
ArrayList<Post> postList = (ArrayList<Post>) mPostAdapter.getAllItems();
startActivity(PlaybackActivity.newStartIntent(getActivity(), post, postList));
} else {
ToastFactory.createWifiErrorToast(getActivity()).show();
}
} else if (item instanceof Option) {
Option option = (Option) item;
if (option.title.equals(getString(R.string.title_oops)) ||
option.title.equals(getString(R.string.title_no_videos))) {
mPostAdapter.removeReloadCard();
addPageLoadSubscription();
}
}
}
};
private OnItemViewSelectedListener mOnItemViewSelectedListener = new OnItemViewSelectedListener() {
@Override
public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
RowPresenter.ViewHolder rowViewHolder, Row row) {
if (item instanceof Post) {
String backgroundUrl = ((Post) item).thumbnailUrl;
if (backgroundUrl != null) startBackgroundTimer(URI.create(backgroundUrl));
ArrayList<Post> posts = (ArrayList<Post>) mPostAdapter.getAllItems();
// If any item on the bottom row is selected...
int itemIndex = mPostAdapter.indexOf(item);
int minimumIndex = posts.size() - NUM_COLUMNS;
if (itemIndex >= minimumIndex && mPostAdapter.shouldLoadNextPage()) {
addPageLoadSubscription();
}
}
}
};
}