-
Notifications
You must be signed in to change notification settings - Fork 523
Expand file tree
/
Copy pathMaterialShowcaseSequence.java
More file actions
209 lines (161 loc) · 6.5 KB
/
MaterialShowcaseSequence.java
File metadata and controls
209 lines (161 loc) · 6.5 KB
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
package uk.co.deanwild.materialshowcaseview;
import android.app.Activity;
import android.view.View;
import java.util.LinkedList;
import java.util.Queue;
public class MaterialShowcaseSequence implements IDetachedListener {
PrefsManager mPrefsManager;
Queue<MaterialShowcaseView> mShowcaseQueue;
private boolean mSingleUse = false;
Activity mActivity;
private ShowcaseConfig mConfig;
private int mSequencePosition = 0;
private OnSequenceItemShownListener mOnItemShownListener = null;
private OnSequenceItemDismissedListener mOnItemDismissedListener = null;
private MaterialShowcaseView mCurrentShownShowcase;
public MaterialShowcaseSequence(Activity activity) {
mActivity = activity;
mShowcaseQueue = new LinkedList<>();
}
public MaterialShowcaseSequence(Activity activity, String sequenceID) {
this(activity);
this.singleUse(sequenceID);
}
public MaterialShowcaseSequence addSequenceItem(int contentResId, int dismissTextResId) {
addSequenceItem(mActivity.getString(contentResId), mActivity.getString(dismissTextResId));
return this;
}
public MaterialShowcaseSequence addSequenceItem(String content, String dismissText) {
addSequenceItem(null, content, dismissText);
return this;
}
public MaterialShowcaseSequence addSequenceItem(int targetViewId, int contentResId, int dismissTextResId) {
addSequenceItem(mActivity.findViewById(targetViewId), mActivity.getString(contentResId), mActivity.getString(dismissTextResId));
return this;
}
public MaterialShowcaseSequence addSequenceItem(int targetViewId, int titleResId, int contentResId, int dismissTextResId) {
addSequenceItem(mActivity.findViewById(targetViewId), mActivity.getString(titleResId),
mActivity.getString(contentResId), mActivity.getString(dismissTextResId));
return this;
}
public MaterialShowcaseSequence addSequenceItem(View targetView, String content, String dismissText) {
addSequenceItem(targetView, "", content, dismissText);
return this;
}
public MaterialShowcaseSequence addSequenceItem(View targetView, String title, String content, String dismissText) {
MaterialShowcaseView.Builder builder = new MaterialShowcaseView.Builder(mActivity)
.setTitleText(title)
.setDismissText(dismissText)
.setContentText(content);
if (targetView != null)
builder.setTarget(targetView);
else
builder.withoutShape();
MaterialShowcaseView sequenceItem = builder.build();
if (mConfig != null) {
sequenceItem.setConfig(mConfig);
}
mShowcaseQueue.add(sequenceItem);
return this;
}
public MaterialShowcaseSequence addSequenceItem(MaterialShowcaseView sequenceItem) {
mShowcaseQueue.add(sequenceItem);
return this;
}
public MaterialShowcaseSequence singleUse(String sequenceID) {
mSingleUse = true;
mPrefsManager = new PrefsManager(mActivity, sequenceID);
return this;
}
public void setOnItemShownListener(OnSequenceItemShownListener listener) {
this.mOnItemShownListener = listener;
}
public void setOnItemDismissedListener(OnSequenceItemDismissedListener listener) {
this.mOnItemDismissedListener = listener;
}
public boolean hasFired() {
return mPrefsManager.getSequenceStatus() == PrefsManager.SEQUENCE_FINISHED;
}
public void start() {
/**
* Check if we've already shot our bolt and bail out if so *
*/
if (mSingleUse) {
if (hasFired()) {
return;
}
/**
* See if we have started this sequence before, if so then skip to the point we reached before
* instead of showing the user everything from the start
*/
mSequencePosition = mPrefsManager.getSequenceStatus();
if (mSequencePosition > 0) {
for (int i = 0; i < mSequencePosition; i++) {
mShowcaseQueue.poll();
}
}
}
// do start
if (mShowcaseQueue.size() > 0)
showNextItem();
}
private void showNextItem() {
if (mShowcaseQueue.size() > 0 && !mActivity.isFinishing()) {
mCurrentShownShowcase = mShowcaseQueue.remove();
mCurrentShownShowcase.setDetachedListener(this);
mCurrentShownShowcase.show(mActivity);
if (mOnItemShownListener != null) {
mOnItemShownListener.onShow(mCurrentShownShowcase, mSequencePosition);
}
} else {
/**
* We've reached the end of the sequence, save the fired state
*/
if (mSingleUse) {
mPrefsManager.setFired();
}
}
}
// Note: This is not meant to be a pause method, it will act like if user has finished this sequence
public void cancel() {
/**
* Act like we've reached the end of the sequence
*/
while (mShowcaseQueue.poll() != null)
mSequencePosition++;
if (mPrefsManager != null)
mPrefsManager.setSequenceStatus(mSequencePosition);
if (mCurrentShownShowcase != null)
mCurrentShownShowcase.hide();
}
@Override
public void onShowcaseDetached(MaterialShowcaseView showcaseView, boolean wasDismissed) {
mCurrentShownShowcase = null;
showcaseView.setDetachedListener(null);
/**
* We're only interested if the showcase was purposefully dismissed
*/
if (wasDismissed) {
if (mOnItemDismissedListener != null) {
mOnItemDismissedListener.onDismiss(showcaseView, mSequencePosition);
}
/**
* If so, update the prefsManager so we can potentially resume this sequence in the future
*/
if (mPrefsManager != null) {
mSequencePosition++;
mPrefsManager.setSequenceStatus(mSequencePosition);
}
showNextItem();
}
}
public void setConfig(ShowcaseConfig config) {
this.mConfig = config;
}
public interface OnSequenceItemShownListener {
void onShow(MaterialShowcaseView itemView, int position);
}
public interface OnSequenceItemDismissedListener {
void onDismiss(MaterialShowcaseView itemView, int position);
}
}