Skip to content

Commit f88a81a

Browse files
committed
Moved view functions to ThemeModel
1 parent 450b1cc commit f88a81a

5 files changed

Lines changed: 114 additions & 85 deletions

File tree

CodeEdit/Features/Settings/Pages/ThemeSettings/Models/ThemeModel+CRUD.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,14 @@ extension ThemeModel {
202202
if var index = self.themes.firstIndex(where: { $0.fileURL == destinationFileURL }) {
203203
self.themes[index].displayName = newFileName
204204
self.themes[index].name = newFileName.lowercased().replacingOccurrences(of: " ", with: "-")
205+
205206
if isImporting != true {
206207
self.themes[index].author = NSFullUserName()
207208
self.save(self.themes[index])
208209
}
209-
self.selectedTheme = self.themes[index]
210+
211+
activateTheme(self.themes[index])
212+
210213
self.detailsTheme = self.themes[index]
211214
}
212215
} catch {

CodeEdit/Features/Settings/Pages/ThemeSettings/Models/ThemeModel.swift

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ import SwiftUI
1717
final class ThemeModel: ObservableObject {
1818
static let shared: ThemeModel = .init()
1919

20+
@Environment(\.colorScheme)
21+
var colorScheme
22+
23+
@AppSettings(\.theme)
24+
var settings
25+
2026
/// Default instance of the `FileManager`
2127
let filemanager = FileManager.default
2228

@@ -70,18 +76,7 @@ final class ThemeModel: ObservableObject {
7076

7177
@Published var isAdding: Bool = false
7278

73-
@Published var detailsTheme: Theme? {
74-
didSet {
75-
if detailsTheme == nil {
76-
isAdding = false
77-
}
78-
}
79-
}
80-
81-
/// The selected appearance in the sidebar.
82-
/// - **0**: dark mode themes
83-
/// - **1**: light mode themes
84-
@Published var selectedAppearance: Int = 0
79+
@Published var detailsTheme: Theme?
8580

8681
/// An array of loaded ``Theme``.
8782
@Published var themes: [Theme] = []
@@ -130,4 +125,44 @@ final class ThemeModel: ObservableObject {
130125
self.save(self.themes[index])
131126
}
132127
}
128+
129+
@Published var selectedAppearance: ThemeSettingsAppearances = .dark
130+
131+
enum ThemeSettingsAppearances: String, CaseIterable {
132+
case light = "Light Appearance"
133+
case dark = "Dark Appearance"
134+
}
135+
136+
func getThemeActive (_ theme: Theme) -> Bool {
137+
if settings.matchAppearance {
138+
return selectedAppearance == .dark
139+
? selectedDarkTheme == theme
140+
: selectedAppearance == .light
141+
? selectedLightTheme == theme
142+
: selectedTheme == theme
143+
}
144+
return selectedTheme == theme
145+
}
146+
147+
func activateTheme (_ theme: Theme) {
148+
if settings.matchAppearance {
149+
if selectedAppearance == .dark {
150+
selectedDarkTheme = theme
151+
} else if selectedAppearance == .light {
152+
selectedLightTheme = theme
153+
}
154+
if (selectedAppearance == .dark && colorScheme == .dark)
155+
|| (selectedAppearance == .light && colorScheme == .light) {
156+
selectedTheme = theme
157+
}
158+
} else {
159+
selectedTheme = theme
160+
if colorScheme == .light {
161+
selectedLightTheme = theme
162+
}
163+
if colorScheme == .dark {
164+
selectedDarkTheme = theme
165+
}
166+
}
167+
}
133168
}

CodeEdit/Features/Settings/Pages/ThemeSettings/ThemeSettingThemeRow.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import SwiftUI
1010
struct ThemeSettingsThemeRow: View {
1111
@Binding var theme: Theme
1212
var active: Bool
13-
var action: (Theme) -> Void
1413

1514
@ObservedObject private var themeModel: ThemeModel = .shared
1615

@@ -32,7 +31,7 @@ struct ThemeSettingsThemeRow: View {
3231
.frame(maxWidth: .infinity, alignment: .leading)
3332
if !active {
3433
Button {
35-
action(theme)
34+
themeModel.activateTheme(theme)
3635
} label: {
3736
Text("Choose")
3837
}

CodeEdit/Features/Settings/Pages/ThemeSettings/ThemeSettingsThemeDetails.swift

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,27 @@ struct ThemeSettingsThemeDetails: View {
134134
.formStyle(.grouped)
135135
Divider()
136136
HStack {
137-
if !themeModel.isAdding {
137+
if theme.isBundled {
138+
HStack {
139+
Image(systemName: "exclamationmark.triangle.fill")
140+
.font(.body)
141+
.foregroundStyle(Color.yellow)
142+
Text("Duplicate this theme to make changes.")
143+
.font(.subheadline)
144+
.lineLimit(2)
145+
}
146+
.help("Bundled themes must be duplicated to make changes.")
147+
.accessibilityElement(children: .combine)
148+
.accessibilityLabel("Warning: Duplicate this theme to make changes.")
149+
} else if !themeModel.isAdding {
150+
Button(role: .destructive) {
151+
themeModel.delete(theme)
152+
dismiss()
153+
} label: {
154+
Text("Delete")
155+
.foregroundStyle(.red)
156+
.frame(minWidth: 56)
157+
}
138158
Button {
139159
if let fileURL = theme.fileURL {
140160
themeModel.duplicate(fileURL)
@@ -143,33 +163,36 @@ struct ThemeSettingsThemeDetails: View {
143163
Text("Duplicate")
144164
.frame(minWidth: 56)
145165
}
146-
if !theme.isBundled {
147-
Button(role: .destructive) {
148-
themeModel.delete(theme)
149-
dismiss()
150-
} label: {
151-
Text("Delete")
152-
.foregroundStyle(.red)
153-
.frame(minWidth: 56)
154-
}
155-
}
156166
}
157167
Spacer()
158-
Button {
159-
if themeModel.isAdding {
160-
themeModel.delete(theme)
161-
} else {
162-
themeModel.cancelDetails(theme)
168+
if !themeModel.isAdding && theme.isBundled {
169+
Button {
170+
if let fileURL = theme.fileURL {
171+
themeModel.duplicate(fileURL)
172+
}
173+
} label: {
174+
Text("Duplicate")
175+
.frame(minWidth: 56)
163176
}
177+
} else {
178+
Button {
179+
if themeModel.isAdding {
180+
themeModel.delete(theme)
181+
} else {
182+
themeModel.cancelDetails(theme)
183+
}
164184

165-
dismiss()
166-
} label: {
167-
Text("Cancel")
168-
.frame(minWidth: 56)
185+
dismiss()
186+
} label: {
187+
Text("Cancel")
188+
.frame(minWidth: 56)
189+
}
190+
.buttonStyle(.bordered)
169191
}
170-
.buttonStyle(.bordered)
171192
Button {
172-
themeModel.rename(to: theme.displayName, theme: theme)
193+
if !theme.isBundled {
194+
themeModel.rename(to: theme.displayName, theme: theme)
195+
}
173196
dismiss()
174197
} label: {
175198
Text("Done")

CodeEdit/Features/Settings/Pages/ThemeSettings/ThemeSettingsView.swift

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,6 @@ struct ThemeSettingsView: View {
1818
var useDarkTerminalAppearance
1919

2020
@State private var listView: Bool = false
21-
@State private var selectedAppearance: ThemeSettingsAppearances = .dark
22-
23-
enum ThemeSettingsAppearances: String, CaseIterable {
24-
case light = "Light Appearance"
25-
case dark = "Dark Appearance"
26-
}
27-
28-
func getThemeActive (_ theme: Theme) -> Bool {
29-
if settings.matchAppearance {
30-
return selectedAppearance == .dark
31-
? themeModel.selectedDarkTheme == theme
32-
: selectedAppearance == .light
33-
? themeModel.selectedLightTheme == theme
34-
: themeModel.selectedTheme == theme
35-
}
36-
return themeModel.selectedTheme == theme
37-
}
38-
39-
func activateTheme (_ theme: Theme) {
40-
if settings.matchAppearance {
41-
if selectedAppearance == .dark {
42-
themeModel.selectedDarkTheme = theme
43-
} else if selectedAppearance == .light {
44-
themeModel.selectedLightTheme = theme
45-
}
46-
if (selectedAppearance == .dark && colorScheme == .dark)
47-
|| (selectedAppearance == .light && colorScheme == .light) {
48-
themeModel.selectedTheme = theme
49-
}
50-
} else {
51-
themeModel.selectedTheme = theme
52-
if colorScheme == .light {
53-
themeModel.selectedLightTheme = theme
54-
}
55-
if colorScheme == .dark {
56-
themeModel.selectedDarkTheme = theme
57-
}
58-
}
59-
}
6021

6122
var body: some View {
6223
SettingsForm {
@@ -70,8 +31,8 @@ struct ThemeSettingsView: View {
7031
Section {
7132
VStack(spacing: 0) {
7233
if settings.matchAppearance {
73-
Picker("", selection: $selectedAppearance) {
74-
ForEach(ThemeSettingsAppearances.allCases, id: \.self) { tab in
34+
Picker("", selection: $themeModel.selectedAppearance) {
35+
ForEach(ThemeModel.ThemeSettingsAppearances.allCases, id: \.self) { tab in
7536
Text(tab.rawValue)
7637
.tag(tab)
7738
}
@@ -81,20 +42,26 @@ struct ThemeSettingsView: View {
8142
.padding(10)
8243
}
8344
VStack(spacing: 0) {
84-
ForEach(selectedAppearance == .dark ? themeModel.darkThemes : themeModel.lightThemes) { theme in
45+
ForEach(
46+
themeModel.selectedAppearance == .dark
47+
? themeModel.darkThemes
48+
: themeModel.lightThemes
49+
) { theme in
8550
Divider().padding(.horizontal, 10)
8651
ThemeSettingsThemeRow(
8752
theme: $themeModel.themes[themeModel.themes.firstIndex(of: theme)!],
88-
active: getThemeActive(theme),
89-
action: activateTheme
53+
active: themeModel.getThemeActive(theme)
9054
).id(theme)
9155
}
92-
ForEach(selectedAppearance == .dark ? themeModel.lightThemes : themeModel.darkThemes) { theme in
56+
ForEach(
57+
themeModel.selectedAppearance == .dark
58+
? themeModel.lightThemes
59+
: themeModel.darkThemes
60+
) { theme in
9361
Divider().padding(.horizontal, 10)
9462
ThemeSettingsThemeRow(
9563
theme: $themeModel.themes[themeModel.themes.firstIndex(of: theme)!],
96-
active: getThemeActive(theme),
97-
action: activateTheme
64+
active: themeModel.getThemeActive(theme)
9865
).id(theme)
9966
}
10067
}
@@ -110,7 +77,9 @@ struct ThemeSettingsView: View {
11077
.padding(.top, 10)
11178
}
11279
}
113-
.sheet(item: $themeModel.detailsTheme) { theme in
80+
.sheet(item: $themeModel.detailsTheme) {
81+
themeModel.isAdding = false
82+
} content: { theme in
11483
if let index = themeModel.themes.firstIndex(where: {
11584
$0.fileURL?.absoluteString == theme.fileURL?.absoluteString
11685
}) {

0 commit comments

Comments
 (0)