|
8 | 8 | import SwiftUI |
9 | 9 |
|
10 | 10 | struct SYAboutView: View { |
| 11 | + typealias CreditsDataHandler = Result<[CreditsModel], Error> |
| 12 | + private let _dataService = NBFetchService() |
| 13 | + |
| 14 | + @State private var _credits: [CreditsModel] = [] |
| 15 | + @State private var _donators: [CreditsModel] = [] |
| 16 | + @State var isLoading = true |
| 17 | + |
| 18 | + private let _creditsUrl = "https://raw.githubusercontent.com/khcrysalis/project-credits/refs/heads/main/protokolle/credits.json" |
| 19 | + private let _donatorsUrl = "https://raw.githubusercontent.com/khcrysalis/project-credits/refs/heads/main/sponsors/credits.json" |
| 20 | + |
11 | 21 | var body: some View { |
12 | | - ZStack { |
13 | | - VStack { |
14 | | - Image(uiImage: (UIImage(named: Bundle.main.iconFileName ?? ""))! ) |
15 | | - .appIconStyle(size: 72) |
16 | | - |
17 | | - Text(Bundle.main.name) |
18 | | - .font(.largeTitle) |
19 | | - .bold() |
20 | | - .foregroundStyle(.tint) |
21 | | - |
22 | | - HStack(spacing: 4) { |
23 | | - Text(.localized("Version")) |
24 | | - Text(Bundle.main.version) |
| 22 | + Form { |
| 23 | + Section { |
| 24 | + VStack { |
| 25 | + Image(uiImage: (UIImage(named: Bundle.main.iconFileName ?? ""))! ) |
| 26 | + .appIconStyle(size: 72) |
| 27 | + |
| 28 | + Text(Bundle.main.name) |
| 29 | + .font(.largeTitle) |
| 30 | + .bold() |
| 31 | + .foregroundStyle(.tint) |
| 32 | + |
| 33 | + HStack(spacing: 4) { |
| 34 | + Text(.localized("Version")) |
| 35 | + Text(Bundle.main.version) |
| 36 | + } |
| 37 | + .font(.footnote) |
| 38 | + .foregroundStyle(.secondary) |
| 39 | + |
| 40 | + } |
| 41 | + } |
| 42 | + .frame(maxWidth: .infinity) |
| 43 | + .listRowBackground(EmptyView()) |
| 44 | + |
| 45 | + Section("Credits") { |
| 46 | + if !_credits.isEmpty { |
| 47 | + ForEach(_credits, id: \.github) { credit in |
| 48 | + _credit(name: credit.name, desc: credit.desc, github: credit.github) |
| 49 | + } |
| 50 | + .transition(.slide) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + Section("Sponsors") { |
| 55 | + if !_donators.isEmpty { |
| 56 | + Group { |
| 57 | + Text(try! AttributedString(markdown: _donators.map { |
| 58 | + "[\($0.name ?? $0.github)](https://github.com/\($0.github))" |
| 59 | + }.joined(separator: ", "))) |
| 60 | + |
| 61 | + Text(.localized("💚 This couldn't of been done without my sponsors!")) |
| 62 | + .foregroundStyle(.secondary) |
| 63 | + .padding(.vertical, 2) |
| 64 | + } |
| 65 | + .transition(.slide) |
25 | 66 | } |
26 | | - .font(.footnote) |
27 | | - .foregroundStyle(.secondary) |
28 | 67 | } |
29 | | - .ignoresSafeArea(.all) |
30 | 68 | } |
31 | | - .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center) |
32 | 69 | .navigationTitle(.localized("About")) |
| 70 | + .animation(.default, value: isLoading) |
| 71 | + .task { |
| 72 | + await _fetchAllData() |
| 73 | + } |
33 | 74 | } |
| 75 | + |
| 76 | + private func _fetchAllData() async { |
| 77 | + await withTaskGroup(of: (String, CreditsDataHandler).self) { group in |
| 78 | + group.addTask { return await _fetchCredits(self._creditsUrl, using: _dataService) } |
| 79 | + group.addTask { return await _fetchCredits(self._donatorsUrl, using: _dataService) } |
| 80 | + |
| 81 | + for await (type, result) in group { |
| 82 | + await MainActor.run { |
| 83 | + switch result { |
| 84 | + case .success(let data): |
| 85 | + if type == "credits" { |
| 86 | + self._credits = data |
| 87 | + } else { |
| 88 | + self._donators = data |
| 89 | + } |
| 90 | + case .failure(_): break |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + await MainActor.run { |
| 97 | + isLoading = false |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private func _fetchCredits(_ urlString: String, using service: NBFetchService) async -> (String, CreditsDataHandler) { |
| 102 | + let type = urlString == _creditsUrl |
| 103 | + ? "credits" |
| 104 | + : "donators" |
| 105 | + |
| 106 | + return await withCheckedContinuation { continuation in |
| 107 | + service.fetch(from: urlString) { (result: CreditsDataHandler) in |
| 108 | + continuation.resume(returning: (type, result)) |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// MARK: - Extension: view |
| 115 | +extension SYAboutView { |
| 116 | + @ViewBuilder |
| 117 | + private func _credit( |
| 118 | + name: String?, |
| 119 | + desc: String?, |
| 120 | + github: String |
| 121 | + ) -> some View { |
| 122 | + Button { |
| 123 | + UIApplication.open("https://github.com/\(github)") |
| 124 | + } label: { |
| 125 | + HStack(spacing: 12) { |
| 126 | + AsyncImage(url: URL(string: "https://github.com/\(github).png")) { image in |
| 127 | + image.appIconStyle(isCircle: true) |
| 128 | + } placeholder: { |
| 129 | + EmptyView() |
| 130 | + } |
| 131 | + |
| 132 | + VStack(alignment: .leading, spacing: 2) { |
| 133 | + Text(name ?? github) |
| 134 | + .font(.headline) |
| 135 | + Text(desc ?? "") |
| 136 | + .font(.subheadline) |
| 137 | + .foregroundColor(.secondary) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
34 | 142 | } |
| 143 | + |
0 commit comments