Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Aug 2, 2023
1 parent 9f8bc53 commit 6cfa316
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
5 changes: 4 additions & 1 deletion BDKSwiftExampleWallet/Extensions/Double+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
import Foundation

extension Double {

func formattedPrice(currencyCode: CurrencyCode) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currency
numberFormatter.currencyCode = currencyCode.rawValue

return numberFormatter.string(from: NSNumber(value: self)) ?? "\(self)"
}

func valueInUSD(price: Double) -> String {
let bitcoin = self / 100000000.0 // Convert satoshis to bitcoin
let bitcoin = self / 100_000_000.0
let usdValue = bitcoin * price
return usdValue.formattedPrice(currencyCode: .USD)
}

}
2 changes: 2 additions & 0 deletions BDKSwiftExampleWallet/Extensions/Int+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ extension UInt64 {
let date = Date(timeIntervalSince1970: TimeInterval(self))
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

return dateFormatter.string(from: date)
}
}
Expand All @@ -75,6 +76,7 @@ extension Int {
let formatter = RelativeDateTimeFormatter()
formatter.unitsStyle = .full
let relativeDate = formatter.localizedString(for: date, relativeTo: Date.now)

return relativeDate
}
}
12 changes: 4 additions & 8 deletions BDKSwiftExampleWallet/View/WalletView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class WalletViewModel: ObservableObject {
self.priceService = priceService
}

func fetchPrice() async {
func getPrice() async {
do {
let response = try await priceService.hourlyPrice()
if let latestPrice = response.prices.first?.usd {
Expand All @@ -50,18 +50,13 @@ class WalletViewModel: ObservableObject {
}

private func valueInUSD() {
let bitcoin = Double(balanceTotal) / 100000000.0 // Convert satoshis to bitcoin
let usdValue = bitcoin * price
let sats = usdValue.formattedPrice(currencyCode: .USD)

self.satsPrice = sats
self.satsPrice = Double(balanceTotal).valueInUSD(price: price)
}

func getBalance() {
do {
let balance = try BDKService.shared.getBalance()
self.balanceTotal = balance.total
self.valueInUSD()
} catch let error as WalletError {
print("getBalance - Wallet Error: \(error.localizedDescription)")
} catch {
Expand Down Expand Up @@ -90,6 +85,7 @@ class WalletViewModel: ObservableObject {
self.lastSyncTime = Date()
self.getBalance()
self.getTransactions()
self.valueInUSD()
}
} catch {
DispatchQueue.main.async {
Expand Down Expand Up @@ -277,7 +273,7 @@ struct WalletView: View {
}
.task {
await viewModel.sync()
await viewModel.fetchPrice()
await viewModel.getPrice()
}

}
Expand Down

0 comments on commit 6cfa316

Please sign in to comment.