Trying to make share extension, have to recreate project
This commit is contained in:
@@ -11,6 +11,12 @@ class GIFFileManager {
|
||||
// MARK: - File Storage
|
||||
|
||||
private var documentsDirectory: URL {
|
||||
// First try to get the App Group container
|
||||
if let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.gifcollector") {
|
||||
return containerURL
|
||||
}
|
||||
|
||||
// Fall back to the app's documents directory if App Group is not available
|
||||
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
|
||||
return paths[0]
|
||||
}
|
||||
@@ -32,6 +38,11 @@ class GIFFileManager {
|
||||
}
|
||||
|
||||
func storeGIF(data: Data, fromURL urlString: String) -> String? {
|
||||
// Check if this is a shared GIF from the share extension
|
||||
if let sharedGIFPath = checkForSharedGIF(withURL: urlString), FileManager.default.fileExists(atPath: sharedGIFPath) {
|
||||
return sharedGIFPath
|
||||
}
|
||||
|
||||
// Create a unique filename based on the URL hash and timestamp
|
||||
let urlHash = urlString.hashValue
|
||||
let timestamp = Int(Date().timeIntervalSince1970)
|
||||
@@ -129,4 +140,38 @@ class GIFFileManager {
|
||||
print("Error cleaning up old GIFs: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
// Check if we already have this GIF in the shared container
|
||||
private func checkForSharedGIF(withURL urlString: String) -> String? {
|
||||
guard let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.gifcollector") else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let sharedGIFsFolder = containerURL.appendingPathComponent("GIFs", isDirectory: true)
|
||||
|
||||
guard FileManager.default.fileExists(atPath: sharedGIFsFolder.path) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
do {
|
||||
let fileURLs = try FileManager.default.contentsOfDirectory(at: sharedGIFsFolder, includingPropertiesForKeys: nil)
|
||||
|
||||
// Find any shared GIF that matches our URL (usually won't find any, but helps avoid duplicates)
|
||||
let userDefaults = UserDefaults(suiteName: "group.gifcollector")
|
||||
if let pendingGIFs = userDefaults?.array(forKey: "pendingGIFs") as? [[String: Any]] {
|
||||
for gifInfo in pendingGIFs {
|
||||
if let originURL = gifInfo["originalURL"] as? String,
|
||||
let path = gifInfo["localFilePath"] as? String,
|
||||
originURL == urlString {
|
||||
return path
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
} catch {
|
||||
print("Error checking for shared GIFs: \(error)")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,16 @@ class GIFStorageService {
|
||||
|
||||
private let userDefaults = UserDefaults(suiteName: "group.gifcollector")
|
||||
private let savedGIFsKey = "savedGIFs"
|
||||
private let pendingGIFsKey = "pendingGIFs"
|
||||
|
||||
private init() {
|
||||
// Make sure the shared UserDefaults exists
|
||||
if userDefaults == nil {
|
||||
print("Error: Could not create UserDefaults with app group")
|
||||
}
|
||||
|
||||
// Process any pending GIFs from the Share Extension
|
||||
checkForSharedGIFs()
|
||||
}
|
||||
|
||||
func saveGIF(data: Data, fromURL urlString: String, completion: @escaping (GIF?) -> Void) {
|
||||
@@ -86,4 +90,46 @@ class GIFStorageService {
|
||||
func getGIFData(for gif: GIF) -> Data? {
|
||||
return GIFFileManager.shared.loadGIFData(from: gif.localFilePath)
|
||||
}
|
||||
|
||||
// MARK: - Share Extension Integration
|
||||
|
||||
func checkForSharedGIFs() {
|
||||
guard let pendingGIFsData = userDefaults?.array(forKey: pendingGIFsKey) as? [[String: Any]] else {
|
||||
return
|
||||
}
|
||||
|
||||
guard !pendingGIFsData.isEmpty else {
|
||||
return
|
||||
}
|
||||
|
||||
var savedGIFs = fetchGIFs()
|
||||
var newGIFsAdded = false
|
||||
|
||||
for gifInfo in pendingGIFsData {
|
||||
if let localFilePath = gifInfo["localFilePath"] as? String,
|
||||
let originalURL = gifInfo["originalURL"] as? String,
|
||||
let createdAt = gifInfo["createdAt"] as? TimeInterval {
|
||||
|
||||
// Create a GIF object
|
||||
let gif = GIF(
|
||||
localFilePath: localFilePath,
|
||||
originalURL: originalURL
|
||||
)
|
||||
|
||||
// Don't add duplicates
|
||||
if !savedGIFs.contains(where: { $0.localFilePath == localFilePath }) {
|
||||
savedGIFs.append(gif)
|
||||
newGIFsAdded = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Save updated GIFs list and clear pending ones
|
||||
if newGIFsAdded {
|
||||
saveToUserDefaults(gifs: savedGIFs)
|
||||
}
|
||||
|
||||
// Clear pending GIFs
|
||||
userDefaults?.removeObject(forKey: pendingGIFsKey)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user