Skip to content

Commit cf9f62e

Browse files
committed
Wrap AppStorage in an observable object
1 parent ee429f4 commit cf9f62e

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

CustomSuggestionService/ContentView.swift

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ let store = StoreOf<TheApp>(
99
)
1010

1111
struct ContentView: View {
12-
@AppStorage(\.chatModelId) var chatModelId
13-
@AppStorage(\.installBetaBuild) var installBetaBuild
14-
@AppStorage(\.verboseLog) var verboseLog
12+
final class Settings: ObservableObject {
13+
@AppStorage(\.chatModelId) var chatModelId
14+
@AppStorage(\.installBetaBuild) var installBetaBuild
15+
@AppStorage(\.verboseLog) var verboseLog
16+
}
1517

18+
@StateObject var settings = Settings()
1619
@State var isEditingCustomModel: Bool = false
1720

1821
@Environment(\.updateChecker) var updateChecker
@@ -24,7 +27,7 @@ struct ContentView: View {
2427
Form {
2528
HStack {
2629
ExistedChatModelPicker()
27-
if CustomModelType(rawValue: chatModelId) != nil {
30+
if CustomModelType(rawValue: settings.chatModelId) != nil {
2831
Button("Edit Model") {
2932
isEditingCustomModel = true
3033
}
@@ -41,7 +44,7 @@ struct ContentView: View {
4144
)) {
4245
Text("Automatically Check for Update")
4346
}
44-
47+
4548
Button(action: {
4649
updateChecker.checkForUpdates()
4750
}) {
@@ -51,9 +54,9 @@ struct ContentView: View {
5154
}
5255
}
5356
}
54-
55-
Toggle("Install Beta Build", isOn: $installBetaBuild)
56-
Toggle("Verbose Log (Logs to Console.app)", isOn: $verboseLog)
57+
58+
Toggle("Install Beta Build", isOn: settings.$installBetaBuild)
59+
Toggle("Verbose Log (Logs to Console.app)", isOn: settings.$verboseLog)
5760
}
5861
}
5962
.formStyle(.grouped)
@@ -62,7 +65,7 @@ struct ContentView: View {
6265
.padding(.horizontal, 24)
6366
}
6467
.sheet(isPresented: $isEditingCustomModel) {
65-
if let type = CustomModelType(rawValue: chatModelId) {
68+
if let type = CustomModelType(rawValue: settings.chatModelId) {
6669
switch type {
6770
case .chatModel:
6871
ChatModelEditView(store: store.scope(
@@ -93,22 +96,26 @@ struct ContentView: View {
9396
}
9497

9598
struct ExistedChatModelPicker: View {
96-
@AppStorage(\.chatModelsFromCopilotForXcode) var chatModels: [ChatModel]
97-
@AppStorage(\.chatModelId) var chatModelId: String
99+
final class Settings: ObservableObject {
100+
@AppStorage(\.chatModelsFromCopilotForXcode) var chatModels: [ChatModel]
101+
@AppStorage(\.chatModelId) var chatModelId: String
102+
}
103+
104+
@StateObject var settings = Settings()
98105

99106
var body: some View {
100107
let unknownId: String? =
101-
if !chatModels.contains(where: { $0.id == chatModelId }),
102-
!chatModelId.isEmpty,
103-
CustomModelType(rawValue: chatModelId) == nil
108+
if !settings.chatModels.contains(where: { $0.id == settings.chatModelId }),
109+
!settings.chatModelId.isEmpty,
110+
CustomModelType(rawValue: settings.chatModelId) == nil
104111
{
105-
chatModelId
112+
settings.chatModelId
106113
} else {
107114
nil
108115
}
109116

110117
Picker(
111-
selection: $chatModelId,
118+
selection: settings.$chatModelId,
112119
label: Text("Model"),
113120
content: {
114121
if let unknownId {
@@ -126,7 +133,7 @@ struct ExistedChatModelPicker: View {
126133
}
127134
}
128135

129-
ForEach(chatModels, id: \.id) { chatModel in
136+
ForEach(settings.chatModels, id: \.id) { chatModel in
130137
Text(chatModel.name).tag(chatModel.id)
131138
}
132139
}
@@ -135,17 +142,23 @@ struct ExistedChatModelPicker: View {
135142
}
136143

137144
struct RequestStrategyPicker: View {
138-
@AppStorage(\.requestStrategyId) var requestStrategyId
145+
final class Settings: ObservableObject {
146+
@AppStorage(\.requestStrategyId) var requestStrategyId
147+
}
148+
149+
@StateObject var settings = Settings()
139150

140151
var body: some View {
141-
let unknownId: String? = if RequestStrategyOption(rawValue: requestStrategyId) == nil {
142-
requestStrategyId
152+
let unknownId: String? =
153+
if RequestStrategyOption(rawValue: settings.requestStrategyId) == nil
154+
{
155+
settings.requestStrategyId
143156
} else {
144157
nil
145158
}
146159

147160
Picker(
148-
selection: $requestStrategyId,
161+
selection: settings.$requestStrategyId,
149162
label: Text("Request Strategy"),
150163
content: {
151164
if let unknownId {

0 commit comments

Comments
 (0)