CutBox.app

Coverage Report

Created: 2024-03-12 03:40

.../Source/App/Preferences/ColorTheme/CutBoxColorThemeDefinition.swift
Line
Count
Source (jump to first uncovered line)
1
//
2
//  CutBoxColorThemeDefinition.swift
3
//  CutBox
4
//
5
//  Created by Jason Milkins on 13/6/22.
6
//  Copyright © 2018-2023 ocodo. All rights reserved.
7
//
8
9
import Foundation
10
11
extension CutBoxColorTheme {
12
13
3
    convenience init(name: String, theme: CutBoxColorTheme) {
14
3
        self.init(name: name,
15
3
                  popupBackgroundColor: theme.popupBackgroundColor,
16
3
                  searchText: theme.searchText,
17
3
                  clip: theme.clip,
18
3
                  preview: theme.preview,
19
3
                  spacing: theme.spacing
20
3
        )
21
3
    }
22
23
1.85k
    convenience init(_ json: String) {
24
1.85k
        do {
25
1.85k
            self.init(theme: try CutBoxColorThemeDefinition(json))
26
1.85k
        } catch { fatalError("CutBoxTheme invalid: \(json)")}
27
1.85k
    }
28
29
1.85k
    convenience init(theme: CutBoxColorThemeDefinition) {
30
1.85k
        self.init(name: theme.name,
31
1.85k
                  popupBackgroundColor: theme.popupBackgroundColor.color!,
32
1.85k
                  searchText: SearchTextTheme(
33
1.85k
                    cursorColor: theme.searchText.cursorColor.color!,
34
1.85k
                    textColor: theme.searchText.textColor.color!,
35
1.85k
                    backgroundColor: theme.searchText.backgroundColor.color!,
36
1.85k
                    placeholderTextColor: theme.searchText.placeholderTextColor.color!
37
1.85k
                  ),
38
1.85k
                  clip: ClipTheme(
39
1.85k
                    backgroundColor: theme.clip.backgroundColor.color!,
40
1.85k
                    textColor: theme.clip.textColor.color!,
41
1.85k
                    highlightColor: theme.clip.highlightColor.color!,
42
1.85k
                    highlightTextColor: theme.clip.highlightTextColor.color!
43
1.85k
                  ),
44
1.85k
                  preview: PreviewTheme(
45
1.85k
                    textColor: theme.preview.textColor.color!,
46
1.85k
                    backgroundColor: theme.preview.backgroundColor.color!,
47
1.85k
                    selectedTextBackgroundColor: theme.preview.selectedTextBackgroundColor.color!,
48
1.85k
                    selectedTextColor: theme.preview.selectedTextColor.color!
49
1.85k
                  ),
50
1.85k
                  spacing: CGFloat(theme.spacing))
51
1.85k
    }
52
}
53
54
struct CutBoxColorThemeDefinition: Codable {
55
    let name: String
56
    let popupBackgroundColor: String
57
    let searchText: SearchText
58
    let clip: Clip
59
    let preview: Preview
60
    let spacing: CGFloat
61
62
    enum CodingKeys: String, CodingKey {
63
        case name
64
        case popupBackgroundColor
65
        case searchText
66
        case clip
67
        case preview
68
        case spacing
69
    }
70
}
71
72
extension CutBoxColorThemeDefinition {
73
1.85k
    init(data: Data) throws {
74
1.85k
        do {
75
1.85k
            _ = try newJSONDecoder()
76
1.85k
              .decode(CutBoxColorThemeDefinition.self, from: data)
77
1.85k
        } catch DecodingError.dataCorrupted(let context) {
78
2
            print(context, to: &errStream)
79
2
        } catch DecodingError.keyNotFound(let key, let context) {
80
1
            print("Key '\(key)' not found:", context.debugDescription, to: &errStream)
81
1
            print("codingPath:", context.codingPath, to: &errStream)
82
1
        } catch DecodingError.typeMismatch(let type, let context) {
83
1
            print("Type '\(type)' mismatch:", context.debugDescription, to: &errStream)
84
1
            print("codingPath:", context.codingPath, to: &errStream)
85
1.85k
        }
86
1.85k
        self = try newJSONDecoder()
87
1.85k
            .decode(CutBoxColorThemeDefinition.self, from: data)
88
1.85k
    }
89
90
1.85k
    init(_ json: String, using encoding: String.Encoding = .utf8) throws {
91
1.85k
        guard let data = json.data(using: encoding) else {
92
0
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
93
1.85k
        }
94
1.85k
        try self.init(data: data)
95
1.85k
    }
96
}
97
98
struct Clip: Codable {
99
    let backgroundColor: String
100
    let textColor: String
101
    let highlightColor: String
102
    let highlightTextColor: String
103
104
    enum CodingKeys: String, CodingKey {
105
        case backgroundColor
106
        case textColor
107
        case highlightColor
108
        case highlightTextColor
109
    }
110
}
111
112
struct Preview: Codable {
113
    let textColor: String
114
    let backgroundColor: String
115
    let selectedTextBackgroundColor: String
116
    let selectedTextColor: String
117
118
    enum CodingKeys: String, CodingKey {
119
        case textColor
120
        case backgroundColor
121
        case selectedTextBackgroundColor
122
        case selectedTextColor
123
    }
124
}
125
126
struct SearchText: Codable {
127
    let cursorColor: String
128
    let textColor: String
129
    let backgroundColor: String
130
    let placeholderTextColor: String
131
132
    enum CodingKeys: String, CodingKey {
133
        case cursorColor
134
        case textColor
135
        case backgroundColor
136
        case placeholderTextColor
137
    }
138
}
139
140
3.70k
func newJSONDecoder() -> JSONDecoder {
141
3.70k
    let decoder = JSONDecoder()
142
3.70k
    if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
143
3.70k
        decoder.dateDecodingStrategy = .iso8601
144
3.70k
    }
145
3.70k
    return decoder
146
3.70k
}