BlueWallet/ios/Widgets/WalletInformationAndMarketWidget/WalletInformationAndMarketWidget.swift

133 lines
6.5 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
static var lastSuccessfulEntries: [WalletInformationAndMarketWidgetEntry] = []
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-06-04 22:50:27 -04:00
let userPreferredCurrency = Currency.getUserPreferredCurrency()
let allwalletsBalance = WalletData(balance: UserDefaultsGroup.getAllWalletsBalance(), latestTransactionTime: UserDefaultsGroup.getAllWalletsLatestTransactionTime())
MarketAPI.fetchMarketData(currency: userPreferredCurrency) { (result, error) in
let entry: WalletInformationAndMarketWidgetEntry
if let result = result {
entry = WalletInformationAndMarketWidgetEntry(date: Date(), marketData: result, allWalletsBalance: allwalletsBalance)
WalletInformationAndMarketWidgetProvider.lastSuccessfulEntries.append(entry)
if WalletInformationAndMarketWidgetProvider.lastSuccessfulEntries.count > 5 {
WalletInformationAndMarketWidgetProvider.lastSuccessfulEntries.removeFirst()
}
} else {
if let lastEntry = WalletInformationAndMarketWidgetProvider.lastSuccessfulEntries.last {
entry = lastEntry
} else {
entry = WalletInformationAndMarketWidgetEntry.placeholder
}
}
entries.append(entry)
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
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
}