taler-ios

iOS apps for GNU Taler (wallet)
Log | Files | Refs | README | LICENSE

WithdrawTOSView.swift (8344B)


      1 /*
      2  * This file is part of GNU Taler, ©2022-26 Taler Systems S.A.
      3  * See LICENSE.md
      4  */
      5 /**
      6  * @author Marc Stibane
      7  */
      8 import SwiftUI
      9 import SymLog
     10 import MarkdownUI
     11 
     12 struct WithdrawTOSView: View {
     13     private let symLog = SymLogV(0)
     14     let stack: CallStack
     15     let exchangeBaseUrl: String?
     16     let shouldDismiss: Bool
     17     let viewID: Int         // either VIEW_WITHDRAW_TOS or SHEET_WITHDRAW_TOS
     18 
     19     @Environment(\.dismiss) var dismiss     // pop back once
     20     @EnvironmentObject private var controller: Controller
     21     @EnvironmentObject private var model: WalletModel
     22     @AppStorage("myListStyle") var myListStyle: MyListStyle = .automatic
     23 
     24     @State private var exchangeTOS: ExchangeTermsOfService?
     25 
     26     let navTitle = String(localized: "Terms of Service")
     27 
     28     @MainActor
     29     func loadToS(_ language: String) async {
     30         if let exchangeBaseUrl {
     31             let acceptedFormat: [String] = [MARKDOWN, PLAINTEXT]      // MARKDOWN, HTML, PLAINTEXT
     32             if let someTOS = try? await model.getExchangeTos(exchangeBaseUrl,
     33                                               acceptedFormat: acceptedFormat,
     34                                               acceptLanguage: language) {
     35                 exchangeTOS = someTOS
     36             }
     37         } else {
     38             // TODO: Yikes! No baseURL
     39         }
     40     }
     41 
     42     @MainActor
     43     func acceptToS() async {
     44         if exchangeTOS != nil, let exchangeBaseUrl {
     45             _ = await controller.setTosAccepted(for: exchangeBaseUrl)
     46             if shouldDismiss { // just go back - caller will reload
     47                 dismiss()
     48             }
     49         } else {
     50             // TODO: error
     51         }
     52     }
     53 
     54     var body: some View {
     55         let languageCode = Locale.preferredLanguageCode
     56 //        let languageName = Locale.current.localizedString(forLanguageCode: languageCode)
     57         if let exchangeTOS, let exchangeBaseUrl {
     58             Content(symLog: symLog,
     59                        tos: exchangeTOS,
     60                myListStyle: $myListStyle,
     61                   language: languageCode,
     62             languageAction: loadToS) {
     63                 Task { await acceptToS() }
     64             }
     65             .navigationTitle(navTitle)
     66             .refreshable {
     67                 try? await model.updateExchange(exchangeBaseUrl: exchangeBaseUrl, force: true)
     68                 await loadToS(languageCode)
     69             }
     70             .onAppear() {
     71                 if viewID > SHEET_WITHDRAWAL {
     72                     DebugViewC.shared.setSheetID(SHEET_WITHDRAW_TOS, stack: stack.push())
     73                 } else {
     74                     DebugViewC.shared.setViewID(VIEW_WITHDRAW_TOS, stack: stack.push())
     75                 }
     76             }
     77         } else {
     78             let fallback = String(localized: "No payment service", comment: "loading")
     79             LoadingView(stack: stack.push(), scopeInfo: nil,
     80                         message: exchangeBaseUrl?.trimURL ?? fallback)
     81                 .task {
     82                     await loadToS(languageCode)
     83                 }
     84         }
     85     }
     86 }
     87 // MARK: -
     88 extension WithdrawTOSView {
     89 
     90     static func cleanupText(_ term0: String) -> String {
     91         let term1 = term0.replacingOccurrences(of: "\n     ", with: " ")    // newline + 5 blanks
     92         let term2 = term1.replacingOccurrences(of: "\n    ", with: " ")     // newline + 4 blanks
     93         let term3 = term2.replacingOccurrences(of: "\n   ", with: " ")      // newline + 3 blanks
     94         let term4 = term3.replacingOccurrences(of: "\n  ", with: " ")       // newline + 2 blanks
     95         let term5 = term4.replacingOccurrences(of: "\n ", with: " ")        // newline + 1 blank
     96         let term6 = term5.replacingOccurrences(of: "\n", with: " ")         // remove all other linebreaks
     97         let term7 = term6.replacingOccurrences(of: " ====", with: "\n====") // add them back for underscoring
     98         let term8 = term7.replacingOccurrences(of: " ----", with: "\n----") // special for "Highlights:"
     99         let term9 = term8.replacingOccurrences(of: " ****", with: "\n****") // special for "Terms of Service:"
    100         return term9
    101     }
    102 
    103     struct plaintextToSV: View {
    104         let plaintext: String
    105 
    106         var body: some View {
    107             let components = plaintext.components(separatedBy: "\n\n")
    108             ForEach (components, id: \.self) { term0 in
    109                 Section {
    110                     Text(cleanupText(term0))
    111                         .talerFont(.footnote)
    112                         .foregroundColor(Color(UIColor.label))
    113                 }
    114             } // for
    115         }
    116     }
    117 
    118     struct Content: View {
    119         let symLog: SymLogV
    120         var tos: ExchangeTermsOfService
    121         @Binding var myListStyle: MyListStyle
    122         let language: String
    123         var languageAction: (String) async -> ()
    124         var acceptAction: () -> ()
    125 
    126         @Environment(\.colorScheme) private var colorScheme
    127         @Environment(\.colorSchemeContrast) private var colorSchemeContrast
    128 #if DEBUG
    129         @AppStorage("developerMode") var developerMode: Bool = true
    130 #else
    131         @AppStorage("developerMode") var developerMode: Bool = false
    132 #endif
    133         @State private var selectedLanguage = Locale.preferredLanguageCode
    134 
    135         var showButton: Bool {
    136             if tos.tosStatus == .missingTos {
    137                 return false                // nothing to accept
    138             }
    139             if let acceptedEtag = tos.acceptedEtag {
    140                 if acceptedEtag == tos.currentEtag {
    141                     return false            // user already accepted current ToS
    142                 }
    143             }
    144             return true
    145         }
    146 
    147         var body: some View {
    148             let title = String(localized: "Language:", comment: "title of ToS language selection")
    149             let list = List {
    150                 if tos.tosAvailableLanguages.count > 1 {
    151                     Picker(title, selection: $selectedLanguage) {
    152                         ForEach(tos.tosAvailableLanguages, id: \.self) { code in
    153                             let languageName = Locale.current.localizedString(forLanguageCode: code)
    154                             Text(languageName ?? code)
    155                         }
    156                     }
    157                     .talerFont(.title3)
    158                     .pickerStyle(.menu)
    159                     .onAppear() {
    160                         withAnimation { selectedLanguage = language }
    161                     }
    162                     .onChange(of: selectedLanguage) { selected in
    163                         Task {
    164                             await languageAction(selected)
    165                         }
    166                     }
    167                 }
    168                 if tos.tosStatus == .missingTos {
    169                     Section {
    170                         Text("This payment service has not published any terms of service.")
    171                             .talerFont(.body)
    172                             .foregroundColor(Color(UIColor.label))
    173                     }
    174                     .accessibility(sortPriority: 1)     // read this last
    175                 } else if tos.contentType == MARKDOWN {
    176                     Section {
    177                         let content = MarkdownContent(tos.content)
    178                         Markdown(content)
    179                     }
    180                     .accessibility(sortPriority: 1)     // read this last
    181                 } else {
    182                     let components = tos.content.components(separatedBy: "\n\n")
    183                     ForEach (components, id: \.self) { term0 in
    184                         Section {
    185                             Text(cleanupText(term0))
    186                                 .talerFont(.footnote)
    187                                 .foregroundColor(Color(UIColor.label))
    188                         }
    189                         .accessibility(sortPriority: 1)     // read this last
    190                     } // for
    191                 } // plain text
    192             }.listStyle(myListStyle.style).anyView
    193             list.safeAreaInset(edge: .bottom) {
    194                 if showButton {
    195                         Button(String(localized: "Accept Terms of Service", comment: "Button"), action: acceptAction)
    196                             .buttonStyle(TalerButtonStyle(type: .prominent))
    197                             .accessibility(sortPriority: 2)     // read this second
    198                             .accessibilityIdentifier(A11yID.tosAcceptButton)
    199                             .padding(.horizontal)
    200                 }
    201             }
    202         }
    203     }
    204 }