]> git.r.bdr.sh - rbdr/captura/blobdiff - Captura/Data/CapturaSettings.swift
Add AppleScript support
[rbdr/captura] / Captura / Data / CapturaSettings.swift
index d0d04f2506c3c851afc2e8620fc08fa64150c909..2d0e70b63bfe80babfecbcbd67fba88a938eba72 100644 (file)
@@ -2,28 +2,105 @@ import Foundation
 
 struct CapturaSettings {
   static var frameRate: Int {
-    UserDefaults.standard.integer(forKey: "frameRate")
+    get {
+      if NSUbiquitousKeyValueStore.default.object(forKey: "frameRate") == nil {
+        return 10
+      } else {
+        return min(10, max(4, Int(NSUbiquitousKeyValueStore.default.longLong(forKey: "frameRate"))))
+      }
+    }
+    set {
+      NSUbiquitousKeyValueStore.default.setValue(newValue, forKey: "frameRate")
+    }
   }
   
   static var outputFormats: OutputFormatSetting {
-    OutputFormatSetting(rawValue: UserDefaults.standard.integer(forKey: "outputFormats")) ?? .all
+    get {
+      OutputFormatSetting(rawValue: Int(NSUbiquitousKeyValueStore.default.longLong(forKey: "outputFormats"))) ?? .all
+    }
+    set {
+      NSUbiquitousKeyValueStore.default.setValue(newValue.rawValue, forKey: "outputFormats")
+    }
   }
   
   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 shouldUploadMp4: Bool {
+    backendFormat.shouldSaveMp4()
+  }
+  
+  static var shouldUseBackend: Bool {
+    backend != nil
+  }
+  
+  static var backend: URL? {
+    get {
+      if let url = NSUbiquitousKeyValueStore.default.string(forKey: "backendUrl") {
+        return URL(string: url)
+      }
+      return nil
+    }
+    set {
+      NSUbiquitousKeyValueStore.default.setValue(newValue?.absoluteString, forKey: "backendUrl")
+    }
   }
   
+  static var backendFormat: OutputFormatSetting {
+    get {
+      OutputFormatSetting(rawValue: Int(NSUbiquitousKeyValueStore.default.longLong(forKey: "backendFormat"))) ?? .gifOnly
+    }
+    set {
+      NSUbiquitousKeyValueStore.default.setValue(newValue.rawValue, forKey: "backendFormat")
+    }
+  }
   
-  static var shouldSendNotifications: Bool {
+  static var shouldKeepLocalFiles: Bool {
     get {
-      UserDefaults.standard.bool(forKey: "shouldSendNotifications")
+      if NSUbiquitousKeyValueStore.default.object(forKey: "keepFiles") == nil {
+        return true
+      } else {
+        return NSUbiquitousKeyValueStore.default.bool(forKey: "keepFiles")
+      }
     }
     set {
-      UserDefaults.standard.setValue(newValue, forKey: "shouldSendNotifications")
+      NSUbiquitousKeyValueStore.default.set(newValue, forKey: "keepFiles")
+    }
+  }
+  
+  static var shouldAllowURLAutomation: Bool {
+    get {
+      NSUbiquitousKeyValueStore.default.bool(forKey: "allowURLAutomation")
+    }
+    set {
+      NSUbiquitousKeyValueStore.default.setValue(newValue, 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
     }
   }
 }