diff --git a/package.json b/package.json index 7f02c99..0164626 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-screenshot-prevent", - "version": "1.1.9", + "version": "1.1.10", "description": "This fork contains fully working blank screenshot on IOS13+ including screen recording", "main": "lib/module/index", "module": "lib/module/index", @@ -127,6 +127,6 @@ "codegenConfig": { "name": "RNScreenshotPreventSpec", "type": "modules", - "jsSrcsDir": "src" + "jsSrcsDir": "lib" } } diff --git a/src/index.ts b/src/index.ts index 987821b..4bc23e7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,15 @@ import { NativeModules, NativeEventEmitter, Platform } from 'react-native'; import { useEffect } from 'react'; -type FN = (resp: any) => void +type FN = (resp: any) => void; type Return = { - readonly remove: () => void -} + readonly remove: () => void; +}; let addListen: any, RNScreenshotPrevent: any; -if(Platform.OS !== "web") { +if (Platform.OS !== 'web') { const { RNScreenshotPrevent: RNScreenshotPreventNative } = NativeModules; - RNScreenshotPrevent = RNScreenshotPreventNative + RNScreenshotPrevent = RNScreenshotPreventNative; const eventEmitter = new NativeEventEmitter(RNScreenshotPrevent); /** @@ -18,67 +18,114 @@ if(Platform.OS !== "web") { * @returns {function} unsubscribe fn */ addListen = (fn: FN): Return => { - if(typeof(fn) !== 'function') { - console.error('RNScreenshotPrevent: addListener requires valid callback function'); + if (typeof fn !== 'function') { + console.error( + 'RNScreenshotPrevent: addListener requires valid callback function' + ); return { remove: (): void => { - console.error("RNScreenshotPrevent: remove not work because addListener requires valid callback function"); - } + console.error( + 'RNScreenshotPrevent: remove not work because addListener requires valid callback function' + ); + }, }; } - return eventEmitter.addListener("userDidTakeScreenshot", fn); - } + return eventEmitter.addListener('userDidTakeScreenshot', fn); + }; } else { RNScreenshotPrevent = { enabled: (enabled: boolean): void => { - console.warn("RNScreenshotPrevent: enabled not work in web. value: "+enabled); + console.warn( + 'RNScreenshotPrevent: enabled not work in web. value: ' + + enabled + ); }, enableSecureView: (): void => { - console.warn("RNScreenshotPrevent: enableSecureView not work in web"); + console.warn( + 'RNScreenshotPrevent: enableSecureView not work in web' + ); }, disableSecureView: (): void => { - console.warn("RNScreenshotPrevent: disableSecureView not work in web"); - } - } + console.warn( + 'RNScreenshotPrevent: disableSecureView not work in web' + ); + }, + }; addListen = (fn: FN): Return => { - if(typeof(fn) !== 'function') { - console.error('RNScreenshotPrevent: addListener requires valid callback function'); + if (typeof fn !== 'function') { + console.error( + 'RNScreenshotPrevent: addListener requires valid callback function' + ); return { remove: (): void => { - console.error("RNScreenshotPrevent: remove not work because addListener requires valid callback function"); - } + console.error( + 'RNScreenshotPrevent: remove not work because addListener requires valid callback function' + ); + }, }; } - console.warn("RNScreenshotPrevent: addListener not work in web"); + console.warn('RNScreenshotPrevent: addListener not work in web'); return { remove: (): void => { - console.warn("RNScreenshotPrevent: remove addListener not work in web"); - } + console.warn( + 'RNScreenshotPrevent: remove addListener not work in web' + ); + }, + }; + }; +} + +class RNScreenshotPreventSend { + public static enabled(enabled: boolean): void { + return RNScreenshotPrevent.enabled(enabled); + } + + public static enableSecureView(): void { + if (!__DEV__) { + return; } + RNScreenshotPrevent.enableSecureView(); + } + + public static disableSecureView(): void { + if (!__DEV__) { + return; + } + RNScreenshotPrevent.disableSecureView(); + } + + public static addListener(fn: FN): Return { + return addListen(fn); } } export const usePreventScreenshot = () => { useEffect(() => { - RNScreenshotPrevent.enabled(true); + RNScreenshotPreventSend.enabled(true); return () => { - RNScreenshotPrevent.enabled(false); + RNScreenshotPreventSend.enabled(false); }; }, []); -} +}; export const useDisableSecureView = () => { useEffect(() => { - RNScreenshotPrevent.enableSecureView(); + if (!__DEV__) { + return; + } + RNScreenshotPreventSend.enableSecureView(); return () => { - RNScreenshotPrevent.disableSecureView(); + RNScreenshotPreventSend.disableSecureView(); }; }, []); -} +}; -export const enabled: (enabled: boolean) => void = RNScreenshotPrevent.enabled -export const enableSecureView: () => void = RNScreenshotPrevent.enableSecureView -export const disableSecureView: () => void = RNScreenshotPrevent.disableSecureView -export const addListener: (fn: FN) => void = addListen -export default RNScreenshotPrevent; +export const enabled: (enabled: boolean) => void = + RNScreenshotPreventSend.enabled; +export const enableSecureView: () => void = + RNScreenshotPreventSend.enableSecureView; +export const disableSecureView: () => void = + RNScreenshotPreventSend.disableSecureView; +export const addListener: (fn: FN) => Return = addListen; +export default RNScreenshotPreventSend;