-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
FragmentSlide.java
284 lines (244 loc) · 10.1 KB
/
FragmentSlide.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
/*
* MIT License
*
* Copyright (c) 2017 Jan Heinrich Reimer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.heinrichreimersoftware.materialintro.slide;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.ColorRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.StringRes;
import androidx.annotation.StyleRes;
import androidx.fragment.app.Fragment;
import androidx.appcompat.view.ContextThemeWrapper;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.heinrichreimersoftware.materialintro.app.ButtonCtaFragment;
import com.heinrichreimersoftware.materialintro.app.SlideFragment;
import com.heinrichreimersoftware.materialintro.view.parallax.ParallaxFragment;
public class FragmentSlide implements Slide, RestorableSlide, ButtonCtaSlide {
private Fragment fragment;
@ColorRes
private final int background;
@ColorRes
private final int backgroundDark;
private final boolean canGoForward;
private final boolean canGoBackward;
private CharSequence buttonCtaLabel = null;
@StringRes
private int buttonCtaLabelRes = 0;
private View.OnClickListener buttonCtaClickListener = null;
protected FragmentSlide(Builder builder) {
fragment = builder.fragment;
background = builder.background;
backgroundDark = builder.backgroundDark;
canGoForward = builder.canGoForward;
canGoBackward = builder.canGoBackward;
buttonCtaLabel = builder.buttonCtaLabel;
buttonCtaLabelRes = builder.buttonCtaLabelRes;
buttonCtaClickListener = builder.buttonCtaClickListener;
}
@Override
public Fragment getFragment() {
return fragment;
}
@Override
public void setFragment(Fragment fragment) {
this.fragment = fragment;
}
@Override
public int getBackground() {
return background;
}
@Override
public int getBackgroundDark() {
return backgroundDark;
}
@Override
public boolean canGoForward() {
if (getFragment() instanceof SlideFragment) {
return ((SlideFragment) getFragment()).canGoForward();
}
return canGoForward;
}
@Override
public boolean canGoBackward() {
if (getFragment() instanceof SlideFragment) {
return ((SlideFragment) getFragment()).canGoBackward();
}
return canGoBackward;
}
@Override
public View.OnClickListener getButtonCtaClickListener() {
if (getFragment() instanceof ButtonCtaFragment) {
return ((ButtonCtaFragment) getFragment()).getButtonCtaClickListener();
}
return buttonCtaClickListener;
}
@Override
public CharSequence getButtonCtaLabel() {
if (getFragment() instanceof ButtonCtaFragment) {
return ((ButtonCtaFragment) getFragment()).getButtonCtaLabel();
}
return buttonCtaLabel;
}
@Override
public int getButtonCtaLabelRes() {
if (getFragment() instanceof ButtonCtaFragment) {
return ((ButtonCtaFragment) getFragment()).getButtonCtaLabelRes();
}
return buttonCtaLabelRes;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FragmentSlide that = (FragmentSlide) o;
if (background != that.background) return false;
if (backgroundDark != that.backgroundDark) return false;
if (canGoForward != that.canGoForward) return false;
if (canGoBackward != that.canGoBackward) return false;
if (buttonCtaLabelRes != that.buttonCtaLabelRes) return false;
if (fragment != null ? !fragment.equals(that.fragment) : that.fragment != null)
return false;
if (buttonCtaLabel != null ? !buttonCtaLabel.equals(that.buttonCtaLabel) : that.buttonCtaLabel != null)
return false;
return buttonCtaClickListener != null ? buttonCtaClickListener.equals(that.buttonCtaClickListener) : that.buttonCtaClickListener == null;
}
@Override
public int hashCode() {
int result = fragment != null ? fragment.hashCode() : 0;
result = 31 * result + background;
result = 31 * result + backgroundDark;
result = 31 * result + (canGoForward ? 1 : 0);
result = 31 * result + (canGoBackward ? 1 : 0);
result = 31 * result + (buttonCtaLabel != null ? buttonCtaLabel.hashCode() : 0);
result = 31 * result + buttonCtaLabelRes;
result = 31 * result + (buttonCtaClickListener != null ? buttonCtaClickListener.hashCode() : 0);
return result;
}
public static class Builder {
private Fragment fragment;
@ColorRes
private int background;
@ColorRes
private int backgroundDark = 0;
private boolean canGoForward = true;
private boolean canGoBackward = true;
private CharSequence buttonCtaLabel = null;
@StringRes
private int buttonCtaLabelRes = 0;
private View.OnClickListener buttonCtaClickListener = null;
public Builder fragment(Fragment fragment) {
this.fragment = fragment;
return this;
}
public Builder fragment(@LayoutRes int layoutRes, @StyleRes int themeRes) {
this.fragment = FragmentSlideFragment.newInstance(layoutRes, themeRes);
return this;
}
public Builder fragment(@LayoutRes int layoutRes) {
this.fragment = FragmentSlideFragment.newInstance(layoutRes);
return this;
}
public Builder background(@ColorRes int background) {
this.background = background;
return this;
}
public Builder backgroundDark(@ColorRes int backgroundDark) {
this.backgroundDark = backgroundDark;
return this;
}
public Builder canGoForward(boolean canGoForward) {
this.canGoForward = canGoForward;
return this;
}
public Builder canGoBackward(boolean canGoBackward) {
this.canGoBackward = canGoBackward;
return this;
}
public Builder buttonCtaLabel(CharSequence buttonCtaLabel) {
this.buttonCtaLabel = buttonCtaLabel;
this.buttonCtaLabelRes = 0;
return this;
}
public Builder buttonCtaLabelHtml(String buttonCtaLabelHtml) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
this.buttonCtaLabel = Html.fromHtml(buttonCtaLabelHtml, Html.FROM_HTML_MODE_LEGACY);
} else {
//noinspection deprecation
this.buttonCtaLabel = Html.fromHtml(buttonCtaLabelHtml);
}
this.buttonCtaLabelRes = 0;
return this;
}
public Builder buttonCtaLabel(@StringRes int buttonCtaLabelRes) {
this.buttonCtaLabelRes = buttonCtaLabelRes;
this.buttonCtaLabel = null;
return this;
}
public Builder buttonCtaClickListener(View.OnClickListener buttonCtaClickListener) {
this.buttonCtaClickListener = buttonCtaClickListener;
return this;
}
public FragmentSlide build() {
if (background == 0 || fragment == null)
throw new IllegalArgumentException("You must set at least a fragment and background.");
return new FragmentSlide(this);
}
}
public static class FragmentSlideFragment extends ParallaxFragment {
private static final String ARGUMENT_LAYOUT_RES =
"com.heinrichreimersoftware.materialintro.SimpleFragment.ARGUMENT_LAYOUT_RES";
private static final String ARGUMENT_THEME_RES =
"com.heinrichreimersoftware.materialintro.SimpleFragment.ARGUMENT_THEME_RES";
public FragmentSlideFragment() {
}
public static FragmentSlideFragment newInstance(@LayoutRes int layoutRes, @StyleRes int themeRes) {
Bundle arguments = new Bundle();
arguments.putInt(ARGUMENT_LAYOUT_RES, layoutRes);
arguments.putInt(ARGUMENT_THEME_RES, themeRes);
FragmentSlideFragment fragment = new FragmentSlideFragment();
fragment.setArguments(arguments);
return fragment;
}
public static FragmentSlideFragment newInstance(@LayoutRes int layoutRes) {
return newInstance(layoutRes, 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int themeRes = getArguments().getInt(ARGUMENT_THEME_RES);
Context contextThemeWrapper;
if (themeRes != 0) {
contextThemeWrapper = new ContextThemeWrapper(getActivity(), themeRes);
} else {
contextThemeWrapper = getActivity();
}
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
return localInflater.inflate(getArguments().getInt(ARGUMENT_LAYOUT_RES), container, false);
}
}
}