Trying to make share extension, have to recreate project
This commit is contained in:
@@ -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