BlueWallet/ios/WalletInformationWidget/WalletInformationWidget.swift

95 lines
4.1 KiB
Swift
Raw Normal View History

2020-11-02 14:11:28 +01:00
//
// WalletInformationWidget.swift
// WalletInformationWidget
//
// Created by Marcos Rodriguez on 10/29/20.
// Copyright © 2020 BlueWallet. All rights reserved.
//
import WidgetKit
import SwiftUI
2021-06-06 09:58:39 +02:00
struct WalletInformationWidgetProvider: TimelineProvider {
typealias Entry = WalletInformationWidgetEntry
func placeholder(in context: Context) -> WalletInformationWidgetEntry {
return WalletInformationWidgetEntry(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 14:11:28 +01:00
}
2021-06-06 09:58:39 +02:00
func getSnapshot(in context: Context, completion: @escaping (WalletInformationWidgetEntry) -> ()) {
let entry: WalletInformationWidgetEntry
2020-11-02 14:11:28 +01:00
if (context.isPreview) {
2021-06-06 09:58:39 +02:00
entry = WalletInformationWidgetEntry(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 14:11:28 +01:00
} else {
2021-06-06 09:58:39 +02:00
entry = WalletInformationWidgetEntry(date: Date(), marketData: emptyMarketData)
2020-11-02 14:11:28 +01:00
}
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
2021-06-06 09:58:39 +02:00
var entries: [WalletInformationWidgetEntry] = []
2020-11-02 14:11:28 +01:00
let userPreferredCurrency = WidgetAPI.getUserPreferredCurrency();
2020-11-04 05:16:20 +01:00
2020-11-02 14:11:28 +01:00
let marketDataEntry = MarketData(nextBlock: "...", sats: "...", price: "...", rate: 0)
let allwalletsBalance = WalletData(balance: UserDefaultsGroup.getAllWalletsBalance(), latestTransactionTime: UserDefaultsGroup.getAllWalletsLatestTransactionTime())
WidgetAPI.fetchPrice(currency: userPreferredCurrency, completion: { (result, error) in
2021-06-06 09:58:39 +02:00
let entry: WalletInformationWidgetEntry
2020-11-02 14:11:28 +01:00
if let result = result {
2021-06-06 09:58:39 +02:00
entry = WalletInformationWidgetEntry(date: Date(), marketData: MarketData(nextBlock: "", sats: "", price: result.formattedRate ?? "!", rate: result.rateDouble), allWalletsBalance: allwalletsBalance)
2020-11-02 14:11:28 +01:00
} else {
2021-06-06 09:58:39 +02:00
entry = WalletInformationWidgetEntry(date: Date(), marketData: marketDataEntry, allWalletsBalance: allwalletsBalance)
2020-11-02 14:11:28 +01:00
}
entries.append(entry)
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
})
}
}
2021-06-06 09:58:39 +02:00
struct WalletInformationWidgetEntry: TimelineEntry {
2020-11-02 14:11:28 +01:00
let date: Date
let marketData: MarketData
var allWalletsBalance: WalletData = WalletData(balance: 0)
}
struct WalletInformationWidgetEntryView : View {
2021-06-06 09:58:39 +02:00
let entry: WalletInformationWidgetEntry
2020-11-02 14:11:28 +01:00
var WalletBalance: some View {
2020-11-04 05:16:20 +01:00
WalletInformationView(allWalletsBalance: entry.allWalletsBalance, marketData: entry.marketData)
2020-11-02 14:11:28 +01:00
}
var body: some View {
VStack(content: {
WalletBalance
}).padding().background(Color.widgetBackground)
2020-11-02 14:11:28 +01:00
}
}
struct WalletInformationWidget: Widget {
let kind: String = "WalletInformationWidget"
var body: some WidgetConfiguration {
2022-09-15 01:09:21 +02:00
if #available(iOSApplicationExtension 16.0, *) {
return StaticConfiguration(kind: kind, provider: WalletInformationWidgetProvider()) { entry in
WalletInformationWidgetEntryView(entry: entry)
}
.configurationDisplayName("Balance")
.description("View your accumulated balance.").supportedFamilies([.systemSmall])
2022-09-15 01:09:21 +02:00
} else {
return StaticConfiguration(kind: kind, provider: WalletInformationWidgetProvider()) { entry in
WalletInformationWidgetEntryView(entry: entry)
}
.configurationDisplayName("Balance")
.description("View your accumulated balance.").supportedFamilies([.systemSmall])
2020-11-02 14:11:28 +01:00
}
}
}
struct WalletInformationWidget_Previews: PreviewProvider {
static var previews: some View {
2021-06-06 09:58:39 +02:00
WalletInformationWidgetEntryView(entry: WalletInformationWidgetEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: Double(0)), allWalletsBalance: WalletData(balance: 10000, latestTransactionTime: LatestTransaction(isUnconfirmed: false, epochValue: 1568804029000))))
2020-11-02 14:11:28 +01:00
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}