taler-ios

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

URL+id+iban.swift (2768B)


      1 /*
      2  * This file is part of GNU Taler, ©2022-25 Taler Systems S.A.
      3  * See LICENSE.md
      4  */
      5 /**
      6  * @author Marc Stibane
      7  */
      8 import SwiftUI
      9 
     10 extension URL: Identifiable {
     11     public var id: URL {self}
     12 }
     13 
     14 extension URL {
     15     init(_ string: StaticString) {
     16         self.init(string: "\(string)")!
     17     }
     18 
     19     var iban: String? {
     20         /// https://datatracker.ietf.org/doc/rfc8905/
     21         /// payto://iban/DE75512108001245126199?amount=EUR:200.0&message=hello
     22         if scheme == "payto" && host == "iban" {
     23             return lastPathComponent
     24         }
     25         return nil
     26     }
     27 
     28     var xTaler: String? {
     29         /// https://datatracker.ietf.org/doc/rfc8905/
     30         /// payto://iban/DE75512108001245126199?amount=EUR:200.0&message=hello
     31         if scheme == "payto" && host == "x-taler-bank" {
     32             return lastPathComponent
     33         }
     34         return nil
     35     }
     36 
     37     /// SwifterSwift: Dictionary of the URL's query parameters.
     38     var queryParameters: [String:String]? {
     39         guard let components = URLComponents(url: self, resolvingAgainstBaseURL: false),
     40               let queryItems = components.queryItems else { return nil }
     41 
     42         var items: [String:String] = [:]
     43 
     44         for queryItem in queryItems {
     45             items[queryItem.name] = queryItem.value
     46         }
     47         return items
     48     }
     49 
     50     static var docDirUrl: URL? {                    // accessible by FileSharing
     51         if #available(iOS 16.4, *) {
     52             return URL.documentsDirectory
     53         } else {
     54             let docDirNr: FileManager.SearchPathDirectory = .documentDirectory
     55             if let documentsDir = FileManager.default.urls(for: docDirNr, in: .userDomainMask).first {
     56                 return documentsDir
     57             }
     58         }
     59         return nil
     60     }
     61 
     62     static var appSuppUrl: URL? {
     63         if #available(iOS 16.4, *) {
     64             return URL.applicationSupportDirectory
     65         } else {
     66             let appDirNr: FileManager.SearchPathDirectory = .applicationSupportDirectory
     67             if let appSupportDir = FileManager.default.urls(for: appDirNr, in: .userDomainMask).first {
     68                 return appSupportDir
     69             }
     70         }
     71         return nil
     72     }
     73 
     74     func path(withSlash: Bool) -> String {
     75         let path: String
     76         if #available(iOS 16.4, *) {
     77             path = self.path(percentEncoded: false)
     78         } else {
     79             path = self.path
     80         }
     81         if let char = path.last {
     82             let slash: String.Element = "/"
     83 
     84             if withSlash {
     85                 if char != slash {
     86                     return path + String(slash)
     87                 }
     88             } else {
     89                 if char == slash {
     90                     return String(path.dropLast())
     91                 }
     92             }
     93         }
     94         return path
     95     }
     96 }