damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

RelayPaidDetail.swift (4090B)


      1 //
      2 //  RelayPaidDetail.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2023-02-10.
      6 //
      7 
      8 import SwiftUI
      9 
     10 struct RelayPaidDetail: View {
     11     let payments_url: String?
     12     var fees: Fees? = nil
     13     @Environment(\.openURL) var openURL
     14     
     15     func timeString(time: Int) -> String {
     16         let formatter = DateComponentsFormatter()
     17         formatter.allowedUnits = [.year, .month, .day, .hour, .minute, .second]
     18         formatter.unitsStyle = .full
     19         let formattedString = formatter.string(from: TimeInterval(time)) ?? ""
     20         return formattedString
     21     }
     22     
     23     func Amount(unit: String, amount: Int64) -> some View {
     24         HStack {
     25             if unit == "msats" {
     26                 Text("\(format_msats(amount))")
     27                     .font(.system(size: 13, weight: .heavy))
     28                     .foregroundColor(DamusColors.white)
     29             } else {
     30                 Text("\(amount) \(unit)")
     31                     .font(.system(size: 13, weight: .heavy))
     32                     .foregroundColor(DamusColors.white)
     33             }
     34         }
     35     }
     36     
     37     var body: some View {
     38         HStack(spacing: 0) {
     39             ZStack(alignment: .leading) {
     40                 if let url = payments_url.flatMap({ URL(string: $0) }) {
     41                     RelayType(is_paid: true)
     42                         .zIndex(1)
     43                     
     44                     Button(action: {
     45                         openURL(url)
     46                     }, label: {
     47                         if let admission = fees?.admission {
     48                             if !admission.isEmpty {
     49                                 Amount(unit: admission[0].unit, amount: admission[0].amount)
     50                             } else {
     51                                 Text(verbatim: "Paid Relay")
     52                                     .font(.system(size: 13, weight: .heavy))
     53                                     .foregroundColor(DamusColors.white)
     54                             }
     55                         } else if let subscription = fees?.subscription {
     56                             if !subscription.isEmpty {
     57                                 Amount(unit: subscription[0].unit, amount: subscription[0].amount)
     58                                 Text("/ \(timeString(time: subscription[0].period))")
     59                                     .font(.system(size: 13, weight: .heavy))
     60                                     .foregroundColor(DamusColors.white)
     61                             }
     62                         } else if let publication = fees?.publication {
     63                             if !publication.isEmpty {
     64                                 Amount(unit: publication[0].unit, amount: publication[0].amount)
     65                                 Text("/ event")
     66                                     .font(.system(size: 13, weight: .heavy))
     67                                     .foregroundColor(DamusColors.white)
     68                             }
     69                         } else {
     70                             Text(verbatim: "Paid Relay")
     71                                 .font(.system(size: 13, weight: .heavy))
     72                                 .foregroundColor(DamusColors.white)
     73                         }
     74                     })
     75                     .padding(EdgeInsets(top: 3, leading: 25, bottom: 3, trailing: 10))
     76                     .background(DamusColors.bitcoin.opacity(0.7))
     77                     .cornerRadius(15)
     78                     .overlay(
     79                         RoundedRectangle(cornerRadius: 20)
     80                             .stroke(DamusColors.warningBorder, lineWidth: 1)
     81                     )
     82                     .padding(.leading, 1)
     83                 }
     84             }
     85         }
     86     }
     87 }
     88 
     89 struct RelayPaidDetail_Previews: PreviewProvider {
     90     static var previews: some View {
     91         let admission = Admission(amount: 1000000, unit: "msats")
     92         let sub = Subscription(amount: 5000000, unit: "msats", period: 2592000)
     93         let pub = Publication(kinds: [1, 4], amount: 100, unit: "msats")
     94         let fees = Fees(admission: [admission], subscription: [sub], publication: [pub])
     95         RelayPaidDetail(payments_url: "https://jb55.com", fees: fees)
     96     }
     97 }