112 lines
2.8 KiB
Swift
112 lines
2.8 KiB
Swift
//
|
|
// ContentView.swift
|
|
// SatsPrice
|
|
//
|
|
// Created by Terry Yiu on 2/19/24.
|
|
//
|
|
|
|
import SwiftUI
|
|
import BigDecimal
|
|
import Combine
|
|
|
|
struct ContentView: View {
|
|
@ObservedObject private var satsViewModel = SatsViewModel()
|
|
|
|
@State private var priceSource: PriceSource = .coinbase
|
|
|
|
private let dateFormatter = DateFormatter()
|
|
|
|
private let priceFetcherDelegator = PriceFetcherDelegator()
|
|
|
|
init() {
|
|
dateFormatter.dateStyle = .short
|
|
dateFormatter.timeStyle = .short
|
|
}
|
|
|
|
@MainActor
|
|
func updatePrice() async {
|
|
do {
|
|
guard let price = try await priceFetcherDelegator.btcToUsd() else {
|
|
satsViewModel.btcToUsdString = ""
|
|
return
|
|
}
|
|
|
|
satsViewModel.btcToUsdString = "\(price)"
|
|
} catch {
|
|
satsViewModel.btcToUsdString = ""
|
|
}
|
|
satsViewModel.lastUpdated = Date.now
|
|
}
|
|
|
|
var body: some View {
|
|
Form {
|
|
Section {
|
|
Picker("Price Source", selection: $priceSource) {
|
|
ForEach(PriceSource.allCases, id: \.self) {
|
|
Text($0.description)
|
|
}
|
|
}
|
|
.onChange(of: priceSource) { newPriceSource in
|
|
priceFetcherDelegator.priceSource = newPriceSource
|
|
Task {
|
|
await updatePrice()
|
|
}
|
|
}
|
|
|
|
HStack {
|
|
TextField("", text: $satsViewModel.btcToUsdString)
|
|
.disabled(true)
|
|
Button(action: {
|
|
Task {
|
|
await updatePrice()
|
|
}
|
|
}) {
|
|
Image(systemName: "arrow.clockwise")
|
|
}
|
|
}
|
|
} header: {
|
|
Text("1 BTC to USD")
|
|
} footer: {
|
|
Text("Last updated: \(dateFormatter.string(from: satsViewModel.lastUpdated))")
|
|
}
|
|
|
|
Section {
|
|
TextField("", text: $satsViewModel.satsString)
|
|
#if os(iOS)
|
|
.keyboardType(.numberPad)
|
|
#endif
|
|
} header: {
|
|
Text("Sats")
|
|
}
|
|
|
|
Section {
|
|
TextField("", text: $satsViewModel.btcString)
|
|
#if os(iOS)
|
|
.keyboardType(.decimalPad)
|
|
#endif
|
|
} header: {
|
|
Text("BTC")
|
|
}
|
|
|
|
Section {
|
|
TextField("", text: $satsViewModel.usdString)
|
|
#if os(iOS)
|
|
.keyboardType(.decimalPad)
|
|
#endif
|
|
} header: {
|
|
Text("USD")
|
|
}
|
|
}
|
|
.task {
|
|
await updatePrice()
|
|
}
|
|
#if os(macOS)
|
|
.formStyle(.grouped)
|
|
#endif
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
ContentView()
|
|
}
|