Skip to content

Commit

Permalink
add date arg to clearAppGroupContainer to only delete files older tha…
Browse files Browse the repository at this point in the history
…n date
  • Loading branch information
MaxAst committed Jun 10, 2024
1 parent 9b084bd commit 41939fa
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
8 changes: 8 additions & 0 deletions examples/with-media/app/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import dayjs from "dayjs";
import { clearAppGroupContainer } from "expo-share-extension";
import { StyleSheet, Text, View } from "react-native";

const cleanUpBefore = dayjs().subtract(1, "day").toDate();

clearAppGroupContainer(cleanUpBefore).catch((error) => {
console.error(error);
});

export default function Index() {
return (
<View style={styles.container}>
Expand Down
1 change: 1 addition & 0 deletions examples/with-media/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions examples/with-media/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@
"prebuild": "expo prebuild -p ios --clean"
},
"dependencies": {
"dayjs": "^1.11.11",
"expo": "~51.0.8",
"expo-constants": "~16.0.1",
"expo-dev-client": "~4.0.14",
"expo-file-system": "~17.0.1",
"expo-linking": "~6.3.1",
"expo-router": "~3.5.14",
"expo-splash-screen": "~0.27.4",
"expo-status-bar": "~1.12.1",
"expo-updates": "^0.25.14",
"react": "18.2.0",
"react-native": "0.74.1",
"zod": "^3.23.8",
"expo-router": "~3.5.14",
"react-native-safe-area-context": "4.10.1",
"react-native-screens": "3.31.1",
"expo-linking": "~6.3.1",
"expo-constants": "~16.0.1",
"expo-file-system": "~17.0.1"
"zod": "^3.23.8"
},
"devDependencies": {
"@babel/core": "^7.24.6",
Expand Down
45 changes: 43 additions & 2 deletions ios/ExpoShareExtensionModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ExpoShareExtensionModule: Module {
NotificationCenter.default.post(name: NSNotification.Name("openHostApp"), object: nil, userInfo: userInfo)
}

AsyncFunction("clearAppGroupContainer") { (promise: Promise) in
AsyncFunction("clearAppGroupContainer") { (date: String?, promise: Promise) in
DispatchQueue.global(qos: .background).async {
guard let appGroup = Bundle.main.object(forInfoDictionaryKey: "AppGroup") as? String else {
DispatchQueue.main.async {
Expand All @@ -29,6 +29,21 @@ public class ExpoShareExtensionModule: Module {
return
}

var comparisonDate: Date? = nil
if let isoDateString = date {
let isoFormatter = ISO8601DateFormatter()
// new Date().toISOString() returns fractional seconds, which we need to account for:
isoFormatter.formatOptions.insert(.withFractionalSeconds)
if let date = isoFormatter.date(from: isoDateString) {
comparisonDate = date
} else {
DispatchQueue.main.async {
promise.reject("ERR_INVALID_DATE", "The provided date string is not in a valid ISO 8601 format")
}
return
}
}

let fileManager = FileManager.default
let sharedDataUrl = containerUrl.deletingLastPathComponent().appendingPathComponent("sharedData")

Expand All @@ -37,7 +52,20 @@ public class ExpoShareExtensionModule: Module {
let contents = try fileManager.contentsOfDirectory(atPath: sharedDataUrl.path)
for item in contents {
let itemPath = sharedDataUrl.appendingPathComponent(item).path
try fileManager.removeItem(atPath: itemPath)
if let creationDate = self.getCreationDate(of: itemPath) {
if let comparisonDate = comparisonDate {
if creationDate < comparisonDate {
try fileManager.removeItem(atPath: itemPath)
}
} else {
try fileManager.removeItem(atPath: itemPath)
}
} else {
DispatchQueue.main.async {
promise.reject("ERR_REMOVE_CONTENTS", "Unable to retrieve creation date")
}
return
}
}
DispatchQueue.main.async {
print("sharedData directory contents removed successfully.")
Expand All @@ -57,4 +85,17 @@ public class ExpoShareExtensionModule: Module {
}
}
}

internal func getCreationDate(of filePath: String) -> Date? {
let fileManager = FileManager.default
do {
let attributes = try fileManager.attributesOfItem(atPath: filePath)
if let creationDate = attributes[.creationDate] as? Date {
return creationDate
}
} catch {
print("Error getting file attributes: \(error.localizedDescription)")
}
return nil
}
}
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ export function openHostApp(path: string): void {
return ExpoShareExtensionModule.openHostApp(path);
}

export async function clearAppGroupContainer(): Promise<void> {
return await ExpoShareExtensionModule.clearAppGroupContainer();
export async function clearAppGroupContainer(date?: Date): Promise<void> {
return await ExpoShareExtensionModule.clearAppGroupContainer(
date?.toISOString(),
);
}

export interface IExtensionPreprocessingJS {
Expand Down

0 comments on commit 41939fa

Please sign in to comment.