Skip to content

Commit

Permalink
ui: price minute
Browse files Browse the repository at this point in the history
  • Loading branch information
reez authored Aug 8, 2023
1 parent bd80ff0 commit 9c563b9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
13 changes: 12 additions & 1 deletion BDKSwiftExampleWallet/Service/Price Service/PriceService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

struct PriceService {
func hourlyPrice() async throws -> PriceResponse {
func historicalPrice() 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,
Expand All @@ -18,6 +18,17 @@ struct PriceService {
let jsonObject = try jsonDecoder.decode(PriceResponse.self, from: data)
return jsonObject
}

func prices() async throws -> Price {
guard let url = URL(string: "https://mempool.space/api/v1/prices") 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(Price.self, from: data)
return jsonObject
}
}

enum PriceServiceError: Error {
Expand Down
18 changes: 6 additions & 12 deletions BDKSwiftExampleWallet/View Model/WalletViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,15 @@ class WalletViewModel: ObservableObject {
self.priceService = priceService
}

func getPrice() async {
func getPrices() async {
do {
let response = try await priceService.hourlyPrice()
if let latestPrice = response.prices.first?.usd {
DispatchQueue.main.async {
self.price = latestPrice
}
}
if let latestTime = response.prices.first?.time {
DispatchQueue.main.async {
self.time = latestTime
}
let price = try await priceService.prices()
DispatchQueue.main.async {
self.price = price.usd
self.time = price.time
}
} catch {
print("priceMem error: \(error.localizedDescription)")
print("getPrices error: \(error.localizedDescription)")
}
}

Expand Down
2 changes: 1 addition & 1 deletion BDKSwiftExampleWallet/View/WalletView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ struct WalletView: View {
}
.task {
await viewModel.sync()
await viewModel.getPrice()
await viewModel.getPrices()
}
}

Expand Down

0 comments on commit 9c563b9

Please sign in to comment.