macOS widgets
This commit is contained in:
parent
b226d961a7
commit
e433568e7d
|
|
@ -66,6 +66,61 @@ struct Provider: IntentTimelineProvider {
|
|||
|
||||
Task {
|
||||
|
||||
var value = "Error"
|
||||
var label = ""
|
||||
|
||||
do {
|
||||
let widgetData = try getWidgetData()
|
||||
|
||||
(label, value) = try await getTimelineData(for: configuration)
|
||||
|
||||
print("## VALUE: \(value)")
|
||||
|
||||
let entry = SimpleEntry(date: Date(),
|
||||
configuration: configuration,
|
||||
widgetData: widgetData,
|
||||
field: label,
|
||||
value: value)
|
||||
|
||||
let nextUpdate = Calendar.current.date(
|
||||
byAdding: DateComponents(minute: 15),
|
||||
to: Date()
|
||||
)!
|
||||
|
||||
let timeline = Timeline(
|
||||
entries: [entry],
|
||||
policy: .after(nextUpdate)
|
||||
)
|
||||
|
||||
completion(timeline)
|
||||
} catch {
|
||||
// TODO
|
||||
print("## getTimeline ERROR: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getWidgetData() throws -> (WidgetData?) {
|
||||
|
||||
let sharedDefaults = UserDefaults.init(suiteName: "group.com.invoiceninja.app")
|
||||
var widgetData: WidgetData? = nil
|
||||
|
||||
if sharedDefaults != nil {
|
||||
let shared = sharedDefaults!.string(forKey: "widget_data")
|
||||
if shared != nil {
|
||||
|
||||
//print("## Shared: \(shared!)")
|
||||
|
||||
let decoder = JSONDecoder()
|
||||
widgetData = try decoder.decode(WidgetData.self, from: shared!.data(using: .utf8)!)
|
||||
}
|
||||
}
|
||||
|
||||
return widgetData
|
||||
}
|
||||
|
||||
func getTimelineData(for configuration: ConfigurationIntent) async throws -> (String, String) {
|
||||
|
||||
var rawValue = 0.0
|
||||
var value = "Error"
|
||||
var label = ""
|
||||
|
|
@ -88,9 +143,8 @@ struct Provider: IntentTimelineProvider {
|
|||
let currencyId = configuration.currency?.identifier ?? company?.currencyId
|
||||
let currency = company?.currencies[currencyId!]
|
||||
|
||||
|
||||
if (widgetData?.url == nil) {
|
||||
return
|
||||
throw WidgetError.message("URL is blank")
|
||||
}
|
||||
|
||||
let url = (widgetData?.url ?? "") + "/charts/totals_v2";
|
||||
|
|
@ -110,7 +164,7 @@ struct Provider: IntentTimelineProvider {
|
|||
//print("## URL: \(url)")
|
||||
|
||||
if (token == "") {
|
||||
return
|
||||
throw WidgetError.message("API token is blank")
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -152,26 +206,7 @@ struct Provider: IntentTimelineProvider {
|
|||
}
|
||||
}
|
||||
|
||||
print("## VALUE: \(value)")
|
||||
|
||||
let entry = SimpleEntry(date: Date(),
|
||||
configuration: configuration,
|
||||
widgetData: widgetData,
|
||||
field: label,
|
||||
value: value)
|
||||
|
||||
let nextUpdate = Calendar.current.date(
|
||||
byAdding: DateComponents(minute: 15),
|
||||
to: Date()
|
||||
)!
|
||||
|
||||
let timeline = Timeline(
|
||||
entries: [entry],
|
||||
policy: .after(nextUpdate)
|
||||
)
|
||||
|
||||
completion(timeline)
|
||||
}
|
||||
return (label, value)
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -429,7 +464,7 @@ struct ApiResultError: Codable {
|
|||
let errors: [String: String]
|
||||
}
|
||||
|
||||
enum ApiError: Error {
|
||||
enum WidgetError: Error {
|
||||
case message(String)
|
||||
}
|
||||
|
||||
|
|
@ -440,7 +475,7 @@ struct ApiService {
|
|||
let url = URL(string: "\(urlString)?start_date=\(startDate)&end_date=\(endDate)")!
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
request.addValue(apiToken, forHTTPHeaderField: "X-API-Token")
|
||||
//request.addValue(apiToken, forHTTPHeaderField: "X-API-Token")
|
||||
request.addValue("macOS Widget", forHTTPHeaderField: "X-CLIENT")
|
||||
|
||||
do {
|
||||
|
|
@ -459,11 +494,11 @@ struct ApiService {
|
|||
} else {
|
||||
let result = try JSONDecoder().decode(ApiResultError.self, from: data)
|
||||
|
||||
throw ApiError.message("\(statusCode): \(result.message)")
|
||||
throw WidgetError.message("\(statusCode): \(result.message)")
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
throw ApiError.message("\(error)")
|
||||
throw WidgetError.message("\(error)")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
Loading…
Reference in New Issue