summaryrefslogtreecommitdiff
path: root/Hutch/Views/Lookup/UserProfileView.swift
blob: dc9197cfdd9e3f93d7a79e7fbf85423c031a4f09 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import SwiftUI

struct UserProfileView: View {
    @Environment(AppState.self) private var appState

    let user: User
    @State private var profileViewModel: UserProfileViewModel?

    private static let iso8601Formatter: ISO8601DateFormatter = {
        let formatter = ISO8601DateFormatter()
        formatter.formatOptions = [.withInternetDateTime]
        formatter.timeZone = TimeZone(secondsFromGMT: 0)
        return formatter
    }()

    var body: some View {
        List {
            if let avatarURL = user.avatar.flatMap(URL.init(string:)) {
                Section {
                    HStack {
                        Spacer()
                        AsyncImage(url: avatarURL) { phase in
                            switch phase {
                            case .success(let image):
                                image
                                    .resizable()
                                    .scaledToFill()
                            case .failure, .empty:
                                Image(systemName: "person.crop.circle.fill")
                                    .resizable()
                                    .scaledToFit()
                                    .foregroundStyle(.secondary)
                                    .padding(20)
                            @unknown default:
                                EmptyView()
                            }
                        }
                        .frame(width: 96, height: 96)
                        .clipShape(Circle())
                        .overlay {
                            Circle()
                                .stroke(Color.secondary.opacity(0.2), lineWidth: 1)
                        }
                        Spacer()
                    }
                    .listRowBackground(Color.clear)
                }
            }

            Section {
                LabeledContent("Username", value: user.username)
                LabeledContent("Canonical Name", value: user.canonicalName)
                if let userType = user.userType {
                    LabeledContent("User Type", value: userType)
                }
                if let pronouns = user.pronouns {
                    LabeledContent("Pronouns", value: pronouns)
                }
                if let suspensionNotice = user.suspensionNotice {
                    LabeledContent("Suspension Notice", value: suspensionNotice)
                }
            }

            Section {
                LabeledContent("Email", value: user.email)
                if let urlString = user.url, let url = URL(string: urlString) {
                    LabeledContent("URL") {
                        Link(urlString, destination: url)
                    }
                }
                if let location = user.location {
                    LabeledContent("Location", value: location)
                }
            }

            if let bio = user.bio {
                Section("Bio") {
                    Text(bio)
                }
            }

            if user.created != nil || user.updated != nil {
                Section {
                    if let created = user.created {
                        LabeledContent("Joined", value: formattedTimestamp(created))
                    }
                    if let updated = user.updated {
                        LabeledContent("Updated", value: formattedTimestamp(updated))
                    }
                }
            }

            if let viewModel = profileViewModel {
                Section {
                    if viewModel.isLoadingRepositories && viewModel.repositories.isEmpty {
                        ProgressView()
                    } else if viewModel.repositories.isEmpty {
                        Text("No public repositories.")
                            .foregroundStyle(.secondary)
                    } else {
                        ForEach(viewModel.repositories.prefix(4)) { repo in
                            NavigationLink {
                                RepositoryDetailView(repository: repo)
                            } label: {
                                RepositoryRowView(repository: repo, buildStatus: .none)
                            }
                        }
                        if viewModel.repositories.count > 4 {
                            NavigationLink("See All") {
                                UserRepositoriesView(viewModel: viewModel)
                            }
                        }
                    }
                } header: {
                    Text("Repositories")
                }

                Section {
                    if viewModel.isLoadingTrackers && viewModel.trackers.isEmpty {
                        ProgressView()
                    } else if viewModel.trackers.isEmpty {
                        Text("No public trackers.")
                            .foregroundStyle(.secondary)
                    } else {
                        ForEach(viewModel.trackers.prefix(4)) { tracker in
                            NavigationLink {
                                TicketListView(tracker: tracker)
                            } label: {
                                UserProfileTrackerRowView(tracker: tracker)
                            }
                        }
                        if viewModel.trackers.count > 4 {
                            NavigationLink("See All") {
                                UserTrackersView(viewModel: viewModel)
                            }
                        }
                    }
                } header: {
                    Text("Trackers")
                }
            }
        }
        .listStyle(.insetGrouped)
        .navigationTitle(user.canonicalName)
        .navigationBarTitleDisplayMode(.inline)
        .task {
            let owner = user.canonicalName.hasPrefix("~")
                ? String(user.canonicalName.dropFirst())
                : user.canonicalName
            let vm = UserProfileViewModel(ownerUsername: owner, client: appState.client)
            profileViewModel = vm
            async let repos: () = vm.loadRepositories()
            async let trackers: () = vm.loadTrackers()
            _ = await (repos, trackers)
        }
    }

    private func formattedTimestamp(_ value: String) -> String {
        guard let date = Self.iso8601Formatter.date(from: value) else {
            return value
        }

        return date.formatted(date: .abbreviated, time: .shortened)
    }
}

struct UserProfileTrackerRowView: View {
    let tracker: TrackerSummary

    var body: some View {
        VStack(alignment: .leading, spacing: 4) {
            HStack {
                Text(tracker.name)
                    .font(.subheadline.weight(.medium))

                Spacer()

                VisibilityBadge(visibility: tracker.visibility)
            }

            if let owner = tracker.owner.canonicalName.split(separator: "~").last {
                Text("~\(owner)")
                    .font(.caption)
                    .foregroundStyle(.secondary)
            }

            if let description = tracker.description, !description.isEmpty {
                Text(description)
                    .font(.caption)
                    .foregroundStyle(.secondary)
                    .lineLimit(2)
            }

            Text(tracker.updated.relativeDescription)
                .font(.caption2)
                .foregroundStyle(.tertiary)
        }
        .padding(.vertical, 2)
    }
}