20
20
21
21
import android .content .Context ;
22
22
import android .content .res .TypedArray ;
23
+ import android .graphics .Typeface ;
23
24
import android .os .Build .VERSION ;
24
25
import android .os .Build .VERSION_CODES ;
25
26
import android .os .Bundle ;
40
41
import androidx .annotation .StyleRes ;
41
42
import androidx .core .view .ViewCompat ;
42
43
import androidx .core .widget .TextViewCompat ;
44
+ import com .google .android .material .dialog .MaterialAlertDialogBuilder ;
43
45
import com .google .android .material .resources .TextAppearance ;
44
- import com .google .android .material .snackbar .Snackbar ;
45
46
import io .material .catalog .feature .DemoFragment ;
46
47
import java .util .ArrayList ;
47
48
import java .util .List ;
@@ -61,16 +62,18 @@ public View onCreateDemoView(
61
62
recyclerView .addItemDecoration (
62
63
new DividerItemDecoration (getContext (), DividerItemDecoration .VERTICAL ));
63
64
recyclerView .setAdapter (new FontStyleAdapter (getContext ()));
64
- ViewCompat .setOnApplyWindowInsetsListener (recyclerView , (view1 , windowInsetsCompat ) -> {
65
- recyclerView .setClipToPadding (windowInsetsCompat .getSystemWindowInsetBottom () == 0 );
66
- recyclerView .setPadding (
67
- recyclerView .getPaddingLeft (),
68
- recyclerView .getPaddingTop (),
69
- recyclerView .getPaddingRight (),
70
- windowInsetsCompat .getSystemWindowInsetBottom ());
65
+ ViewCompat .setOnApplyWindowInsetsListener (
66
+ recyclerView ,
67
+ (view1 , windowInsetsCompat ) -> {
68
+ recyclerView .setClipToPadding (windowInsetsCompat .getSystemWindowInsetBottom () == 0 );
69
+ recyclerView .setPadding (
70
+ recyclerView .getPaddingLeft (),
71
+ recyclerView .getPaddingTop (),
72
+ recyclerView .getPaddingRight (),
73
+ windowInsetsCompat .getSystemWindowInsetBottom ());
71
74
72
- return windowInsetsCompat ;
73
- });
75
+ return windowInsetsCompat ;
76
+ });
74
77
75
78
return view ;
76
79
}
@@ -89,29 +92,54 @@ protected int getFontStyleNames() {
89
92
return R .array .cat_font_style_names_array ;
90
93
}
91
94
92
- protected String convertFontFamilyToDescription (String fontFamily ) {
93
- if (fontFamily == null ) {
94
- return "Regular" ;
95
+ @ SuppressWarnings ("StringCaseLocaleUsage" )
96
+ @ NonNull
97
+ protected String getFontFamilyName (@ Nullable String fontFamily ) {
98
+ if (fontFamily != null ) {
99
+ switch (fontFamily .toLowerCase ()) {
100
+ case "sans-serif-light" :
101
+ return "Light" ;
102
+ case "sans-serif-medium" :
103
+ return "Medium" ;
104
+ default :
105
+ // fall through
106
+ }
95
107
}
96
- switch (fontFamily .toLowerCase ()) {
97
- case "sans-serif-light" :
98
- return "Light" ;
99
- case "sans-serif" :
100
- return "Regular" ;
101
- case "sans-serif-medium" :
102
- return "Medium" ;
108
+
109
+ return "Regular" ;
110
+ }
111
+
112
+ @ NonNull
113
+ protected String getTextStyleName (int textStyle ) {
114
+ switch (textStyle ) {
115
+ case Typeface .ITALIC :
116
+ return "Italic" ;
117
+ case Typeface .BOLD :
118
+ return "Bold" ;
119
+ case Typeface .BOLD_ITALIC :
120
+ return "Bold-Italic" ;
103
121
default :
104
- return fontFamily ;
122
+ // fall through
105
123
}
124
+
125
+ return "Normal" ;
126
+ }
127
+
128
+ @ NonNull
129
+ protected String convertFontFamilyToDescription (@ Nullable String fontFamily , int textStyle ) {
130
+ return getFontFamilyName (fontFamily ) + "/" + getTextStyleName (textStyle );
106
131
}
107
132
108
133
@ NonNull
109
- protected String maybeGetFontVariationSettingsDescription (
110
- @ Nullable String fontVariationSettings ) {
134
+ protected String getFontVariationSettingsDescription (@ Nullable String fontVariationSettings ) {
111
135
if (VERSION .SDK_INT < VERSION_CODES .O || fontVariationSettings == null ) {
112
- return "" ;
136
+ return "Unsupported " ;
113
137
} else {
114
- return " " + fontVariationSettings ;
138
+ if (fontVariationSettings .isEmpty ()) {
139
+ return "None" ;
140
+ } else {
141
+ return fontVariationSettings ;
142
+ }
115
143
}
116
144
}
117
145
@@ -166,8 +194,14 @@ private class FontStyleViewHolder extends ViewHolder {
166
194
private final TextView descriptionView ;
167
195
private final ImageView infoView ;
168
196
197
+ private String name ;
198
+
199
+ @ SuppressWarnings ("RestrictTo" )
200
+ private TextAppearance textAppearance ;
201
+
169
202
private String attributeName ;
170
203
204
+ @ SuppressWarnings ("RestrictTo" )
171
205
public FontStyleViewHolder (ViewGroup parent ) {
172
206
super (
173
207
LayoutInflater .from (parent .getContext ())
@@ -179,32 +213,41 @@ public FontStyleViewHolder(ViewGroup parent) {
179
213
180
214
infoView .setOnClickListener (
181
215
view ->
182
- Snackbar .make (
183
- view ,
184
- view .getContext ().getString (R .string .cat_font_style_message , attributeName ),
185
- Snackbar .LENGTH_LONG )
216
+ new MaterialAlertDialogBuilder (infoView .getContext ())
217
+ .setTitle (name )
218
+ .setMessage (
219
+ view .getContext ()
220
+ .getString (
221
+ R .string .cat_font_style_dialog_message ,
222
+ /* attribute= */ attributeName ,
223
+ /*font family=*/ getFontFamilyName (textAppearance .fontFamily ),
224
+ /*text style=*/ getTextStyleName (textAppearance .textStyle ),
225
+ /*text size=*/ pxToSp (textAppearance .getTextSize ()),
226
+ /*variation settings=*/ getFontVariationSettingsDescription (
227
+ textAppearance .fontVariationSettings )))
186
228
.show ());
187
229
}
188
230
231
+ @ SuppressWarnings ("RestrictTo" )
189
232
public void bind (@ StyleRes int style , String name , String attributeName ) {
233
+ this .name = name ;
190
234
this .attributeName = attributeName ;
235
+ this .textAppearance = new TextAppearance (itemView .getContext (), style );
191
236
192
237
nameView .setText (name );
193
- descriptionView .setText (createDescription (name , style ));
238
+ descriptionView .setText (createDescription (name , textAppearance ));
194
239
195
240
TextViewCompat .setTextAppearance (nameView , style );
196
241
}
197
242
198
243
@ SuppressWarnings ("RestrictTo" )
199
- private String createDescription (String name , @ StyleRes int style ) {
200
- TextAppearance textAppearance = new TextAppearance (itemView .getContext (), style );
244
+ private String createDescription (String name , TextAppearance textAppearance ) {
201
245
return name
202
246
+ " - "
203
- + convertFontFamilyToDescription (textAppearance .fontFamily )
247
+ + convertFontFamilyToDescription (textAppearance .fontFamily , textAppearance . textStyle )
204
248
+ " "
205
249
+ pxToSp (textAppearance .getTextSize ())
206
- + "sp"
207
- + maybeGetFontVariationSettingsDescription (textAppearance .fontVariationSettings );
250
+ + "sp" ;
208
251
}
209
252
210
253
private int pxToSp (float px ) {
0 commit comments