BlueWallet/ios/WalletInformationWidget/Widgets/MarketWidget/MarketWidget.swift

82 lines
2.6 KiB
Swift
Raw Normal View History

2020-10-30 10:19:38 -04:00
//
// MarketWidget.swift
// MarketWidget
//
2020-11-06 12:05:18 -05:00
// Created by Marcos Rodriguez on 11/6/20.
2020-10-30 10:19:38 -04:00
// Copyright © 2020 BlueWallet. All rights reserved.
//
import WidgetKit
import SwiftUI
struct Provider: TimelineProvider {
2020-11-06 12:05:18 -05:00
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), marketData: emptyMarketData)
2020-10-30 10:19:38 -04:00
}
2020-11-06 12:05:18 -05:00
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
2020-11-02 08:11:28 -05:00
let entry: SimpleEntry
2020-11-06 12:05:18 -05:00
if (context.isPreview) {
entry = SimpleEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10 000", rate: 10000))
2020-11-02 08:11:28 -05:00
} else {
2020-11-06 12:05:18 -05:00
entry = SimpleEntry(date: Date(), marketData: emptyMarketData)
2020-10-30 10:19:38 -04:00
}
2020-11-06 12:05:18 -05:00
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [SimpleEntry] = []
let userPreferredCurrency = WidgetAPI.getUserPreferredCurrency();
let marketDataEntry = MarketData(nextBlock: "...", sats: "...", price: "...", rate: 0)
WidgetAPI.fetchMarketData(currency: userPreferredCurrency, completion: { (result, error) in
let entry: SimpleEntry
if let result = result {
entry = SimpleEntry(date: Date(), marketData: result)
} else {
entry = SimpleEntry(date: Date(), marketData: marketDataEntry)
}
entries.append(entry)
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
})
}
2020-10-30 10:19:38 -04:00
}
struct SimpleEntry: TimelineEntry {
2020-11-06 12:05:18 -05:00
let date: Date
2020-10-30 10:19:38 -04:00
let marketData: MarketData
}
struct MarketWidgetEntryView : View {
2020-11-06 12:05:18 -05:00
var entry: Provider.Entry
2020-11-02 08:11:28 -05:00
var MarketStack: some View {
2020-11-03 23:16:20 -05:00
MarketView(marketData: entry.marketData).padding(EdgeInsets(top: 18, leading: 11, bottom: 18, trailing: 11))
2020-11-02 08:11:28 -05:00
}
2020-11-06 12:05:18 -05:00
var body: some View {
2020-11-02 08:11:28 -05:00
MarketStack.background(Color.widgetBackground)
2020-11-06 12:05:18 -05:00
}
2020-10-30 10:19:38 -04:00
}
@main
struct MarketWidget: Widget {
2020-11-06 12:05:18 -05:00
let kind: String = "MarketWidget"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
MarketWidgetEntryView(entry: entry)
}
.configurationDisplayName("Market")
.description("View the current market information.").supportedFamilies([.systemSmall])
2020-10-30 10:19:38 -04:00
}
}
struct MarketWidget_Previews: PreviewProvider {
2020-11-06 12:05:18 -05:00
static var previews: some View {
MarketWidgetEntryView(entry: SimpleEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: 0)))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
2020-10-30 10:19:38 -04:00
}