Skip to content

Commit

Permalink
refactor: extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
reez authored Aug 25, 2024
1 parent 273110b commit 98951c2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 30 deletions.
4 changes: 4 additions & 0 deletions LDKNodeMonday.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -123,6 +124,7 @@
AE17E90C29A42D430058C9C9 /* LightningNodeService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LightningNodeService.swift; sourceTree = "<group>"; };
AE186B8D2A1540B700338463 /* StartView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StartView.swift; sourceTree = "<group>"; };
AE1D9C0A2B2A251500620748 /* ChannelDetails+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ChannelDetails+Extensions.swift"; sourceTree = "<group>"; };
AE33D3AD2C7BD0EE00AF562B /* URL+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URL+Extensions.swift"; sourceTree = "<group>"; };
AE3815352B6A9705006B2952 /* LightningNodesService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LightningNodesService.swift; sourceTree = "<group>"; };
AE3815382B6A978A006B2952 /* LightningNodeServiceError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LightningNodeServiceError.swift; sourceTree = "<group>"; };
AE38153A2B6A979E006B2952 /* LightningNodeInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LightningNodeInfo.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -534,6 +536,7 @@
AE0BD4AB2B598C3F00B9901E /* UInt32+Extensions.swift */,
AE6BB56E2A008CBA009E16E3 /* UInt64+Extensions.swift */,
AEF66F512BA4B11A00FA15F9 /* PaymentKind+Extensions.swift */,
AE33D3AD2C7BD0EE00AF562B /* URL+Extensions.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
34 changes: 5 additions & 29 deletions LDKNodeMonday/Extensions/String+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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) {
Expand Down
25 changes: 25 additions & 0 deletions LDKNodeMonday/Extensions/URL+Extensions.swift
Original file line number Diff line number Diff line change
@@ -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)
}
)
}
}
2 changes: 1 addition & 1 deletion LDKNodeMonday/View/Home/Send/AmountView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 98951c2

Please sign in to comment.