-
Notifications
You must be signed in to change notification settings - Fork 162
/
MainFragment.java
346 lines (305 loc) · 14 KB
/
MainFragment.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package com.hitherejoe.vineyard.ui.fragment;
import android.content.Intent;
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.BrowseFragment;
import android.support.v17.leanback.widget.ArrayObjectAdapter;
import android.support.v17.leanback.widget.HeaderItem;
import android.support.v17.leanback.widget.ListRow;
import android.support.v17.leanback.widget.ListRowPresenter;
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.PresenterSelector;
import android.support.v17.leanback.widget.Row;
import android.support.v17.leanback.widget.RowPresenter;
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.VineyardApplication;
import com.hitherejoe.vineyard.data.BusEvent;
import com.hitherejoe.vineyard.data.DataManager;
import com.hitherejoe.vineyard.data.local.PreferencesHelper;
import com.hitherejoe.vineyard.data.model.Option;
import com.hitherejoe.vineyard.data.model.Post;
import com.hitherejoe.vineyard.data.remote.VineyardService.PostResponse;
import com.hitherejoe.vineyard.ui.activity.BaseActivity;
import com.hitherejoe.vineyard.ui.activity.GuidedStepActivity;
import com.hitherejoe.vineyard.ui.activity.PlaybackActivity;
import com.hitherejoe.vineyard.ui.activity.SearchActivity;
import com.hitherejoe.vineyard.ui.adapter.OptionsAdapter;
import com.hitherejoe.vineyard.ui.adapter.PaginationAdapter;
import com.hitherejoe.vineyard.ui.adapter.PostAdapter;
import com.hitherejoe.vineyard.ui.presenter.IconHeaderItemPresenter;
import com.hitherejoe.vineyard.util.NetworkUtil;
import com.hitherejoe.vineyard.util.ToastFactory;
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;
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 MainFragment extends BrowseFragment {
private static final int BACKGROUND_UPDATE_DELAY = 300;
@Inject Bus mEventBus;
@Inject CompositeSubscription mCompositeSubscription;
@Inject DataManager mDataManager;
private PreferencesHelper mPreferencesHelper;
private ArrayObjectAdapter mRowsAdapter;
private BackgroundManager mBackgroundManager;
private DisplayMetrics mMetrics;
private Drawable mDefaultBackground;
private Handler mHandler;
private Option mAutoLoopOption;
private OptionsAdapter mOptionsAdapter;
private Runnable mBackgroundRunnable;
private String mPopularText;
private String mEditorsPicksText;
private boolean mIsStopping;
public static MainFragment newInstance() {
return new MainFragment();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((BaseActivity) getActivity()).getActivityComponent().inject(this);
mPreferencesHelper =
VineyardApplication.get(getActivity()).getComponent().preferencesHelper();
mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
mHandler = new Handler();
mPopularText = getString(R.string.header_text_popular);
mEditorsPicksText = getString(R.string.header_text_editors_picks);
mEventBus.register(this);
loadPosts();
setAdapter(mRowsAdapter);
prepareBackgroundManager();
setupUIElements();
setupListeners();
}
@Override
public void onDestroy() {
super.onDestroy();
if (mBackgroundRunnable != null) {
mHandler.removeCallbacks(mBackgroundRunnable);
mBackgroundRunnable = null;
}
mBackgroundManager = null;
mCompositeSubscription.unsubscribe();
mEventBus.unregister(this);
}
@Override
public void onStart() {
super.onStart();
mIsStopping = false;
}
@Override
public void onStop() {
super.onStop();
mBackgroundManager.release();
mIsStopping = true;
}
@Subscribe
public void onAutoLoopUpdated(BusEvent.AutoLoopUpdated event) {
boolean isEnabled = mPreferencesHelper.getShouldAutoLoop();
mAutoLoopOption.value = isEnabled
? getString(R.string.text_auto_loop_enabled)
: getString(R.string.text_auto_loop_disabled);
mOptionsAdapter.updateOption(mAutoLoopOption);
}
public boolean isStopping() {
return mIsStopping;
}
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 setupUIElements() {
setBadgeDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.banner_shadow));
setHeadersState(HEADERS_ENABLED);
setHeadersTransitionOnBackEnabled(true);
setBrandColor(ContextCompat.getColor(getActivity(), R.color.primary));
setSearchAffordanceColor(ContextCompat.getColor(getActivity(), R.color.accent));
setHeaderPresenterSelector(new PresenterSelector() {
@Override
public Presenter getPresenter(Object o) {
return new IconHeaderItemPresenter();
}
});
boolean shouldAutoLoop = mPreferencesHelper.getShouldAutoLoop();
String optionValue = shouldAutoLoop
? getString(R.string.text_auto_loop_enabled)
: getString(R.string.text_auto_loop_disabled);
mAutoLoopOption = new Option(
getString(R.string.text_auto_loop_title),
optionValue,
R.drawable.lopp);
HeaderItem gridHeader =
new HeaderItem(mRowsAdapter.size(), getString(R.string.header_text_options));
mOptionsAdapter = new OptionsAdapter(getActivity());
mOptionsAdapter.addOption(mAutoLoopOption);
mRowsAdapter.add(new ListRow(gridHeader, mOptionsAdapter));
}
private void setupListeners() {
setOnItemViewClickedListener(mOnItemViewClickedListener);
setOnItemViewSelectedListener(mOnItemViewSelectedListener);
setOnSearchClickedListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), SearchActivity.class);
startActivity(intent);
}
});
}
private void loadPosts() {
String[] categories = getResources().getStringArray(R.array.categories);
for (int i = 0; i < categories.length; i++) loadPostsFromCategory(categories[i], i);
}
private void loadPostsFromCategory(String tag, int headerPosition) {
PostAdapter listRowAdapter = new PostAdapter(getActivity(), tag);
addPostLoadSubscription(listRowAdapter);
HeaderItem header = new HeaderItem(headerPosition, tag);
mRowsAdapter.add(new ListRow(header, listRowAdapter));
}
private void prepareBackgroundManager() {
mBackgroundManager = BackgroundManager.getInstance(getActivity());
mBackgroundManager.attach(getActivity().getWindow());
mDefaultBackground =
new ColorDrawable(ContextCompat.getColor(getActivity(), R.color.bg_grey));
mBackgroundManager.setColor(ContextCompat.getColor(getActivity(), R.color.bg_grey));
mMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(mMetrics);
}
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);
}
private void addPostLoadSubscription(final PostAdapter adapter) {
if (adapter.shouldShowLoadingIndicator()) adapter.showLoadingIndicator();
Map<String, String> options = adapter.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<PostResponse> observable;
if (tag.equals(mPopularText)) {
observable = mDataManager.getPopularPosts(nextPage, anchor);
} else if (tag.equals(mEditorsPicksText)) {
observable = mDataManager.getEditorsPicksPosts(nextPage, anchor);
} else {
observable = mDataManager.getPostsByTag(tag, nextPage, anchor);
}
mCompositeSubscription.add(observable
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.subscribe(new Subscriber<PostResponse>() {
@Override
public void onCompleted() { }
@Override
public void onError(Throwable e) {
adapter.removeLoadingIndicator();
if (adapter.size() == 0) {
adapter.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(PostResponse postResponse) {
adapter.removeLoadingIndicator();
if (adapter.size() == 0 && postResponse.data.records.isEmpty()) {
adapter.showReloadCard();
} else {
if (anchor == null) adapter.setAnchor(postResponse.data.anchorStr);
adapter.setNextPage(postResponse.data.nextPage);
adapter.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.isNetworkConnected(getActivity())) {
Post post = (Post) item;
int index = mRowsAdapter.indexOf(row);
PostAdapter adapter =
((PostAdapter) ((ListRow) mRowsAdapter.get(index)).getAdapter());
ArrayList<Post> postList = (ArrayList<Post>) adapter.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_no_videos)) ||
option.title.equals(getString(R.string.title_oops))) {
int index = mRowsAdapter.indexOf(row);
PostAdapter adapter =
((PostAdapter) ((ListRow) mRowsAdapter.get(index)).getAdapter());
adapter.removeReloadCard();
addPostLoadSubscription(adapter);
} else {
startActivity(GuidedStepActivity.getStartIntent(getActivity()));
}
}
}
};
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));
int index = mRowsAdapter.indexOf(row);
PostAdapter adapter =
((PostAdapter) ((ListRow) mRowsAdapter.get(index)).getAdapter());
if (adapter.get(adapter.size() - 1).equals(item) && adapter.shouldLoadNextPage()) {
addPostLoadSubscription(adapter);
}
}
}
};
}