IceCubesApp/Packages/Notifications/Sources/Notifications/NotificationsListView.swift

107 lines
2.6 KiB
Swift
Raw Normal View History

2022-12-19 11:28:55 +00:00
import SwiftUI
import Network
import Models
import Shimmer
import DesignSystem
2022-12-25 12:09:43 +00:00
import Env
2022-12-19 11:28:55 +00:00
public struct NotificationsListView: View {
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
@StateObject private var viewModel = NotificationsViewModel()
public init() { }
public var body: some View {
ScrollView {
LazyVStack {
2023-01-04 07:14:37 +00:00
Group {
notificationsView
2022-12-19 11:28:55 +00:00
}
2023-01-04 07:14:37 +00:00
.padding(.top, 16)
2022-12-19 11:28:55 +00:00
}
.padding(.horizontal, .layoutPadding)
.padding(.top, .layoutPadding)
2022-12-19 11:28:55 +00:00
}
2023-01-04 07:14:37 +00:00
.navigationTitle(viewModel.selectedType?.menuTitle() ?? "All Notifications")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarTitleMenu {
Button {
viewModel.selectedType = nil
} label: {
Label("All Notifications", systemImage: "bell.fill")
}
Divider()
ForEach(Notification.NotificationType.allCases, id: \.self) { type in
Button {
viewModel.selectedType = type
} label: {
Label(type.menuTitle(), systemImage: type.iconName())
}
}
}
}
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
2022-12-21 16:39:48 +00:00
await viewModel.fetchNotifications()
2022-12-19 11:28:55 +00:00
}
.refreshable {
await viewModel.fetchNotifications()
}
2022-12-25 12:09:43 +00:00
.onChange(of: watcher.latestEvent?.id, perform: { _ in
if let latestEvent = watcher.latestEvent {
viewModel.handleEvent(event: latestEvent)
}
})
2022-12-19 11:28:55 +00:00
}
2022-12-21 11:39:29 +00:00
@ViewBuilder
private var notificationsView: some View {
switch viewModel.state {
case .loading:
ForEach(Models.Notification.placeholders()) { notification in
NotificationRowView(notification: notification)
.redacted(reason: .placeholder)
.shimmering()
Divider()
.padding(.vertical, .dividerPadding)
2022-12-21 11:39:29 +00:00
}
case let .display(notifications, nextPageState):
ForEach(notifications) { notification in
NotificationRowView(notification: notification)
Divider()
.padding(.vertical, .dividerPadding)
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
}
case let .error(error):
Text(error.localizedDescription)
}
}
private var loadingRow: some View {
HStack {
Spacer()
ProgressView()
Spacer()
}
}
2022-12-19 11:28:55 +00:00
}