Terms.swift (3567B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 import SwiftUI 3 4 /// Warning to accept at first launch. Changing `version` makes the user accept it again. 5 public struct Terms: Sendable { 6 public var version: Int 7 /// UserDefaults key holding the accepted version. 8 public var acceptedKey: String 9 public var appName: String 10 /// The risk specific to the device ("The watch can fail…"), in the language of the interface. 11 public var devicePoint: String 12 /// Full terms of use. 13 public var link: URL 14 15 public init(version: Int, acceptedKey: String = "termsAcceptedVersion", appName: String, 16 devicePoint: String, link: URL) { 17 self.version = version 18 self.acceptedKey = acceptedKey 19 self.appName = appName 20 self.devicePoint = devicePoint 21 self.link = link 22 } 23 24 public var title: String { String(localized: "Before you dive", bundle: .module) } 25 26 public var points: [String] { 27 [String(localized: "\(appName) is not a certified dive computer.", bundle: .module), 28 String(localized: "Always dive with a primary dive computer, and follow it when the two disagree.", bundle: .module), 29 String(localized: "No computation guarantees the absence of decompression sickness.", bundle: .module), 30 devicePoint, 31 String(localized: "Dive within the limits of your training. You are responsible for your settings and your gas.", bundle: .module)] 32 } 33 34 public var accept: String { String(localized: "I understand and accept", bundle: .module) } 35 } 36 37 /// Warning to accept at first launch (and after a change of `Terms.version`). 38 public struct TermsView: View { 39 let terms: Terms 40 let onAccept: () -> Void 41 42 public init(terms: Terms, onAccept: @escaping () -> Void) { 43 self.terms = terms 44 self.onAccept = onAccept 45 } 46 47 public var body: some View { 48 ScrollView { 49 VStack(alignment: .leading, spacing: 10) { 50 Label(terms.title, systemImage: "exclamationmark.triangle.fill") 51 .font(.headline) 52 .foregroundStyle(.orange) 53 ForEach(terms.points, id: \.self) { point in 54 HStack(alignment: .firstTextBaseline, spacing: 6) { 55 Text(verbatim: "•") 56 Text(point).fixedSize(horizontal: false, vertical: true) 57 } 58 .font(.footnote) 59 } 60 Link(destination: terms.link) { Text("Terms of use", bundle: .module) } 61 .font(.footnote) 62 Button(terms.accept, action: onAccept) 63 .buttonStyle(.borderedProminent) 64 .tint(.orange) 65 .padding(.top, 4) 66 } 67 .padding() 68 } 69 } 70 } 71 72 extension View { 73 /// Shows `TermsView` until the current version has been accepted (the `-acceptTerms` launch argument 74 /// skips it, for screenshots). 75 public func requiresTerms(_ terms: Terms) -> some View { modifier(TermsGate(terms: terms)) } 76 } 77 78 private struct TermsGate: ViewModifier { 79 let terms: Terms 80 @AppStorage private var accepted: Int 81 82 init(terms: Terms) { 83 self.terms = terms 84 _accepted = AppStorage(wrappedValue: 0, terms.acceptedKey) 85 } 86 87 func body(content: Content) -> some View { 88 if accepted >= terms.version || CommandLine.arguments.contains("-acceptTerms") { 89 content 90 } else { 91 TermsView(terms: terms) { accepted = terms.version } 92 } 93 } 94 }