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

148 lines
4 KiB
Swift
Raw Normal View History

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 {
@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
@StateObject private var viewModel = NotificationsViewModel()
2023-01-22 05:38:30 +00:00
let lockedType: Models.Notification.NotificationType?
2023-01-17 10:36:01 +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 {
ScrollView {
LazyVStack {
2023-01-18 18:11:30 +00:00
notificationsView
2023-01-22 05:38:30 +00:00
.frame(maxWidth: .maxColumnWidth)
2022-12-19 11:28:55 +00:00
}
2023-01-18 18:11:30 +00:00
.padding(.top, .layoutPadding + 16)
2023-01-05 13:11:55 +00:00
.background(theme.primaryBackgroundColor)
2022-12-19 11:28:55 +00:00
}
.navigationTitle(lockedType?.menuTitle() ?? viewModel.selectedType?.menuTitle() ?? "notifications.navigation-title")
2023-01-04 07:14:37 +00:00
.navigationBarTitleDisplayMode(.inline)
.toolbar {
if lockedType == nil {
ToolbarTitleMenu {
2023-01-04 07:14:37 +00:00
Button {
viewModel.selectedType = nil
2023-01-04 07:14:37 +00:00
} label: {
Label("notifications.navigation-title", systemImage: "bell.fill")
}
Divider()
ForEach(Notification.NotificationType.allCases, id: \.self) { type in
Button {
viewModel.selectedType = type
} label: {
Label(type.menuTitle(), systemImage: type.iconName())
}
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
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
}
.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)
}
})
.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:
ForEach(Models.Notification.placeholders()) { notification in
NotificationRowView(notification: notification)
.redacted(reason: .placeholder)
2023-01-18 18:11:30 +00:00
.padding(.leading, .layoutPadding + 4)
2023-01-18 12:54:12 +00:00
.padding(.trailing, .layoutPadding)
2023-01-18 18:11:30 +00:00
.padding(.top, 6)
.padding(.bottom, 2)
2022-12-21 11:39:29 +00:00
.shimmering()
Divider()
.padding(.vertical, .dividerPadding)
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):
if notifications.isEmpty {
EmptyView(iconName: "bell.slash",
title: "notifications.empty.title",
message: "notifications.empty.message")
} else {
ForEach(notifications) { notification in
2023-01-17 10:36:01 +00:00
if notification.supportedType != nil {
NotificationRowView(notification: notification)
2023-01-18 18:11:30 +00:00
.padding(.leading, .layoutPadding + 4)
2023-01-18 12:54:12 +00:00
.padding(.trailing, .layoutPadding)
2023-01-18 18:11:30 +00:00
.padding(.top, 6)
.padding(.bottom, 2)
2023-01-17 10:36:01 +00:00
Divider()
.padding(.vertical, .dividerPadding)
}
}
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:
ErrorView(title: "notifications.error.title",
message: "notifications.error.message",
buttonTitle: "action.retry") {
2023-01-07 17:01:06 +00:00
Task {
await viewModel.fetchNotifications()
}
}
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()
}
}
2022-12-19 11:28:55 +00:00
}