BlueWallet/ios/Widgets/WalletInformationAndMarketWidget/WalletInformationAndMarketWidget.swift

170 lines
7.7 KiB
Swift
Raw Normal View History

2020-11-02 08:11:28 -05:00
//
// WalletInformationAndMarketWidget.swift
// WalletInformationAndMarketWidget
//
// Created by Marcos Rodriguez on 10/29/20.
// Copyright © 2020 BlueWallet. All rights reserved.
//
import WidgetKit
import SwiftUI
2021-06-06 03:58:39 -04:00
struct WalletInformationAndMarketWidgetProvider: TimelineProvider {
2024-06-04 22:50:27 -04:00
typealias Entry = WalletInformationAndMarketWidgetEntry
2024-12-12 23:21:17 -04:00
actor LastSuccessfulEntryStore {
private var lastSuccessfulEntry: WalletInformationAndMarketWidgetEntry?
func getLastSuccessfulEntry() -> WalletInformationAndMarketWidgetEntry? {
return lastSuccessfulEntry
}
func setLastSuccessfulEntry(_ entry: WalletInformationAndMarketWidgetEntry) {
lastSuccessfulEntry = entry
}
}
let entryStore = LastSuccessfulEntryStore()
2024-06-04 22:50:27 -04:00
func placeholder(in context: Context) -> WalletInformationAndMarketWidgetEntry {
return WalletInformationAndMarketWidgetEntry.placeholder
2020-11-02 08:11:28 -05:00
}
2024-06-04 22:50:27 -04:00
func getSnapshot(in context: Context, completion: @escaping (WalletInformationAndMarketWidgetEntry) -> ()) {
2021-06-06 03:58:39 -04:00
let entry: WalletInformationAndMarketWidgetEntry
2024-06-04 22:50:27 -04:00
if (context.isPreview) {
entry = WalletInformationAndMarketWidgetEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: 10000), allWalletsBalance: WalletData(balance: 1000000, latestTransactionTime: LatestTransaction(isUnconfirmed: false, epochValue: 1568804029000)))
} else {
entry = WalletInformationAndMarketWidgetEntry(date: Date(), marketData: emptyMarketData)
}
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [WalletInformationAndMarketWidgetEntry] = []
if (context.isPreview) {
let entry = WalletInformationAndMarketWidgetEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: 10000), allWalletsBalance: WalletData(balance: 1000000, latestTransactionTime: LatestTransaction(isUnconfirmed: false, epochValue: 1568804029000)))
entries.append(entry)
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
2021-03-07 19:00:48 -05:00
} else {
2024-12-12 23:38:41 -04:00
let userPreferredCurrency = Currency.getUserPreferredCurrency()
let allWalletsBalance = WalletData(balance: UserDefaultsGroup.getAllWalletsBalance(), latestTransactionTime: UserDefaultsGroup.getAllWalletsLatestTransactionTime())
2024-06-04 22:50:27 -04:00
2024-12-12 23:38:41 -04:00
fetchMarketDataWithRetry(currency: userPreferredCurrency, retries: 3) { marketData in
let entry = WalletInformationAndMarketWidgetEntry(date: Date(), marketData: marketData, allWalletsBalance: allWalletsBalance)
Task {
await entryStore.setLastSuccessfulEntry(entry)
entries.append(entry)
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
}
}
}
private func fetchMarketDataWithRetry(currency: String, retries: Int, completion: @escaping (MarketData) -> ()) {
var attempt = 0
2024-06-04 22:50:27 -04:00
2024-12-12 23:38:41 -04:00
func attemptFetch() {
attempt += 1
print("Attempt \(attempt) to fetch market data.")
MarketAPI.fetchMarketData(currency: currency) { result in
switch result {
case .success(let marketData):
print("Successfully fetched market data on attempt \(attempt).")
completion(marketData)
case .failure(let error):
print("Error fetching market data: \(error.localizedDescription). Retry \(attempt)/\(retries)")
if attempt < retries {
DispatchQueue.global().asyncAfter(deadline: .now() + 2) {
attemptFetch()
}
} else {
print("Max retries reached.")
Task {
2024-12-12 23:21:17 -04:00
if let lastEntry = await entryStore.getLastSuccessfulEntry() {
2024-12-12 23:38:41 -04:00
completion(lastEntry.marketData)
} else {
2024-12-12 23:38:41 -04:00
completion(WalletInformationAndMarketWidgetEntry.placeholder.marketData)
}
}
2024-06-04 22:50:27 -04:00
}
}
}
2021-03-07 19:00:48 -05:00
}
2024-12-12 23:38:41 -04:00
attemptFetch()
2021-03-07 19:00:48 -05:00
}
2020-11-02 08:11:28 -05:00
}
2021-06-06 03:58:39 -04:00
struct WalletInformationAndMarketWidgetEntry: TimelineEntry {
2024-06-04 22:50:27 -04:00
let date: Date
let marketData: MarketData
var allWalletsBalance: WalletData = WalletData(balance: 0)
static var placeholder = WalletInformationAndMarketWidgetEntry(date: Date(), marketData: MarketData(nextBlock: "...", sats: "...", price: "...", rate: 0), allWalletsBalance: WalletData(balance: 0, latestTransactionTime: LatestTransaction(isUnconfirmed: false, epochValue: 1568804029000)))
2020-11-02 08:11:28 -05:00
}
2024-06-04 22:50:27 -04:00
struct WalletInformationAndMarketWidgetEntryView: View {
@Environment(\.widgetFamily) var family
let entry: WalletInformationAndMarketWidgetEntry
var WalletBalance: some View {
WalletInformationView(allWalletsBalance: entry.allWalletsBalance, marketData: entry.marketData).background(Color.widgetBackground)
}
var MarketStack: some View {
MarketView(marketData: entry.marketData)
}
var SendReceiveButtonsView: some View {
SendReceiveButtons().padding(.all, 10)
}
var body: some View {
if family == .systemLarge {
HStack(alignment: .center, spacing: nil, content: {
VStack(alignment: .leading, spacing: nil, content: {
HStack(content: {
WalletBalance.padding()
}).background(Color.widgetBackground)
HStack(content: {
MarketStack
}).padding()
SendReceiveButtonsView
}).background(Color(.lightGray).opacity(0.77))
})
} else {
HStack(content: {
WalletBalance.padding()
2020-11-03 23:16:20 -05:00
HStack(content: {
2024-06-04 22:50:27 -04:00
MarketStack.padding()
}).background(Color(.lightGray).opacity(0.77))
}).background(Color.widgetBackground)
}
2020-11-03 00:31:21 -05:00
}
2020-11-02 08:11:28 -05:00
}
struct WalletInformationAndMarketWidget: Widget {
2024-06-04 22:50:27 -04:00
let kind: String = "WalletInformationAndMarketWidget"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: WalletInformationAndMarketWidgetProvider()) { entry in
WalletInformationAndMarketWidgetEntryView(entry: entry)
}
.configurationDisplayName("Wallet and Market")
.description("View your total wallet balance and network prices.").supportedFamilies([.systemMedium, .systemLarge])
.contentMarginsDisabledIfAvailable()
2020-11-02 08:11:28 -05:00
}
}
struct WalletInformationAndMarketWidget_Previews: PreviewProvider {
2024-06-04 22:50:27 -04:00
static var previews: some View {
WalletInformationAndMarketWidgetEntryView(entry: WalletInformationAndMarketWidgetEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: 0), allWalletsBalance: WalletData(balance: 10000, latestTransactionTime: LatestTransaction(isUnconfirmed: false, epochValue: 1568804029000))))
.previewContext(WidgetPreviewContext(family: .systemMedium))
WalletInformationAndMarketWidgetEntryView(entry: WalletInformationAndMarketWidgetEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: 0), allWalletsBalance: WalletData(balance: 10000, latestTransactionTime: LatestTransaction(isUnconfirmed: false, epochValue: 1568804029000))))
.previewContext(WidgetPreviewContext(family: .systemLarge))
}
2020-11-02 08:11:28 -05:00
}