UNPKG

react-native-notificare-geo

Version:
148 lines (147 loc) 5.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withNotificareGeoAndroid = 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 BEACONS_SUPPORT_FLAG = 'notificareBeaconsSupportEnabled'; const BEACONS_FOREGROUND_SERVICE_ICON_NAME = 'notificare_beacon_notification_icon'; const withBeaconsSupport = (config, props) => { if (props?.android?.beaconSupportEnabled === undefined) { return config; } const beaconsProp = { type: 'property', key: BEACONS_SUPPORT_FLAG, value: `${props.android.beaconSupportEnabled}`, }; return (0, config_plugins_1.withGradleProperties)(config, (newConfig) => { const index = newConfig.modResults.findIndex((item) => item.type === 'property' && item.key === BEACONS_SUPPORT_FLAG); if (index !== -1) { newConfig.modResults[index] = beaconsProp; } else { newConfig.modResults.push(beaconsProp); } return newConfig; }); }; const withForegroundServiceNotificationIcon = (config, props) => { return (0, config_plugins_1.withDangerousMod)(config, [ 'android', async (newConfig) => { if (props?.android?.beaconForegroundServiceSmallIcon) { await createForegroundServiceSmallIcon(props.android.beaconForegroundServiceSmallIcon, newConfig.modRequest.projectRoot); } return newConfig; }, ]); }; const withGeoMetaData = (config, props) => { const metaDataOptions = [ { enabled: !!props?.android?.monitoredRegionsLimit, metaData: { $: { 'android:name': 're.notifica.geo.monitored_regions_limit', 'android:value': `${props?.android?.monitoredRegionsLimit ?? 10}`, }, }, }, { enabled: !!props?.android?.beaconForegroundServiceEnabled, metaData: { $: { 'android:name': 're.notifica.geo.beacons.foreground_service_enabled', 'android:value': 'true', }, }, }, { enabled: !!props?.android?.beaconForegroundServiceSmallIcon, metaData: { $: { 'android:name': 're.notifica.geo.beacons.service_notification_small_icon', 'android:resource': `@drawable/${BEACONS_FOREGROUND_SERVICE_ICON_NAME}`, }, }, }, { enabled: !!props?.android?.beaconForegroundServiceTitle, metaData: { $: { 'android:name': 're.notifica.geo.beacons.service_notification_content_title', 'android:value': props?.android?.beaconForegroundServiceTitle, }, }, }, { enabled: !!props?.android?.beaconForegroundServiceMessage, metaData: { $: { 'android:name': 're.notifica.geo.beacons.service_notification_content_text', 'android:value': props?.android?.beaconForegroundServiceMessage, }, }, }, { enabled: !!props?.android?.beaconForegroundServiceShowProgress, metaData: { $: { 'android:name': 're.notifica.geo.beacons.service_notification_progress', 'android:value': 'true', }, }, }, ]; return (0, plugin_1.processManifestMetaDataOptions)(config, metaDataOptions); }; const withBeaconForegroundServiceIntent = (config, props) => { const intent = { enabled: !!props?.android?.beaconForegroundServiceEnabled, intentFilter: { action: [ { $: { 'android:name': 're.notifica.intent.action.BeaconNotificationOpened', }, }, ], category: [ { $: { 'android:name': 'android.intent.category.DEFAULT', }, }, ], }, }; return (0, plugin_1.processIntentFilter)(config, intent); }; async function createForegroundServiceSmallIcon(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-beacon-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, BEACONS_FOREGROUND_SERVICE_ICON_NAME + '.png'); (0, fs_1.writeFileSync)(resizedIconDest, resizedIcon); } } const withNotificareGeoAndroid = (config, props) => { config = withBeaconsSupport(config, props); config = withForegroundServiceNotificationIcon(config, props); config = withGeoMetaData(config, props); config = withBeaconForegroundServiceIntent(config, props); return config; }; exports.withNotificareGeoAndroid = withNotificareGeoAndroid;