1import {
2  AndroidConfig,
3  ConfigPlugin,
4  withAndroidColors,
5  withAndroidStyles,
6} from '@expo/config-plugins';
7import { ExpoConfig } from '@expo/config-types';
8
9const { assignColorValue } = AndroidConfig.Colors;
10const { assignStylesValue, getAppThemeLightNoActionBarGroup } = AndroidConfig.Styles;
11
12const ANDROID_WINDOW_BACKGROUND = 'android:windowBackground';
13const WINDOW_BACKGROUND_COLOR = 'activityBackground';
14
15export const withAndroidRootViewBackgroundColor: ConfigPlugin = (config) => {
16  config = withRootViewBackgroundColorColors(config);
17  config = withRootViewBackgroundColorStyles(config);
18  return config;
19};
20
21export const withRootViewBackgroundColorColors: ConfigPlugin = (config) => {
22  return withAndroidColors(config, async (config) => {
23    config.modResults = assignColorValue(config.modResults, {
24      value: getRootViewBackgroundColor(config),
25      name: WINDOW_BACKGROUND_COLOR,
26    });
27    return config;
28  });
29};
30
31export const withRootViewBackgroundColorStyles: ConfigPlugin = (config) => {
32  return withAndroidStyles(config, async (config) => {
33    config.modResults = assignStylesValue(config.modResults, {
34      add: !!getRootViewBackgroundColor(config),
35      parent: getAppThemeLightNoActionBarGroup(),
36      name: ANDROID_WINDOW_BACKGROUND,
37      value: `@color/${WINDOW_BACKGROUND_COLOR}`,
38    });
39    return config;
40  });
41};
42
43export function getRootViewBackgroundColor(
44  config: Pick<ExpoConfig, 'android' | 'backgroundColor'>
45) {
46  return config.android?.backgroundColor || config.backgroundColor || null;
47}
48