gitbay/Repos/BookmarkListViewModel.swift

v1.6.1
gitbay-ios/gitbay/Repos/BookmarkListViewModel.swift history · blame · raw

30 lines · 854 bytes

 1import Foundation
 2import Observation
 3
 4/// `repo bookmarks` — the caller's public bookmarks, in server order.
 5/// The rows decode as `RepoSummary`; a repository made private since it
 6/// was bookmarked is already left out by the server.
 7@Observable
 8@MainActor
 9final class BookmarkListViewModel {
10
11    private(set) var state: LoadState<[RepoSummary]> = .loading
12
13    private let client: GitbayClient
14
15    init(client: GitbayClient) {
16        self.client = client
17    }
18
19    func load() async {
20        state = .loading
21        do {
22            let repos = try await client.readList(["repo", "bookmarks"], of: RepoSummary.self)
23            state = repos.isEmpty
24                ? .empty("No bookmarks yet. Bookmark a repository from its actions menu.")
25                : .loaded(repos)
26        } catch {
27            state = .from(error)
28        }
29    }
30}