mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2024-06-15 03:30:35 +00:00
46 lines
1.4 KiB
Swift
46 lines
1.4 KiB
Swift
import Foundation
|
|
import SwiftUI
|
|
import Network
|
|
import Models
|
|
|
|
@MainActor
|
|
class NotificationsViewModel: ObservableObject {
|
|
public enum State {
|
|
public enum PagingState {
|
|
case hasNextPage, loadingNextPage
|
|
}
|
|
case loading
|
|
case display(notifications: [Models.Notification], nextPageState: State.PagingState)
|
|
case error(error: Error)
|
|
}
|
|
|
|
var client: Client?
|
|
@Published var state: State = .loading
|
|
|
|
private var notifications: [Models.Notification] = []
|
|
|
|
func fetchNotifications() async {
|
|
guard let client else { return }
|
|
do {
|
|
state = .loading
|
|
notifications = try await client.get(endpoint: Notifications.notifications(maxId: nil))
|
|
state = .display(notifications: notifications, nextPageState: .hasNextPage)
|
|
} catch {
|
|
state = .error(error: error)
|
|
}
|
|
}
|
|
|
|
func fetchNextPage() async {
|
|
guard let client else { return }
|
|
do {
|
|
guard let lastId = notifications.last?.id else { return }
|
|
state = .display(notifications: notifications, nextPageState: .loadingNextPage)
|
|
let newNotifications: [Models.Notification] = try await client.get(endpoint: Notifications.notifications(maxId: lastId))
|
|
notifications.append(contentsOf: newNotifications)
|
|
state = .display(notifications: notifications, nextPageState: .hasNextPage)
|
|
} catch {
|
|
state = .error(error: error)
|
|
}
|
|
}
|
|
}
|