Skip to content

Commit fdbbc8b

Browse files
committed
Add information about a subtab's id for the remove action in the standard actions
1 parent 8f031e4 commit fdbbc8b

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

Documentation/Reference/SettingsKit/structs/SettingsTab.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,16 @@ Add actions to the settings sidebar by providing an array.
233233
```swift
234234
public func standardActions(
235235
add: @escaping () -> Void,
236-
remove: @escaping (Int?) -> Void,
236+
remove: @escaping (String?, Int?) -> Void,
237237
options: (() -> Void)? = nil
238238
) -> Self
239239
```
240240

241241
The standard set of actions with an add button, a remove button and optionally an options button.
242242
- Parameters:
243243
- add: The action that is called when the add button is pressed.
244-
- remove: The action that is called when the remove button is pressed, giving the the selected subtab's index.
244+
- remove: The action that is called when the remove button is pressed,
245+
giving the the selected subtab's id and index.
245246
- options: The action that is called when the options button is pressed.
246247
If it is nil, there is no options button.
247248
- Returns: The new tab with the actions.
@@ -251,23 +252,24 @@ The standard set of actions with an add button, a remove button and optionally a
251252
| Name | Description |
252253
| ---- | ----------- |
253254
| add | The action that is called when the add button is pressed. |
254-
| remove | The action that is called when the remove button is pressed, giving the the selected subtab’s index. |
255+
| remove | The action that is called when the remove button is pressed, giving the the selected subtab’s id and index. |
255256
| options | The action that is called when the options button is pressed. If it is nil, there is no options button. |
256257

257258
### `standardActions(add:remove:options:)`
258259

259260
```swift
260261
public func standardActions<ContentView>(
261262
@ViewBuilder add: @escaping () -> ContentView,
262-
remove: @escaping (Int?) -> Void,
263+
remove: @escaping (String?, Int?) -> Void,
263264
options: (() -> Void)? = nil
264265
) -> Self where ContentView: View
265266
```
266267

267268
The standard set of actions with an add menu, a remove button and optionally an options button.
268269
- Parameters:
269270
- add: The menu that is opened when the add button is pressed.
270-
- remove: The action that is called when the remove button is pressed, giving the the selected subtab's index.
271+
- remove: The action that is called when the remove button is pressed,
272+
giving the the selected subtab's id and index.
271273
- options: The action that is called when the options button is pressed.
272274
If it is nil, there is no options button.
273275
- Returns: The new tab with the actions.
@@ -277,7 +279,7 @@ The standard set of actions with an add menu, a remove button and optionally an
277279
| Name | Description |
278280
| ---- | ----------- |
279281
| add | The menu that is opened when the add button is pressed. |
280-
| remove | The action that is called when the remove button is pressed, giving the the selected subtab’s index. |
282+
| remove | The action that is called when the remove button is pressed, giving the the selected subtab’s id and index. |
281283
| options | The action that is called when the options button is pressed. If it is nil, there is no options button. |
282284

283285
### `frame(width:height:)`

Sources/SettingsKit/Model/Data/SettingsTab.swift

+11-9
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,14 @@ public struct SettingsTab: Identifiable, View {
223223
/// The standard set of actions with an add button, a remove button and optionally an options button.
224224
/// - Parameters:
225225
/// - add: The action that is called when the add button is pressed.
226-
/// - remove: The action that is called when the remove button is pressed, giving the the selected subtab's index.
226+
/// - remove: The action that is called when the remove button is pressed,
227+
/// giving the the selected subtab's id and index.
227228
/// - options: The action that is called when the options button is pressed.
228229
/// If it is nil, there is no options button.
229230
/// - Returns: The new tab with the actions.
230231
public func standardActions(
231232
add: @escaping () -> Void,
232-
remove: @escaping (Int?) -> Void,
233+
remove: @escaping (String?, Int?) -> Void,
233234
options: (() -> Void)? = nil
234235
) -> Self {
235236
actions {
@@ -243,7 +244,8 @@ public struct SettingsTab: Identifiable, View {
243244
.init(localized: "Remove", comment: "SettingsTab (Label of the standard \"Remove\" action)"),
244245
systemSymbol: .minus
245246
) {
246-
remove(content.firstIndex { $0.id == SettingsModel.shared.selectedSubtabs[id] })
247+
let index = content.firstIndex { $0.id == SettingsModel.shared.selectedSubtabs[id] }
248+
remove(content[safe: index]?.id, index)
247249
}
248250
}
249251
.spacer()
@@ -265,13 +267,14 @@ public struct SettingsTab: Identifiable, View {
265267
/// The standard set of actions with an add menu, a remove button and optionally an options button.
266268
/// - Parameters:
267269
/// - add: The menu that is opened when the add button is pressed.
268-
/// - remove: The action that is called when the remove button is pressed, giving the the selected subtab's index.
270+
/// - remove: The action that is called when the remove button is pressed,
271+
/// giving the the selected subtab's id and index.
269272
/// - options: The action that is called when the options button is pressed.
270273
/// If it is nil, there is no options button.
271274
/// - Returns: The new tab with the actions.
272275
public func standardActions<ContentView>(
273276
@ViewBuilder add: @escaping () -> ContentView,
274-
remove: @escaping (Int?) -> Void,
277+
remove: @escaping (String?, Int?) -> Void,
275278
options: (() -> Void)? = nil
276279
) -> Self where ContentView: View {
277280
actions {
@@ -282,14 +285,13 @@ public struct SettingsTab: Identifiable, View {
282285
comment: "SettingsTab (Label of the standard \"Add\" action)"
283286
),
284287
systemSymbol: .plus
285-
) {
286-
add()
287-
}
288+
) { add() }
288289
ToolbarAction(
289290
.init(localized: "Remove", comment: "SettingsTab (Label of the standard \"Remove\" action)"),
290291
systemSymbol: .minus
291292
) {
292-
remove(content.firstIndex { $0.id == SettingsModel.shared.selectedSubtabs[id] })
293+
let index = content.firstIndex { $0.id == SettingsModel.shared.selectedSubtabs[id] }
294+
remove(content[safe: index]?.id, index)
293295
}
294296
}
295297
.spacer()

Tests/Examples/Examples/ExamplesApp.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct ExamplesApp: App {
4444
}
4545
.standardActions {
4646
accountsCount += 1
47-
} remove: { _ in
47+
} remove: { _, _ in
4848
if accountsCount > 0 {
4949
accountsCount -= 1
5050
}

Tests/TestApp/TestApp/ViewModel/TestAppModel.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class TestAppModel: ObservableObject {
4242
self.settings.append(self.colorTab)
4343
}
4444
}
45-
} remove: { id in
46-
if let id {
47-
self.settings.remove(at: id)
45+
} remove: { _, index in
46+
if let index {
47+
self.settings.remove(at: index)
4848
}
4949
}
5050

0 commit comments

Comments
 (0)