CutBox.app

Coverage Report

Created: 2024-03-12 03:40

.../Source/App/Preferences/PreferencesService/CutBoxPreferences+SelectTheme.swift
Line
Count
Source
1
//
2
//  CutBoxPreferences+SelectTheme.swift
3
//  CutBox
4
//
5
//  Created by Jason Milkins on 12/4/18.
6
//  Copyright © 2018-2023 ocodo. All rights reserved.
7
//
8
9
import Foundation
10
11
extension CutBoxPreferencesService {
12
    var theme: Int {
13
624
        get {
14
624
            // 1.5.8 - theme saved to prefs as themeName
15
624
            if let name = defaults.string(forKey: "themeName"),
16
624
            let index = themes.firstIndex(where: { $0.name == name }) {
17
15
                return index
18
609
            }
19
609
20
609
            let legacyIndex = defaults.integer(forKey: "theme")
21
609
            if themes.count < legacyIndex + 1 {
22
1
                self.theme = 0
23
1
                return 0
24
608
            }
25
608
            return legacyIndex
26
609
        }
27
28
16
        set {
29
16
            // 1.5.8 - theme saved to prefs as themeName
30
16
            defaults.set(themes[newValue].name, forKey: "themeName")
31
16
32
16
            defaults.set(newValue, forKey: "theme")
33
16
            self.events.onNext(.themeChanged)
34
16
        }
35
    }
36
37
3
    var themeName: String {
38
3
        if let name = defaults.string(forKey: "themeName"),
39
23
           let themeIndex = themes.firstIndex(where: { $0.name == name }) {
40
1
            theme = themeIndex
41
1
            return name
42
2
        }
43
2
        return themes[theme].name
44
3
    }
45
46
557
    var currentTheme: CutBoxColorTheme { return themes[theme] }
47
48
13
    func cycleTheme() {
49
13
        theme = (theme + 1) % themes.count
50
13
    }
51
}