From a89c799442ab4dc205b7494491c9b2e2201736ad Mon Sep 17 00:00:00 2001 From: Robert Herber Date: Thu, 7 Nov 2024 17:17:18 +0100 Subject: [PATCH] Add doesSelectionHaveOverlap --- ios/ReactNativeDeviceActivityModule.swift | 45 +++++++++++++++++++++++ src/ReactNativeDeviceActivity.types.ts | 3 ++ src/ReactNativeDeviceActivityModule.ts | 6 +++ 3 files changed, 54 insertions(+) diff --git a/ios/ReactNativeDeviceActivityModule.swift b/ios/ReactNativeDeviceActivityModule.swift index bf8afd0..2b274d8 100644 --- a/ios/ReactNativeDeviceActivityModule.swift +++ b/ios/ReactNativeDeviceActivityModule.swift @@ -210,6 +210,51 @@ public class ReactNativeDeviceActivityModule: Module { return filteredDict } + Function("doesSelectionHaveOverlap") { (familyActivitySelections: [String]) in + let decodedFamilyActivitySelections: [FamilyActivitySelection] = familyActivitySelections.map { familyActivitySelection in + let decoder = JSONDecoder() + let data = Data(base64Encoded: familyActivitySelection) + do { + let activitySelection = try decoder.decode(FamilyActivitySelection.self, from: data!) + return activitySelection + } + catch { + return FamilyActivitySelection() + } + } + + let hasOverlap = decodedFamilyActivitySelections.contains { selection in + return decodedFamilyActivitySelections.contains { compareWith in + // if it's the same instance - skip comparison + if(compareWith == selection){ + return false + } + + if(compareWith.applicationTokens.contains(where: { token in + return selection.applicationTokens.contains(token) + } )){ + return true + } + + if(compareWith.categoryTokens.contains(where: { token in + return selection.categoryTokens.contains(token) + } )){ + return true + } + + if(compareWith.webDomainTokens.contains(where: { token in + return selection.webDomainTokens.contains(token) + } )){ + return true + } + + return false + } + } + + return hasOverlap + } + Function("authorizationStatus") { let currentStatus = AuthorizationCenter.shared.authorizationStatus diff --git a/src/ReactNativeDeviceActivity.types.ts b/src/ReactNativeDeviceActivity.types.ts index daee89f..917e0dd 100644 --- a/src/ReactNativeDeviceActivity.types.ts +++ b/src/ReactNativeDeviceActivity.types.ts @@ -91,6 +91,9 @@ export type ReactNativeDeviceActivityNativeModule = { revokeAuthorization: () => PromiseLike | void; blockAllApps: () => PromiseLike | void; unblockApps: () => PromiseLike | void; + doesSelectionHaveOverlap: ( + familyActivitySelections: FamilyActivitySelection[], + ) => boolean; updateShieldConfiguration: ( shieldConfiguration: ShieldConfiguration, ) => PromiseLike | void; diff --git a/src/ReactNativeDeviceActivityModule.ts b/src/ReactNativeDeviceActivityModule.ts index b3005b8..24cba48 100644 --- a/src/ReactNativeDeviceActivityModule.ts +++ b/src/ReactNativeDeviceActivityModule.ts @@ -18,10 +18,16 @@ const warnFnNumber = () => { return 0; }; +const warnFnBoolean = () => { + console.warn(warnText); + return false; +}; + const mockModule: ReactNativeDeviceActivityNativeModule & ProxyNativeModule = { isAvailable: () => false, requestAuthorization: warnFn, revokeAuthorization: warnFn, + doesSelectionHaveOverlap: warnFnBoolean, updateShieldConfiguration: warnFn, unblockApps: warnFn, blockAllApps: warnFn,