gitbay/Views/Repos/RepoView.swift

1c5622210d0f72efd86582ce195a6741401e7939
gitbay-ios/gitbay/Views/Repos/RepoView.swift history · blame · raw

163 lines · 6757 bytes

  1import SwiftUI
  2
  3struct RepoView: View {
  4
  5    @State private var model: RepoDetailViewModel
  6    private let path: String
  7
  8    init(client: GitbayClient, path: String) {
  9        self.path = path
 10        _model = State(initialValue: RepoDetailViewModel(client: client, path: path))
 11    }
 12
 13    @State private var confirmingArchive = false
 14
 15    var body: some View {
 16        List {
 17            if let detail = model.state.value {
 18                if let error = model.actionError {
 19                    Section {
 20                        GBNotice(error, .gbWarn)
 21                    }
 22                }
 23                header(detail)
 24
 25                Section {
 26                    NavigationLink(value: RepoRoute.tree(repo: path, directory: "", ref: nil)) {
 27                        Label("Files", systemImage: "folder")
 28                    }
 29                    NavigationLink(value: RepoRoute.log(repo: path, ref: nil, path: nil)) {
 30                        Label("History", systemImage: "clock")
 31                    }
 32                    NavigationLink(value: RepoRoute.refs(repo: path)) {
 33                        Label("Branches & Tags", systemImage: "arrow.triangle.branch")
 34                    }
 35                    NavigationLink(value: MRRoute.list(repo: path)) {
 36                        Label("Merge Requests", systemImage: "arrow.triangle.merge")
 37                    }
 38                    NavigationLink(value: IssueRoute.list(repo: path)) {
 39                        Label("Issues", systemImage: "smallcircle.filled.circle")
 40                    }
 41                    NavigationLink(value: RepoRoute.milestones(repo: path)) {
 42                        Label("Milestones", systemImage: "flag")
 43                    }
 44                    NavigationLink(value: BuildRoute.list(repo: path)) {
 45                        Label("Builds", systemImage: "hammer")
 46                    }
 47                    NavigationLink(value: ReleaseRoute.list(repo: path)) {
 48                        Label("Releases", systemImage: "shippingbox")
 49                    }
 50                    NavigationLink(value: RepoRoute.grep(repo: path)) {
 51                        Label("Search in Files", systemImage: "text.magnifyingglass")
 52                    }
 53                    NavigationLink(value: RepoRoute.settings(repo: path)) {
 54                        Label("Settings", systemImage: "gearshape")
 55                    }
 56                    NavigationLink(value: RepoRoute.profile(String(path.split(separator: "/").first ?? ""))) {
 57                        Label(String(path.split(separator: "/").first ?? ""), systemImage: "person.crop.circle")
 58                    }
 59                }
 60
 61                if let readme = model.readme {
 62                    Section("README") {
 63                        ReadmeView(name: model.readmeName ?? "README.md", content: readme)
 64                            .padding(.vertical, 4)
 65                    }
 66                }
 67            }
 68        }
 69        .overlay { LoadStateOverlay(state: model.state) }
 70        .navigationTitle(String(path.split(separator: "/").last ?? ""))
 71        .navigationBarTitleDisplayMode(.inline)
 72        .toolbar { toolbar }
 73        .task { await model.load() }
 74        .refreshable { await model.load() }
 75        .confirmationDialog(
 76            model.state.value?.isArchived == true
 77                ? "Unarchive \(path)?"
 78                : "Archive \(path)? Pushes and issue/MR writes will be refused.",
 79            isPresented: $confirmingArchive
 80        ) {
 81            Button(
 82                model.state.value?.isArchived == true ? "Unarchive" : "Archive",
 83                role: model.state.value?.isArchived == true ? nil : .destructive
 84            ) {
 85                Task { await model.setArchived(!(model.state.value?.isArchived ?? false)) }
 86            }
 87            Button("Cancel", role: .cancel) {}
 88        }
 89    }
 90
 91    @ToolbarContentBuilder
 92    private var toolbar: some ToolbarContent {
 93        ToolbarItem(placement: .topBarTrailing) {
 94            if let detail = model.state.value {
 95                Menu {
 96                    if let pinned = model.isPinned {
 97                        Button {
 98                            Task { await model.setPinned(!pinned) }
 99                        } label: {
100                            Label(pinned ? "Unpin" : "Pin",
101                                  systemImage: pinned ? "pin.slash" : "pin")
102                        }
103                    }
104                    Divider()
105                    Button(role: detail.isArchived ? nil : .destructive) {
106                        confirmingArchive = true
107                    } label: {
108                        Label(detail.isArchived ? "Unarchive" : "Archive",
109                              systemImage: "archivebox")
110                    }
111                } label: {
112                    if model.working {
113                        ProgressView()
114                    } else {
115                        Image(systemName: "ellipsis.circle")
116                    }
117                }
118                .disabled(model.working)
119                .accessibilityIdentifier("repo-actions-menu")
120            }
121        }
122    }
123
124    @ViewBuilder
125    private func header(_ detail: RepoDetail) -> some View {
126        Section {
127            VStack(alignment: .leading, spacing: 8) {
128                HStack(spacing: 6) {
129                    Text(detail.path)
130                        .font(.gbSans(.headline))
131                    if detail.visibility == "private" {
132                        Image(systemName: "lock.fill")
133                            .font(.gbSans(.caption))
134                            .foregroundStyle(.secondary)
135                    }
136                    if detail.isArchived {
137                        GBChip("archived", .secondary)
138                    }
139                }
140                if let description = detail.description, !description.isEmpty {
141                    Text(description)
142                        .font(.gbSans(.subheadline))
143                        .foregroundStyle(.secondary)
144                }
145                if let website = detail.website, let url = URL(string: website) {
146                    Link(website, destination: url)
147                        .font(.gbSans(.caption))
148                        .lineLimit(1)
149                }
150                HStack(spacing: 12) {
151                    Label(detail.defaultBranch, systemImage: "arrow.triangle.branch")
152                    if let topics = detail.topics, !topics.isEmpty {
153                        Text(topics.joined(separator: " · "))
154                            .lineLimit(1)
155                    }
156                }
157                .font(.gbSans(.caption))
158                .foregroundStyle(.secondary)
159            }
160            .padding(.vertical, 4)
161        }
162    }
163}