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
|
|
|
|
|
2021-06-06 03:58:39 -04:00
|
|
|
struct MarketWidgetProvider: TimelineProvider {
|
2023-12-26 23:00:30 -04:00
|
|
|
static var lastSuccessfulEntry: MarketWidgetEntry?
|
|
|
|
|
2021-06-06 03:58:39 -04:00
|
|
|
func placeholder(in context: Context) -> MarketWidgetEntry {
|
|
|
|
return MarketWidgetEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10 000", rate: 10000))
|
2021-02-16 21:37:57 -05:00
|
|
|
}
|
|
|
|
|
2021-06-06 03:58:39 -04:00
|
|
|
func getSnapshot(in context: Context, completion: @escaping (MarketWidgetEntry) -> ()) {
|
|
|
|
let entry: MarketWidgetEntry
|
2021-02-16 21:37:57 -05:00
|
|
|
if (context.isPreview) {
|
2021-06-06 03:58:39 -04:00
|
|
|
entry = MarketWidgetEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10 000", rate: 10000))
|
2021-02-16 21:37:57 -05:00
|
|
|
} else {
|
2021-06-06 03:58:39 -04:00
|
|
|
entry = MarketWidgetEntry(date: Date(), marketData: emptyMarketData)
|
2020-10-30 10:19:38 -04:00
|
|
|
}
|
2021-02-16 21:37:57 -05:00
|
|
|
completion(entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
|
2023-12-26 23:00:30 -04:00
|
|
|
var entries: [MarketWidgetEntry] = []
|
|
|
|
if context.isPreview {
|
|
|
|
let entry = MarketWidgetEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10 000", rate: 10000))
|
2021-03-07 19:00:48 -05:00
|
|
|
entries.append(entry)
|
|
|
|
let timeline = Timeline(entries: entries, policy: .atEnd)
|
|
|
|
completion(timeline)
|
2023-12-26 23:00:30 -04:00
|
|
|
} else {
|
2024-04-14 12:51:09 -04:00
|
|
|
let userPreferredCurrency = Currency.getUserPreferredCurrency()
|
|
|
|
MarketAPI.fetchMarketData(currency: userPreferredCurrency) { (result, error) in
|
2023-12-26 23:00:30 -04:00
|
|
|
let entry: MarketWidgetEntry
|
|
|
|
|
|
|
|
if let result = result {
|
|
|
|
entry = MarketWidgetEntry(date: Date(), marketData: result)
|
|
|
|
MarketWidgetProvider.lastSuccessfulEntry = entry
|
|
|
|
} else {
|
|
|
|
// Use the last successful entry if available
|
|
|
|
if let lastEntry = MarketWidgetProvider.lastSuccessfulEntry {
|
|
|
|
entry = lastEntry
|
|
|
|
} else {
|
|
|
|
// Fallback to a default entry if no successful entry is available
|
|
|
|
entry = MarketWidgetEntry(date: Date(), marketData: emptyMarketData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
entries.append(entry)
|
|
|
|
let timeline = Timeline(entries: entries, policy: .atEnd)
|
|
|
|
completion(timeline)
|
|
|
|
}
|
|
|
|
}
|
2021-02-16 21:37:57 -05:00
|
|
|
}
|
2020-10-30 10:19:38 -04:00
|
|
|
}
|
|
|
|
|
2021-06-06 03:58:39 -04:00
|
|
|
struct MarketWidgetEntry: TimelineEntry {
|
2021-02-16 21:37:57 -05:00
|
|
|
let date: Date
|
2020-10-30 10:19:38 -04:00
|
|
|
let marketData: MarketData
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MarketWidgetEntryView : View {
|
2021-06-06 03:58:39 -04:00
|
|
|
var entry: MarketWidgetProvider.Entry
|
2020-11-06 12:05:18 -05:00
|
|
|
|
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
|
|
|
}
|
2021-02-16 21:37:57 -05:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
VStack(content: {
|
2020-11-02 08:11:28 -05:00
|
|
|
MarketStack.background(Color.widgetBackground)
|
2021-02-16 21:37:57 -05:00
|
|
|
})
|
|
|
|
}
|
2020-10-30 10:19:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct MarketWidget: Widget {
|
2021-02-16 21:37:57 -05:00
|
|
|
let kind: String = "MarketWidget"
|
|
|
|
|
|
|
|
var body: some WidgetConfiguration {
|
2022-09-14 19:09:21 -04:00
|
|
|
if #available(iOSApplicationExtension 16.0, *) {
|
|
|
|
return StaticConfiguration(kind: kind, provider: MarketWidgetProvider()) { entry in
|
|
|
|
MarketWidgetEntryView(entry: entry)
|
|
|
|
}
|
|
|
|
.configurationDisplayName("Market")
|
2023-05-14 08:16:45 -05:00
|
|
|
.description("View the current market information.").supportedFamilies([.systemSmall])
|
2024-02-27 20:23:15 -04:00
|
|
|
.contentMarginsDisabledIfAvailable()
|
2022-09-14 19:09:21 -04:00
|
|
|
} else {
|
|
|
|
return StaticConfiguration(kind: kind, provider: MarketWidgetProvider()) { entry in
|
|
|
|
MarketWidgetEntryView(entry: entry)
|
|
|
|
}
|
|
|
|
.configurationDisplayName("Market")
|
|
|
|
.description("View the current market information.").supportedFamilies([.systemSmall])
|
2024-02-27 20:23:15 -04:00
|
|
|
.contentMarginsDisabledIfAvailable()
|
2020-10-30 10:19:38 -04:00
|
|
|
}
|
2021-02-16 21:37:57 -05:00
|
|
|
}
|
2020-10-30 10:19:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct MarketWidget_Previews: PreviewProvider {
|
2021-02-16 21:37:57 -05:00
|
|
|
static var previews: some View {
|
2021-06-06 03:58:39 -04:00
|
|
|
MarketWidgetEntryView(entry: MarketWidgetEntry(date: Date(), marketData: MarketData(nextBlock: "26", sats: "9 134", price: "$10,000", rate: 0)))
|
2021-02-16 21:37:57 -05:00
|
|
|
.previewContext(WidgetPreviewContext(family: .systemSmall))
|
|
|
|
}
|
2020-10-30 10:19:38 -04:00
|
|
|
}
|