macOS widgets
This commit is contained in:
parent
87bb99979e
commit
fe6f3abbd5
|
|
@ -95,18 +95,25 @@ class _WindowManagerState extends State<WindowManager> with WindowListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
class WidgetData {
|
class WidgetData {
|
||||||
WidgetData({this.url, this.companies});
|
WidgetData({
|
||||||
|
this.url,
|
||||||
|
this.companies,
|
||||||
|
this.companyId,
|
||||||
|
});
|
||||||
|
|
||||||
WidgetData.fromJson(Map<String, dynamic> json)
|
WidgetData.fromJson(Map<String, dynamic> json)
|
||||||
: url = json['url'],
|
: url = json['url'],
|
||||||
|
companyId = json['company_id'],
|
||||||
companies = json['companies'];
|
companies = json['companies'];
|
||||||
|
|
||||||
Map<String, dynamic> toJson() => <String, dynamic>{
|
Map<String, dynamic> toJson() => <String, dynamic>{
|
||||||
'companies': companies,
|
'companies': companies,
|
||||||
|
'company_id': companyId,
|
||||||
'url': url,
|
'url': url,
|
||||||
};
|
};
|
||||||
|
|
||||||
final String url;
|
final String url;
|
||||||
|
final String companyId;
|
||||||
final Map<String, WidgetCompany> companies;
|
final Map<String, WidgetCompany> companies;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,38 +4,42 @@ class IntentHandler: INExtension, ConfigurationIntentHandling {
|
||||||
|
|
||||||
private func loadWidgetData() -> WidgetData {
|
private func loadWidgetData() -> WidgetData {
|
||||||
let sharedDefaults = UserDefaults(suiteName: "group.com.invoiceninja.app")
|
let sharedDefaults = UserDefaults(suiteName: "group.com.invoiceninja.app")
|
||||||
var exampleData: WidgetData = WidgetData(url: "", companies: [:])
|
var widgetData: WidgetData = WidgetData(url: "", companyId: "", companies: [:])
|
||||||
|
|
||||||
if let sharedDefaults = sharedDefaults {
|
if let sharedDefaults = sharedDefaults {
|
||||||
do {
|
do {
|
||||||
if let shared = sharedDefaults.string(forKey: "widget_data") {
|
if let shared = sharedDefaults.string(forKey: "widget_data") {
|
||||||
let decoder = JSONDecoder()
|
let decoder = JSONDecoder()
|
||||||
exampleData = try decoder.decode(WidgetData.self, from: shared.data(using: .utf8)!)
|
widgetData = try decoder.decode(WidgetData.self, from: shared.data(using: .utf8)!)
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
print(error)
|
print(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return exampleData
|
return widgetData
|
||||||
}
|
}
|
||||||
|
|
||||||
func provideCompanyOptionsCollection(for intent: ConfigurationIntent) async throws -> INObjectCollection<Company> {
|
func provideCompanyOptionsCollection(for intent: ConfigurationIntent) async throws -> INObjectCollection<Company> {
|
||||||
let exampleData = loadWidgetData()
|
let widgetData = loadWidgetData()
|
||||||
|
|
||||||
let companies = exampleData.companies.values.map { company in
|
let companies = widgetData.companies.values.map { company in
|
||||||
Company(identifier: company.id, display: company.name)
|
Company(identifier: company.id, display: company.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
return INObjectCollection(items: companies)
|
return INObjectCollection(items: companies)
|
||||||
}
|
}
|
||||||
|
|
||||||
//func defaultCompany(for intent: ConfigurationIntent) -> Company? {}
|
func defaultCompany(for intent: ConfigurationIntent) -> Company? {
|
||||||
|
let widgetData = loadWidgetData()
|
||||||
|
let company = widgetData.companies[widgetData.companyId];
|
||||||
|
return Company(identifier: company!.id, display: company!.name)
|
||||||
|
}
|
||||||
|
|
||||||
func provideCurrencyOptionsCollection(for intent: ConfigurationIntent) async throws -> INObjectCollection<Currency> {
|
func provideCurrencyOptionsCollection(for intent: ConfigurationIntent) async throws -> INObjectCollection<Currency> {
|
||||||
let exampleData = loadWidgetData()
|
let widgetData = loadWidgetData()
|
||||||
|
|
||||||
let company = exampleData.companies[(intent.company?.identifier!)!]
|
let company = widgetData.companies[(intent.company?.identifier!)!]
|
||||||
let currencies = company!.currencies.values.map { currency in
|
let currencies = company!.currencies.values.map { currency in
|
||||||
Currency(identifier: currency.id, display: currency.name)
|
Currency(identifier: currency.id, display: currency.name)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,11 +11,11 @@ import Intents
|
||||||
|
|
||||||
struct Provider: IntentTimelineProvider {
|
struct Provider: IntentTimelineProvider {
|
||||||
func placeholder(in context: Context) -> SimpleEntry {
|
func placeholder(in context: Context) -> SimpleEntry {
|
||||||
SimpleEntry(date: Date(), configuration: ConfigurationIntent(), widgetData: WidgetData(url: "url", companies: [:]), field: "Invoices", value: 0)
|
SimpleEntry(date: Date(), configuration: ConfigurationIntent(), widgetData: WidgetData(url: "url", companyId: "", companies: [:]), field: "Invoices", value: 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
|
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
|
||||||
let entry = SimpleEntry(date: Date(), configuration: configuration, widgetData: WidgetData(url: "url", companies: [:]), field: "Invoices", value: 0)
|
let entry = SimpleEntry(date: Date(), configuration: configuration, widgetData: WidgetData(url: "url", companyId: "", companies: [:]), field: "Invoices", value: 0)
|
||||||
completion(entry)
|
completion(entry)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -96,7 +96,14 @@ struct Provider: IntentTimelineProvider {
|
||||||
|
|
||||||
struct WidgetData: Decodable, Hashable {
|
struct WidgetData: Decodable, Hashable {
|
||||||
let url: String
|
let url: String
|
||||||
|
let companyId: String
|
||||||
let companies: [String: WidgetCompany]
|
let companies: [String: WidgetCompany]
|
||||||
|
|
||||||
|
enum CodingKeys: String, CodingKey {
|
||||||
|
case url
|
||||||
|
case companyId = "company_id"
|
||||||
|
case companies
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct WidgetCompany: Decodable, Hashable {
|
struct WidgetCompany: Decodable, Hashable {
|
||||||
|
|
@ -185,7 +192,7 @@ struct DashboardWidget: Widget {
|
||||||
|
|
||||||
struct DashboardWidget_Previews: PreviewProvider {
|
struct DashboardWidget_Previews: PreviewProvider {
|
||||||
static var previews: some View {
|
static var previews: some View {
|
||||||
DashboardWidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent(), widgetData: WidgetData(url: "url", companies: [:]), field: "Invoices", value: 0))
|
DashboardWidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent(), widgetData: WidgetData(url: "url", companyId: "", companies: [:]), field: "Invoices", value: 0))
|
||||||
.previewContext(WidgetPreviewContext(family: .systemSmall))
|
.previewContext(WidgetPreviewContext(family: .systemSmall))
|
||||||
//.environment(\.sizeCategory, .extraLarge)
|
//.environment(\.sizeCategory, .extraLarge)
|
||||||
//.environment(\.colorScheme, .dark)
|
//.environment(\.colorScheme, .dark)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue