From 9c563b9c34f59f9bed7b83394e389aab17851771 Mon Sep 17 00:00:00 2001 From: Matthew <6657488+reez@users.noreply.github.com> Date: Tue, 8 Aug 2023 11:52:07 -0500 Subject: [PATCH] ui: price minute --- .../Service/Price Service/PriceService.swift | 13 ++++++++++++- .../View Model/WalletViewModel.swift | 18 ++++++------------ BDKSwiftExampleWallet/View/WalletView.swift | 2 +- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/BDKSwiftExampleWallet/Service/Price Service/PriceService.swift b/BDKSwiftExampleWallet/Service/Price Service/PriceService.swift index aa00010..f6f9f8d 100644 --- a/BDKSwiftExampleWallet/Service/Price Service/PriceService.swift +++ b/BDKSwiftExampleWallet/Service/Price Service/PriceService.swift @@ -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, @@ -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 { diff --git a/BDKSwiftExampleWallet/View Model/WalletViewModel.swift b/BDKSwiftExampleWallet/View Model/WalletViewModel.swift index bcc15f5..0ea8a36 100644 --- a/BDKSwiftExampleWallet/View Model/WalletViewModel.swift +++ b/BDKSwiftExampleWallet/View Model/WalletViewModel.swift @@ -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)") } } diff --git a/BDKSwiftExampleWallet/View/WalletView.swift b/BDKSwiftExampleWallet/View/WalletView.swift index e97be5b..466a5dc 100644 --- a/BDKSwiftExampleWallet/View/WalletView.swift +++ b/BDKSwiftExampleWallet/View/WalletView.swift @@ -102,7 +102,7 @@ struct WalletView: View { } .task { await viewModel.sync() - await viewModel.getPrice() + await viewModel.getPrices() } }