Skip to content

Commit 64a455e

Browse files
hunterstichimhappi
authored andcommitted
[Typography] Update typography catalog demo
PiperOrigin-RevId: 663271084
1 parent 25083f4 commit 64a455e

File tree

2 files changed

+81
-36
lines changed

2 files changed

+81
-36
lines changed

catalog/java/io/material/catalog/font/FontMainDemoFragment.java

Lines changed: 78 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import android.content.Context;
2222
import android.content.res.TypedArray;
23+
import android.graphics.Typeface;
2324
import android.os.Build.VERSION;
2425
import android.os.Build.VERSION_CODES;
2526
import android.os.Bundle;
@@ -40,8 +41,8 @@
4041
import androidx.annotation.StyleRes;
4142
import androidx.core.view.ViewCompat;
4243
import androidx.core.widget.TextViewCompat;
44+
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
4345
import com.google.android.material.resources.TextAppearance;
44-
import com.google.android.material.snackbar.Snackbar;
4546
import io.material.catalog.feature.DemoFragment;
4647
import java.util.ArrayList;
4748
import java.util.List;
@@ -61,16 +62,18 @@ public View onCreateDemoView(
6162
recyclerView.addItemDecoration(
6263
new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
6364
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());
7174

72-
return windowInsetsCompat;
73-
});
75+
return windowInsetsCompat;
76+
});
7477

7578
return view;
7679
}
@@ -89,29 +92,54 @@ protected int getFontStyleNames() {
8992
return R.array.cat_font_style_names_array;
9093
}
9194

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+
}
95107
}
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";
103121
default:
104-
return fontFamily;
122+
// fall through
105123
}
124+
125+
return "Normal";
126+
}
127+
128+
@NonNull
129+
protected String convertFontFamilyToDescription(@Nullable String fontFamily, int textStyle) {
130+
return getFontFamilyName(fontFamily) + "/" + getTextStyleName(textStyle);
106131
}
107132

108133
@NonNull
109-
protected String maybeGetFontVariationSettingsDescription(
110-
@Nullable String fontVariationSettings) {
134+
protected String getFontVariationSettingsDescription(@Nullable String fontVariationSettings) {
111135
if (VERSION.SDK_INT < VERSION_CODES.O || fontVariationSettings == null) {
112-
return "";
136+
return "Unsupported";
113137
} else {
114-
return " " + fontVariationSettings;
138+
if (fontVariationSettings.isEmpty()) {
139+
return "None";
140+
} else {
141+
return fontVariationSettings;
142+
}
115143
}
116144
}
117145

@@ -166,8 +194,14 @@ private class FontStyleViewHolder extends ViewHolder {
166194
private final TextView descriptionView;
167195
private final ImageView infoView;
168196

197+
private String name;
198+
199+
@SuppressWarnings("RestrictTo")
200+
private TextAppearance textAppearance;
201+
169202
private String attributeName;
170203

204+
@SuppressWarnings("RestrictTo")
171205
public FontStyleViewHolder(ViewGroup parent) {
172206
super(
173207
LayoutInflater.from(parent.getContext())
@@ -179,32 +213,41 @@ public FontStyleViewHolder(ViewGroup parent) {
179213

180214
infoView.setOnClickListener(
181215
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)))
186228
.show());
187229
}
188230

231+
@SuppressWarnings("RestrictTo")
189232
public void bind(@StyleRes int style, String name, String attributeName) {
233+
this.name = name;
190234
this.attributeName = attributeName;
235+
this.textAppearance = new TextAppearance(itemView.getContext(), style);
191236

192237
nameView.setText(name);
193-
descriptionView.setText(createDescription(name, style));
238+
descriptionView.setText(createDescription(name, textAppearance));
194239

195240
TextViewCompat.setTextAppearance(nameView, style);
196241
}
197242

198243
@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) {
201245
return name
202246
+ " - "
203-
+ convertFontFamilyToDescription(textAppearance.fontFamily)
247+
+ convertFontFamilyToDescription(textAppearance.fontFamily, textAppearance.textStyle)
204248
+ " "
205249
+ pxToSp(textAppearance.getTextSize())
206-
+ "sp"
207-
+ maybeGetFontVariationSettingsDescription(textAppearance.fontVariationSettings);
250+
+ "sp";
208251
}
209252

210253
private int pxToSp(float px) {

catalog/java/io/material/catalog/font/res/values/strings.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
slightly wider and rounder, giving it greater clarity and making it more optimistic.
2626
</string>
2727

28-
<string name="cat_font_style_message">Use ?attr/%s in your layout.</string>
28+
<string name="cat_font_style_dialog_title" translatable="false">Style details</string>
29+
<string name="cat_font_style_dialog_message" translatable="false">Attribute:\n?attr/%1$s\n\nFont Family:\n%2$s\n\nText style:\n%3$s\n\nText size:\n%4$d\n\nVariation settings:\n%5$s
30+
</string>
2931

3032
</resources>

0 commit comments

Comments
 (0)