From 98951c2ec170bc05521bb494f5eb706b54e2046c Mon Sep 17 00:00:00 2001 From: Matthew Ramsden <6657488+reez@users.noreply.github.com> Date: Sun, 25 Aug 2024 17:50:48 -0500 Subject: [PATCH] refactor: extensions --- LDKNodeMonday.xcodeproj/project.pbxproj | 4 +++ .../Extensions/String+Extensions.swift | 34 +++---------------- LDKNodeMonday/Extensions/URL+Extensions.swift | 25 ++++++++++++++ LDKNodeMonday/View/Home/Send/AmountView.swift | 2 +- 4 files changed, 35 insertions(+), 30 deletions(-) create mode 100644 LDKNodeMonday/Extensions/URL+Extensions.swift diff --git a/LDKNodeMonday.xcodeproj/project.pbxproj b/LDKNodeMonday.xcodeproj/project.pbxproj index 64e71c3..a8cf748 100644 --- a/LDKNodeMonday.xcodeproj/project.pbxproj +++ b/LDKNodeMonday.xcodeproj/project.pbxproj @@ -34,6 +34,7 @@ AE186B8E2A1540B700338463 /* StartView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE186B8D2A1540B700338463 /* StartView.swift */; }; AE1D9BEC2B2A1FFD00620748 /* BitcoinUI in Frameworks */ = {isa = PBXBuildFile; productRef = AE1D9BEB2B2A1FFD00620748 /* BitcoinUI */; }; AE1D9C0B2B2A251500620748 /* ChannelDetails+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE1D9C0A2B2A251500620748 /* ChannelDetails+Extensions.swift */; }; + AE33D3AE2C7BD0EE00AF562B /* URL+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE33D3AD2C7BD0EE00AF562B /* URL+Extensions.swift */; }; AE3815362B6A9705006B2952 /* LightningNodesService.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE3815352B6A9705006B2952 /* LightningNodesService.swift */; }; AE3815392B6A978A006B2952 /* LightningNodeServiceError.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE3815382B6A978A006B2952 /* LightningNodeServiceError.swift */; }; AE38153B2B6A979E006B2952 /* LightningNodeInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE38153A2B6A979E006B2952 /* LightningNodeInfo.swift */; }; @@ -123,6 +124,7 @@ AE17E90C29A42D430058C9C9 /* LightningNodeService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LightningNodeService.swift; sourceTree = ""; }; AE186B8D2A1540B700338463 /* StartView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StartView.swift; sourceTree = ""; }; AE1D9C0A2B2A251500620748 /* ChannelDetails+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ChannelDetails+Extensions.swift"; sourceTree = ""; }; + AE33D3AD2C7BD0EE00AF562B /* URL+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URL+Extensions.swift"; sourceTree = ""; }; AE3815352B6A9705006B2952 /* LightningNodesService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LightningNodesService.swift; sourceTree = ""; }; AE3815382B6A978A006B2952 /* LightningNodeServiceError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LightningNodeServiceError.swift; sourceTree = ""; }; AE38153A2B6A979E006B2952 /* LightningNodeInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LightningNodeInfo.swift; sourceTree = ""; }; @@ -534,6 +536,7 @@ AE0BD4AB2B598C3F00B9901E /* UInt32+Extensions.swift */, AE6BB56E2A008CBA009E16E3 /* UInt64+Extensions.swift */, AEF66F512BA4B11A00FA15F9 /* PaymentKind+Extensions.swift */, + AE33D3AD2C7BD0EE00AF562B /* URL+Extensions.swift */, ); path = Extensions; sourceTree = ""; @@ -644,6 +647,7 @@ AE028A2E2B9638EC00B336E7 /* FileManager+Extensions.swift in Sources */, AE028A282B96325700B336E7 /* AmountInvoiceViewModel.swift in Sources */, AE49E8662A2537D5002623E8 /* PeerViewModel.swift in Sources */, + AE33D3AE2C7BD0EE00AF562B /* URL+Extensions.swift in Sources */, AEBAA4942A01C6BD0042EA82 /* DisconnectView.swift in Sources */, AE49E8682A2537FB002623E8 /* DisconnectViewModel.swift in Sources */, AE5BFE862C0B9D7B003B467C /* Bolt12InvoiceViewModel.swift in Sources */, diff --git a/LDKNodeMonday/Extensions/String+Extensions.swift b/LDKNodeMonday/Extensions/String+Extensions.swift index d8161b9..41bc80c 100644 --- a/LDKNodeMonday/Extensions/String+Extensions.swift +++ b/LDKNodeMonday/Extensions/String+Extensions.swift @@ -56,7 +56,7 @@ extension String { return nil } - func formattedAmount() -> String { + func formattedAmount(defaultValue: String = "") -> String { let formatter = NumberFormatter() formatter.numberStyle = .decimal formatter.groupingSize = 3 @@ -66,21 +66,7 @@ extension String { { return formattedNumber } else { - return "" - } - } - - func formattedAmountZero() -> String { - let formatter = NumberFormatter() - formatter.numberStyle = .decimal - formatter.groupingSize = 3 - - if let number = Int(self), - let formattedNumber = formatter.string(from: NSNumber(value: number)) - { - return formattedNumber - } else { - return "0" + return defaultValue } } @@ -146,19 +132,9 @@ extension String { } } - private func queryParameters() -> [String: String] { - guard let range = self.range(of: "?") else { return [:] } - let queryString = self[range.upperBound...] - - var params: [String: String] = [:] - queryString.split(separator: "&").forEach { - let pair = $0.split(separator: "=") - if pair.count == 2 { - params[String(pair[0])] = String(pair[1]) - } - } - - return params + func queryParameters() -> [String: String] { + guard let url = URL(string: self) else { return [:] } + return url.queryParameters() } func processBIP21(_ input: String, spendableBalance: UInt64) -> (String, String, Payment) { diff --git a/LDKNodeMonday/Extensions/URL+Extensions.swift b/LDKNodeMonday/Extensions/URL+Extensions.swift new file mode 100644 index 0000000..4270d2a --- /dev/null +++ b/LDKNodeMonday/Extensions/URL+Extensions.swift @@ -0,0 +1,25 @@ +// +// URL+Extensions.swift +// LDKNodeMonday +// +// Created by Matthew Ramsden on 8/25/24. +// + +import Foundation + +extension URL { + func queryParameters() -> [String: String] { + guard let components = URLComponents(url: self, resolvingAgainstBaseURL: false), + let queryItems = components.queryItems + else { + return [:] + } + + return Dictionary( + uniqueKeysWithValues: queryItems.compactMap { item in + guard let value = item.value else { return nil } + return (item.name, value) + } + ) + } +} diff --git a/LDKNodeMonday/View/Home/Send/AmountView.swift b/LDKNodeMonday/View/Home/Send/AmountView.swift index d132a56..73c4523 100644 --- a/LDKNodeMonday/View/Home/Send/AmountView.swift +++ b/LDKNodeMonday/View/Home/Send/AmountView.swift @@ -103,7 +103,7 @@ struct AmountView: View { .font(.caption) .foregroundColor(.secondary) } else { - Text("\(numpadAmount.formattedAmountZero()) sats") + Text("\(numpadAmount.formattedAmount(defaultValue: "0")) sats") .textStyle(BitcoinTitle1()) Text(address) .lineLimit(1)