macOS widgets
This commit is contained in:
parent
e433568e7d
commit
1c04306d5b
|
|
@ -120,7 +120,7 @@ struct Provider: IntentTimelineProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTimelineData(for configuration: ConfigurationIntent) async throws -> (String, String) {
|
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 = ""
|
||||||
|
|
@ -128,86 +128,82 @@ struct Provider: IntentTimelineProvider {
|
||||||
let sharedDefaults = UserDefaults.init(suiteName: "group.com.invoiceninja.app")
|
let sharedDefaults = UserDefaults.init(suiteName: "group.com.invoiceninja.app")
|
||||||
var widgetData: WidgetData? = nil
|
var widgetData: WidgetData? = nil
|
||||||
|
|
||||||
if sharedDefaults != nil {
|
if sharedDefaults == nil {
|
||||||
do {
|
throw WidgetError.message("Not connected")
|
||||||
let shared = sharedDefaults!.string(forKey: "widget_data")
|
}
|
||||||
if shared != nil {
|
|
||||||
|
let shared = sharedDefaults!.string(forKey: "widget_data")
|
||||||
//print("## Shared: \(shared!)")
|
if shared == nil {
|
||||||
|
throw WidgetError.message("Not connected")
|
||||||
let decoder = JSONDecoder()
|
}
|
||||||
widgetData = try decoder.decode(WidgetData.self, from: shared!.data(using: .utf8)!)
|
|
||||||
|
//print("## Shared: \(shared!)")
|
||||||
let companyId = configuration.company?.identifier ?? ""
|
|
||||||
let company = widgetData?.companies[companyId]
|
let decoder = JSONDecoder()
|
||||||
let currencyId = configuration.currency?.identifier ?? company?.currencyId
|
widgetData = try decoder.decode(WidgetData.self, from: shared!.data(using: .utf8)!)
|
||||||
let currency = company?.currencies[currencyId!]
|
|
||||||
|
let companyId = configuration.company?.identifier ?? ""
|
||||||
if (widgetData?.url == nil) {
|
let company = widgetData?.companies[companyId]
|
||||||
throw WidgetError.message("URL is blank")
|
let currencyId = configuration.currency?.identifier ?? company?.currencyId
|
||||||
}
|
let currency = company?.currencies[currencyId!]
|
||||||
|
|
||||||
let url = (widgetData?.url ?? "") + "/charts/totals_v2";
|
if (widgetData?.url == nil) {
|
||||||
var token = company?.token
|
throw WidgetError.message("URL is blank")
|
||||||
let (startDate, endDate) = getDateRange(dateRange: (configuration.dateRange?.identifier)!,
|
}
|
||||||
firstMonthOfYear: company!.firstMonthOfYear)
|
|
||||||
|
let url = (widgetData?.url ?? "") + "/charts/totals_v2";
|
||||||
if (token == "" && !(widgetData?.companies.isEmpty)!) {
|
var token = company?.token
|
||||||
print("## WARNING: using first token")
|
let (startDate, endDate) = getDateRange(dateRange: (configuration.dateRange?.identifier)!,
|
||||||
let company = widgetData?.companies.values.first;
|
firstMonthOfYear: company!.firstMonthOfYear)
|
||||||
token = company?.token ?? ""
|
|
||||||
}
|
if (token == "" && !(widgetData?.companies.isEmpty)!) {
|
||||||
|
print("## WARNING: using first token")
|
||||||
print("## company.name: \(configuration.company?.displayString ?? "")")
|
let company = widgetData?.companies.values.first;
|
||||||
print("## company.id: \(configuration.company?.identifier ?? "")")
|
token = company?.token ?? ""
|
||||||
print("## Date Range: \(String(describing: configuration.dateRange?.identifier)) => \(startDate) - \(endDate)")
|
}
|
||||||
//print("## URL: \(url)")
|
|
||||||
|
print("## company.name: \(configuration.company?.displayString ?? "")")
|
||||||
if (token == "") {
|
print("## company.id: \(configuration.company?.identifier ?? "")")
|
||||||
throw WidgetError.message("API token is blank")
|
print("## Date Range: \(String(describing: configuration.dateRange?.identifier)) => \(startDate) - \(endDate)")
|
||||||
}
|
//print("## URL: \(url)")
|
||||||
|
|
||||||
|
if (token == "") {
|
||||||
let result = try await ApiService.post(urlString: url,
|
throw WidgetError.message("API token is blank")
|
||||||
apiToken: token!,
|
}
|
||||||
startDate: startDate,
|
|
||||||
endDate: endDate)!
|
|
||||||
|
let result = try await ApiService.post(urlString: url,
|
||||||
let data = result[currencyId ?? "1"]
|
apiToken: token!,
|
||||||
|
startDate: startDate,
|
||||||
if (data != nil) {
|
endDate: endDate)!
|
||||||
if (configuration.field == Field.active_invoices) {
|
|
||||||
if (data?.invoices?.invoicedAmount != nil) {
|
let data = result[currencyId ?? "1"]
|
||||||
rawValue = Double(data?.invoices?.invoicedAmount ?? "")!
|
|
||||||
}
|
if (data != nil) {
|
||||||
label = "Active Invoices"
|
if (configuration.field == Field.active_invoices) {
|
||||||
} else if (configuration.field == Field.outstanding_invoices) {
|
if (data?.invoices?.invoicedAmount != nil) {
|
||||||
if (data?.outstanding?.amount != nil) {
|
rawValue = Double(data?.invoices?.invoicedAmount ?? "")!
|
||||||
rawValue = Double(data?.outstanding?.amount ?? "")!
|
|
||||||
}
|
|
||||||
label = "Outstanding Invoices"
|
|
||||||
} else if (configuration.field == Field.completed_payments) {
|
|
||||||
if (data?.revenue?.paidToDate != nil) {
|
|
||||||
rawValue = Double(data?.revenue?.paidToDate ?? "")!
|
|
||||||
}
|
|
||||||
label = "Completed Payments"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let formatter = NumberFormatter()
|
|
||||||
formatter.numberStyle = .currency
|
|
||||||
formatter.currencyCode = currency?.code ?? "USD"
|
|
||||||
value = formatter.string(from: NSNumber(value: rawValue))!
|
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch {
|
label = "Active Invoices"
|
||||||
print("## ERROR: \(error)")
|
} else if (configuration.field == Field.outstanding_invoices) {
|
||||||
//value = "\(error)"
|
if (data?.outstanding?.amount != nil) {
|
||||||
|
rawValue = Double(data?.outstanding?.amount ?? "")!
|
||||||
|
}
|
||||||
|
label = "Outstanding Invoices"
|
||||||
|
} else if (configuration.field == Field.completed_payments) {
|
||||||
|
if (data?.revenue?.paidToDate != nil) {
|
||||||
|
rawValue = Double(data?.revenue?.paidToDate ?? "")!
|
||||||
|
}
|
||||||
|
label = "Completed Payments"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (label, value)
|
let formatter = NumberFormatter()
|
||||||
|
formatter.numberStyle = .currency
|
||||||
|
formatter.currencyCode = currency?.code ?? "USD"
|
||||||
|
value = formatter.string(from: NSNumber(value: rawValue))!
|
||||||
|
|
||||||
|
return (label, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue