forked from kean/Pulse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleListView.swift
183 lines (162 loc) · 6.27 KB
/
ConsoleListView.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// The MIT License (MIT)
//
// Copyright (c) 2020-2024 Alexander Grebenyuk (github.com/kean).
#if os(iOS) || os(macOS) || os(visionOS)
import SwiftUI
import CoreData
import Pulse
import Combine
@available(iOS 15, macOS 13, visionOS 1.0, *)
struct ConsoleListView: View {
@EnvironmentObject var environment: ConsoleEnvironment
@EnvironmentObject var filters: ConsoleFiltersViewModel
var body: some View {
_InternalConsoleListView(environment: environment, filters: filters)
}
}
@available(iOS 15, macOS 13, visionOS 1.0, *)
private struct _InternalConsoleListView: View {
private let environment: ConsoleEnvironment
@StateObject private var listViewModel: IgnoringUpdates<ConsoleListViewModel>
@StateObject private var searchBarViewModel: ConsoleSearchBarViewModel
@StateObject private var searchViewModel: IgnoringUpdates<ConsoleSearchViewModel>
init(environment: ConsoleEnvironment, filters: ConsoleFiltersViewModel) {
self.environment = environment
let listViewModel = ConsoleListViewModel(environment: environment, filters: filters)
let searchBarViewModel = ConsoleSearchBarViewModel()
let searchViewModel = ConsoleSearchViewModel(environment: environment, source: listViewModel, searchBar: searchBarViewModel)
_listViewModel = StateObject(wrappedValue: IgnoringUpdates(listViewModel))
_searchBarViewModel = StateObject(wrappedValue: searchBarViewModel)
_searchViewModel = StateObject(wrappedValue: IgnoringUpdates(searchViewModel))
}
var body: some View {
contents
.environmentObject(listViewModel.value)
.environmentObject(searchViewModel.value)
.environmentObject(searchBarViewModel)
.onAppear { listViewModel.value.isViewVisible = true }
.onDisappear { listViewModel.value.isViewVisible = false }
}
@ViewBuilder private var contents: some View {
if #available(iOS 16, *) {
_ConsoleListView()
.environment(\.defaultMinListRowHeight, 8)
.searchable(text: $searchBarViewModel.text, tokens: $searchBarViewModel.tokens, token: {
if let image = $0.systemImage {
Label($0.title, systemImage: image)
} else {
Text($0.title)
}
})
#if os(macOS)
.searchSuggestions {
ConsoleSearchSuggestionsView()
}
#endif
.onSubmit(of: .search, searchViewModel.value.onSubmitSearch)
.disableAutocorrection(true)
#if os(iOS) || os(visionOS)
.textInputAutocapitalization(.never)
#endif
} else {
_ConsoleListView()
.searchable(text: $searchBarViewModel.text)
.onSubmit(of: .search, searchViewModel.value.onSubmitSearch)
.disableAutocorrection(true)
#if os(iOS) || os(visionOS)
.textInputAutocapitalization(.never)
#endif
}
}
}
#endif
#if os(iOS) || os(visionOS)
@available(iOS 15, visionOS 1.0, *)
private struct _ConsoleListView: View {
@Environment(\.isSearching) private var isSearching
@Environment(\.store) private var store
@ObservedObject private var syncSession = WatchConnectivityService.shared
@State private var presentedStore: LoggerStore?
var body: some View {
List {
if isSearching {
ConsoleSearchListContentView()
} else {
ConsoleToolbarView()
.listRowSeparator(.hidden, edges: .top)
if store === LoggerStore.shared, let storeURL = syncSession.importedStoreURL {
buttonShowImportedStore(storeURL: storeURL)
}
ConsoleListContentView()
}
}
.listStyle(.plain)
.sheet(item: $presentedStore) { store in
NavigationView {
ConsoleView(store: store)
}
}
}
private func buttonShowImportedStore(storeURL: URL) -> some View {
HStack {
Button(action: {
presentedStore = try? LoggerStore(storeURL: storeURL, options: [.readonly])
}) {
HStack {
Text(Image(systemName: "applewatch"))
Text("Show Imported Store")
}.foregroundColor(.accentColor)
}.buttonStyle(.plain)
Spacer()
Button(role: .destructive, action: syncSession.removeImportedDocument) {
Image(systemName: "trash")
}
}
}
}
#endif
#if os(macOS)
@available(iOS 15, macOS 13, visionOS 1.0, *)
private struct _ConsoleListView: View {
@EnvironmentObject private var environment: ConsoleEnvironment
@EnvironmentObject private var listViewModel: ConsoleListViewModel
@EnvironmentObject private var searchViewModel: ConsoleSearchViewModel
@State private var selectedObjectID: NSManagedObjectID? // Has to use for Table
@State private var selection: ConsoleSelectedItem?
@State private var shareItems: ShareItems?
@Environment(\.isSearching) private var isSearching
var body: some View {
content
.onChange(of: selectedObjectID) {
environment.router.selection = $0.map(ConsoleSelectedItem.entity)
}
.onChange(of: selection) {
environment.router.selection = $0
}
.onChange(of: isSearching) {
searchViewModel.isSearchActive = $0
}
}
@ViewBuilder
private var content: some View {
VStack(spacing: 0) {
if isSearching && !searchViewModel.parameters.isEmpty {
ConsoleSearchToolbar()
} else {
ConsoleToolbarView()
}
Divider()
ScrollViewReader { proxy in
List(selection: $selection) {
if isSearching && !searchViewModel.parameters.isEmpty {
ConsoleSearchResultsListContentView()
} else {
ConsoleListContentView(proxy: proxy)
}
}.scrollContentBackground(.hidden)
}
.environment(\.defaultMinListRowHeight, 1)
}
}
}
#endif