2024-01-31 16:47:29 +01:00
|
|
|
//Made by Lumaa
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct NotificationsView: View {
|
|
|
|
@Environment(AccountManager.self) private var accountManager
|
|
|
|
|
|
|
|
@State private var navigator: Navigator = Navigator()
|
|
|
|
@State private var notifications: [Notification] = []
|
|
|
|
@State private var loadingNotifs: Bool = false
|
|
|
|
@State private var lastId: Int? = nil
|
|
|
|
private let notifLimit = 50
|
|
|
|
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
NavigationStack(path: $navigator.path) {
|
|
|
|
if !notifications.isEmpty {
|
|
|
|
ScrollView(.vertical, showsIndicators: false) {
|
2024-02-04 08:43:56 +01:00
|
|
|
LazyVStack(alignment: .leading, spacing: 15) {
|
2024-01-31 16:47:29 +01:00
|
|
|
ForEach(notifications) { notif in
|
|
|
|
NotificationRow(notif: notif)
|
2024-02-04 08:43:56 +01:00
|
|
|
.environment(navigator)
|
2024-01-31 16:47:29 +01:00
|
|
|
.onDisappear() {
|
|
|
|
guard !notifications.isEmpty else { return }
|
|
|
|
lastId = notifications.firstIndex(where: { $0.id == notif.id })
|
|
|
|
}
|
|
|
|
}
|
2024-02-04 08:43:56 +01:00
|
|
|
|
|
|
|
if loadingNotifs {
|
|
|
|
ProgressView()
|
|
|
|
.progressViewStyle(.circular)
|
|
|
|
.padding(.vertical)
|
|
|
|
}
|
2024-01-31 16:47:29 +01:00
|
|
|
}
|
|
|
|
.onChange(of: lastId ?? 0) { _, new in
|
|
|
|
guard !loadingNotifs else { return }
|
|
|
|
Task {
|
|
|
|
loadingNotifs = true
|
|
|
|
await fetchNotifications(lastId: new)
|
|
|
|
loadingNotifs = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-04 08:43:56 +01:00
|
|
|
.withAppRouter(navigator)
|
|
|
|
.background(Color.appBackground)
|
|
|
|
.refreshable {
|
|
|
|
await fetchNotifications(lastId: nil)
|
|
|
|
}
|
|
|
|
.navigationTitle(String(localized: "activity"))
|
2024-01-31 16:47:29 +01:00
|
|
|
} else if loadingNotifs == false && notifications.isEmpty {
|
|
|
|
ZStack {
|
|
|
|
Color.appBackground
|
|
|
|
.ignoresSafeArea()
|
|
|
|
|
|
|
|
ContentUnavailableView("activity.no-notifications", systemImage: "bolt.heart")
|
|
|
|
}
|
|
|
|
} else if loadingNotifs == true && notifications.isEmpty {
|
|
|
|
ZStack {
|
|
|
|
Color.appBackground
|
|
|
|
.ignoresSafeArea()
|
|
|
|
|
|
|
|
ProgressView()
|
|
|
|
.progressViewStyle(.circular)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.task {
|
|
|
|
loadingNotifs = true
|
|
|
|
await fetchNotifications(lastId: nil)
|
|
|
|
loadingNotifs = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchNotifications(lastId: Int? = nil) async {
|
|
|
|
guard let client = accountManager.getClient() else { return }
|
|
|
|
|
|
|
|
if lastId != nil {
|
|
|
|
guard lastId! >= notifications.count - 6 else { return }
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
let notifs: [Notification] = try await client.get(endpoint: Notifications.notifications(minId: nil, maxId: nil, types: nil, limit: lastId != nil ? notifLimit : 30))
|
|
|
|
guard !notifs.isEmpty else { return }
|
|
|
|
|
|
|
|
if notifications.isEmpty {
|
|
|
|
notifications = notifs
|
|
|
|
} else {
|
|
|
|
notifications.append(contentsOf: notifs)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
print(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|