-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathInboxView+Views.swift
242 lines (231 loc) · 8.49 KB
/
InboxView+Views.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//
// InboxView+Views.swift
// Mlem
//
// Created by Sjmarf on 22/09/2024.
//
import MlemMiddleware
import SwiftUI
extension InboxView {
@ViewBuilder
var inboxFeedView: some View {
LazyVStack(spacing: 0, pinnedViews: [.sectionHeaders]) {
Section {
ForEach(feedLoader.items, id: \.inboxId) { item in
Group {
switch item {
case let .message(message):
MessageView(message: message, isInInbox: true)
case let .reply(reply):
ReplyView(reply: reply)
}
}
.padding([.horizontal, .bottom], Constants.main.standardSpacing)
.onAppear {
do {
try inboxFeedLoader.loadIfThreshold(item)
} catch {
handleError(error)
}
}
}
EndOfFeedView(loadingState: feedLoader.loadingState, loadMore: nil, viewType: .cartoon)
} header: { sectionHeader }
}
}
@ViewBuilder
var modMailFeedView: some View {
LazyVStack(spacing: 0) {
if appState.firstApi.isAdmin {
BubblePicker(
ModTab.allCases,
selected: $selectedModTab,
label: \.label,
value: { tab in
if let unreadCount = (appState.firstSession as? UserSession)?.unreadCount {
switch tab {
case .reports:
return unreadCount.reportTotal
case .applications:
return unreadCount.registrationApplications
}
}
return 0
}
)
}
ForEach(currentModFeedLoader.items, id: \.inboxId) { item in
Group {
switch item {
case let .application(application):
RegistrationApplicationView(application: application)
case let .report(report):
ReportView(report: report)
}
}
.padding([.horizontal, .bottom], Constants.main.standardSpacing)
.onAppear {
do {
try currentModFeedLoader.loadIfThreshold(item)
} catch {
handleError(error)
}
}
}
}
.padding(.top, Constants.main.standardSpacing)
}
@ViewBuilder
var sectionHeader: some View {
BubblePicker(
Tab.allCases,
selected: $selectedTab,
label: \.label,
value: { tab in
if let unreadCount = (appState.firstSession as? UserSession)?.unreadCount {
switch tab {
case .all:
return unreadCount.total
case .replies:
return unreadCount.replies
case .mentions:
return unreadCount.mentions
case .messages:
return unreadCount.messages
}
}
return 0
}
)
.background(palette.groupedBackground.opacity(headerPinned ? 1 : 0))
.background(.bar)
}
@ToolbarContentBuilder
var toolbar: some ToolbarContent {
ToolbarItem(placement: .topBarLeading) {
Button {
showRead.toggle()
} label: {
Label("Hide Read", systemImage: Icons.filter)
.symbolVariant(showRead ? .none : .fill)
}
}
ToolbarItem(placement: .topBarTrailing) {
markAllAsReadButton
}
}
@ViewBuilder
var markAllAsReadButton: some View {
let newMessagesExist = !waitingOnMarkAllAsRead && ((appState.firstSession as? UserSession)?.unreadCount?.total ?? 0) != 0
PhaseAnimator([0, 1], trigger: markAllAsReadTrigger) { value in
Button {
HapticManager.main.play(haptic: .gentleInfo, priority: .low)
waitingOnMarkAllAsRead = true
markAllAsReadTrigger.toggle()
Task {
do {
try await appState.firstApi.markAllAsRead()
try await Task.sleep(for: .seconds(0.05))
} catch {
handleError(error)
}
waitingOnMarkAllAsRead = false
}
} label: {
HStack {
Image(systemName: Icons.markRead)
.imageScale(.small)
Text("All")
}
.opacity((value == 0 && newMessagesExist) ? 1 : 0)
.overlay {
if value != 0 {
Image(systemName: Icons.success)
.imageScale(.small)
.fontWeight(.semibold)
}
}
.fixedSize()
.padding(.vertical, 2)
.padding(.horizontal, 10)
.background(.bar, in: .capsule)
}
.opacity((newMessagesExist || value != 0) ? 1 : 0)
}
}
@ViewBuilder
var headerView: some View {
let availableFeeds = availableFeeds
Menu {
if availableFeeds.count > 1 {
Picker("Feed", selection: $selectedFeed) {
ForEach(availableFeeds) { feedType in
Label(String(localized: feedType.label), systemImage: feedType.systemImage)
.tag(feedType)
}
}
}
} label: {
FeedHeaderView(
feedDescription: .init(
label: selectedFeed.label,
subtitle: selectedFeed.subtitle(isAdmin: appState.firstApi.isAdmin),
color: { _ in selectedFeed.color },
iconName: selectedFeed.systemImage,
iconNameFill: selectedFeed.systemImageFill,
iconScaleFactor: 0.5
),
dropdownStyle: availableFeeds.count > 1 ? .enabled(showBadge: showBadge) : .disabled
)
}
}
@ViewBuilder
var signedOutInfoView: some View {
VStack {
Image(systemName: Icons.inbox)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(height: 80)
.foregroundColor(palette.accent)
Text(AccountsTracker.main.isEmpty ? "Log in or sign up to view your inbox." : "Switch account to view your inbox.")
.font(.title2)
.padding(.horizontal)
.fontWeight(.semibold)
.padding(.bottom, Constants.main.halfSpacing)
if AccountsTracker.main.isEmpty {
HStack {
infoViewButton("Log In") {
navigation.openSheet(.logIn(.pickInstance))
}
infoViewButton("Sign Up") {
navigation.openSheet(.signUp())
}
}
} else {
infoViewButton("Switch Account") {
navigation.openSheet(.quickSwitcher)
}
}
}
.buttonStyle(.borderedProminent)
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity)
.padding(.horizontal)
}
@ViewBuilder
private func infoViewButton(_ title: LocalizedStringResource, callback: @escaping () -> Void) -> some View {
Button(action: callback) {
Text(title)
.padding(.vertical, 4)
.padding(.horizontal, 8)
.frame(minWidth: 100)
}
}
var showBadge: Bool {
guard let unreadCount = (appState.firstSession as? UserSession)?.unreadCount else { return false }
switch selectedFeed {
case .inbox: return unreadCount.moderationTotal > 0
case .modMail: return unreadCount.personalTotal > 0
}
}
}