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

101 lines
2.5 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 {
if client.isAuth {
2022-12-22 06:00:44 +00:00
Picker("", selection: $viewModel.tab) {
ForEach(NotificationsViewModel.Tab.allCases, id: \.self) { tab in
Text(tab.rawValue)
}
}
.pickerStyle(.segmented)
Group {
notificationsView
}
.padding(.top, 16)
2022-12-19 11:28:55 +00:00
} else {
Text("Please Sign In to see your notifications")
.font(.title3)
}
}
.padding(.horizontal, .layoutPadding)
.padding(.top, .layoutPadding)
2022-12-19 11:28:55 +00:00
}
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
.navigationTitle(Text("Notifications"))
.navigationBarTitleDisplayMode(.inline)
}
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
}