Skip to content

Commit

Permalink
fiix: check emit before listener is add
Browse files Browse the repository at this point in the history
  • Loading branch information
wn-na committed Jun 10, 2024
1 parent 5b983a6 commit e693ee5
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions ios/CaptureProtection.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
static int TAG_SCREEN_PROTECTION = -1004;

@implementation CaptureProtection {
bool hasListeners;
bool hasScreenRecordObserver;
bool hasScreenshotObserver;
bool isPreventScreenRecord;
Expand Down Expand Up @@ -52,7 +53,9 @@ - (NSDictionary *)eventMessage: (CaptureProtectionStatus)status {

// Observer Event
- (void)eventScreenshot: (NSNotification *)notification {
[self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:CAPTURE_DETECTED]];
if (hasListeners) {
[self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:CAPTURE_DETECTED]];
}
}

- (void)bundleObserver {
Expand All @@ -79,11 +82,15 @@ - (void)eventScreenRecordWithInit: (NSNotification *)notification init:(BOOL) in
if (isPreventScreenRecord) {
[self createRecordProtectionScreen];
}
[self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:RECORD_DETECTED_START]];
if (hasListeners) {
[self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:RECORD_DETECTED_START]];
}
} else {
[self removeRecordProtectionScreen];
if (!init) {
[self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:RECORD_DETECTED_END]];
if (hasListeners) {
[self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:RECORD_DETECTED_END]];
}
}
}
}
Expand Down Expand Up @@ -197,6 +204,13 @@ - (void) removeScreenRecordObserver {
hasScreenRecordObserver = NO;
}
}
- (void) startObserving {
hasListeners = YES;
}

- (void) stopObserving {
hasListeners = NO;
}

RCT_REMAP_METHOD(setScreenRecordScreenWithImage,
screenImage: (NSDictionary*) screenImage
Expand Down Expand Up @@ -328,7 +342,9 @@ - (void) removeScreenRecordObserver {
[self removeScreenShotObserver];
}
isPreventScreenshot = NO;
[self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:UNKNOWN]];
if (hasListeners) {
[self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:UNKNOWN]];
}
resolve(@(YES));
}
@catch (NSException *e) {
Expand All @@ -345,7 +361,9 @@ - (void) removeScreenRecordObserver {
[self secureScreenshotView:true];
[self addScreenShotObserver];
isPreventScreenshot = YES;
[self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:UNKNOWN]];
if (hasListeners) {
[self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:UNKNOWN]];
}
resolve(@(YES));
}
@catch (NSException *e) {
Expand All @@ -365,7 +383,9 @@ - (void) removeScreenRecordObserver {
[self removeScreenRecordObserver];
}
isPreventScreenRecord = NO;
[self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:UNKNOWN]];
if (hasListeners) {
[self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:UNKNOWN]];
}
resolve(@(YES));
}
@catch (NSException *e) {
Expand All @@ -385,7 +405,9 @@ - (void) removeScreenRecordObserver {
if (isImmediate) {
[self eventScreenRecordWithInit:nil init:true];
}
[self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:UNKNOWN]];
if (hasListeners) {
[self sendEventWithName:@"CaptureProtectionListener" body:[self eventMessage:UNKNOWN]];
}
resolve(@(YES));
}
@catch (NSException *e) {
Expand Down

0 comments on commit e693ee5

Please sign in to comment.