macOS widgets
This commit is contained in:
parent
b226d961a7
commit
e433568e7d
|
|
@ -66,6 +66,61 @@ struct Provider: IntentTimelineProvider {
|
||||||
|
|
||||||
Task {
|
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 rawValue = 0.0
|
||||||
var value = "Error"
|
var value = "Error"
|
||||||
var label = ""
|
var label = ""
|
||||||
|
|
@ -88,9 +143,8 @@ struct Provider: IntentTimelineProvider {
|
||||||
let currencyId = configuration.currency?.identifier ?? company?.currencyId
|
let currencyId = configuration.currency?.identifier ?? company?.currencyId
|
||||||
let currency = company?.currencies[currencyId!]
|
let currency = company?.currencies[currencyId!]
|
||||||
|
|
||||||
|
|
||||||
if (widgetData?.url == nil) {
|
if (widgetData?.url == nil) {
|
||||||
return
|
throw WidgetError.message("URL is blank")
|
||||||
}
|
}
|
||||||
|
|
||||||
let url = (widgetData?.url ?? "") + "/charts/totals_v2";
|
let url = (widgetData?.url ?? "") + "/charts/totals_v2";
|
||||||
|
|
@ -110,7 +164,7 @@ struct Provider: IntentTimelineProvider {
|
||||||
//print("## URL: \(url)")
|
//print("## URL: \(url)")
|
||||||
|
|
||||||
if (token == "") {
|
if (token == "") {
|
||||||
return
|
throw WidgetError.message("API token is blank")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -152,26 +206,7 @@ struct Provider: IntentTimelineProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
print("## VALUE: \(value)")
|
return (label, 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -429,7 +464,7 @@ struct ApiResultError: Codable {
|
||||||
let errors: [String: String]
|
let errors: [String: String]
|
||||||
}
|
}
|
||||||
|
|
||||||
enum ApiError: Error {
|
enum WidgetError: Error {
|
||||||
case message(String)
|
case message(String)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -440,7 +475,7 @@ struct ApiService {
|
||||||
let url = URL(string: "\(urlString)?start_date=\(startDate)&end_date=\(endDate)")!
|
let url = URL(string: "\(urlString)?start_date=\(startDate)&end_date=\(endDate)")!
|
||||||
var request = URLRequest(url: url)
|
var request = URLRequest(url: url)
|
||||||
request.httpMethod = "POST"
|
request.httpMethod = "POST"
|
||||||
request.addValue(apiToken, forHTTPHeaderField: "X-API-Token")
|
//request.addValue(apiToken, forHTTPHeaderField: "X-API-Token")
|
||||||
request.addValue("macOS Widget", forHTTPHeaderField: "X-CLIENT")
|
request.addValue("macOS Widget", forHTTPHeaderField: "X-CLIENT")
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
|
@ -459,11 +494,11 @@ struct ApiService {
|
||||||
} else {
|
} else {
|
||||||
let result = try JSONDecoder().decode(ApiResultError.self, from: data)
|
let result = try JSONDecoder().decode(ApiResultError.self, from: data)
|
||||||
|
|
||||||
throw ApiError.message("\(statusCode): \(result.message)")
|
throw WidgetError.message("\(statusCode): \(result.message)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
throw ApiError.message("\(error)")
|
throw WidgetError.message("\(error)")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue