Skip to content

Commit bb4aba5

Browse files
committed
refactor(plugin): replace inline Picker with RefreshIntervalPicker in all plugins
Apply the shared RefreshIntervalPicker component to CPU, Memory, Disk, GPU, and Battery plugin config views, removing duplicated @State and onChange boilerplate from each.
1 parent bde6b2a commit bb4aba5

5 files changed

Lines changed: 45 additions & 81 deletions

File tree

DockMasterPro/Plugins/BatteryHealthPlugin/BatteryHealthPlugin.swift

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -212,28 +212,20 @@ class BatteryHealthPlugin: WidgetPlugin, PluginLocalizable, ObservableObject {
212212
struct BatteryPluginConfigView: View {
213213
let plugin: BatteryHealthPlugin
214214

215-
/// 可选的刷新间隔(秒)
216215
private let options: [TimeInterval] = [1, 3, 5, 10, 30, 60]
217216

218-
@State private var selected: TimeInterval
219-
220-
init(plugin: BatteryHealthPlugin) {
221-
self.plugin = plugin
222-
self._selected = State(initialValue: plugin.refreshInterval)
223-
}
224-
225217
var body: some View {
226218
Form {
227219
Section {
228-
Picker(plugin.localized("config.refreshInterval"), selection: $selected) {
229-
ForEach(options, id: \.self) { interval in
230-
Text(plugin.localized("config.seconds", Int(interval)))
231-
.tag(interval)
232-
}
233-
}
234-
.onChange(of: selected) { newValue in
235-
plugin.refreshInterval = newValue
236-
}
220+
RefreshIntervalPicker(
221+
label: plugin.localized("config.refreshInterval"),
222+
options: options,
223+
value: Binding(
224+
get: { plugin.refreshInterval },
225+
set: { plugin.refreshInterval = $0 }
226+
),
227+
formatOption: { plugin.localized("config.seconds", Int($0)) }
228+
)
237229
} header: {
238230
Text(plugin.localized("config.title"))
239231
}

DockMasterPro/Plugins/CPUDetailPlugin/CPUDetailPlugin.swift

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -287,25 +287,18 @@ struct CPUPluginConfigView: View {
287287

288288
private let options: [TimeInterval] = [1, 2, 3, 5, 10, 30]
289289

290-
@State private var selected: TimeInterval
291-
292-
init(plugin: CPUDetailPlugin) {
293-
self.plugin = plugin
294-
self._selected = State(initialValue: plugin.refreshInterval)
295-
}
296-
297290
var body: some View {
298291
Form {
299292
Section {
300-
Picker(plugin.localized("config.refreshInterval"), selection: $selected) {
301-
ForEach(options, id: \.self) { interval in
302-
Text(plugin.localized("config.seconds", Int(interval)))
303-
.tag(interval)
304-
}
305-
}
306-
.onChange(of: selected) { newValue in
307-
plugin.refreshInterval = newValue
308-
}
293+
RefreshIntervalPicker(
294+
label: plugin.localized("config.refreshInterval"),
295+
options: options,
296+
value: Binding(
297+
get: { plugin.refreshInterval },
298+
set: { plugin.refreshInterval = $0 }
299+
),
300+
formatOption: { plugin.localized("config.seconds", Int($0)) }
301+
)
309302
} header: {
310303
Text(plugin.localized("config.title"))
311304
}

DockMasterPro/Plugins/DiskDetailPlugin/DiskDetailPlugin.swift

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -226,25 +226,18 @@ struct DiskPluginConfigView: View {
226226

227227
private let options: [TimeInterval] = [1, 2, 3, 5, 10, 30]
228228

229-
@State private var selected: TimeInterval
230-
231-
init(plugin: DiskDetailPlugin) {
232-
self.plugin = plugin
233-
self._selected = State(initialValue: plugin.refreshInterval)
234-
}
235-
236229
var body: some View {
237230
Form {
238231
Section {
239-
Picker(plugin.localized("config.refreshInterval"), selection: $selected) {
240-
ForEach(options, id: \.self) { interval in
241-
Text(plugin.localized("config.seconds", Int(interval)))
242-
.tag(interval)
243-
}
244-
}
245-
.onChange(of: selected) { newValue in
246-
plugin.refreshInterval = newValue
247-
}
232+
RefreshIntervalPicker(
233+
label: plugin.localized("config.refreshInterval"),
234+
options: options,
235+
value: Binding(
236+
get: { plugin.refreshInterval },
237+
set: { plugin.refreshInterval = $0 }
238+
),
239+
formatOption: { plugin.localized("config.seconds", Int($0)) }
240+
)
248241
} header: {
249242
Text(plugin.localized("config.title"))
250243
}

DockMasterPro/Plugins/GPUDetailPlugin/GPUDetailPlugin.swift

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -245,25 +245,18 @@ struct GPUPluginConfigView: View {
245245

246246
private let options: [TimeInterval] = [1, 2, 3, 5, 10, 30]
247247

248-
@State private var selected: TimeInterval
249-
250-
init(plugin: GPUDetailPlugin) {
251-
self.plugin = plugin
252-
self._selected = State(initialValue: plugin.refreshInterval)
253-
}
254-
255248
var body: some View {
256249
Form {
257250
Section {
258-
Picker(plugin.localized("config.refreshInterval"), selection: $selected) {
259-
ForEach(options, id: \.self) { interval in
260-
Text(plugin.localized("config.seconds", Int(interval)))
261-
.tag(interval)
262-
}
263-
}
264-
.onChange(of: selected) { newValue in
265-
plugin.refreshInterval = newValue
266-
}
251+
RefreshIntervalPicker(
252+
label: plugin.localized("config.refreshInterval"),
253+
options: options,
254+
value: Binding(
255+
get: { plugin.refreshInterval },
256+
set: { plugin.refreshInterval = $0 }
257+
),
258+
formatOption: { plugin.localized("config.seconds", Int($0)) }
259+
)
267260
} header: {
268261
Text(plugin.localized("config.title"))
269262
}

DockMasterPro/Plugins/MemoryDetailPlugin/MemoryDetailPlugin.swift

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -218,25 +218,18 @@ struct MemoryPluginConfigView: View {
218218

219219
private let options: [TimeInterval] = [1, 2, 3, 5, 10, 30]
220220

221-
@State private var selected: TimeInterval
222-
223-
init(plugin: MemoryDetailPlugin) {
224-
self.plugin = plugin
225-
self._selected = State(initialValue: plugin.refreshInterval)
226-
}
227-
228221
var body: some View {
229222
Form {
230223
Section {
231-
Picker(plugin.localized("config.refreshInterval"), selection: $selected) {
232-
ForEach(options, id: \.self) { interval in
233-
Text(plugin.localized("config.seconds", Int(interval)))
234-
.tag(interval)
235-
}
236-
}
237-
.onChange(of: selected) { newValue in
238-
plugin.refreshInterval = newValue
239-
}
224+
RefreshIntervalPicker(
225+
label: plugin.localized("config.refreshInterval"),
226+
options: options,
227+
value: Binding(
228+
get: { plugin.refreshInterval },
229+
set: { plugin.refreshInterval = $0 }
230+
),
231+
formatOption: { plugin.localized("config.seconds", Int($0)) }
232+
)
240233
} header: {
241234
Text(plugin.localized("config.title"))
242235
}

0 commit comments

Comments
 (0)