|
5 | 5 | import SwiftUI
|
6 | 6 | import Sync
|
7 | 7 |
|
| 8 | + |
8 | 9 | struct ItemDestinationView: View {
|
9 |
| - private let item: Item |
| 10 | + @ObservedObject |
| 11 | + var selection: ItemSelection |
| 12 | + |
| 13 | + @ObservedObject |
| 14 | + private var settings: ReaderSettings |
| 15 | + |
| 16 | + init( |
| 17 | + selection: ItemSelection, |
| 18 | + readerSettings: ReaderSettings |
| 19 | + ) { |
| 20 | + self.selection = selection |
| 21 | + self.settings = readerSettings |
| 22 | + } |
| 23 | + |
| 24 | + private var item: Item? { |
| 25 | + selection.selectedItem |
| 26 | + } |
10 | 27 |
|
11 | 28 | private var article: Article? {
|
12 |
| - item.particle |
| 29 | + item?.particle |
| 30 | + } |
| 31 | + |
| 32 | + var characterDirection: LayoutDirection { |
| 33 | + item?.characterDirection ?? LayoutDirection.leftToRight |
13 | 34 | }
|
14 | 35 |
|
15 |
| - @Environment(\.presentationMode) @Binding |
16 |
| - private var presentationMode: PresentationMode |
17 |
| - |
18 |
| - @State |
19 |
| - private var isPresentingWebView = false |
20 |
| - |
21 |
| - @State |
22 |
| - private var isPresentingOverflow = false |
23 |
| - |
24 |
| - @StateObject |
25 |
| - private var settings = ReaderSettings() |
26 |
| - |
27 |
| - @ViewBuilder |
28 |
| - private var destinationView: some View { |
| 36 | + var body: some View { |
29 | 37 | if let article = article {
|
30 | 38 | ArticleView(article: article)
|
31 |
| - .navigationBarHidden(true) |
32 |
| - .environment(\.characterDirection, item.characterDirection) |
| 39 | + .environmentObject(settings) |
| 40 | + .environment(\.characterDirection, characterDirection) |
33 | 41 | } else {
|
34 | 42 | // TODO: Implement a view for when an article for the item doesn't exist.
|
35 | 43 | EmptyView()
|
36 | 44 | }
|
37 | 45 | }
|
38 |
| - |
39 |
| - init(item: Item) { |
40 |
| - self.item = item |
41 |
| - } |
42 |
| - |
43 |
| - var body: some View { |
44 |
| - destinationView |
45 |
| - .toolbar( |
46 |
| - item: item, |
47 |
| - presentationMode: _presentationMode.wrappedValue, |
48 |
| - isPresentingWebView: $isPresentingWebView, |
49 |
| - isPresentingOverflow: $isPresentingOverflow |
50 |
| - ).environmentObject(settings) |
51 |
| - } |
52 |
| -} |
53 |
| - |
54 |
| -private struct ItemToolbar: ViewModifier { |
55 |
| - private let item: Item |
56 |
| - |
57 |
| - @Binding |
58 |
| - private var presentationMode: PresentationMode |
59 |
| - |
60 |
| - @Binding |
61 |
| - private var isPresentingWebView: Bool |
62 |
| - |
63 |
| - @Binding |
64 |
| - private var isPresentingOverflow: Bool |
65 |
| - |
66 |
| - init( |
67 |
| - item: Item, presentationMode: Binding<PresentationMode>, |
68 |
| - isPresentingWebView: Binding<Bool>, |
69 |
| - isPresentingOverflow: Binding<Bool> |
70 |
| - ) { |
71 |
| - self.item = item |
72 |
| - _presentationMode = presentationMode |
73 |
| - _isPresentingWebView = isPresentingWebView |
74 |
| - _isPresentingOverflow = isPresentingOverflow |
75 |
| - } |
76 |
| - |
77 |
| - func body(content: Content) -> some View { |
78 |
| - content |
79 |
| - .toolbar { |
80 |
| - ToolbarItemGroup(placement: .bottomBar) { |
81 |
| - Button(action: { |
82 |
| - presentationMode.dismiss() |
83 |
| - }) { |
84 |
| - Image(systemName: "chevron.backward") |
85 |
| - } |
86 |
| - |
87 |
| - Spacer() |
88 |
| - |
89 |
| - Button(action: { |
90 |
| - guard item.url != nil else { |
91 |
| - return |
92 |
| - } |
93 |
| - |
94 |
| - isPresentingWebView = true |
95 |
| - }) { |
96 |
| - Image(systemName: "safari") |
97 |
| - } |
98 |
| - .disabled(item.url == nil) |
99 |
| - .accessibility(identifier: "web-reader-button") |
100 |
| - |
101 |
| - Button(action: { |
102 |
| - isPresentingOverflow = true |
103 |
| - }) { |
104 |
| - Image(systemName: "ellipsis") |
105 |
| - } |
106 |
| - } |
107 |
| - } |
108 |
| - .sheet(isPresented: $isPresentingWebView) { |
109 |
| - SafariView(url: item.url!).ignoresSafeArea() |
110 |
| - } |
111 |
| - .popover(isPresented: $isPresentingOverflow) { |
112 |
| - ReaderSettingsView() |
113 |
| - } |
114 |
| - } |
115 |
| -} |
116 |
| - |
117 |
| -private extension View { |
118 |
| - func toolbar( |
119 |
| - item: Item, |
120 |
| - presentationMode: Binding<PresentationMode>, |
121 |
| - isPresentingWebView: Binding<Bool>, |
122 |
| - isPresentingOverflow: Binding<Bool> |
123 |
| - ) -> some View { |
124 |
| - self.modifier( |
125 |
| - ItemToolbar( |
126 |
| - item: item, |
127 |
| - presentationMode: presentationMode, |
128 |
| - isPresentingWebView: isPresentingWebView, |
129 |
| - isPresentingOverflow: isPresentingOverflow |
130 |
| - ) |
131 |
| - ) |
132 |
| - } |
133 | 46 | }
|
0 commit comments