-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainView.swift
69 lines (55 loc) · 1.59 KB
/
MainView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import SwiftUI
import Northwind
@available(iOS 16.0, macOS 13, *)
struct MainView: View {
enum Section: String {
case products
}
enum Detail {
case product(Product)
}
/// The active section in the sidebar
@State private var section : Section? = .products
/// The array of products that got fetched
@State private var products : [ Product ] = []
/// We track the currently selected product
@State private var selectedProductID : Product.ID?
private var selectedProduct : Product? {
selectedProductID.flatMap { id in products.first(where: { $0.id == id })}
}
private func updateSavedProduct(_ product: Product) {
guard let idx = products.firstIndex(where: { $0.id == product.id }) else {
return // not in the list?
}
products[idx] = product
}
var body: some View {
NavigationSplitView(
sidebar: {
Sidebar(section: $section)
},
content: {
switch section {
case .products:
ProductsList(products: $products,
selectedProduct: $selectedProductID)
#if os(macOS)
.frame(minWidth: 274) // otherwise overlaps on macOS 13
#endif
case .none:
Text("No section is selected")
}
},
detail: {
if let product = selectedProduct {
ProductPage(snapshot: product, onSave: updateSavedProduct)
}
else {
Text("Nothing Selected")
.font(.title)
.foregroundColor(.accentColor)
}
}
)
}
}