diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-07-31 10:52:11 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-07-31 10:52:11 +0200 |
| commit | 533cd932281300fb444c07e80f81fc683a410b60 (patch) | |
| tree | 483abbcd7461e155578b858dac4eb0145c13ce3e /Captura/Data | |
| parent | c9b9e1d654ea697afad9f6427d94623bfdf55cce (diff) | |
Complete initial release
Diffstat (limited to 'Captura/Data')
| -rw-r--r-- | Captura/Data/BackendResponse.swift | 4 | ||||
| -rw-r--r-- | Captura/Data/Captura.xcdatamodeld/CapturaRemoteFile.xcdatamodel/contents | 7 | ||||
| -rw-r--r-- | Captura/Data/CapturaRemoteFile+name.swift | 13 | ||||
| -rw-r--r-- | Captura/Data/CapturaRemoteFile.swift | 13 | ||||
| -rw-r--r-- | Captura/Data/CapturaSettings.swift | 33 | ||||
| -rw-r--r-- | Captura/Data/Persistence.swift | 46 |
6 files changed, 95 insertions, 21 deletions
diff --git a/Captura/Data/BackendResponse.swift b/Captura/Data/BackendResponse.swift new file mode 100644 index 0000000..25c1988 --- /dev/null +++ b/Captura/Data/BackendResponse.swift @@ -0,0 +1,4 @@ +import Foundation +struct BackendResponse: Decodable { + let url: URL +} diff --git a/Captura/Data/Captura.xcdatamodeld/CapturaRemoteFile.xcdatamodel/contents b/Captura/Data/Captura.xcdatamodeld/CapturaRemoteFile.xcdatamodel/contents new file mode 100644 index 0000000..3311233 --- /dev/null +++ b/Captura/Data/Captura.xcdatamodeld/CapturaRemoteFile.xcdatamodel/contents @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> +<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22189.1" systemVersion="23A5286i" minimumToolsVersion="Automatic" sourceLanguage="Swift" usedWithSwiftData="YES" userDefinedModelVersionIdentifier=""> + <entity name="CapturaRemoteFile" representedClassName="CapturaRemoteFile" syncable="YES" codeGenerationType="class"> + <attribute name="timestamp" optional="YES" attributeType="Date" usesScalarValueType="NO"/> + <attribute name="url" optional="YES" attributeType="String"/> + </entity> +</model>
\ No newline at end of file diff --git a/Captura/Data/CapturaRemoteFile+name.swift b/Captura/Data/CapturaRemoteFile+name.swift new file mode 100644 index 0000000..feebdba --- /dev/null +++ b/Captura/Data/CapturaRemoteFile+name.swift @@ -0,0 +1,13 @@ +import Foundation +extension CapturaRemoteFile { + var name: String { + let dateFormatter = DateFormatter() + dateFormatter.dateStyle = .medium + dateFormatter.timeStyle = .medium + dateFormatter.locale = Locale.current + if let timestamp { + return dateFormatter.string(from: timestamp).replacingOccurrences(of: ":", with: ".") + } + return "Unknown" + } +} diff --git a/Captura/Data/CapturaRemoteFile.swift b/Captura/Data/CapturaRemoteFile.swift deleted file mode 100644 index 24d821e..0000000 --- a/Captura/Data/CapturaRemoteFile.swift +++ /dev/null @@ -1,13 +0,0 @@ -import Foundation -import SwiftData - -@Model -final class CapturaRemoteFile { - var timestamp: Date - var url: URL - - init(url: URL) { - self.timestamp = Date() - self.url = url - } -} diff --git a/Captura/Data/CapturaSettings.swift b/Captura/Data/CapturaSettings.swift index d0d04f2..01ef78c 100644 --- a/Captura/Data/CapturaSettings.swift +++ b/Captura/Data/CapturaSettings.swift @@ -10,20 +10,37 @@ struct CapturaSettings { } static var shouldSaveMp4: Bool { - outputFormats.shouldSaveMp4() + outputFormats.shouldSaveMp4() || (shouldUseBackend && shouldUploadMp4) } static var shouldSaveGif: Bool { - outputFormats.shouldSaveGif() + outputFormats.shouldSaveGif() || (shouldUseBackend && shouldUploadGif) } + static var shouldUploadGif: Bool { + backendFormat.shouldSaveGif() + } - static var shouldSendNotifications: Bool { - get { - UserDefaults.standard.bool(forKey: "shouldSendNotifications") - } - set { - UserDefaults.standard.setValue(newValue, forKey: "shouldSendNotifications") + static var shouldUploadMp4: Bool { + backendFormat.shouldSaveMp4() + } + + static var shouldUseBackend: Bool { + backend != nil + } + + static var backend: URL? { + if let url = UserDefaults.standard.string(forKey: "backendUrl") { + return URL(string: url) } + return nil + } + + static var backendFormat: OutputFormatSetting { + OutputFormatSetting(rawValue: UserDefaults.standard.integer(forKey: "backendFormat")) ?? .all + } + + static var shouldKeepLocalFiles: Bool { + UserDefaults.standard.bool(forKey: "keepFiles") } } diff --git a/Captura/Data/Persistence.swift b/Captura/Data/Persistence.swift new file mode 100644 index 0000000..4490e38 --- /dev/null +++ b/Captura/Data/Persistence.swift @@ -0,0 +1,46 @@ +import CoreData + +struct PersistenceController { + static let shared = PersistenceController() + + static var preview: PersistenceController = { + let result = PersistenceController(inMemory: true) + return result + }() + + let container: NSPersistentCloudKitContainer + + init(inMemory: Bool = false) { + container = NSPersistentCloudKitContainer(name: "Captura") + + #if DEBUG + do { + // Use the container to initialize the development schema. + try container.initializeCloudKitSchema(options: []) + } catch { + // Handle any errors. + } + #endif + + if inMemory { + container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null") + } + container.loadPersistentStores(completionHandler: { (storeDescription, error) in + if let error = error as NSError? { + // Replace this implementation with code to handle the error appropriately. + // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. + + /* + Typical reasons for an error here include: + * The parent directory does not exist, cannot be created, or disallows writing. + * The persistent store is not accessible, due to permissions or data protection when the device is locked. + * The device is out of space. + * The store could not be migrated to the current model version. + Check the error message to determine what the actual problem was. + */ + fatalError("Unresolved error \(error), \(error.userInfo)") + } + }) + container.viewContext.automaticallyMergesChangesFromParent = true + } +} |