gitbay/Dashboard/DashboardViewModel.swift

v1.11.0
gitbay-ios/gitbay/Dashboard/DashboardViewModel.swift history · blame · raw

32 lines · 1002 bytes

 1import Foundation
 2import Observation
 3
 4/// The web dashboard contract — attention queues, open work, pins, and
 5/// recent activity — in one `dashboard` read. This replaced a per-repo fan-out that
 6/// drained the rate bucket at 64 of 66 repos; krz/gitbay#41 added the
 7/// aggregate.
 8@Observable
 9@MainActor
10final class DashboardViewModel {
11
12    private(set) var state: LoadState<DashboardData> = .loading
13
14    private let client: GitbayClient
15
16    init(client: GitbayClient) {
17        self.client = client
18    }
19
20    func load() async {
21        do {
22            let data = try await client.read(["dashboard"], as: DashboardData.self)
23            state = .loaded(data)
24            // The authoritative resync. A push sets the badge on arrival
25            // and nothing lowers it afterwards, so this is what corrects
26            // it — on every app open, against the account you are in.
27            await setAppBadge(data.unread)
28        } catch {
29            state = .from(error)
30        }
31    }
32}