gitbay/Issues/IssueListViewModel.swift
43 lines · 1145 bytes
1import Foundation
2import Observation
3
4/// `issue list <repo> --state <s>`, paginated.
5@Observable
6@MainActor
7final class IssueListViewModel {
8
9 enum StateFilter: String, CaseIterable, Identifiable, Sendable {
10 case open, closed, all
11 var id: String { rawValue }
12 }
13
14 let list: PagedListModel<Issue>
15 let repoPath: String
16 var filter: StateFilter = .open {
17 didSet {
18 guard filter != oldValue else { return }
19 configureList()
20 Task { await list.reload() }
21 }
22 }
23
24 init(client: GitbayClient, repoPath: String) {
25 self.repoPath = repoPath
26 list = PagedListModel(
27 client: client,
28 argv: ["issue", "list", repoPath, "--state", StateFilter.open.rawValue],
29 emptyMessage: "No open issues."
30 )
31 }
32
33 var state: LoadState<[Issue]> { list.state }
34
35 func load() async {
36 await list.reload()
37 }
38
39 private func configureList() {
40 list.argv = ["issue", "list", repoPath, "--state", filter.rawValue]
41 list.emptyMessage = "No \(filter == .all ? "" : filter.rawValue + " ")issues."
42 }
43}