Skip to content

Commit

Permalink
Introducing familyActivitySelectionId to reduce load on userDefaults …
Browse files Browse the repository at this point in the history
…and streamline structure
  • Loading branch information
robertherber committed Dec 16, 2024
1 parent f4bd3ca commit 4994c12
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 37 deletions.
2 changes: 1 addition & 1 deletion example/components/CreateActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const startMonitoring = async (
) => {
await requestPermissionsAsync();

// ReactNativeDeviceActivity.updateFamilyActivitySelectionToActivityNameMap({
// ReactNativeDeviceActivity.setFamilyActivitySelectionId({
// activityName,
// familyActivitySelection: activitySelection,
// });
Expand Down
10 changes: 4 additions & 6 deletions example/screens/AllTheThings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function requestPermissionsAsync() {
/* console.log(
JSON.stringify(
ReactNativeDeviceActivity.userDefaultsGet(
"familyActivitySelectionToActivityNameMap",
"familyActivitySelectionIds",
),
null,
2,
Expand Down Expand Up @@ -223,7 +223,7 @@ export function AllTheThings() {
<Button
title="Start monitoring"
disabled={!familyActivitySelection}
onPress={() => startMonitoring(familyActivitySelection!)}
// onPress={() => startMonitoring(familyActivitySelection!)}
/>

<Button
Expand Down Expand Up @@ -263,7 +263,7 @@ export function AllTheThings() {
onChangeText={(text) => setShieldTitle(text)}
value={shieldTitle}
onSubmitEditing={() =>
ReactNativeDeviceActivity.updateShieldConfiguration(
ReactNativeDeviceActivity.updateShield(
{
title: shieldTitle,
backgroundBlurStyle: UIBlurEffectStyle.systemMaterialDark,
Expand Down Expand Up @@ -320,9 +320,7 @@ export function AllTheThings() {
}}
headerText="a header text!"
footerText="a footer text!"
onSelectionChange={(
event: NativeSyntheticEvent<{ familyActivitySelection: string }>,
) => {
onSelectionChange={(event) => {
if (
event.nativeEvent.familyActivitySelection !==
familyActivitySelection
Expand Down
2 changes: 1 addition & 1 deletion example/screens/ShieldTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function ShieldTab() {

const onSubmitEditing = useCallback(
() =>
ReactNativeDeviceActivity.updateShieldConfiguration(
ReactNativeDeviceActivity.updateShield(
{
title: shieldTitle,
backgroundBlurStyle: UIBlurEffectStyle.systemMaterialDark,
Expand Down
55 changes: 37 additions & 18 deletions ios/Shared.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,14 @@ func executeAction(action: [String: Any], placeholders: [String: String?]) {
let type = action["type"] as? String

if type == "blockSelection" {
if let familyActivitySelectionStr = action["familyActivitySelection"] as? String {
let activitySelection = getActivitySelectionFromStr(
familyActivitySelectionStr: familyActivitySelectionStr)
if let familyActivitySelectionId = action["familyActivitySelectionId"] as? String {
if let activitySelection = getFamilyActivitySelectionById(id: familyActivitySelectionId) {
updateShield(shieldId: action["shieldId"] as? String)

updateShield(shieldId: action["shieldId"] as? String)
sleep(ms: 50)

sleep(ms: 50)

blockSelectedApps(activitySelection: activitySelection)
blockSelectedApps(activitySelection: activitySelection)
}
}
} else if type == "unblockAllApps" {
unblockAllApps()
Expand All @@ -80,6 +79,7 @@ func executeAction(action: [String: Any], placeholders: [String: String?]) {

task = sendHttpRequest(with: url, config: config, placeholders: placeholders)

// required for it to have time to trigger before process/callback ends
sleep(ms: 1000)
}
}
Expand Down Expand Up @@ -258,9 +258,9 @@ func sendHttpRequest(with url: String, config: [String: Any], placeholders: [Str
}

@available(iOS 15.0, *)
struct SelectionWithActivityName {
struct FamilyActivitySelectionWithId {
var selection: FamilyActivitySelection
var activityName: String
var id: String
}

struct TextToReplaceWithOptionalSpecialTreatment {
Expand Down Expand Up @@ -339,15 +339,15 @@ func replacePlaceholders(_ text: String, with placeholders: [String: String?]) -
let store = ManagedSettingsStore()

@available(iOS 15.0, *)
func getFamilyActivitySelectionToActivityNameMap() -> [SelectionWithActivityName?] {
if let familyActivitySelectionToActivityNameMap = userDefaults?.dictionary(
forKey: "familyActivitySelectionToActivityNameMap") {
return familyActivitySelectionToActivityNameMap.map { (key: String, value: Any) in
func getFamilyActivitySelectionIds() -> [FamilyActivitySelectionWithId?] {
if let familyActivitySelectionIds = userDefaults?.dictionary(
forKey: "familyActivitySelectionIds") {
return familyActivitySelectionIds.map { (key: String, value: Any) in
if let familyActivitySelectionStr = value as? String {
let activitySelection = getActivitySelectionFromStr(
familyActivitySelectionStr: familyActivitySelectionStr)

return SelectionWithActivityName(selection: activitySelection, activityName: key)
return FamilyActivitySelectionWithId(selection: activitySelection, id: key)
}
return nil
}
Expand All @@ -356,14 +356,33 @@ func getFamilyActivitySelectionToActivityNameMap() -> [SelectionWithActivityName
}

@available(iOS 15.0, *)
func getPossibleActivityName(
func getFamilyActivitySelectionById(id: String) -> FamilyActivitySelection? {
if let familyActivitySelectionIds = userDefaults?.dictionary(forKey: "familyActivitySelectionIds") {
if let pair =
familyActivitySelectionIds
.first(where: { (key: String, _: Any) in
return key == id
}) {
if let familyActivitySelectionStr = pair.value as? String {
let activitySelection = getActivitySelectionFromStr(
familyActivitySelectionStr: familyActivitySelectionStr
)
return activitySelection
}
}
}
return nil
}

@available(iOS 15.0, *)
func getPossibleFamilyActivitySelectionId(
applicationToken: ApplicationToken?,
webDomainToken: WebDomainToken?,
categoryToken: ActivityCategoryToken?
) -> String? {
let familyActivitySelectionToActivityNameMap = getFamilyActivitySelectionToActivityNameMap()
let familyActivitySelectionIds = getFamilyActivitySelectionIds()

let foundIt = familyActivitySelectionToActivityNameMap.first(where: { (mapping) in
let foundIt = familyActivitySelectionIds.first(where: { (mapping) in
if let mapping = mapping {
if let applicationToken = applicationToken {
if mapping.selection.applicationTokens.contains(applicationToken) {
Expand All @@ -387,7 +406,7 @@ func getPossibleActivityName(
return false
})

return foundIt??.activityName
return foundIt??.id
}

@available(iOS 15.0, *)
Expand Down
2 changes: 1 addition & 1 deletion src/ReactNativeDeviceActivity.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export type ShieldActions = {
export type Action =
| {
type: "blockSelection";
familyActivitySelection: string;
familyActivitySelectionId: string;
shieldId?: string;
}
| {
Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,21 @@ export const cleanUpAfterActivity = (activityName: string) => {
);
};

export const updateFamilyActivitySelectionToActivityNameMap = ({
activityName,
export const setFamilyActivitySelectionId = ({
id,
familyActivitySelection,
}: {
activityName: string;
id: string;
familyActivitySelection: string;
}) => {
const previousValue =
(ReactNativeDeviceActivityModule.userDefaultsGet(
"familyActivitySelectionToActivityNameMap",
"familyActivitySelectionIds",
) as Record<string, string>) ?? {};

userDefaultsSet("familyActivitySelectionToActivityNameMap", {
userDefaultsSet("familyActivitySelectionIds", {
...previousValue,
[activityName]: familyActivitySelection,
[id]: familyActivitySelection,
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class ShieldConfigurationExtension: ShieldConfigurationDataSource {
"applicationOrDomainDisplayName": application.localizedDisplayName,
"token": "\(application.token!.hashValue)",
"tokenType": "application",
"activityName": getPossibleActivityName(
"familyActivitySelectionId": getPossibleFamilyActivitySelectionId(
applicationToken: application.token,
webDomainToken: nil,
categoryToken: nil
Expand All @@ -127,7 +127,7 @@ class ShieldConfigurationExtension: ShieldConfigurationDataSource {
"applicationOrDomainDisplayName": application.localizedDisplayName,
"token": "\(category.token!.hashValue)",
"tokenType": "application_category",
"activityName": getPossibleActivityName(
"familyActivitySelectionId": getPossibleFamilyActivitySelectionId(
applicationToken: application.token,
webDomainToken: nil,
categoryToken: category.token
Expand All @@ -148,7 +148,7 @@ class ShieldConfigurationExtension: ShieldConfigurationDataSource {
"applicationOrDomainDisplayName": webDomain.domain,
"token": "\(webDomain.token!.hashValue)",
"tokenType": "web_domain",
"activityName": getPossibleActivityName(
"familyActivitySelectionId": getPossibleFamilyActivitySelectionId(
applicationToken: nil,
webDomainToken: webDomain.token,
categoryToken: nil
Expand All @@ -172,7 +172,7 @@ class ShieldConfigurationExtension: ShieldConfigurationDataSource {
"applicationOrDomainDisplayName": webDomain.domain,
"token": "\(category.token!.hashValue)",
"tokenType": "web_domain_category",
"activityName": getPossibleActivityName(
"familyActivitySelectionId": getPossibleFamilyActivitySelectionId(
applicationToken: nil,
webDomainToken: webDomain.token,
categoryToken: category.token
Expand Down

0 comments on commit 4994c12

Please sign in to comment.