taler-ios

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

URL+id+iban.swift (3443B)


      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 trimmedString: String {
     20         absoluteString.trimmingCharacters(in: .whitespacesAndNewlines)
     21     }
     22 
     23     var cyclos: String? {
     24         /// "payto://cyclos/" host ["/" fpath] "/" account-id [ "?" opts ]
     25         if scheme == "payto", let host = host {
     26             let path = path.dropFirst(1)
     27             if host == "cyclos" {
     28                 return String(path)
     29             } else if host.hasPrefix("cyclos") {
     30                 let cyclos = host.dropFirst(6)
     31                 if path.hasPrefix("/") {
     32                     return String(cyclos + path)
     33                 }
     34                 return String(cyclos + "/" + path)
     35             }
     36         }
     37         return nil
     38     }
     39 
     40     var iban: String? {
     41         /// https://datatracker.ietf.org/doc/rfc8905/
     42         /// payto://iban/DE75512108001245126199?amount=EUR:200.0&message=hello
     43         if scheme == "payto" && host == "iban" {
     44             return lastPathComponent
     45         }
     46         return nil
     47     }
     48 
     49     var xTaler: String? {
     50         /// https://datatracker.ietf.org/doc/rfc8905/
     51         /// payto://iban/DE75512108001245126199?amount=EUR:200.0&message=hello
     52         if scheme == "payto" && host == "x-taler-bank" {
     53             return lastPathComponent
     54         }
     55         return nil
     56     }
     57 
     58     /// SwifterSwift: Dictionary of the URL's query parameters.
     59     var queryParameters: [String:String]? {
     60         guard let components = URLComponents(url: self, resolvingAgainstBaseURL: false),
     61               let queryItems = components.queryItems else { return nil }
     62 
     63         var items: [String:String] = [:]
     64 
     65         for queryItem in queryItems {
     66             items[queryItem.name] = queryItem.value
     67         }
     68         return items
     69     }
     70 
     71     static var docDirUrl: URL? {                    // accessible by FileSharing
     72         if #available(iOS 16.4, *) {
     73             return URL.documentsDirectory
     74         } else {
     75             let docDirNr: FileManager.SearchPathDirectory = .documentDirectory
     76             if let documentsDir = FileManager.default.urls(for: docDirNr, in: .userDomainMask).first {
     77                 return documentsDir
     78             }
     79         }
     80         return nil
     81     }
     82 
     83     static var appSuppUrl: URL? {
     84         if #available(iOS 16.4, *) {
     85             return URL.applicationSupportDirectory
     86         } else {
     87             let appDirNr: FileManager.SearchPathDirectory = .applicationSupportDirectory
     88             if let appSupportDir = FileManager.default.urls(for: appDirNr, in: .userDomainMask).first {
     89                 return appSupportDir
     90             }
     91         }
     92         return nil
     93     }
     94 
     95     func path(withSlash: Bool) -> String {
     96         let path: String
     97         if #available(iOS 16.4, *) {
     98             path = self.path(percentEncoded: false)
     99         } else {
    100             path = self.path
    101         }
    102         if let char = path.last {
    103             let slash: String.Element = "/"
    104 
    105             if withSlash {
    106                 if char != slash {
    107                     return path + String(slash)
    108                 }
    109             } else {
    110                 if char == slash {
    111                     return String(path.dropLast())
    112                 }
    113             }
    114         }
    115         return path
    116     }
    117 }