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

214 lines
5.5 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.
2024-01-14 13:34:21 +00:00
// Copyright (c) 2024 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 {
var licensesHeader: String? = "Licenses"
var noticesHeader: String? = "Notices"
var translationsHeader: String? = "Translations"
let licenses: [License]
let notices: [Notice]
let translations: [String: String]
@State private var contentForLicense: [String: String] = [:]
var body: some View {
List {
if !licenses.isEmpty {
licensesSection
}
if !notices.isEmpty {
noticesSection
}
if !translations.isEmpty {
translationsSection
}
}
}
}
extension GenericCreditsView {
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
private extension GenericCreditsView {
struct LicenseView: View {
let url: URL
2023-03-17 20:55:47 +00:00
@Binding var content: String?
2023-03-17 20:55:47 +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()
}
}.onAppear(perform: loadURL)
2022-04-12 13:09:14 +00:00
}
}
}
// MARK: -
2023-03-17 20:55:47 +00:00
private extension GenericCreditsView {
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
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
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
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
}
}
}
}
}
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
}
}
}
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
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)
2023-12-16 19:58:54 +00:00
#if !os(tvOS)
2022-04-12 13:09:14 +00:00
.navigationBarTitleDisplayMode(.inline)
2023-12-16 19:58:54 +00:00
#endif
2022-04-12 13:09:14 +00:00
}
}
private extension String {
var localizedAsCountryCode: String {
Locale.current.localizedString(forLanguageCode: self)?.capitalized ?? self
}
}
2023-03-17 20:55:47 +00:00
// MARK: -
2023-03-17 20:55:47 +00:00
@MainActor
private extension GenericCreditsView.LicenseView {
func loadURL() {
guard content == nil else {
return
2022-04-12 13:09:14 +00:00
}
Task {
withAnimation {
do {
content = try String(contentsOf: url)
} catch {
content = AppError(error).localizedDescription
2022-04-12 13:09:14 +00:00
}
}
}
}
}