passepartout-apple/Passepartout/App/Reusable/GenericCreditsView.swift

180 lines
4.9 KiB
Swift
Raw Normal View History

2022-04-12 13:09:14 +00:00
//
// GenericCreditsView.swift
// Passepartout
//
// Created by Davide De Rosa on 2/27/22.
2023-03-17 15:56:19 +00:00
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
2022-04-12 13:09:14 +00:00
//
// https://github.com/passepartoutvpn
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//
import SwiftUI
struct GenericCreditsView: View {
typealias License = (String, String, URL)
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
typealias Notice = (String, String)
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
var licensesHeader: String? = "Licenses"
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
var noticesHeader: String? = "Notices"
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
var translationsHeader: String? = "Translations"
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
let licenses: [License]
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
let notices: [Notice]
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
let translations: [String: String]
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
@State private var contentForLicense: [String: String] = [:]
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
var body: some View {
List {
if !licenses.isEmpty {
licensesSection
}
if !notices.isEmpty {
noticesSection
}
if !translations.isEmpty {
translationsSection
}
}
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
private var sortedLicenses: [License] {
2022-09-04 18:09:31 +00:00
licenses.sorted {
2022-04-12 13:09:14 +00:00
$0.0.lowercased() < $1.0.lowercased()
}
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
private var sortedNotices: [Notice] {
2022-09-04 18:09:31 +00:00
notices.sorted {
2022-04-12 13:09:14 +00:00
$0.0.lowercased() < $1.0.lowercased()
}
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
private var sortedLanguages: [String] {
2022-09-04 18:09:31 +00:00
translations.keys.sorted {
2022-04-12 13:09:14 +00:00
$0.localizedAsCountryCode < $1.localizedAsCountryCode
}
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
private var licensesSection: some View {
2023-03-17 20:55:47 +00:00
Section(
2022-04-12 13:09:14 +00:00
header: licensesHeader.map(Text.init)
) {
ForEach(sortedLicenses, id: \.0) { license in
NavigationLink {
LicenseView(
url: license.2,
content: $contentForLicense[license.0]
).navigationTitle(license.0)
} label: {
HStack {
Text(license.0)
Spacer()
Text(license.1)
}
}
}
}
}
private var noticesSection: some View {
2023-03-17 20:55:47 +00:00
Section(
2022-04-12 13:09:14 +00:00
header: noticesHeader.map(Text.init)
) {
ForEach(sortedNotices, id: \.0) { notice in
NavigationLink(notice.0, destination: noticeView(notice))
}
}
}
private var translationsSection: some View {
2023-03-17 20:55:47 +00:00
Section(
2022-04-12 13:09:14 +00:00
header: translationsHeader.map(Text.init)
) {
ForEach(sortedLanguages, id: \.self) { code in
HStack {
Text(code.localizedAsCountryCode)
Spacer()
translations[code].map { author in
Text(author)
.padding()
}
}
}
}
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
private func noticeView(_ content: (String, String)) -> some View {
VStack {
Text(content.1)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
.padding()
}.navigationTitle(content.0)
.navigationBarTitleDisplayMode(.inline)
}
}
extension GenericCreditsView {
struct LicenseView: View {
let url: URL
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
@Binding var content: String?
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
var body: some View {
ZStack {
content.map { unwrapped in
ScrollView {
Text(unwrapped)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
.padding()
}
}
if content == nil {
ProgressView()
2022-04-12 13:09:14 +00:00
}
}.onAppear(perform: loadURL)
2022-04-12 13:09:14 +00:00
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
private func loadURL() {
guard content == nil else {
return
}
Task { @MainActor in
2022-04-12 13:09:14 +00:00
withAnimation {
do {
content = try String(contentsOf: url)
} catch {
content = error.localizedDescription
}
}
}
}
}
}
private extension String {
var localizedAsCountryCode: String {
2022-09-04 18:09:31 +00:00
Locale.current.localizedString(forLanguageCode: self)?.capitalized ?? self
2022-04-12 13:09:14 +00:00
}
}