taler-ios

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

ContactsView.swift (7454B)


      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 taler_swift
     10 import SymLog
     11 
     12 #if GNU_TALER || TALER_NIGHTLY
     13 // MARK: -
     14 struct ContactRowView: View {
     15     let stack: CallStack
     16     let contact: ContactEntry
     17     let index: Int
     18     @Binding var showExport: Int?
     19     let talerURI: String?
     20     let subject: String?
     21 
     22     @EnvironmentObject private var model: WalletModel
     23 
     24     @State private var showSend: Bool = false
     25     @State private var qrImage: UIImage? = nil
     26 
     27     private var cancelButton: some View {
     28         Button("Cancel", role: .cancel) {
     29             showSend = false
     30         }
     31     }
     32 
     33     private var sendNowButton: some View {
     34         Button(contact.petname.isEmpty ? "Send now" : "Send to \(contact.petname)") {
     35             showSend = false
     36             if let talerURI {
     37                 Task {
     38                     try? await model.sendTalerUriMailboxMessage(talerURI,
     39 //                                                   subjectHint: subject ?? EMPTYSTRING,
     40                                                             to: contact)
     41                     dismissTop(stack.push())
     42                 }
     43             }
     44         }
     45     }
     46 
     47     var contactURI: String {
     48         var mboxBaseURL = contact.mailboxBaseUrl
     49         if mboxBaseURL.hasPrefix(HTTPS) {
     50             mboxBaseURL.removeFirst(HTTPS.count)
     51         }
     52         var addContact = TALER + "add-contact/"
     53                        + contact.aliasType + SLASH
     54                        + contact.alias + SLASH
     55                        + mboxBaseURL + SLASH
     56                        + contact.mailboxAddress
     57         let source = contact.source
     58         if source.isEmpty {
     59             return addContact
     60         }
     61         return addContact + "?sourceBaseUrl="
     62              + source.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
     63     }
     64 
     65     var body: some View {
     66         let label = VStack {
     67             HStack {
     68                 VStack(alignment: .leading) {
     69                     if !contact.petname.isEmpty {
     70                         Text(contact.petname)
     71                             .talerFont(.title3)
     72                     }
     73                     Text(contact.alias)
     74                         .talerFont(.body)
     75                 }
     76                 Spacer()
     77                 if showExport == index {
     78                     Button("Export") {
     79                         showExport = nil
     80                     }.buttonStyle(.plain)
     81                 } else {
     82                     Button("Export") {
     83                         showExport = index
     84                     }.buttonStyle(.borderless)
     85                 }
     86             }
     87             if showExport == index {
     88                 let size = 240.0
     89                 QRGeneratorView(image: qrImage, size: size,
     90                                 logo: nil, logoSize: size / 4)
     91                 .task {
     92                     print("🚩 \(contactURI)")
     93                     if let image = QRCode(from: contactURI) {
     94                         qrImage = image
     95                     }
     96                 }
     97             }
     98         }
     99 
    100         if let talerURI {       // send this to the selected contact
    101             label
    102                 .contentShape(.rect)
    103                 .frame(maxWidth: .infinity, alignment: .leading)
    104                 .background {
    105                     Color.primary.opacity(showSend ? 0.12 : INVISIBLE)
    106                 }
    107                 .onTapGesture { showSend.toggle() }
    108                 .alert("Send to \(contact.petname)",
    109                        isPresented: $showSend,
    110                        actions: {   sendNowButton
    111                                     cancelButton },
    112                        message: {   if let subject {
    113                                         Text(subject)
    114                                     } })
    115         } else {                // edit the selected contact
    116             NavigationLink {
    117                 let title = String(localized: "TitleEditContact",
    118                                 defaultValue: "Edit Contact", comment: "View Title, Button")
    119                 AddContactView(stack.push(), contact: contact, navTitle: title)
    120             } label: { label }
    121         }
    122     }
    123 }
    124 // MARK: -
    125 /// This view shows the list of contacts
    126 ///  called either from TalDir settings (without talerURI), or from CopyShare->TalDirButton (with talerURI)
    127 struct ContactsView: View {
    128     private let symLog = SymLogV(0)
    129     let stack: CallStack
    130     let talerURI: String?
    131     let subject: String?
    132 
    133     @EnvironmentObject private var model: WalletModel
    134     @EnvironmentObject private var controller: Controller
    135     @AppStorage("minimalistic") var minimalistic: Bool = false
    136     @AppStorage("myListStyle") var myListStyle: MyListStyle = .automatic
    137 
    138     @State private var contacts: [ContactEntry]? = nil
    139     @State private var showAddDialog: Bool = false
    140     @State private var showExport: Int? = nil
    141 
    142     var navTitle: String {
    143         String(localized: "TitleContacts", defaultValue: "Contacts")
    144     }
    145 
    146     var body: some View {
    147 #if PRINT_CHANGES
    148         let _ = Self._printChanges()
    149         let _ = symLog.vlog()       // just to get the # to compare it with .onAppear & onDisappear
    150 #endif
    151         let a11yLabelStr = String(localized: "Add contact", comment: "a11y for the + button")
    152         let plusButton = PlusButton(accessibilityLabelStr: a11yLabelStr) {
    153             showAddDialog = true
    154         }
    155         let addContactDest = AddContactView(stack.push())
    156         let actions = Group {
    157             NavLink($showAddDialog) { addContactDest }
    158         }
    159         let contacts = controller.contacts
    160         let ownEntries = contacts.filter({$0.isSelf == true })
    161         let otherEntries = contacts.filter({$0.isSelf != true })
    162         List {
    163             if !ownEntries.isEmpty {
    164                 Section(header: Text("Own contacts")) {
    165                     ForEachWithIndex(array: ownEntries) { index, contact in
    166                         ContactRowView(stack: stack.push(),
    167                                      contact: contact,
    168                                        index: index,
    169                                   showExport: $showExport,
    170                                     talerURI: talerURI,
    171                                      subject: subject)
    172                     }
    173                 }
    174             }
    175             Section {
    176                 if otherEntries.isEmpty {
    177                     Text("No contacts yet...")
    178                 } else {
    179                     let count = ownEntries.count
    180                     ForEachWithIndex(array: otherEntries) { index, contact in
    181                         ContactRowView(stack: stack.push(),
    182                                      contact: contact,
    183                                        index: index + count,
    184                                   showExport: $showExport,
    185                                     talerURI: talerURI,
    186                                      subject: subject)
    187                     }
    188                 }
    189             }
    190         }
    191         .background(actions)
    192         .listStyle(myListStyle.style).anyView
    193 //        .task { await viewDidLoad() }
    194         .refreshable {
    195             controller.hapticNotification(.success)
    196             symLog.log("refreshing")
    197             await controller.loadContacts(stack.push())
    198         }
    199         .navigationTitle(navTitle)
    200         .navigationBarItems(trailing: plusButton)
    201         .onAppear() {
    202             DebugViewC.shared.setViewID(VIEW_CONTACTS, stack: stack.push())
    203         }
    204     } // body
    205 }
    206 #endif