gitbay/Dashboard/DashboardModels.swift

v1.6.1
gitbay-ios/gitbay/Dashboard/DashboardModels.swift history · blame · raw

78 lines · 2964 bytes

 1import Foundation
 2
 3/// The `dashboard` command: the same account aggregate as the web
 4/// dashboard, in one read. Newer arrays default empty so an updated app
 5/// can still talk to a server from before the dashboard parity contract.
 6nonisolated struct DashboardData: Decodable, Sendable, Hashable {
 7    let reviewQueue: [DashboardItem]
 8    let assignedIssues: [DashboardItem]
 9    let openMRs: [DashboardItem]
10    let openIssues: [DashboardItem]
11    /// Pinned repos share `repo list`'s row shape.
12    let pinned: [RepoSummary]
13    let recentActivity: [FeedEvent]
14    /// Retained by the CLI for compatibility; builds are not a web
15    /// dashboard section.
16    let builds: [DashboardBuild]
17    /// The unread notification count, so a client showing a badge does
18    /// not need a second read to fill it.
19    let unread: Int
20
21    enum CodingKeys: String, CodingKey {
22        case pinned, builds, unread
23        case reviewQueue = "review_queue"
24        case openMRs = "open_mrs"
25        case assignedIssues = "assigned_issues"
26        case openIssues = "open_issues"
27        case recentActivity = "recent_activity"
28    }
29
30    init(from decoder: any Decoder) throws {
31        let values = try decoder.container(keyedBy: CodingKeys.self)
32        reviewQueue = try values.decodeIfPresent([DashboardItem].self, forKey: .reviewQueue) ?? []
33        assignedIssues = try values.decodeIfPresent([DashboardItem].self, forKey: .assignedIssues) ?? []
34        openMRs = try values.decodeIfPresent([DashboardItem].self, forKey: .openMRs) ?? []
35        openIssues = try values.decodeIfPresent([DashboardItem].self, forKey: .openIssues) ?? []
36        pinned = try values.decodeIfPresent([RepoSummary].self, forKey: .pinned) ?? []
37        recentActivity = try values.decodeIfPresent([FeedEvent].self, forKey: .recentActivity) ?? []
38        builds = try values.decodeIfPresent([DashboardBuild].self, forKey: .builds) ?? []
39        unread = try values.decodeIfPresent(Int.self, forKey: .unread) ?? 0
40    }
41}
42
43/// One open issue or MR row, repo resolved server-side.
44nonisolated struct DashboardItem: Decodable, Sendable, Hashable, Identifiable {
45    let repo: String
46    let number: Int64
47    let title: String
48    let author: String
49    let state: String
50    let updatedAt: Date
51
52    enum CodingKeys: String, CodingKey {
53        case repo, number, title, author, state
54        case updatedAt = "updated_at"
55    }
56
57    var id: String { "\(repo)#\(number)" }
58}
59
60/// One build row with its repo attached.
61nonisolated struct DashboardBuild: Decodable, Sendable, Hashable, Identifiable {
62    let repo: String
63    let number: Int64
64    let job: String
65    let status: String
66    let sha: String
67    let ref: String
68    let createdAt: Date
69    let finishedAt: Date?
70
71    enum CodingKeys: String, CodingKey {
72        case repo, number, job, status, sha, ref
73        case createdAt = "created_at"
74        case finishedAt = "finished_at"
75    }
76
77    var id: String { "\(repo)#\(number)" }
78}