forked from yuliskov/SmartTube
-
Notifications
You must be signed in to change notification settings - Fork 3
/
HeaderGridFragment.java
155 lines (129 loc) · 5.43 KB
/
HeaderGridFragment.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
package com.liskovsoft.smartyoutubetv2.tv.ui.browse.grid;
import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.leanback.widget.FocusHighlight;
import androidx.leanback.widget.OnItemViewClickedListener;
import androidx.leanback.widget.OnItemViewSelectedListener;
import androidx.leanback.widget.Presenter;
import androidx.leanback.widget.Row;
import androidx.leanback.widget.RowPresenter;
import androidx.leanback.widget.VerticalGridPresenter;
import com.liskovsoft.sharedutils.mylogger.Log;
import com.liskovsoft.smartyoutubetv2.common.app.models.data.Video;
import com.liskovsoft.smartyoutubetv2.common.app.models.data.VideoGroup;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.BrowsePresenter;
import com.liskovsoft.smartyoutubetv2.tv.adapter.VideoGroupObjectAdapter;
import com.liskovsoft.smartyoutubetv2.tv.ui.browse.VideoGroupFragment;
import com.liskovsoft.smartyoutubetv2.tv.ui.common.LeanbackActivity;
import com.liskovsoft.smartyoutubetv2.tv.ui.common.UriBackgroundManager;
import com.liskovsoft.smartyoutubetv2.tv.ui.mod.GridFragment;
import java.util.ArrayList;
import java.util.List;
public class HeaderGridFragment extends GridFragment implements VideoGroupFragment {
private static final String TAG = HeaderGridFragment.class.getSimpleName();
private static final int COLUMNS_NUM = 4;
private static final int ZOOM_FACTOR = FocusHighlight.ZOOM_FACTOR_SMALL;
private static final int CHECK_SCROLL_ITEMS_NUM = 15;
private VideoGroupObjectAdapter mGridAdapter;
private final List<VideoGroup> mPendingUpdates = new ArrayList<>();
private UriBackgroundManager mBackgroundManager;
private BrowsePresenter mMainPresenter;
private boolean mInvalidate;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMainPresenter = BrowsePresenter.instance(getContext());
mBackgroundManager = ((LeanbackActivity) getActivity()).getBackgroundManager();
setupAdapter();
setupEventListeners();
applyPendingUpdates();
if (getMainFragmentAdapter().getFragmentHost() != null) {
getMainFragmentAdapter().getFragmentHost().notifyDataReady(getMainFragmentAdapter());
}
}
private void setupEventListeners() {
setOnItemViewClickedListener(new ItemViewClickedListener());
setOnItemViewSelectedListener(new ItemViewSelectedListener());
}
private void applyPendingUpdates() {
for (VideoGroup group : mPendingUpdates) {
update(group);
}
mPendingUpdates.clear();
}
private void setupAdapter() {
VerticalGridPresenter presenter = new VerticalGridPresenter(ZOOM_FACTOR, false);
presenter.setNumberOfColumns(COLUMNS_NUM);
setGridPresenter(presenter);
if (mGridAdapter == null) {
mGridAdapter = new VideoGroupObjectAdapter();
setAdapter(mGridAdapter);
}
}
@Override
public void update(VideoGroup group) {
if (mGridAdapter == null) {
mPendingUpdates.add(group);
return;
}
if (mInvalidate) {
clear();
mInvalidate = false;
}
mGridAdapter.append(group);
}
@Override
public void invalidate() {
mInvalidate = true;
}
@Override
public void clear() {
if (mGridAdapter != null) {
mGridAdapter.clear();
}
}
@Override
public boolean isEmpty() {
if (mGridAdapter == null) {
return false;
}
return mGridAdapter.size() == 0;
}
private final class ItemViewClickedListener implements OnItemViewClickedListener {
@Override
public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
RowPresenter.ViewHolder rowViewHolder, Row row) {
if (item instanceof Video) {
if (getActivity() instanceof LeanbackActivity) {
boolean longClick = ((LeanbackActivity) getActivity()).isLongClick();
Log.d(TAG, "Is long click: " + longClick);
if (longClick) {
mMainPresenter.onVideoItemLongClicked((Video) item);
} else {
mMainPresenter.onVideoItemClicked((Video) item);
}
}
} else {
Toast.makeText(getActivity(), item.toString(), Toast.LENGTH_SHORT).show();
}
}
}
private final class ItemViewSelectedListener implements OnItemViewSelectedListener {
@Override
public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
RowPresenter.ViewHolder rowViewHolder, Row row) {
if (item instanceof Video) {
mBackgroundManager.setBackgroundFrom((Video) item);
checkScrollEnd((Video) item);
}
}
private void checkScrollEnd(Video item) {
int size = mGridAdapter.size();
int index = mGridAdapter.indexOf(item);
if (index > (size - CHECK_SCROLL_ITEMS_NUM)) {
mMainPresenter.onScrollEnd(mGridAdapter.getGroup());
}
}
}
}