react-native-notificare-push
Version:
Notificare Push React Native module.
80 lines (79 loc) • 3.41 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.withNotificarePushAndroidNotification = void 0;
const image_utils_1 = require("@expo/image-utils");
const config_plugins_1 = require("expo/config-plugins");
const fs_1 = require("fs");
const path_1 = require("path");
const plugin_1 = require("react-native-notificare/lib/plugin");
const { Colors } = config_plugins_1.AndroidConfig;
const ICON_NAME = 'notificare_notification_small_icon';
const ACCENT_COLOR_NAME = 'notificare_notification_small_icon_color';
const withNotificationSmallIcon = (config, props) => {
return (0, config_plugins_1.withDangerousMod)(config, [
'android',
async (newConfig) => {
if (props?.android?.notification?.smallIcon) {
await createNotificationSmallIcon(props.android.notification.smallIcon, newConfig.modRequest.projectRoot);
}
return newConfig;
},
]);
};
const withNotificationSmallIconAccentColor = (config, props) => {
return (0, config_plugins_1.withAndroidColors)(config, (newConfig) => {
// Sets or removes color based on nullish factor
Colors.assignColorValue(newConfig.modResults, {
name: ACCENT_COLOR_NAME,
value: props?.android?.notification?.smallIconAccentColor ?? null,
});
return newConfig;
});
};
const withNotificationOptionsMetaData = (config, props) => {
const metaDataOptions = [
{
enabled: !!props?.android?.notification?.smallIcon,
metaData: {
$: {
'android:name': 're.notifica.push.notification_small_icon',
'android:resource': `@drawable/${ICON_NAME}`,
},
},
},
{
enabled: !!props?.android?.notification?.smallIconAccentColor,
metaData: {
$: {
'android:name': 're.notifica.push.notification_accent_color',
'android:resource': `@color/${ACCENT_COLOR_NAME}`,
},
},
},
];
return (0, plugin_1.processManifestMetaDataOptions)(config, metaDataOptions);
};
async function createNotificationSmallIcon(icon, projectRoot) {
for (const formatType in plugin_1.SMALL_ICON_FORMATS) {
const path = (0, path_1.resolve)(projectRoot, plugin_1.RESOURCE_ROOT_PATH, formatType);
if (!(0, fs_1.existsSync)(path)) {
(0, fs_1.mkdirSync)(path, { recursive: true });
}
const resizedIcon = (await (0, image_utils_1.generateImageAsync)({ projectRoot, cacheType: 'notificare-notification-icon' }, {
src: icon,
width: plugin_1.SMALL_ICON_FORMATS[formatType],
height: plugin_1.SMALL_ICON_FORMATS[formatType],
resizeMode: 'cover',
backgroundColor: 'transparent',
})).source;
const resizedIconDest = (0, path_1.resolve)(path, ICON_NAME + '.png');
(0, fs_1.writeFileSync)(resizedIconDest, resizedIcon);
}
}
const withNotificarePushAndroidNotification = (config, props) => {
config = withNotificationSmallIcon(config, props);
config = withNotificationSmallIconAccentColor(config, props);
config = withNotificationOptionsMetaData(config, props);
return config;
};
exports.withNotificarePushAndroidNotification = withNotificarePushAndroidNotification;
;