2022-12-19 11:28:55 +00:00
|
|
|
import DesignSystem
|
2022-12-25 12:09:43 +00:00
|
|
|
import Env
|
2023-01-17 10:36:01 +00:00
|
|
|
import Models
|
|
|
|
import Network
|
|
|
|
import Shimmer
|
|
|
|
import SwiftUI
|
2022-12-19 11:28:55 +00:00
|
|
|
|
|
|
|
public struct NotificationsListView: View {
|
2023-01-09 17:52:33 +00:00
|
|
|
@Environment(\.scenePhase) private var scenePhase
|
2022-12-29 09:39:34 +00:00
|
|
|
@EnvironmentObject private var theme: Theme
|
2022-12-25 12:09:43 +00:00
|
|
|
@EnvironmentObject private var watcher: StreamWatcher
|
2022-12-19 11:28:55 +00:00
|
|
|
@EnvironmentObject private var client: Client
|
2023-02-15 08:04:35 +00:00
|
|
|
@EnvironmentObject private var routerPath: RouterPath
|
2023-01-31 18:43:07 +00:00
|
|
|
@EnvironmentObject private var account: CurrentAccount
|
2022-12-19 11:28:55 +00:00
|
|
|
@StateObject private var viewModel = NotificationsViewModel()
|
2023-01-22 05:38:30 +00:00
|
|
|
|
2023-01-19 06:24:24 +00:00
|
|
|
let lockedType: Models.Notification.NotificationType?
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2023-01-19 06:24:24 +00:00
|
|
|
public init(lockedType: Models.Notification.NotificationType?) {
|
|
|
|
self.lockedType = lockedType
|
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2022-12-19 11:28:55 +00:00
|
|
|
public var body: some View {
|
2023-01-29 15:06:46 +00:00
|
|
|
List {
|
|
|
|
topPaddingView
|
|
|
|
notificationsView
|
2022-12-19 11:28:55 +00:00
|
|
|
}
|
2023-01-31 18:43:07 +00:00
|
|
|
.id(account.account?.id)
|
2023-01-29 15:06:46 +00:00
|
|
|
.environment(\.defaultMinListRowHeight, 1)
|
|
|
|
.listStyle(.plain)
|
2023-01-19 17:14:08 +00:00
|
|
|
.navigationTitle(lockedType?.menuTitle() ?? viewModel.selectedType?.menuTitle() ?? "notifications.navigation-title")
|
2023-01-04 07:14:37 +00:00
|
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
|
|
.toolbar {
|
2023-01-19 06:24:24 +00:00
|
|
|
if lockedType == nil {
|
|
|
|
ToolbarTitleMenu {
|
2023-01-04 07:14:37 +00:00
|
|
|
Button {
|
2023-01-19 06:24:24 +00:00
|
|
|
viewModel.selectedType = nil
|
2023-01-04 07:14:37 +00:00
|
|
|
} label: {
|
2023-01-19 17:14:08 +00:00
|
|
|
Label("notifications.navigation-title", systemImage: "bell.fill")
|
2023-01-19 06:24:24 +00:00
|
|
|
}
|
|
|
|
Divider()
|
|
|
|
ForEach(Notification.NotificationType.allCases, id: \.self) { type in
|
|
|
|
Button {
|
|
|
|
viewModel.selectedType = type
|
|
|
|
} label: {
|
2023-02-28 17:48:22 +00:00
|
|
|
Label {
|
|
|
|
Text(type.menuTitle())
|
|
|
|
} icon: {
|
|
|
|
type.icon(isPrivate: false)
|
|
|
|
}
|
2023-01-19 06:24:24 +00:00
|
|
|
}
|
2023-01-04 07:14:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-05 13:11:55 +00:00
|
|
|
.scrollContentBackground(.hidden)
|
2022-12-29 09:39:34 +00:00
|
|
|
.background(theme.primaryBackgroundColor)
|
2022-12-19 11:28:55 +00:00
|
|
|
.task {
|
2022-12-21 17:28:21 +00:00
|
|
|
viewModel.client = client
|
2023-02-06 16:52:36 +00:00
|
|
|
viewModel.currentAccount = account
|
2023-01-19 06:24:24 +00:00
|
|
|
if let lockedType {
|
|
|
|
viewModel.selectedType = lockedType
|
|
|
|
}
|
2022-12-21 16:39:48 +00:00
|
|
|
await viewModel.fetchNotifications()
|
2022-12-19 11:28:55 +00:00
|
|
|
}
|
2022-12-19 15:02:55 +00:00
|
|
|
.refreshable {
|
2023-02-28 17:55:08 +00:00
|
|
|
SoundEffectManager.shared.playSound(of: .pull)
|
|
|
|
HapticManager.shared.fireHaptic(of: .dataRefresh(intensity: 0.3))
|
2022-12-24 11:20:42 +00:00
|
|
|
await viewModel.fetchNotifications()
|
2023-02-28 17:55:08 +00:00
|
|
|
HapticManager.shared.fireHaptic(of: .dataRefresh(intensity: 0.7))
|
|
|
|
SoundEffectManager.shared.playSound(of: .refresh)
|
2022-12-19 15:02:55 +00:00
|
|
|
}
|
2022-12-25 12:09:43 +00:00
|
|
|
.onChange(of: watcher.latestEvent?.id, perform: { _ in
|
|
|
|
if let latestEvent = watcher.latestEvent {
|
|
|
|
viewModel.handleEvent(event: latestEvent)
|
|
|
|
}
|
|
|
|
})
|
2023-01-09 17:52:33 +00:00
|
|
|
.onChange(of: scenePhase, perform: { scenePhase in
|
|
|
|
switch scenePhase {
|
|
|
|
case .active:
|
|
|
|
Task {
|
|
|
|
await viewModel.fetchNotifications()
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2022-12-19 11:28:55 +00:00
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2022-12-21 11:39:29 +00:00
|
|
|
@ViewBuilder
|
|
|
|
private var notificationsView: some View {
|
|
|
|
switch viewModel.state {
|
|
|
|
case .loading:
|
2023-01-27 15:58:04 +00:00
|
|
|
ForEach(ConsolidatedNotification.placeholders()) { notification in
|
2023-02-17 17:17:51 +00:00
|
|
|
NotificationRowView(notification: notification,
|
|
|
|
client: client,
|
|
|
|
routerPath: routerPath,
|
|
|
|
followRequests: account.followRequests)
|
2022-12-21 11:39:29 +00:00
|
|
|
.redacted(reason: .placeholder)
|
2023-01-29 15:06:46 +00:00
|
|
|
.listRowInsets(.init(top: 12,
|
|
|
|
leading: .layoutPadding + 4,
|
|
|
|
bottom: 12,
|
|
|
|
trailing: .layoutPadding))
|
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
2023-01-30 18:14:43 +00:00
|
|
|
.redacted(reason: .placeholder)
|
2022-12-21 11:39:29 +00:00
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2022-12-21 11:39:29 +00:00
|
|
|
case let .display(notifications, nextPageState):
|
2023-01-05 11:21:54 +00:00
|
|
|
if notifications.isEmpty {
|
|
|
|
EmptyView(iconName: "bell.slash",
|
2023-01-19 17:14:08 +00:00
|
|
|
title: "notifications.empty.title",
|
|
|
|
message: "notifications.empty.message")
|
2023-02-12 15:29:41 +00:00
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
|
|
|
.listSectionSeparator(.hidden)
|
2023-01-05 11:21:54 +00:00
|
|
|
} else {
|
|
|
|
ForEach(notifications) { notification in
|
2023-02-17 17:17:51 +00:00
|
|
|
NotificationRowView(notification: notification,
|
|
|
|
client: client,
|
|
|
|
routerPath: routerPath,
|
|
|
|
followRequests: account.followRequests)
|
2023-01-29 15:06:46 +00:00
|
|
|
.listRowInsets(.init(top: 12,
|
|
|
|
leading: .layoutPadding + 4,
|
|
|
|
bottom: 12,
|
|
|
|
trailing: .layoutPadding))
|
|
|
|
.listRowBackground(notification.type == .mention && lockedType != .mention ?
|
2023-01-30 06:27:06 +00:00
|
|
|
theme.secondaryBackgroundColor : theme.primaryBackgroundColor)
|
2023-02-17 17:17:51 +00:00
|
|
|
.id(notification.id)
|
2023-01-05 11:21:54 +00:00
|
|
|
}
|
2022-12-21 11:39:29 +00:00
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2022-12-21 11:39:29 +00:00
|
|
|
switch nextPageState {
|
2022-12-29 09:39:34 +00:00
|
|
|
case .none:
|
|
|
|
EmptyView()
|
2022-12-21 11:39:29 +00:00
|
|
|
case .hasNextPage:
|
|
|
|
loadingRow
|
|
|
|
.onAppear {
|
|
|
|
Task {
|
|
|
|
await viewModel.fetchNextPage()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case .loadingNextPage:
|
|
|
|
loadingRow
|
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2023-01-07 17:01:06 +00:00
|
|
|
case .error:
|
2023-01-19 17:14:08 +00:00
|
|
|
ErrorView(title: "notifications.error.title",
|
|
|
|
message: "notifications.error.message",
|
2023-03-13 12:38:28 +00:00
|
|
|
buttonTitle: "action.retry")
|
|
|
|
{
|
2023-01-07 17:01:06 +00:00
|
|
|
Task {
|
|
|
|
await viewModel.fetchNotifications()
|
|
|
|
}
|
|
|
|
}
|
2023-02-04 20:30:50 +00:00
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
|
|
|
.listSectionSeparator(.hidden)
|
2022-12-21 11:39:29 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2022-12-21 11:39:29 +00:00
|
|
|
private var loadingRow: some View {
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
ProgressView()
|
|
|
|
Spacer()
|
|
|
|
}
|
2023-01-29 15:06:46 +00:00
|
|
|
.listRowInsets(.init(top: .layoutPadding,
|
|
|
|
leading: .layoutPadding + 4,
|
|
|
|
bottom: .layoutPadding,
|
|
|
|
trailing: .layoutPadding))
|
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
|
|
|
}
|
2023-01-30 06:27:06 +00:00
|
|
|
|
2023-01-29 15:06:46 +00:00
|
|
|
private var topPaddingView: some View {
|
2023-01-30 06:27:06 +00:00
|
|
|
HStack {}
|
2023-01-29 15:06:46 +00:00
|
|
|
.listRowBackground(Color.clear)
|
|
|
|
.listRowSeparator(.hidden)
|
|
|
|
.listRowInsets(.init())
|
|
|
|
.frame(height: .layoutPadding)
|
2022-12-21 11:39:29 +00:00
|
|
|
}
|
2022-12-19 11:28:55 +00:00
|
|
|
}
|