Skip to content

Commit 18cb2d6

Browse files
author
Reed Es
committed
Simplify callbacks, to support getting child context on edit
1 parent 8413f6f commit 18cb2d6

3 files changed

Lines changed: 25 additions & 57 deletions

File tree

Sources/DetailerEditButton.swift

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public struct DetailerEditButton<Element, Content>: View
3131

3232
private let element: Element
3333
private let canEdit: CanEdit
34-
private let onEdit: OnEdit?
34+
private let onEdit: OnEdit
3535
private let label: () -> Content
3636

3737
public init(element: Element,
3838
canEdit: @escaping CanEdit = { _ in true },
39-
onEdit: OnEdit?,
39+
onEdit: @escaping OnEdit,
4040
@ViewBuilder label: @escaping () -> Content)
4141
{
4242
self.element = element
@@ -48,7 +48,7 @@ public struct DetailerEditButton<Element, Content>: View
4848
// omitting explicit Label
4949
public init(element: Element,
5050
canEdit: @escaping CanEdit = { _ in true },
51-
onEdit: OnEdit?)
51+
onEdit: @escaping OnEdit)
5252
where Content == Text // Label<Text, Image>
5353
{
5454
self.init(element: element,
@@ -60,18 +60,12 @@ public struct DetailerEditButton<Element, Content>: View
6060
}
6161
}
6262

63-
// MARK: Locals
64-
65-
private var netCanEdit: Bool {
66-
onEdit != nil && canEdit(element)
67-
}
68-
6963
// MARK: Views
7064

7165
public var body: some View {
72-
Button(action: { onEdit?(element) }) {
66+
Button(action: { onEdit(element) }) {
7367
label()
7468
}
75-
.disabled(!netCanEdit)
69+
.disabled(!canEdit(element))
7670
}
7771
}

Sources/EditDetailerContextMenu.swift

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,25 @@ public struct EditDetailerContextMenu<Element>: ViewModifier
2424
public typealias CanEdit = (Element) -> Bool
2525
public typealias CanDelete = (Element) -> Bool
2626
public typealias OnDelete = (Element.ID) -> Void
27-
27+
public typealias OnEdit = (Element) -> Void
28+
2829
private let element: Element
29-
@Binding private var toEdit: Element?
30-
private let canEdit: CanEdit
3130
private let canDelete: CanDelete
3231
private let onDelete: OnDelete?
33-
32+
private let canEdit: CanEdit
33+
private let onEdit: OnEdit
34+
3435
public init(_ element: Element,
35-
_ toEdit: Binding<Element?>,
36-
canEdit: @escaping CanEdit = { _ in true },
3736
canDelete: @escaping CanDelete = { _ in true },
38-
onDelete: OnDelete? = nil)
37+
onDelete: OnDelete? = nil,
38+
canEdit: @escaping CanEdit = { _ in true },
39+
onEdit: @escaping OnEdit)
3940
{
4041
self.element = element
41-
_toEdit = toEdit
42-
self.canEdit = canEdit
4342
self.canDelete = canDelete
4443
self.onDelete = onDelete
45-
}
46-
47-
// convenience to unwrap bound element
48-
public init(_ element: Binding<Element>,
49-
_ toEdit: Binding<Element?>,
50-
canEdit: @escaping CanEdit = { _ in true },
51-
canDelete: @escaping CanDelete = { _ in true },
52-
onDelete: OnDelete? = nil)
53-
{
54-
self.init(element.wrappedValue,
55-
toEdit,
56-
canEdit: canEdit,
57-
canDelete: canDelete,
58-
onDelete: onDelete)
44+
self.canEdit = canEdit
45+
self.onEdit = onEdit
5946
}
6047

6148
private var isDeleteAvailable: Bool {
@@ -65,7 +52,7 @@ public struct EditDetailerContextMenu<Element>: ViewModifier
6552
public func body(content: Content) -> some View {
6653
content
6754
.contextMenu {
68-
DetailerEditButton(element: element, canEdit: canEdit) { toEdit = $0 }
55+
DetailerEditButton(element: element, canEdit: canEdit) { onEdit($0) }
6956

7057
// NOTE if no delete handler, hide menu item entirely
7158
if isDeleteAvailable {

Sources/EditDetailerSwipeMenu.swift

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,25 @@ public struct EditDetailerSwipeMenu<Element>: ViewModifier
2525
public typealias CanEdit = (Element) -> Bool
2626
public typealias CanDelete = (Element) -> Bool
2727
public typealias OnDelete = (Element.ID) -> Void
28+
public typealias OnEdit = (Element) -> Void
2829

2930
private let element: Element
30-
@Binding private var toEdit: Element?
31-
private let canEdit: CanEdit
3231
private let canDelete: CanDelete
3332
private let onDelete: OnDelete?
33+
private let canEdit: CanEdit
34+
private let onEdit: OnEdit
3435

3536
public init(_ element: Element,
36-
_ toEdit: Binding<Element?>,
37-
canEdit: @escaping CanEdit = { _ in true },
3837
canDelete: @escaping CanDelete = { _ in true },
39-
onDelete: OnDelete? = nil)
38+
onDelete: OnDelete? = nil,
39+
canEdit: @escaping CanEdit = { _ in true },
40+
onEdit: @escaping OnEdit)
4041
{
4142
self.element = element
42-
_toEdit = toEdit
43-
self.canEdit = canEdit
4443
self.canDelete = canDelete
4544
self.onDelete = onDelete
46-
}
47-
48-
// convenience to unwrap bound element
49-
public init(_ element: Binding<Element>,
50-
_ toEdit: Binding<Element?>,
51-
canEdit: @escaping CanEdit = { _ in true },
52-
canDelete: @escaping CanDelete = { _ in true },
53-
onDelete: OnDelete? = nil)
54-
{
55-
self.init(element.wrappedValue,
56-
toEdit,
57-
canEdit: canEdit,
58-
canDelete: canDelete,
59-
onDelete: onDelete)
45+
self.canEdit = canEdit
46+
self.onEdit = onEdit
6047
}
6148

6249
private var isDeleteAvailable: Bool {
@@ -66,7 +53,7 @@ public struct EditDetailerSwipeMenu<Element>: ViewModifier
6653
public func body(content: Content) -> some View {
6754
content
6855
.swipeActions(edge: .leading, allowsFullSwipe: true) {
69-
DetailerEditButton(element: element, canEdit: canEdit) { toEdit = $0 }
56+
DetailerEditButton(element: element, canEdit: canEdit) { onEdit($0) }
7057
.tint(.accentColor)
7158
}
7259
.swipeActions(edge: .trailing, allowsFullSwipe: true) {

0 commit comments

Comments
 (0)