.../Source/App/Preferences/ColorTheme/CutBoxThemeLoader.swift
Line | Count | Source |
1 | | // |
2 | | // CutBoxThemeLoader.swift |
3 | | // CutBox |
4 | | // |
5 | | // Created by Jason on 30/5/22. |
6 | | // Copyright © 2023 ocodo. All rights reserved. |
7 | | // |
8 | | |
9 | | import Foundation |
10 | | |
11 | | class CutBoxThemeLoader { |
12 | | |
13 | | var cutBoxUserThemesLocation: String! |
14 | | |
15 | 154 | func getBundledThemes() -> [CutBoxColorTheme] { |
16 | 154 | let bundle = Bundle(for: Self.self) |
17 | 154 | |
18 | 154 | let themePaths: [String] = bundle |
19 | 154 | .paths(forResourcesOfType: "cutboxTheme", |
20 | 154 | inDirectory: "themes") |
21 | 154 | |
22 | 154 | let themes: [CutBoxColorTheme] = themePaths.sorted() |
23 | 1.84k | .map { try? String(contentsOfFile: $0) } |
24 | 1.84k | .compactMap { $0 } |
25 | 1.84k | .map { CutBoxColorTheme($0) } |
26 | 154 | |
27 | 154 | return themes |
28 | 154 | } |
29 | | |
30 | 155 | func getUserThemes() -> [CutBoxColorTheme] { |
31 | 155 | let jsonThemes = loadUserThemesFiles() |
32 | 155 | let userThemeIdentifier = "*" |
33 | 155 | |
34 | 155 | return jsonThemes.map { let theme = CutBoxColorTheme($0) |
35 | 1 | return CutBoxColorTheme( |
36 | 1 | name: "\(theme.name) \(userThemeIdentifier)", |
37 | 1 | theme: theme |
38 | 1 | ) |
39 | 1 | } |
40 | 155 | } |
41 | | |
42 | 155 | func loadUserThemesFiles() -> [String] { |
43 | 155 | let fileManager = FileManager.default |
44 | 155 | let cutBoxConfig = String(NSString( |
45 | 155 | string: cutBoxUserThemesLocation |
46 | 155 | ).expandingTildeInPath) |
47 | 155 | |
48 | 155 | guard let themefiles = try? |
49 | 155 | fileManager.contentsOfDirectory(atPath: cutBoxConfig) else { return [] } |
50 | 1 | |
51 | 1 | let jsonThemes: [String] = themefiles |
52 | 117 | .filter { $0.hasSuffix(".cutboxTheme") } |
53 | 1 | .sorted() |
54 | 1 | .map { getStringFromFile("\(cutBoxConfig)/\($0)")! } |
55 | 1 | |
56 | 1 | return jsonThemes |
57 | 155 | } |
58 | | } |