Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: price #42

Merged
merged 6 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions BDKSwiftExampleWallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
AE7953902A2D5B4400CCB277 /* BDKSwiftExampleWalletError.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE79538F2A2D5B4400CCB277 /* BDKSwiftExampleWalletError.swift */; };
AE7953922A2D5E3100CCB277 /* Date+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE7953912A2D5E3100CCB277 /* Date+Extensions.swift */; };
AE7E68962A59A37300368D82 /* BDKSwiftExampleWalletInt+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE7E68952A59A37300368D82 /* BDKSwiftExampleWalletInt+Extensions.swift */; };
AE7F67052A7446B600CED561 /* PriceService.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE7F67042A7446B600CED561 /* PriceService.swift */; };
AE7F67072A744CE200CED561 /* Double+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE7F67062A744CE200CED561 /* Double+Extensions.swift */; };
AE7F67092A7451AA00CED561 /* PriceResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE7F67082A7451AA00CED561 /* PriceResponse.swift */; };
AE7F670C2A7451D700CED561 /* CurrencyCode.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE7F670B2A7451D700CED561 /* CurrencyCode.swift */; };
AE96F6622A424C400055623C /* BDKSwiftExampleWalletReceiveTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE96F6612A424C400055623C /* BDKSwiftExampleWalletReceiveTests.swift */; };
AE96F6672A4259260055623C /* QRCodeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE96F6662A4259260055623C /* QRCodeView.swift */; };
AEB130C92A44E4850087785B /* TransactionDetailsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEB130C82A44E4850087785B /* TransactionDetailsView.swift */; };
Expand Down Expand Up @@ -57,6 +61,10 @@
AE79538F2A2D5B4400CCB277 /* BDKSwiftExampleWalletError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BDKSwiftExampleWalletError.swift; sourceTree = "<group>"; };
AE7953912A2D5E3100CCB277 /* Date+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+Extensions.swift"; sourceTree = "<group>"; };
AE7E68952A59A37300368D82 /* BDKSwiftExampleWalletInt+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BDKSwiftExampleWalletInt+Extensions.swift"; sourceTree = "<group>"; };
AE7F67042A7446B600CED561 /* PriceService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PriceService.swift; sourceTree = "<group>"; };
AE7F67062A744CE200CED561 /* Double+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Double+Extensions.swift"; sourceTree = "<group>"; };
AE7F67082A7451AA00CED561 /* PriceResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PriceResponse.swift; sourceTree = "<group>"; };
AE7F670B2A7451D700CED561 /* CurrencyCode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencyCode.swift; sourceTree = "<group>"; };
AE96F6612A424C400055623C /* BDKSwiftExampleWalletReceiveTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BDKSwiftExampleWalletReceiveTests.swift; sourceTree = "<group>"; };
AE96F6662A4259260055623C /* QRCodeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QRCodeView.swift; sourceTree = "<group>"; };
AEB130C82A44E4850087785B /* TransactionDetailsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionDetailsView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -91,6 +99,7 @@
children = (
AE7953912A2D5E3100CCB277 /* Date+Extensions.swift */,
A73F7A352A3B778E00B87FC6 /* Int+Extensions.swift */,
AE7F67062A744CE200CED561 /* Double+Extensions.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand All @@ -101,6 +110,7 @@
AED4CC0B2A1D3A9400CE1831 /* OnboardingView.swift */,
AE3902A32A3B4CD900BEC318 /* TabHomeView.swift */,
AED4CC0F2A1D522100CE1831 /* WalletView.swift */,
AE7F67042A7446B600CED561 /* PriceService.swift */,
AEB130C82A44E4850087785B /* TransactionDetailsView.swift */,
AE1C34232A424456008F807A /* ReceiveView.swift */,
AE96F6662A4259260055623C /* QRCodeView.swift */,
Expand Down Expand Up @@ -158,6 +168,7 @@
children = (
AE1C34222A424440008F807A /* App */,
AE1C34212A424434008F807A /* BDK Service */,
AE7F670A2A7451B600CED561 /* Model */,
AE1C341F2A424415008F807A /* View */,
AE1C341E2A42440A008F807A /* Extensions */,
AE1C34202A42441F008F807A /* Utilities */,
Expand Down Expand Up @@ -186,6 +197,15 @@
path = BDKSwiftExampleWalletTests;
sourceTree = "<group>";
};
AE7F670A2A7451B600CED561 /* Model */ = {
isa = PBXGroup;
children = (
AE7F67082A7451AA00CED561 /* PriceResponse.swift */,
AE7F670B2A7451D700CED561 /* CurrencyCode.swift */,
);
path = Model;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -296,13 +316,17 @@
buildActionMask = 2147483647;
files = (
AE96F6672A4259260055623C /* QRCodeView.swift in Sources */,
AE7F670C2A7451D700CED561 /* CurrencyCode.swift in Sources */,
AE7F67052A7446B600CED561 /* PriceService.swift in Sources */,
AE7953902A2D5B4400CCB277 /* BDKSwiftExampleWalletError.swift in Sources */,
AE79538E2A2D59F000CCB277 /* Constants.swift in Sources */,
AE1C34242A424456008F807A /* ReceiveView.swift in Sources */,
AEB130C92A44E4850087785B /* TransactionDetailsView.swift in Sources */,
AE7953922A2D5E3100CCB277 /* Date+Extensions.swift in Sources */,
AED4CC0A2A1D297600CE1831 /* BDKService.swift in Sources */,
AED4CC102A1D522100CE1831 /* WalletView.swift in Sources */,
AE7F67092A7451AA00CED561 /* PriceResponse.swift in Sources */,
AE7F67072A744CE200CED561 /* Double+Extensions.swift in Sources */,
A73F7A362A3B778E00B87FC6 /* Int+Extensions.swift in Sources */,
AED4CC0C2A1D3A9400CE1831 /* OnboardingView.swift in Sources */,
AE1C34262A4248A5008F807A /* SendView.swift in Sources */,
Expand Down
6 changes: 4 additions & 2 deletions BDKSwiftExampleWallet/Extensions/Date+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ extension Date {
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .short
formatter.timeZone = TimeZone.autoupdatingCurrent // Use the device's time zone
formatter.timeZone = TimeZone.autoupdatingCurrent

return formatter.string(from: self)
let formattedTime = formatter.string(from: self)

return formattedTime
}
}
26 changes: 26 additions & 0 deletions BDKSwiftExampleWallet/Extensions/Double+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Double+Extensions.swift
// BDKSwiftExampleWallet
//
// Created by Matthew Ramsden on 7/28/23.
//

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 / 100_000_000.0
let usdValue = bitcoin * price
return usdValue.formattedPrice(currencyCode: .USD)
}

}
56 changes: 34 additions & 22 deletions BDKSwiftExampleWallet/Extensions/Int+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,32 @@

import Foundation

extension UInt32 {
private static var numberFormatter: NumberFormatter = {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal

return numberFormatter
}()

var delimiter: String {
return UInt32.numberFormatter.string(from: NSNumber(value: self)) ?? ""
}
}

extension UInt64 {
private static var numberFormatter: NumberFormatter = {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal

return numberFormatter
}()

var delimiter: String {
return UInt64.numberFormatter.string(from: NSNumber(value: self)) ?? ""
}
}

extension UInt64 {
func formattedSatoshis() -> String {
if self == 0 {
Expand Down Expand Up @@ -39,32 +65,18 @@ extension UInt64 {
let date = Date(timeIntervalSince1970: TimeInterval(self))
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return dateFormatter.string(from: date)
}
}

extension UInt64 {
private static var numberFormatter: NumberFormatter = {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal

return numberFormatter
}()

var delimiter: String {
return UInt64.numberFormatter.string(from: NSNumber(value: self)) ?? ""
return dateFormatter.string(from: date)
}
}

extension UInt32 {
private static var numberFormatter: NumberFormatter = {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
extension Int {
func newDateAgo() -> String {
let date = Date(timeIntervalSince1970: TimeInterval(self))
let formatter = RelativeDateTimeFormatter()
formatter.unitsStyle = .full
let relativeDate = formatter.localizedString(for: date, relativeTo: Date.now)

return numberFormatter
}()

var delimiter: String {
return UInt32.numberFormatter.string(from: NSNumber(value: self)) ?? ""
return relativeDate
}
}
18 changes: 18 additions & 0 deletions BDKSwiftExampleWallet/Model/CurrencyCode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// CurrencyCode.swift
// BDKSwiftExampleWallet
//
// Created by Matthew Ramsden on 7/28/23.
//

import Foundation

enum CurrencyCode: String {
case USD
case EUR
case GBP
case CAD
case CHF
case AUD
case JPY
}
53 changes: 53 additions & 0 deletions BDKSwiftExampleWallet/Model/PriceResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// PriceResponse.swift
// BDKSwiftExampleWallet
//
// Created by Matthew Ramsden on 7/28/23.
//

import Foundation

struct PriceResponse: Codable {
let prices: [Price]
let exchangeRates: ExchangeRates
}

struct Price: Codable {
let time: Int
let usd: Double
let eur: Double
let gbp: Double
let cad: Double
let chf: Double
let aud: Double
let jpy: Double

enum CodingKeys: String, CodingKey {
case time
case usd = "USD"
case eur = "EUR"
case gbp = "GBP"
case cad = "CAD"
case chf = "CHF"
case aud = "AUD"
case jpy = "JPY"
}
}

struct ExchangeRates : Codable {
let uSDEUR : Double?
let uSDGBP : Double?
let uSDCAD : Double?
let uSDCHF : Double?
let uSDAUD : Double?
let uSDJPY : Double?

enum CodingKeys: String, CodingKey {
case uSDEUR = "USDEUR"
case uSDGBP = "USDGBP"
case uSDCAD = "USDCAD"
case uSDCHF = "USDCHF"
case uSDAUD = "USDAUD"
case uSDJPY = "USDJPY"
}
}
27 changes: 27 additions & 0 deletions BDKSwiftExampleWallet/View/PriceService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// PriceService.swift
// BDKSwiftExampleWallet
//
// Created by Matthew Ramsden on 7/28/23.
//

import Foundation

struct PriceService {
func hourlyPrice() async throws -> PriceResponse {
guard let url = URL(string: "https://mempool.space/api/v1/historical-price") else { throw PriceServiceError.invalidURL }
let (data, response) = try await URLSession.shared.data(from: url)
guard let httpResponse = response as? HTTPURLResponse,
200...299 ~= httpResponse.statusCode
else { throw PriceServiceError.invalidServerResponse }
let jsonDecoder = JSONDecoder()
let jsonObject = try jsonDecoder.decode(PriceResponse.self, from: data)
return jsonObject
}
}

enum PriceServiceError: Error {
case invalidURL
case invalidServerResponse
case serialization
}
2 changes: 1 addition & 1 deletion BDKSwiftExampleWallet/View/TabHomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct TabHomeView: View {

TabView {

WalletView(viewModel: .init())
WalletView(viewModel: .init(priceService: .init()))
.tabItem {
Label(
"Wallet",
Expand Down
Loading