RelayPaidDetail.swift (4533B)
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 displayAmount(unit: String, amount: Int64) -> String { 24 if unit == "msats" { 25 format_msats(amount) 26 } else { 27 "\(amount) \(unit)" 28 } 29 } 30 31 func Amount(unit: String, amount: Int64) -> some View { 32 HStack { 33 let displayString = displayAmount(unit: unit, amount: amount) 34 Text(displayString) 35 .font(.system(size: 13, weight: .heavy)) 36 .foregroundColor(DamusColors.white) 37 } 38 } 39 40 var body: some View { 41 HStack(spacing: 0) { 42 ZStack(alignment: .leading) { 43 if let url = payments_url.flatMap({ URL(string: $0) }) { 44 RelayType(is_paid: true) 45 .zIndex(1) 46 47 Button(action: { 48 openURL(url) 49 }, label: { 50 if let admission = fees?.admission { 51 if !admission.isEmpty { 52 Amount(unit: admission[0].unit, amount: admission[0].amount) 53 } else { 54 Text("Paid Relay", comment: "Text indicating that this is a paid relay.") 55 .font(.system(size: 13, weight: .heavy)) 56 .foregroundColor(DamusColors.white) 57 } 58 } else if let subscription = fees?.subscription { 59 if !subscription.isEmpty { 60 Text("\(displayAmount(unit: subscription[0].unit, amount: subscription[0].amount)) / \(timeString(time: subscription[0].period))", comment: "Amount of money required to subscribe to the Nostr relay. In English, this would look something like '4,000 sats / 30 days', meaning it costs 4000 sats to subscribe to the Nostr relay for 30 days.") 61 .font(.system(size: 13, weight: .heavy)) 62 .foregroundColor(DamusColors.white) 63 } 64 } else if let publication = fees?.publication { 65 if !publication.isEmpty { 66 Text("\(displayAmount(unit: publication[0].unit, amount: publication[0].amount)) / event", comment: "Amount of money required to publish to the Nostr relay. In English, this would look something like '10 sats / event', meaning it costs 10 sats to publish one event.") 67 .font(.system(size: 13, weight: .heavy)) 68 .foregroundColor(DamusColors.white) 69 } 70 } else { 71 Text("Paid Relay", comment: "Text indicating that this is a paid relay.") 72 .font(.system(size: 13, weight: .heavy)) 73 .foregroundColor(DamusColors.white) 74 } 75 }) 76 .padding(EdgeInsets(top: 3, leading: 25, bottom: 3, trailing: 10)) 77 .background(DamusColors.bitcoin.opacity(0.7)) 78 .cornerRadius(15) 79 .overlay( 80 RoundedRectangle(cornerRadius: 20) 81 .stroke(DamusColors.warningBorder, lineWidth: 1) 82 ) 83 .padding(.leading, 1) 84 } 85 } 86 } 87 } 88 } 89 90 struct RelayPaidDetail_Previews: PreviewProvider { 91 static var previews: some View { 92 let admission = Admission(amount: 1000000, unit: "msats") 93 let sub = Subscription(amount: 5000000, unit: "msats", period: 2592000) 94 let pub = Publication(kinds: [1, 4], amount: 100, unit: "msats") 95 let fees = Fees(admission: [admission], subscription: [sub], publication: [pub]) 96 RelayPaidDetail(payments_url: "https://jb55.com", fees: fees) 97 } 98 }