aboutsummaryrefslogtreecommitdiff
path: root/Captura/Data/CapturaSettings.swift
blob: bfaa1ced0ebd9bdd30aa59af1d78c03924312835 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 Copyright (C) 2024 Rubén Beltrán del Río

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program. If not, see https://captura.tranquil.systems.
 */
import Foundation

struct CapturaSettings {
  static var frameRate: Int {
    get {
      if UserDefaults.standard.object(forKey: "frameRate") == nil {
        return 10
      } else {
        return min(10, max(4, UserDefaults.standard.integer(forKey: "frameRate")))
      }
    }
    set {
      UserDefaults.standard.setValue(newValue, forKey: "frameRate")
    }
  }

  static var outputFormats: OutputFormatSetting {
    get {
      OutputFormatSetting(rawValue: UserDefaults.standard.integer(forKey: "outputFormats")) ?? .all
    }
    set {
      UserDefaults.standard.setValue(newValue.rawValue, forKey: "outputFormats")
    }
  }

  static var shouldSaveMp4: Bool {
    outputFormats.shouldSaveMp4() || (shouldUseBackend && shouldUploadMp4)
  }

  static var shouldSaveGif: Bool {
    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 = UserDefaults.standard.string(forKey: "backendUrl") {
        return URL(string: url)
      }
      return nil
    }
    set {
      UserDefaults.standard.setValue(newValue?.absoluteString, forKey: "backendUrl")
    }
  }

  static var backendFormat: OutputFormatSetting {
    get {
      OutputFormatSetting(rawValue: UserDefaults.standard.integer(forKey: "backendFormat"))
        ?? .gifOnly
    }
    set {
      UserDefaults.standard.setValue(newValue.rawValue, forKey: "backendFormat")
    }
  }

  static var shouldKeepLocalFiles: Bool {
    get {
      if UserDefaults.standard.object(forKey: "keepFiles") == nil {
        return true
      } else {
        return UserDefaults.standard.bool(forKey: "keepFiles")
      }
    }
    set {
      UserDefaults.standard.set(newValue, forKey: "keepFiles")
    }
  }

  static var shouldAllowURLAutomation: Bool {
    get {
      UserDefaults.standard.bool(forKey: "allowURLAutomation")
    }
    set {
      UserDefaults.standard.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
    }
  }
}