Skip to content

Commit

Permalink
Simplified design, replacing isAdd binding with originalID in context
Browse files Browse the repository at this point in the history
  • Loading branch information
Reed Es committed Apr 3, 2022
1 parent 6609654 commit af03ebd
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 20 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct ContentView: View {
Then, to add basic support for a detail page, targeting both macOS and iOS, you'll need to:

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

@State private var toEdit: Fruit? = nil // B
@State private var isAdd: Bool = false

typealias Context = DetailerContext<Fruit>

Expand All @@ -119,7 +118,7 @@ struct ContentView: View {
}
.editDetailer(.init(onSave: saveAction),
toEdit: $toEdit,
isAdd: $isAdd,
originalID: toEdit?.id,
detailContent: editDetail) // D
}

Expand Down Expand Up @@ -155,7 +154,7 @@ struct ContentView: View {

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

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.
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.

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

Expand Down
6 changes: 3 additions & 3 deletions Sources/DetailerContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public struct DetailerContext<Element>

public let config: Config
public let onValidate: OnValidate
public let isAdd: Bool
public let originalID: Element.ID?

public init(config: Config,
onValidate: @escaping OnValidate,
isAdd: Bool)
originalID: Element.ID? = nil)
{
self.config = config
self.onValidate = onValidate
self.isAdd = isAdd
self.originalID = originalID
}
}
5 changes: 2 additions & 3 deletions Sources/Internal/EditDetailBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ where Element: Identifiable,

let config: DetailerConfig<Element>
let element: Element
@Binding var isAdd: Bool
let originalID: Element.ID?
let detailContent: DetailContent

// MARK: Locals
Expand All @@ -43,7 +43,7 @@ where Element: Identifiable,
private var context: DetailerContext<Element> {
DetailerContext<Element>(config: config,
onValidate: validateAction,
isAdd: isAdd)
originalID: originalID)
}

private var isDeleteAvailable: Bool {
Expand Down Expand Up @@ -167,7 +167,6 @@ where Element: Identifiable,
}

private func dismissAction() {
isAdd = false
presentationMode.wrappedValue.dismiss()
}
}
4 changes: 2 additions & 2 deletions Sources/Internal/EditDetailC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ where Element: Identifiable & ObservableObject,

let config: DetailerConfig<Element>
@ObservedObject var element: Element
@Binding var isAdd: Bool
let originalID: Element.ID?
let detailContent: DetailContent

var body: some View {
EditDetailBase(config: config,
element: element,
isAdd: $isAdd) { context in
originalID: originalID) { context in
detailContent(context, $element)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Internal/EditDetailR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ where Element: Identifiable,

let config: DetailerConfig<Element>
@State var element: Element
@Binding var isAdd: Bool
let originalID: Element.ID?
let detailContent: DetailContent

var body: some View {
EditDetailBase(config: config,
element: element,
isAdd: $isAdd) { context in
originalID: originalID) { context in
detailContent(context, $element)
}
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/View+Detailer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public extension View {
/// For Value data source
func editDetailer<E, D>(_ config: DetailerConfig<E> = .init(),
toEdit: Binding<E?>,
isAdd: Binding<Bool>,
originalID: E.ID? = nil,
@ViewBuilder detailContent: @escaping EditContentR<E, D>) -> some View
where E: Identifiable,
D: View
Expand All @@ -36,13 +36,13 @@ public extension View {
#if os(macOS)
EditDetailR(config: config,
element: element,
isAdd: isAdd,
originalID: originalID,
detailContent: detailContent)
#elseif os(iOS)
NavigationView {
EditDetailR(config: config,
element: element,
isAdd: isAdd,
originalID: originalID,
detailContent: detailContent)
}
.navigationViewStyle(StackNavigationViewStyle())
Expand All @@ -53,7 +53,7 @@ public extension View {
/// For Reference data source, including Core Data
func editDetailer<E, D>(_ config: DetailerConfig<E> = .init(),
toEdit: Binding<E?>,
isAdd: Binding<Bool>,
originalID: E.ID? = nil,
@ViewBuilder detailContent: @escaping EditContentC<E, D>) -> some View
where E: Identifiable & ObservableObject,
D: View
Expand All @@ -62,13 +62,13 @@ public extension View {
#if os(macOS)
EditDetailC(config: config,
element: element,
isAdd: isAdd,
originalID: originalID,
detailContent: detailContent)
#elseif os(iOS)
NavigationView {
EditDetailC(config: config,
element: element,
isAdd: isAdd,
originalID: originalID,
detailContent: detailContent)
}
.navigationViewStyle(StackNavigationViewStyle())
Expand Down

0 comments on commit af03ebd

Please sign in to comment.