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

201 lines
5.4 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 {
2023-03-19 15:10:40 +00:00
struct License {
let name: String
2023-03-17 20:55:47 +00:00
2023-03-19 15:10:40 +00:00
let licenseName: String
let licenseURL: URL
init(_ name: String, _ licenseName: String, _ licenseURL: URL) {
self.name = name
self.licenseName = licenseName
self.licenseURL = licenseURL
}
}
struct Notice {
let name: String
let noticeString: String
init(_ name: String, _ noticeString: String) {
self.name = name
self.noticeString = noticeString
}
}
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 {
2023-03-19 15:10:40 +00:00
$0.name.lowercased() < $1.name.lowercased()
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 var sortedNotices: [Notice] {
2022-09-04 18:09:31 +00:00
notices.sorted {
2023-03-19 15:10:40 +00:00
$0.name.lowercased() < $1.name.lowercased()
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 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)
) {
2023-03-19 15:10:40 +00:00
ForEach(sortedLicenses, id: \.name) { license in
2022-04-12 13:09:14 +00:00
NavigationLink {
LicenseView(
2023-03-19 15:10:40 +00:00
url: license.licenseURL,
content: $contentForLicense[license.name]
).navigationTitle(license.name)
2022-04-12 13:09:14 +00:00
} label: {
HStack {
2023-03-19 15:10:40 +00:00
Text(license.name)
2022-04-12 13:09:14 +00:00
Spacer()
2023-03-19 15:10:40 +00:00
Text(license.licenseName)
2022-04-12 13:09:14 +00:00
}
}
}
}
}
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)
) {
2023-03-19 15:10:40 +00:00
ForEach(sortedNotices, id: \.name) { notice in
NavigationLink(notice.name, destination: noticeView(notice))
2022-04-12 13:09:14 +00:00
}
}
}
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
2023-03-19 15:10:40 +00:00
private func noticeView(_ content: Notice) -> some View {
2022-04-12 13:09:14 +00:00
VStack {
2023-03-19 15:10:40 +00:00
Text(content.noticeString)
2022-04-12 13:09:14 +00:00
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
.padding()
2023-03-19 15:10:40 +00:00
}.navigationTitle(content.name)
2022-04-12 13:09:14 +00:00
.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
}
}