diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-07-31 16:58:57 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-07-31 16:58:57 +0200 |
| commit | ba17de891507da74fb07423803fd636a4457354c (patch) | |
| tree | bdc75cff26dfce9bb67679e48ce09cc41817b506 /Captura/Data | |
| parent | 533cd932281300fb444c07e80f81fc683a410b60 (diff) | |
Allow URL configuration
Diffstat (limited to 'Captura/Data')
| -rw-r--r-- | Captura/Data/CapturaSettings.swift | 61 | ||||
| -rw-r--r-- | Captura/Data/CapturaURLDecoder.swift | 144 | ||||
| -rw-r--r-- | Captura/Data/SettingsStructs.swift | 13 |
3 files changed, 211 insertions, 7 deletions
diff --git a/Captura/Data/CapturaSettings.swift b/Captura/Data/CapturaSettings.swift index 01ef78c..85368ea 100644 --- a/Captura/Data/CapturaSettings.swift +++ b/Captura/Data/CapturaSettings.swift @@ -2,11 +2,21 @@ import Foundation struct CapturaSettings { static var frameRate: Int { - UserDefaults.standard.integer(forKey: "frameRate") + get { + UserDefaults.standard.integer(forKey: "frameRate") + } + set { + UserDefaults.standard.setValue(newValue, forKey: "frameRate") + } } static var outputFormats: OutputFormatSetting { - OutputFormatSetting(rawValue: UserDefaults.standard.integer(forKey: "outputFormats")) ?? .all + get { + OutputFormatSetting(rawValue: UserDefaults.standard.integer(forKey: "outputFormats")) ?? .all + } + set { + UserDefaults.standard.setValue(newValue.rawValue, forKey: "outputFormats") + } } static var shouldSaveMp4: Bool { @@ -30,17 +40,54 @@ struct CapturaSettings { } static var backend: URL? { - if let url = UserDefaults.standard.string(forKey: "backendUrl") { - return URL(string: url) + get { + if let url = UserDefaults.standard.string(forKey: "backendUrl") { + return URL(string: url) + } + return nil + } + set { + UserDefaults.standard.setValue(newValue?.absoluteString, forKey: "backendUrl") } - return nil } static var backendFormat: OutputFormatSetting { - OutputFormatSetting(rawValue: UserDefaults.standard.integer(forKey: "backendFormat")) ?? .all + get { + OutputFormatSetting(rawValue: UserDefaults.standard.integer(forKey: "backendFormat")) ?? .gifOnly + } + set { + UserDefaults.standard.setValue(newValue.rawValue, forKey: "backendFormat") + } } static var shouldKeepLocalFiles: Bool { - UserDefaults.standard.bool(forKey: "keepFiles") + get { + UserDefaults.standard.bool(forKey: "keepFiles") + } + set { + UserDefaults.standard.set(newValue, forKey: "keepFiles") + } + } + + static var shouldAllowURLAutomation: Bool { + UserDefaults.standard.bool(forKey: "allowURLAutomation") + } + + static func apply(_ config: ConfigureAction) { + if let fps = config.fps { + frameRate = fps + } + if let outputs = config.outputs { + outputFormats = outputs + } + if let newBackend = config.backend { + backend = newBackend + } + if let backendOutput = config.backendOutput { + backendFormat = backendOutput + } + if let keepLocalFiles = config.keepLocalFiles { + shouldKeepLocalFiles = keepLocalFiles + } } } diff --git a/Captura/Data/CapturaURLDecoder.swift b/Captura/Data/CapturaURLDecoder.swift new file mode 100644 index 0000000..3fa6412 --- /dev/null +++ b/Captura/Data/CapturaURLDecoder.swift @@ -0,0 +1,144 @@ +import Foundation + + +protocol ConfigureActionProtocol { + var action: String { get } + var fps: Int? { get } + var outputs: OutputFormatSetting? { get } + var backend: URL? { get } + var backendOutput: OutputFormatSetting? { get } + var keepLocalFiles: Bool? { get } +} + +protocol RecordActionProtocol { + var action: String { get } + + var x: Int? { get } + var y: Int? { get } + var width: Int? { get } + var height: Int? { get } + var preventResize: Bool? { get } + var preventMove: Bool? { get } + var fps: Int? { get } + var backend: URL? { get } + var outputs: OutputFormatSetting? { get } + var backendOutput: OutputFormatSetting? { get } + var keepLocalFiles: Bool? { get } + var autoStart: Bool? { get } +} + +// The concrete implementations +struct ConfigureAction: ConfigureActionProtocol { + let action: String + var fps: Int? + var outputs: OutputFormatSetting? + var backend: URL? + var backendOutput: OutputFormatSetting? + var keepLocalFiles: Bool? +} + +struct RecordAction: RecordActionProtocol { + let action: String + var x: Int? + var y: Int? + var width: Int? + var height: Int? + var preventResize: Bool? + var preventMove: Bool? + var fps: Int? + var outputs: OutputFormatSetting? + var backend: URL? + var backendOutput: OutputFormatSetting? + var keepLocalFiles: Bool? + var autoStart: Bool? +} + +enum CapturaAction { + case record(RecordAction) + case configure(ConfigureAction) +} + +struct CapturaURLDecoder { + + static func decodeParams(url: URL) -> CapturaAction? { + guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false), + let params = components.queryItems else { + return nil + } + + var paramsDict = [String: Any]() + + params.forEach { item in + paramsDict[item.name] = item.value + } + + guard let action = paramsDict["action"] as? String else { + return nil + } + + switch action { + case "configure": + var fps = Int(paramsDict["fps"] as? String ?? "") + let backend = URL(string: paramsDict["backend"] as? String ?? "") + let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "") + let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "") + var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "") + + if fps != nil { + fps = min(10, max(4, fps!)) + } + + if backendOutput == .all { + backendOutput = .gifOnly + } + + return .configure(ConfigureAction( + action: action, + fps: fps, + outputs: outputs, + backend: backend, + backendOutput: backendOutput, + keepLocalFiles: keepLocalFiles + )) + + case "record": + let x = Int(paramsDict["x"] as? String ?? "") + let y = Int(paramsDict["y"] as? String ?? "") + let width = Int(paramsDict["width"] as? String ?? "") + let height = Int(paramsDict["height"] as? String ?? "") + let preventResize = Bool(paramsDict["prevent_resize"] as? String ?? "") + let preventMove = Bool(paramsDict["prevent_move"] as? String ?? "") + var fps = Int(paramsDict["fps"] as? String ?? "") + let backend = URL(string: paramsDict["backend"] as? String ?? "") + let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "") + let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "") + var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "") + + if fps != nil { + fps = min(10, max(4, fps!)) + } + + if backendOutput == .all { + backendOutput = .gifOnly + } + + return .record(RecordAction( + action: action, + x: x, + y: y, + width: width, + height: height, + preventResize: preventResize, + preventMove: preventMove, + fps: fps, + outputs: outputs, + backend: backend, + backendOutput: backendOutput, + keepLocalFiles: keepLocalFiles + )) + + default: + return nil + } + } +} diff --git a/Captura/Data/SettingsStructs.swift b/Captura/Data/SettingsStructs.swift index 99f407c..16172ca 100644 --- a/Captura/Data/SettingsStructs.swift +++ b/Captura/Data/SettingsStructs.swift @@ -5,6 +5,19 @@ enum OutputFormatSetting: Int { case mp4Only = 1 case all = 2 + init?(_ string: String) { + switch(string) { + case "gif": + self = .gifOnly + case "mp4": + self = .mp4Only + case "all": + self = .all + default: + return nil + } + } + func shouldSaveGif() -> Bool { return self == .gifOnly || self == .all } |