macOS widgets

This commit is contained in:
Hillel Coren 2023-06-22 16:31:01 +03:00
parent 4bddd3ff8c
commit b343074e74
1 changed files with 15 additions and 8 deletions

View File

@ -15,8 +15,8 @@ struct Provider: IntentTimelineProvider {
SimpleEntry(date: Date(), SimpleEntry(date: Date(),
configuration: ConfigurationIntent(), configuration: ConfigurationIntent(),
widgetData: WidgetData(url: "url", companyId: "", companies: [:]), widgetData: WidgetData(url: "url", companyId: "", companies: [:]),
field: "Invoices", field: "Active Invoices",
value: 0) value: "$100.00")
} }
func getSnapshot(for configuration: ConfigurationIntent, func getSnapshot(for configuration: ConfigurationIntent,
@ -26,7 +26,8 @@ struct Provider: IntentTimelineProvider {
let entry = SimpleEntry(date: Date(), let entry = SimpleEntry(date: Date(),
configuration: configuration, configuration: configuration,
widgetData: WidgetData(url: "url", companyId: "", companies: [:]), widgetData: WidgetData(url: "url", companyId: "", companies: [:]),
field: "Invoices", value: 0) field: "Active Invoices",
value: "$100.00")
completion(entry) completion(entry)
} }
@ -85,6 +86,7 @@ struct Provider: IntentTimelineProvider {
} }
let currencyId = configuration.currency?.identifier ?? company?.currencyId let currencyId = configuration.currency?.identifier ?? company?.currencyId
let currency = company?.currencies[currencyId!]
var value = 0.0 var value = 0.0
var label = "" var label = ""
@ -109,11 +111,16 @@ struct Provider: IntentTimelineProvider {
} }
} }
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.currencyCode = currency?.code ?? "USD"
let entry = SimpleEntry(date: Date(), let entry = SimpleEntry(date: Date(),
configuration: configuration, configuration: configuration,
widgetData: widgetData, widgetData: widgetData,
field: label, field: label,
value: value) value: formatter.string(from: NSNumber(value: value))!)
// Next fetch happens 15 minutes later // Next fetch happens 15 minutes later
let nextUpdate = Calendar.current.date( let nextUpdate = Calendar.current.date(
@ -185,7 +192,7 @@ struct SimpleEntry: TimelineEntry {
let configuration: ConfigurationIntent let configuration: ConfigurationIntent
let widgetData: WidgetData? let widgetData: WidgetData?
let field: String let field: String
let value: Double let value: String
} }
struct DashboardWidgetEntryView : View { struct DashboardWidgetEntryView : View {
@ -211,7 +218,7 @@ struct DashboardWidgetEntryView : View {
.font(.body) .font(.body)
.bold() .bold()
.foregroundColor(Color.blue) .foregroundColor(Color.blue)
Text("\(entry.value)") Text(entry.value)
.font(.title) .font(.title)
.privacySensitive() .privacySensitive()
.foregroundColor(Color.gray) .foregroundColor(Color.gray)
@ -240,8 +247,8 @@ struct DashboardWidget_Previews: PreviewProvider {
let entry = SimpleEntry(date: Date(), let entry = SimpleEntry(date: Date(),
configuration: ConfigurationIntent(), configuration: ConfigurationIntent(),
widgetData: WidgetData(url: "url", companyId: "", companies: [:]), widgetData: WidgetData(url: "url", companyId: "", companies: [:]),
field: "Invoices", field: "Active Invoices",
value: 0) value: "$100.00")
DashboardWidgetEntryView(entry: entry) DashboardWidgetEntryView(entry: entry)
.previewContext(WidgetPreviewContext(family: .systemSmall)) .previewContext(WidgetPreviewContext(family: .systemSmall))