BlueWallet/ios/Widgets/WalletInformationAndMarketWidget/WalletInformationAndMarketWidget.swift

123 lines
5.8 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 {
typealias Entry = WalletInformationAndMarketWidgetEntry
func placeholder(in context: Context) -> WalletInformationAndMarketWidgetEntry {
return 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)))
2020-11-02 08:11:28 -05:00
}
2021-06-06 03:58:39 -04:00
func getSnapshot(in context: Context, completion: @escaping (WalletInformationAndMarketWidgetEntry) -> ()) {
let entry: WalletInformationAndMarketWidgetEntry
2020-11-02 08:11:28 -05:00
if (context.isPreview) {
2021-06-06 03:58:39 -04:00
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)))
2020-11-02 08:11:28 -05:00
} else {
2021-06-06 03:58:39 -04:00
entry = WalletInformationAndMarketWidgetEntry(date: Date(), marketData: emptyMarketData)
2020-11-02 08:11:28 -05:00
}
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
2021-06-06 03:58:39 -04:00
var entries: [WalletInformationAndMarketWidgetEntry] = []
2021-03-07 19:00:48 -05:00
if (context.isPreview) {
2021-06-06 03:58:39 -04:00
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)))
2020-11-02 08:11:28 -05:00
entries.append(entry)
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
2021-03-07 19:00:48 -05:00
} else {
let userPreferredCurrency = WidgetAPI.getUserPreferredCurrency();
let allwalletsBalance = WalletData(balance: UserDefaultsGroup.getAllWalletsBalance(), latestTransactionTime: UserDefaultsGroup.getAllWalletsLatestTransactionTime())
let marketDataEntry = MarketData(nextBlock: "...", sats: "...", price: "...", rate: 0)
WidgetAPI.fetchMarketData(currency: userPreferredCurrency, completion: { (result, error) in
2021-06-06 03:58:39 -04:00
let entry: WalletInformationAndMarketWidgetEntry
2021-03-07 19:00:48 -05:00
if let result = result {
2021-06-06 03:58:39 -04:00
entry = WalletInformationAndMarketWidgetEntry(date: Date(), marketData: result, allWalletsBalance: allwalletsBalance)
2021-03-07 19:00:48 -05:00
} else {
2021-06-06 03:58:39 -04:00
entry = WalletInformationAndMarketWidgetEntry(date: Date(), marketData: marketDataEntry, allWalletsBalance: allwalletsBalance)
2021-03-07 19:00:48 -05:00
}
entries.append(entry)
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
})
}
2020-11-02 08:11:28 -05:00
}
}
2021-06-06 03:58:39 -04:00
struct WalletInformationAndMarketWidgetEntry: TimelineEntry {
2020-11-02 08:11:28 -05:00
let date: Date
let marketData: MarketData
var allWalletsBalance: WalletData = WalletData(balance: 0)
}
struct WalletInformationAndMarketWidgetEntryView : View {
2020-11-03 00:31:21 -05:00
@Environment(\.widgetFamily) var family
2021-06-06 03:58:39 -04:00
let entry: WalletInformationAndMarketWidgetEntry
2020-11-03 23:16:20 -05:00
2020-11-02 08:11:28 -05:00
var WalletBalance: some View {
2020-11-03 23:16:20 -05:00
WalletInformationView(allWalletsBalance: entry.allWalletsBalance, marketData: entry.marketData).background(Color.widgetBackground)
2020-11-02 08:11:28 -05:00
}
var MarketStack: some View {
2020-11-03 23:16:20 -05:00
MarketView(marketData: entry.marketData)
2020-11-02 08:11:28 -05:00
}
2020-11-03 23:25:21 -05:00
var SendReceiveButtonsView: some View {
SendReceiveButtons().padding(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/, /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/)
2020-11-03 00:31:21 -05:00
}
2020-11-02 08:11:28 -05:00
var body: some View {
2020-11-03 00:31:21 -05:00
if family == .systemLarge {
2020-11-03 23:16:20 -05:00
HStack(alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/, spacing: /*@START_MENU_TOKEN@*/nil/*@END_MENU_TOKEN@*/, content: {
VStack(alignment: .leading, spacing: nil, content: {
HStack(content: {
WalletBalance.padding()
}).background(Color.widgetBackground)
HStack(content: {
MarketStack }).padding()
2020-11-03 23:25:21 -05:00
SendReceiveButtonsView }).background(Color(.lightGray).opacity(0.77))
2020-11-03 23:16:20 -05:00
})
2020-11-03 00:31:21 -05:00
} else {
2020-11-02 08:11:28 -05:00
HStack(content: {
2020-11-03 23:16:20 -05:00
WalletBalance.padding()
HStack(content: {
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 {
let kind: String = "WalletInformationAndMarketWidget"
var body: some WidgetConfiguration {
2021-06-06 03:58:39 -04:00
StaticConfiguration(kind: kind, provider: WalletInformationAndMarketWidgetProvider()) { entry in
2020-11-02 08:11:28 -05:00
WalletInformationAndMarketWidgetEntryView(entry: entry)
}
.configurationDisplayName("Wallet and Market")
2020-11-03 00:31:21 -05:00
.description("View your total wallet balance and network prices.").supportedFamilies([.systemMedium, .systemLarge])
2020-11-02 08:11:28 -05:00
}
}
struct WalletInformationAndMarketWidget_Previews: PreviewProvider {
static var previews: some View {
2021-06-06 03:58:39 -04:00
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))))
2020-11-02 08:11:28 -05:00
.previewContext(WidgetPreviewContext(family: .systemMedium))
2021-06-06 03:58:39 -04:00
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))))
2020-11-03 00:31:21 -05:00
.previewContext(WidgetPreviewContext(family: .systemLarge))
2020-11-02 08:11:28 -05:00
}
}