-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentViewPagerManager.java
302 lines (241 loc) · 8.4 KB
/
ContentViewPagerManager.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
package ...
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
...
import android.app.Activity;
import android.os.Looper;
import android.support.v4.view.ViewPager;
import android.view.View;
public class ContentViewPagerManager {
public enum SlideViewDirection {
UNDEFINED,
// = date decrement
SLIDE_RIGHT,
// = date increment
SLIDE_LEFT
}
// Must be odd number (side pages + 1 middle page)
public static final int PAGE_COUNT = 5;
public static final int MIDDLE_PAGE_INDEX = PAGE_COUNT / 2;
private final int mTagKey;
private final int mPageResourceId;
private volatile boolean mPageIsIdle;
private volatile boolean mPageIsProcessing;
private final ThreadPoolExecutor mExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
private final ContentViewPager mViewPager;
private volatile ContentViewPagerAdapter mAdapter;
private final ContentViewPagerWaitDialog mWaitDialog;
private ViewPager.OnPageChangeListener mPageChangeListener;
private final Activity mActivity;
private final MeApplication mApplication;
public ContentViewPagerManager(Activity activity, ContentViewPager viewPager, int pageResourceId, int tagKey) {
mActivity = activity;
mViewPager = viewPager;
mPageResourceId = pageResourceId;
mTagKey = tagKey;
mWaitDialog = new ContentViewPagerWaitDialog(activity);
mApplication = MeApplication.getInstance();
}
public void initViewPager(boolean async) {
mAdapter = new ContentViewPagerAdapter();
mViewPager.setAdapter(mAdapter);
mPageIsIdle = true;
addPagesSync();
mViewPager.setAllowSwipe(true);
// set some bigger limit to prevent automatic calling destroyItem() in adapter,
// we want control it thru notifyDataSetChanged()
mViewPager.setOffscreenPageLimit(PAGE_COUNT);
// prevent calling onPageSelected on setCurrentItem
mViewPager.setOnPageChangeListener(null);
mViewPager.setCurrentItem(MIDDLE_PAGE_INDEX, false);
// TODO: fix case, when we quickly slide forward and back at once and can reach last page
mPageChangeListener = new ViewPager.OnPageChangeListener() {
private SlideViewDirection mDirection;
@Override
public void onPageSelected(int position) {
if (mPageIsIdle) {
return;
}
// set current timestamp
ArrayList<DateTime> timestamps = MeApplication.getInstance().getPageTimestamps();
DateTime dateTime = getNextTimestamp();
System.err.println("onPageSelected pos " + position + " ts " + dateTime);
mApplication.setTimestamp(dateTime, mDirection);
DateTime dt;
if (mDirection == SlideViewDirection.SLIDE_RIGHT) {
dt = timestamps.get(0);
} else {
dt = timestamps.get(timestamps.size() - 1);
}
// normal case
if ((position > 0 && mDirection == SlideViewDirection.SLIDE_RIGHT)
|| (position < timestamps.size() - 1 && mDirection == SlideViewDirection.SLIDE_LEFT)) {
addNextPageAsync(mDirection, dt);
}
// we slide too fast or period limits reached
else if ((position == 0 && mDirection == SlideViewDirection.SLIDE_RIGHT)
|| (position == timestamps.size() - 1 && mDirection == SlideViewDirection.SLIDE_LEFT)) {
mWaitDialog.show();
int count = MIDDLE_PAGE_INDEX;
if (mPageIsProcessing) {
--count;
}
for (int i = 0; i < count; i++) {
System.out.println("onPage second page");
addNextPageAsync(mDirection, dt);
}
}
}
// called after onPageScrollStateChanged SCROLL_STATE_DRAGGING
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
mDirection = (mViewPager.getCurrentItem() == position) ? SlideViewDirection.SLIDE_LEFT
: SlideViewDirection.SLIDE_RIGHT;
}
@Override
public void onPageScrollStateChanged(int state) {
System.err.println("onPageScrollStateChanged STATE " + state);
switch (state) {
case ViewPager.SCROLL_STATE_IDLE:
mPageIsIdle = true;
notifyDataSetChanged();
break;
case ViewPager.SCROLL_STATE_DRAGGING:
mPageIsIdle = false;
break;
case ViewPager.SCROLL_STATE_SETTLING:
ContentLayout contentLayout = (ContentLayout) WrapperLayout.getCurrentLayout();
contentLayout.scrollTo(0, 0);
break;
default:
break;
}
}
private DateTime getNextTimestamp() {
ArrayList<DateTime> timestamps = MeApplication.getInstance().getPageTimestamps();
DateTime dateTime = MeApplication.getInstance().getTimestamp();
int index = -1;
for (int i = 0; i < timestamps.size(); i++) {
if (dateTime.equals(timestamps.get(i))) {
index = i;
break;
}
}
System.err
.println("onPage getNextTimestamp curr " + dateTime + " arr " + timestamps + " index " + index);
if (index == 0 || index == timestamps.size() - 1) {
return dateTime;
}
index = (mDirection == SlideViewDirection.SLIDE_RIGHT) ? --index : ++index;
return timestamps.get(index);
}
};
mViewPager.setOnPageChangeListener(mPageChangeListener);
}
public synchronized void addNextPage(SlideViewDirection direction, final DateTime dateTime) {
ContentLayout contentLayout = (ContentLayout) WrapperLayout.getCurrentLayout();
DateTime dt = contentLayout.getNextTimestamp(direction, dateTime);
if (dt == null || !dt.isInLimits()) {
return;
}
createNextPage(direction);
}
public void addNextPageAsync(final SlideViewDirection direction, final DateTime dateTime) {
mExecutor.execute(new Runnable() {
@Override
public void run() {
mPageIsProcessing = true;
System.out
.println("onPage addNextPageAsync " + dateTime + " thread " + Thread.currentThread().getName());
addNextPage(direction, dateTime);
mPageIsProcessing = false;
if (mExecutor.getQueue().size() == 0) {
mWaitDialog.dismiss();
}
}
});
}
public void notifyDataSetChanged() {
if ((isMainThread() && mPageIsProcessing) || (!isMainThread() && !mPageIsIdle)
|| mExecutor.getQueue().size() > 0) {
return;
}
System.out.println("onPage notifyDataSetChanged thread " + Thread.currentThread().getName());
if (isMainThread()) {
mAdapter.notifyDataSetChanged();
mWaitDialog.dismiss();
} else {
Callable<Void> callable = new Callable<Void>() {
@Override
public Void call() {
mAdapter.notifyDataSetChanged();
return null;
}
};
FutureTask<Void> task = new FutureTask<Void>(callable);
mActivity.runOnUiThread(task);
try {
// wait until runOnUiThread returns and block next task beginning
task.get();
} catch (InterruptedException e1) {
e1.printStackTrace();
} catch (ExecutionException e1) {
e1.printStackTrace();
}
}
}
public ContentViewPagerAdapter getAdapter() {
return mAdapter;
}
public ContentViewPager getViewPager() {
return mViewPager;
}
public void setCurrentItem(int item, boolean smoothScroll) {
// reset listener to prevent calling onPageSelected
mViewPager.setOnPageChangeListener(null);
mViewPager.setCurrentItem(item, smoothScroll);
mViewPager.setOnPageChangeListener(mPageChangeListener);
}
public static boolean isMainThread() {
return Thread.currentThread().equals(Looper.getMainLooper().getThread());
}
private void addPagesSync() {
System.err.println("addPagesSync " + Thread.currentThread().getName());
for (int i = 0; i < PAGE_COUNT; i++) {
View view = inflatePage();
view.setTag(mTagKey, i);
mAdapter.getViews().add(view);
mApplication.sendLayoutDidChange(view);
}
notifyDataSetChanged();
}
private void createNextPage(SlideViewDirection direction) {
mApplication.setSlideViewDirection(direction);
switch (direction) {
case SLIDE_RIGHT:
mAdapter.removeView(mAdapter.getCount() - 1);
mAdapter.addView(inflatePage(), 0);
mAdapter.getView(0).setTag(R.id.fragment_tag, -1);
mApplication.sendLayoutDidChange(mAdapter.getView(0));
break;
case SLIDE_LEFT:
mAdapter.removeView(0);
mAdapter.addView(inflatePage());
int lastIndex = mAdapter.getCount() - 1;
mAdapter.getView(lastIndex).setTag(R.id.fragment_tag, lastIndex + 1);
mApplication.sendLayoutDidChange(mAdapter.getView(lastIndex));
break;
default:
break;
}
}
private View inflatePage() {
return mActivity.getLayoutInflater().inflate(mPageResourceId, mViewPager, false);
}
}