Repository navigation
Expand file tree
/
Copy pathwithAndroidModernAlertDialog.cjs
More file actions
166 lines (154 loc) · 5.71 KB
/
Copy pathwithAndroidModernAlertDialog.cjs
File metadata and controls
166 lines (154 loc) · 5.71 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
const fs = require("node:fs");
const path = require("node:path");
const themeVariables = require("../generated-uniwind-default-theme-variables.json");
const {
AndroidConfig,
withAndroidColors,
withAndroidColorsNight,
withAndroidStyles,
withDangerousMod,
} = require("expo/config-plugins");
// Preserve AppCompat's default 16dp dialog inset.
const DIALOG_BACKGROUND_DRAWABLE = `<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="16dp"
android:insetTop="16dp"
android:insetRight="16dp"
android:insetBottom="16dp">
<shape android:shape="rectangle">
<solid android:color="@color/alert_dialog_background" />
<corners android:radius="24dp" />
</shape>
</inset>
`;
const colorsFor = (appearance) => {
const variables = themeVariables[appearance];
return {
background: variables["--color-card"],
text: variables["--color-foreground"],
secondaryText: variables["--color-foreground-secondary"],
buttonText: variables["--color-primary-text"],
};
};
const COLORS = { light: colorsFor("light"), night: colorsFor("dark") };
function assignStyleItem(style, name, value) {
style.item = style.item ?? [];
const existing = style.item.find((item) => item.$?.name === name);
if (existing) {
existing._ = value;
} else {
style.item.push({ _: value, $: { name } });
}
}
function withAlertDialogStyles(config) {
return withAndroidStyles(config, (config) => {
const resources = config.modResults.resources;
resources.style = resources.style ?? [];
const appTheme = resources.style.find((style) => style.$?.name === "AppTheme");
if (appTheme) {
// React Native uses the AppCompat attr; platform dialogs need the framework attr too.
assignStyleItem(appTheme, "alertDialogTheme", "@style/AppAlertDialog");
assignStyleItem(appTheme, "android:alertDialogTheme", "@style/AppAlertDialog");
}
resources.style = resources.style.filter(
(style) =>
!["AppAlertDialog", "AppAlertDialog.Title", "AppAlertDialog.Button"].includes(
style.$?.name,
),
);
resources.style.push(
{
$: { name: "AppAlertDialog", parent: "ThemeOverlay.AppCompat.Dialog.Alert" },
item: [
{ _: "@drawable/alert_dialog_background", $: { name: "android:windowBackground" } },
{ _: "@color/alert_dialog_secondary_text", $: { name: "android:textColorPrimary" } },
{ _: "@color/alert_dialog_secondary_text", $: { name: "android:textColorSecondary" } },
// Theme fontFamily covers text views without their own font, including the message body.
{ _: "@font/xml_dm_sans_regular", $: { name: "android:fontFamily" } },
// AppCompat titles use android:windowTitleStyle; there is no unprefixed equivalent.
{ _: "@style/AppAlertDialog.Title", $: { name: "android:windowTitleStyle" } },
{ _: "@style/AppAlertDialog.Button", $: { name: "buttonBarPositiveButtonStyle" } },
{ _: "@style/AppAlertDialog.Button", $: { name: "buttonBarNegativeButtonStyle" } },
{ _: "@style/AppAlertDialog.Button", $: { name: "buttonBarNeutralButtonStyle" } },
],
},
{
$: { name: "AppAlertDialog.Title", parent: "RtlOverlay.DialogWindowTitle.AppCompat" },
item: [
{ _: "@font/dm_sans_500medium", $: { name: "android:fontFamily" } },
{ _: "18sp", $: { name: "android:textSize" } },
{ _: "@color/alert_dialog_text", $: { name: "android:textColor" } },
],
},
{
$: {
name: "AppAlertDialog.Button",
parent: "Widget.AppCompat.Button.ButtonBar.AlertDialog",
},
item: [
// Override AppCompat's hardcoded sans-serif-medium button font.
{ _: "@font/dm_sans_500medium", $: { name: "android:fontFamily" } },
{ _: "@color/alert_dialog_button_text", $: { name: "android:textColor" } },
{ _: "false", $: { name: "android:textAllCaps" } },
{ _: "0", $: { name: "android:letterSpacing" } },
],
},
);
return config;
});
}
function assignColors(colorsResource, palette) {
let result = colorsResource;
result = AndroidConfig.Colors.assignColorValue(result, {
name: "alert_dialog_background",
value: palette.background,
});
result = AndroidConfig.Colors.assignColorValue(result, {
name: "alert_dialog_text",
value: palette.text,
});
result = AndroidConfig.Colors.assignColorValue(result, {
name: "alert_dialog_secondary_text",
value: palette.secondaryText,
});
result = AndroidConfig.Colors.assignColorValue(result, {
name: "alert_dialog_button_text",
value: palette.buttonText,
});
return result;
}
function withAlertDialogColors(config) {
config = withAndroidColors(config, (config) => {
config.modResults = assignColors(config.modResults, COLORS.light);
return config;
});
config = withAndroidColorsNight(config, (config) => {
config.modResults = assignColors(config.modResults, COLORS.night);
return config;
});
return config;
}
function withAlertDialogBackgroundDrawable(config) {
return withDangerousMod(config, [
"android",
async (config) => {
const drawableDir = path.join(
config.modRequest.platformProjectRoot,
"app",
"src",
"main",
"res",
"drawable",
);
fs.mkdirSync(drawableDir, { recursive: true });
fs.writeFileSync(
path.join(drawableDir, "alert_dialog_background.xml"),
DIALOG_BACKGROUND_DRAWABLE,
);
return config;
},
]);
}
module.exports = function withAndroidModernAlertDialog(config) {
return withAlertDialogBackgroundDrawable(withAlertDialogColors(withAlertDialogStyles(config)));
};