taler-ios

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

MailboxView.swift (3542B)


      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 /// This view shows the messages
     13 struct MailboxView: View {
     14     private let symLog = SymLogV(0)
     15     let stack: CallStack
     16     let navTitle: String
     17 
     18     @EnvironmentObject private var model: WalletModel
     19     @EnvironmentObject private var controller: Controller
     20     @AppStorage("minimalistic") var minimalistic: Bool = false
     21     @AppStorage("myListStyle") var myListStyle: MyListStyle = .automatic
     22 
     23     @State private var created: Bool = false
     24     @State private var progress: Double = 0
     25     @State private var isCancelled: Bool = false
     26 
     27     private var dismissAlertButton: some View {
     28         Button("Cancel", role: .cancel) {
     29 //            showRestoreAlert = false
     30 //            withAnimation { selectedBackup = nil }
     31         }
     32     }
     33 
     34     @ViewBuilder func messageRowV(_ message: MailboxMessage) -> some View {
     35         VStack(alignment: .leading) {
     36             Text(message.talerUri)
     37                 .talerFont(.body)
     38             let (dateString, date) = TalerDater.dateString(message.downloadedAt, minimalistic)
     39             let a11yDate = TalerDater.accessibilityDate(date) ?? dateString
     40             Text(dateString)
     41                 .talerFont(.body)
     42                 .accessibilityLabel(a11yDate)
     43         }
     44         .contentShape(.rect)
     45         .frame(maxWidth: .infinity, alignment: .leading)
     46         .onTapGesture {
     47             if let url = URL(string: message.talerUri) {
     48                 dismissTop(stack.push())
     49                 DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
     50                     controller.talerURI = url    // pass it via .onChange(of: controller.talerURI) directly to MainView
     51                 }
     52             } else {
     53                 // TODO: show alert that the url is not recognized
     54             }
     55         }
     56     }
     57 
     58     var body: some View {
     59 #if PRINT_CHANGES
     60         let _ = Self._printChanges()
     61         let _ = symLog.vlog()       // just to get the # to compare it with .onAppear & onDisappear
     62 #endif
     63         List {
     64             let messages = controller.messages
     65             Section {
     66                 if messages.isEmpty {
     67                     Text("No messages yet...")
     68                 } else {
     69                     ForEachWithIndex(array: messages) { index, message in
     70                         messageRowV(message)
     71                             .swipeActions(edge: .trailing) {
     72                                 Button {
     73 //                                    symLog?.log("deleteAction")
     74                                     Task { // runs on MainActor
     75                                         try? await model.deleteMailboxMessage(message)
     76                                         controller.messages.remove(at: index)
     77                                     }
     78                                 } label: {
     79                                     Label("Delete", systemImage: "trash")
     80                                 }
     81                                 .tint(WalletColors().negative)
     82                             }
     83                     }
     84                 }
     85             }
     86         }
     87         .listStyle(myListStyle.style).anyView
     88         .navigationTitle(navTitle)
     89         .refreshable {
     90             controller.hapticNotification(.success)
     91             symLog.log("refreshing")
     92             await controller.refreshMailbox(stack.push())
     93         }
     94         .onAppear() {
     95             DebugViewC.shared.setViewID(VIEW_MAILBOX, stack: stack.push())
     96         }
     97     } // body
     98 }