UNPKG

@gaddario98/react-native-core

Version:
446 lines (445 loc) 15.7 kB
'use strict';var compilerRuntime=require('react/compiler-runtime'),react=require('react'),Device=require('expo-device'),Notifications=require('expo-notifications'),reactNative=require('react-native');function _interopNamespaceDefault(e){var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var Device__namespace=/*#__PURE__*/_interopNamespaceDefault(Device);var Notifications__namespace=/*#__PURE__*/_interopNamespaceDefault(Notifications);function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); } function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = true, o = false; try { if (i = (t = t.call(r)).next, 0 === l) ; else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = true); } catch (r) { o = true, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = false, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function (t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = true, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i.return) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), true), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine(u), _regeneratorDefine(u, o, "Generator"), _regeneratorDefine(u, n, function () { return this; }), _regeneratorDefine(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function () { return { w: i, m: f }; })(); } function _regeneratorDefine(e, r, n, t) { var i = Object.defineProperty; _regeneratorDefine = function (e, r, n, t) { function o(r, n) { _regeneratorDefine(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine(e, r, n, t); } function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }Notifications__namespace.setNotificationHandler({ handleNotification: function () { var _handleNotification = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() { return _regenerator().w(function (_context) { while (1) switch (_context.n) { case 0: return _context.a(2, { shouldShowBanner: true, shouldShowList: true, shouldPlaySound: true, shouldSetBadge: true }); } }, _callee); })); function handleNotification() { return _handleNotification.apply(this, arguments); } return handleNotification; }() }); var useExpoNotifications = function useExpoNotifications(t0) { var $ = compilerRuntime.c(12); var updateToken = t0.updateToken, setNotification = t0.setNotification; var _useState = react.useState(""), _useState2 = _slicedToArray(_useState, 2), expoPushToken = _useState2[0], setExpoPushToken = _useState2[1]; var _useState3 = react.useState(null), _useState4 = _slicedToArray(_useState3, 2), permissionStatus = _useState4[0], setPermissionStatus = _useState4[1]; var resolvePermissionStatus = _temp; var t1; if ($[0] === Symbol["for"]("react.memo_cache_sentinel")) { t1 = /*#__PURE__*/function () { var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() { var existingPermissions, existingStatus, permissions_0, normalizedStatus, _yield$Notifications$, token, _t; return _regenerator().w(function (_context2) { while (1) switch (_context2.n) { case 0: if (!(reactNative.Platform.OS === "android")) { _context2.n = 1; break; } _context2.n = 1; return Notifications__namespace.setNotificationChannelAsync("default", { name: "default", importance: Notifications__namespace.AndroidImportance.MAX, vibrationPattern: [0, 250, 250, 250], lightColor: "#FF231F7C" }); case 1: if (Device__namespace.isDevice) { _context2.n = 2; break; } setPermissionStatus("undetermined"); return _context2.a(2); case 2: _context2.n = 3; return Notifications__namespace.getPermissionsAsync(); case 3: existingPermissions = _context2.v; existingStatus = resolvePermissionStatus(existingPermissions); if (!(existingStatus === "granted")) { _context2.n = 4; break; } _t = existingPermissions; _context2.n = 6; break; case 4: _context2.n = 5; return Notifications__namespace.requestPermissionsAsync(); case 5: _t = _context2.v; case 6: permissions_0 = _t; normalizedStatus = resolvePermissionStatus(permissions_0); setPermissionStatus(normalizedStatus); if (!(normalizedStatus !== "granted")) { _context2.n = 7; break; } return _context2.a(2); case 7: _context2.n = 8; return Notifications__namespace.getExpoPushTokenAsync({ projectId: process.env.EXPO_PUBLIC_EAS_PROJECT_ID }); case 8: _yield$Notifications$ = _context2.v; token = _yield$Notifications$.data; return _context2.a(2, token); } }, _callee2); })); return function t1() { return _ref.apply(this, arguments); }; }(); $[0] = t1; } else { t1 = $[0]; } var registerForPushNotificationsAsync = t1; var t2; if ($[1] !== updateToken) { t2 = /*#__PURE__*/function () { var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(token_0) { var error, _t2; return _regenerator().w(function (_context3) { while (1) switch (_context3.p = _context3.n) { case 0: _context3.p = 1; _context3.n = 2; return updateToken(token_0); case 2: _context3.n = 4; break; case 3: _context3.p = 3; _t2 = _context3.v; error = _t2; throw new Error("Failed to save token to Firestore " + error); case 4: return _context3.a(2); } }, _callee3, null, [[1, 3]]); })); return function t2(_x) { return _ref2.apply(this, arguments); }; }(); $[1] = updateToken; $[2] = t2; } else { t2 = $[2]; } var saveTokenToFirestore = t2; var t3; if ($[3] !== saveTokenToFirestore) { t3 = /*#__PURE__*/function () { var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4() { var token_1, error_0, _t3; return _regenerator().w(function (_context4) { while (1) switch (_context4.p = _context4.n) { case 0: if (!(reactNative.Platform.OS === "web")) { _context4.n = 1; break; } return _context4.a(2); case 1: _context4.p = 2; _context4.n = 3; return registerForPushNotificationsAsync(); case 3: token_1 = _context4.v; if (!token_1) { _context4.n = 4; break; } setExpoPushToken(token_1); _context4.n = 4; return saveTokenToFirestore(token_1); case 4: _context4.n = 6; break; case 5: _context4.p = 5; _t3 = _context4.v; error_0 = _t3; throw new Error("Failed to initialize notifications: " + error_0); case 6: return _context4.a(2); } }, _callee4, null, [[2, 5]]); })); return function t3() { return _ref3.apply(this, arguments); }; }(); $[3] = saveTokenToFirestore; $[4] = t3; } else { t3 = $[4]; } var initializeNotifications = t3; var sendLocalNotification = _temp2; var t4; var t5; if ($[5] !== setNotification) { t4 = function t4() { var notificationListener = Notifications__namespace.addNotificationReceivedListener(setNotification); var responseListener = Notifications__namespace.addNotificationResponseReceivedListener(_temp3); return function () { notificationListener.remove(); responseListener.remove(); }; }; t5 = [setNotification]; $[5] = setNotification; $[6] = t4; $[7] = t5; } else { t4 = $[6]; t5 = $[7]; } react.useEffect(t4, t5); var t6; if ($[8] !== expoPushToken || $[9] !== initializeNotifications || $[10] !== permissionStatus) { t6 = { expoPushToken: expoPushToken, permissionStatus: permissionStatus, initializeNotifications: initializeNotifications, sendLocalNotification: sendLocalNotification }; $[8] = expoPushToken; $[9] = initializeNotifications; $[10] = permissionStatus; $[11] = t6; } else { t6 = $[11]; } return t6; }; function _temp(permissions) { var granted = permissions.granted, canAskAgain = permissions.canAskAgain; if (granted) { return "granted"; } return canAskAgain ? "undetermined" : "denied"; } function _temp2(_x2, _x3, _x4) { return _temp4.apply(this, arguments); } function _temp4() { _temp4 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(title, body, t0) { var data, error_1, _t4; return _regenerator().w(function (_context5) { while (1) switch (_context5.p = _context5.n) { case 0: data = t0 === undefined ? {} : t0; _context5.p = 1; _context5.n = 2; return Notifications__namespace.scheduleNotificationAsync({ content: { title: title, body: body, data: data }, trigger: null }); case 2: _context5.n = 4; break; case 3: _context5.p = 3; _t4 = _context5.v; error_1 = _t4; throw new Error("Error sending local notification: " + error_1); case 4: return _context5.a(2); } }, _callee5, null, [[1, 3]]); })); return _temp4.apply(this, arguments); } function _temp3(_) {}exports.useExpoNotifications=useExpoNotifications;//# sourceMappingURL=index.js.map