Skip to content

Commit af03ebd

Browse files
author
Reed Es
committed
Simplified design, replacing isAdd binding with originalID in context
1 parent 6609654 commit af03ebd

File tree

6 files changed

+18
-20
lines changed

6 files changed

+18
-20
lines changed

README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ struct ContentView: View {
7171
Then, to add basic support for a detail page, targeting both macOS and iOS, you'll need to:
7272

7373
* A. Import the `SwiftDetailer` and `SwiftDetailerMenu` packages.
74-
* B. Add state properties, and a typealias for cleaner code.
74+
* B. Add state property for element to edit, and a typealias for cleaner code.
7575
* C. Give each row a menu (context for macOS; swipe for iOS).
7676
* D. Add a call to `editDetailer`, available as a modifier.
7777
* E. Include a `Form` containing the fields to edit, and ...
@@ -103,7 +103,6 @@ struct ContentView: View {
103103
]
104104

105105
@State private var toEdit: Fruit? = nil // B
106-
@State private var isAdd: Bool = false
107106

108107
typealias Context = DetailerContext<Fruit>
109108

@@ -119,7 +118,7 @@ struct ContentView: View {
119118
}
120119
.editDetailer(.init(onSave: saveAction),
121120
toEdit: $toEdit,
122-
isAdd: $isAdd,
121+
originalID: toEdit?.id,
123122
detailContent: editDetail) // D
124123
}
125124

@@ -155,7 +154,7 @@ struct ContentView: View {
155154

156155
On macOS, ctrl-click (or right-click) on a row to invoke the context menu. On iOS, swipe the row to invoke the menu.
157156

158-
For a full implementation, see the _DetailerDemo_ project (link below). It extends the example with operations to add new records, delete records, and validate input.
157+
For a full implementation, with ability to add new records, see the _DetailerDemo_ project (link below). It extends the example with operations to add new records, delete records, and validate input.
159158

160159
It shows _Detailer_ used with `LazyVGrid` and `Table` containers.
161160

Sources/DetailerContext.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public struct DetailerContext<Element>
2828

2929
public let config: Config
3030
public let onValidate: OnValidate
31-
public let isAdd: Bool
31+
public let originalID: Element.ID?
3232

3333
public init(config: Config,
3434
onValidate: @escaping OnValidate,
35-
isAdd: Bool)
35+
originalID: Element.ID? = nil)
3636
{
3737
self.config = config
3838
self.onValidate = onValidate
39-
self.isAdd = isAdd
39+
self.originalID = originalID
4040
}
4141
}

Sources/Internal/EditDetailBase.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ where Element: Identifiable,
3131

3232
let config: DetailerConfig<Element>
3333
let element: Element
34-
@Binding var isAdd: Bool
34+
let originalID: Element.ID?
3535
let detailContent: DetailContent
3636

3737
// MARK: Locals
@@ -43,7 +43,7 @@ where Element: Identifiable,
4343
private var context: DetailerContext<Element> {
4444
DetailerContext<Element>(config: config,
4545
onValidate: validateAction,
46-
isAdd: isAdd)
46+
originalID: originalID)
4747
}
4848

4949
private var isDeleteAvailable: Bool {
@@ -167,7 +167,6 @@ where Element: Identifiable,
167167
}
168168

169169
private func dismissAction() {
170-
isAdd = false
171170
presentationMode.wrappedValue.dismiss()
172171
}
173172
}

Sources/Internal/EditDetailC.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ where Element: Identifiable & ObservableObject,
2828

2929
let config: DetailerConfig<Element>
3030
@ObservedObject var element: Element
31-
@Binding var isAdd: Bool
31+
let originalID: Element.ID?
3232
let detailContent: DetailContent
3333

3434
var body: some View {
3535
EditDetailBase(config: config,
3636
element: element,
37-
isAdd: $isAdd) { context in
37+
originalID: originalID) { context in
3838
detailContent(context, $element)
3939
}
4040
}

Sources/Internal/EditDetailR.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ where Element: Identifiable,
2828

2929
let config: DetailerConfig<Element>
3030
@State var element: Element
31-
@Binding var isAdd: Bool
31+
let originalID: Element.ID?
3232
let detailContent: DetailContent
3333

3434
var body: some View {
3535
EditDetailBase(config: config,
3636
element: element,
37-
isAdd: $isAdd) { context in
37+
originalID: originalID) { context in
3838
detailContent(context, $element)
3939
}
4040
}

Sources/View+Detailer.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public extension View {
2727
/// For Value data source
2828
func editDetailer<E, D>(_ config: DetailerConfig<E> = .init(),
2929
toEdit: Binding<E?>,
30-
isAdd: Binding<Bool>,
30+
originalID: E.ID? = nil,
3131
@ViewBuilder detailContent: @escaping EditContentR<E, D>) -> some View
3232
where E: Identifiable,
3333
D: View
@@ -36,13 +36,13 @@ public extension View {
3636
#if os(macOS)
3737
EditDetailR(config: config,
3838
element: element,
39-
isAdd: isAdd,
39+
originalID: originalID,
4040
detailContent: detailContent)
4141
#elseif os(iOS)
4242
NavigationView {
4343
EditDetailR(config: config,
4444
element: element,
45-
isAdd: isAdd,
45+
originalID: originalID,
4646
detailContent: detailContent)
4747
}
4848
.navigationViewStyle(StackNavigationViewStyle())
@@ -53,7 +53,7 @@ public extension View {
5353
/// For Reference data source, including Core Data
5454
func editDetailer<E, D>(_ config: DetailerConfig<E> = .init(),
5555
toEdit: Binding<E?>,
56-
isAdd: Binding<Bool>,
56+
originalID: E.ID? = nil,
5757
@ViewBuilder detailContent: @escaping EditContentC<E, D>) -> some View
5858
where E: Identifiable & ObservableObject,
5959
D: View
@@ -62,13 +62,13 @@ public extension View {
6262
#if os(macOS)
6363
EditDetailC(config: config,
6464
element: element,
65-
isAdd: isAdd,
65+
originalID: originalID,
6666
detailContent: detailContent)
6767
#elseif os(iOS)
6868
NavigationView {
6969
EditDetailC(config: config,
7070
element: element,
71-
isAdd: isAdd,
71+
originalID: originalID,
7272
detailContent: detailContent)
7373
}
7474
.navigationViewStyle(StackNavigationViewStyle())

0 commit comments

Comments
 (0)