forked from yuliskov/SmartTube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrowseFragment.java
387 lines (314 loc) · 12.6 KB
/
BrowseFragment.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package com.liskovsoft.smartyoutubetv2.tv.ui.browse;
import android.os.Bundle;
import android.os.Handler;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.leanback.app.BrowseSupportFragment;
import androidx.leanback.widget.ArrayObjectAdapter;
import androidx.leanback.widget.HeaderItem;
import androidx.leanback.widget.ListRowPresenter;
import androidx.leanback.widget.PageRow;
import androidx.leanback.widget.Presenter;
import androidx.leanback.widget.PresenterSelector;
import androidx.leanback.widget.TitleHelper;
import com.liskovsoft.sharedutils.helpers.Helpers;
import com.liskovsoft.smartyoutubetv2.common.app.models.data.Category;
import com.liskovsoft.smartyoutubetv2.common.app.models.data.SettingsGroup;
import com.liskovsoft.smartyoutubetv2.common.app.models.data.VideoGroup;
import com.liskovsoft.smartyoutubetv2.common.app.models.errors.ErrorFragmentData;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.BrowsePresenter;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.SearchPresenter;
import com.liskovsoft.smartyoutubetv2.common.app.views.BrowseView;
import com.liskovsoft.smartyoutubetv2.common.utils.Utils;
import com.liskovsoft.smartyoutubetv2.tv.R;
import com.liskovsoft.smartyoutubetv2.tv.presenter.IconHeaderItemPresenter;
import com.liskovsoft.smartyoutubetv2.tv.ui.browse.dialog.ErrorDialogFragment;
import com.liskovsoft.smartyoutubetv2.tv.ui.mod.leanback.misc.ProgressBarManager;
import java.util.LinkedHashMap;
import java.util.Map;
/*
* Main class to show BrowseFragment with header and rows of videos
*/
public class BrowseFragment extends BrowseSupportFragment implements BrowseView {
private static final String TAG = BrowseFragment.class.getSimpleName();
private static final String SELECTED_HEADER_INDEX = "SelectedHeaderIndex";
private static final String SELECTED_ITEM_INDEX = "SelectedItemIndex";
private ArrayObjectAdapter mCategoryRowAdapter;
private BrowsePresenter mBrowsePresenter;
private Map<Integer, Category> mCategories;
private CategoryFragmentFactory mCategoryFragmentFactory;
private Handler mHandler;
private ProgressBarManager mProgressBarManager;
private boolean mIsFragmentCreated;
private int mRestoredHeaderIndex = -1;
private int mRestoredItemIndex = -1;
private boolean mFocusOnChildFragment;
private GestureDetector mGestureDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(null);
mRestoredHeaderIndex = savedInstanceState != null ? savedInstanceState.getInt(SELECTED_HEADER_INDEX, -1) : -1;
mRestoredItemIndex = savedInstanceState != null ? savedInstanceState.getInt(SELECTED_ITEM_INDEX, -1) : -1;
mIsFragmentCreated = true;
mCategories = new LinkedHashMap<>();
mHandler = new Handler();
mBrowsePresenter = BrowsePresenter.instance(getContext());
mBrowsePresenter.setView(this);
mProgressBarManager = new ProgressBarManager();
setupAdapter();
setupFragmentFactory();
setupUi();
enableMainFragmentScaling(false);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Store position in case activity is crashed
outState.putInt(SELECTED_HEADER_INDEX, getSelectedPosition());
// Not robust. Because tab content often changed after reloading.
outState.putInt(SELECTED_ITEM_INDEX, mCategoryFragmentFactory.getCurrentFragmentItemIndex());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = super.onCreateView(inflater, container, savedInstanceState);
mProgressBarManager.setRootView((ViewGroup) root);
return root;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupEventListeners();
prepareEntranceTransition();
mBrowsePresenter.onViewInitialized();
// Restore state after crash
selectCategory(mRestoredHeaderIndex);
mRestoredHeaderIndex = -1;
// Restore state after crash
selectItem(mRestoredItemIndex);
mRestoredItemIndex = -1;
}
private void setupEventListeners() {
getHeadersSupportFragment().setOnHeaderClickedListener(
(viewHolder, row) -> {
long headerId = row.getHeaderItem().getId();
int newPosition = indexOf(headerId);
if (getHeadersSupportFragment().getSelectedPosition() != newPosition) {
// touch screen support
getHeadersSupportFragment().setSelectedPosition(newPosition);
} else {
// update section when clicked or pressed
mBrowsePresenter.onCategoryFocused((int) headerId);
startHeadersTransition(false);
}
}
);
setOnSearchClickedListener(view -> SearchPresenter.instance(getActivity()).startSearch(null));
}
private int indexOf(long headerId) {
int index = 0;
for (Integer id : mCategories.keySet()) {
if (id == headerId) {
return index;
}
index++;
}
return 0;
}
private void setupAdapter() {
// Map category results from the database to ListRow objects.
// This Adapter is used to render the MainFragment sidebar labels.
mCategoryRowAdapter = new ArrayObjectAdapter(new ListRowPresenter());
setAdapter(mCategoryRowAdapter);
}
private void setupFragmentFactory() {
mCategoryFragmentFactory = new CategoryFragmentFactory(
(viewHolder, row) -> {
focusOnChildFragment();
mBrowsePresenter.onCategoryFocused(getSelectedHeaderId());
}
);
getMainFragmentRegistry().registerFragment(PageRow.class, mCategoryFragmentFactory);
}
private void setupUi() {
setTitle(getString(R.string.browse_title)); // Badge, when set, takes precedent over title
setHeadersState(HEADERS_ENABLED);
setHeadersTransitionOnBackEnabled(true);
int brandColorRes = Helpers.getThemeAttr(getActivity(), R.attr.brandColor);
int brandAccentColorRes = Helpers.getThemeAttr(getActivity(), R.attr.brandAccentColor);
int appLogoRes = Helpers.getThemeAttr(getActivity(), R.attr.appLogo);
// Top right corner logo
setBadgeDrawable(ContextCompat.getDrawable(getActivity(), appLogoRes));
// Set fastLane (or headers) background color
setBrandColor(ContextCompat.getColor(getActivity(), brandColorRes));
// Set search icon color.
setSearchAffordanceColor(ContextCompat.getColor(getActivity(), brandAccentColorRes));
setHeaderPresenterSelector(new PresenterSelector() {
@Override
public Presenter getPresenter(Object o) {
return new IconHeaderItemPresenter(getHeaderResId(o), getIconUrl(o));
}
});
}
private int getHeaderResId(Object o) {
if (o instanceof PageRow) {
return ((CategoryHeaderItem) ((PageRow) o).getHeaderItem()).getResId();
}
return -1;
}
private String getIconUrl(Object o) {
if (o instanceof PageRow) {
return ((CategoryHeaderItem) ((PageRow) o).getHeaderItem()).getIconUrl();
}
return null;
}
private int getSelectedHeaderId() {
if (getSelectedPosition() >= mCategoryRowAdapter.size()) {
return -1;
}
return (int) ((PageRow) mCategoryRowAdapter.get(getSelectedPosition())).getHeaderItem().getId();
}
public void updateErrorIfEmpty(ErrorFragmentData data) {
mHandler.postDelayed(() -> showErrorIfEmpty(data), 500); // need delay because header may be not updated
}
@Override
public void showError(ErrorFragmentData data) {
//replaceMainFragment(new ErrorDialogFragment(data));
showErrorIfEmpty(data);
}
private void showErrorIfEmpty(ErrorFragmentData data) {
if (mCategoryFragmentFactory.isEmpty()) {
replaceMainFragment(new ErrorDialogFragment(data));
}
}
private void replaceMainFragment(Fragment fragment) {
Object currentFragment = Helpers.getField(this,"mMainFragment");
if (currentFragment != null && fragment != null && currentFragment != fragment) {
Helpers.setField(this, "mMainFragment", fragment);
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.replace(R.id.scale_frame, fragment);
ft.commitAllowingStateLoss(); // FIX: "Can not perform this action after onSaveInstanceState"
}
}
@Override
public void addCategory(int index, Category category) {
if (category == null) {
return;
}
if (mCategories.get(category.getId()) == null) {
mCategories.put(category.getId(), category);
createHeader(index, category);
}
}
@Override
public void removeCategory(Category category) {
if (category == null) {
return;
}
mCategories.remove(category.getId());
removeHeader(category);
}
@Override
public void updateCategory(VideoGroup group) {
restoreMainFragment();
mCategoryFragmentFactory.updateCurrentFragment(group);
fixInvisibleSearchOrb();
}
@Override
public void updateCategory(SettingsGroup group) {
restoreMainFragment();
mCategoryFragmentFactory.updateCurrentFragment(group);
}
@Override
public void selectCategory(int index) {
if (index >= 0 && index < mCategoryRowAdapter.size()) {
setSelectedPosition(index);
mFocusOnChildFragment = true;
}
}
private void focusOnChildFragment() {
if (!Utils.checkActivity(getActivity())) {
return;
}
if (mFocusOnChildFragment) {
startHeadersTransition(false);
mFocusOnChildFragment = false;
}
}
@Override
public void selectItem(int index) {
if (index >= 0) {
mCategoryFragmentFactory.setCurrentFragmentItemIndex(index);
}
}
private void restoreMainFragment() {
Fragment currentFragment = mCategoryFragmentFactory.getCurrentFragment();
if (currentFragment != null) {
replaceMainFragment(currentFragment);
}
}
private void createHeader(int index, Category header) {
HeaderItem headerItem = new CategoryHeaderItem(header);
PageRow pageRow = new PageRow(headerItem);
if (index == -1) {
mCategoryRowAdapter.add(pageRow); // add to the end
} else {
mCategoryRowAdapter.add(index, pageRow);
}
}
private void removeHeader(Category header) {
Object foundHeader = null;
for (Object item : mCategoryRowAdapter.unmodifiableList()) {
if (((PageRow) item).getHeaderItem().getId() == header.getId()) {
foundHeader = item;
break;
}
}
if (foundHeader != null) {
mCategoryRowAdapter.remove(foundHeader);
}
}
@Override
public void clearCategory(Category category) {
mCategoryFragmentFactory.clearCurrentFragment();
}
@Override
public void onDestroy() {
super.onDestroy();
mBrowsePresenter.onViewDestroyed();
}
@Override
public void onResume() {
super.onResume();
if (!mIsFragmentCreated) {
mBrowsePresenter.onViewResumed();
}
mIsFragmentCreated = false;
}
/**
* Fix suddenly invisible search orb<br/>
* Could happen on topmost category when the page partially scrolled<br/>
* More info: {@link TitleHelper}
*/
private void fixInvisibleSearchOrb() {
if (isShowingTitle() && getTitleView() != null && getTitleView().getVisibility() != View.VISIBLE) {
getTitleView().setVisibility(View.VISIBLE);
}
}
@Override
public void showProgressBar(boolean show) {
if (show) {
mProgressBarManager.show();
} else {
mProgressBarManager.hide();
}
}
@Override
public boolean isProgressBarShowing() {
return mProgressBarManager.isShowing();
}
}