Skip to content

Commit

Permalink
try fix's
Browse files Browse the repository at this point in the history
- try fix TurboModule for #27
- try fix crash restart app #23
  • Loading branch information
killserver committed Oct 4, 2023
1 parent 65255ce commit 56a72de
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 36 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -127,6 +127,6 @@
"codegenConfig": {
"name": "RNScreenshotPreventSpec",
"type": "modules",
"jsSrcsDir": "src"
"jsSrcsDir": "lib"
}
}
115 changes: 81 additions & 34 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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);

/**
Expand All @@ -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;

0 comments on commit 56a72de

Please sign in to comment.