IntentHandler.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // IntentHandler.swift
  3. // WidgetDashboardIntentHandler
  4. //
  5. // Created by Marino Faggiana on 08/10/22.
  6. // Copyright © 2022 Marino Faggiana. All rights reserved.
  7. //
  8. import Intents
  9. import RealmSwift
  10. class IntentHandler: INExtension, DashboardIntentHandling, AccountIntentHandling {
  11. // MARK: - Account
  12. // Account
  13. func provideAccountsOptionsCollection(for intent: AccountIntent, with completion: @escaping (INObjectCollection<Accounts>?, Error?) -> Void) {
  14. var accounts: [Accounts] = []
  15. let results = NCManageDatabase.shared.getAllAccount()
  16. accounts.append(Accounts(identifier: "active", display: NSLocalizedString("_account_active_", comment: "")))
  17. if results.isEmpty {
  18. return completion(nil, nil)
  19. } else if results.count == 1 {
  20. return completion(INObjectCollection(items: accounts), nil)
  21. }
  22. for result in results {
  23. let display = (result.alias.isEmpty) ? result.account : result.alias
  24. let account = Accounts(identifier: result.account, display: display)
  25. accounts.append(account)
  26. }
  27. completion(INObjectCollection(items: accounts), nil)
  28. }
  29. func defaultAccounts(for intent: AccountIntent) -> Accounts? {
  30. if NCManageDatabase.shared.getActiveAccount() == nil {
  31. return nil
  32. } else {
  33. return Accounts(identifier: "active", display: NSLocalizedString("_account_active_", comment: ""))
  34. }
  35. }
  36. // MARK: - Dashboard
  37. // Application
  38. func provideApplicationsOptionsCollection(for intent: DashboardIntent, with completion: @escaping (INObjectCollection<Applications>?, Error?) -> Void) {
  39. var applications: [Applications] = []
  40. guard let account = NCManageDatabase.shared.getActiveAccount() else {
  41. return completion(nil, nil)
  42. }
  43. let results = NCManageDatabase.shared.getDashboardWidgetApplications(account: account.account)
  44. for result in results {
  45. let application = Applications(identifier: result.id, display: result.title)
  46. applications.append(application)
  47. }
  48. completion(INObjectCollection(items: applications), nil)
  49. }
  50. func defaultApplications(for intent: DashboardIntent) -> Applications? {
  51. guard let account = NCManageDatabase.shared.getActiveAccount() else {
  52. return nil
  53. }
  54. if let result = NCManageDatabase.shared.getDashboardWidgetApplications(account: account.account).first {
  55. return Applications(identifier: result.id, display: result.title)
  56. }
  57. return nil
  58. }
  59. // Account
  60. func provideAccountsOptionsCollection(for intent: DashboardIntent, with completion: @escaping (INObjectCollection<Accounts>?, Error?) -> Void) {
  61. var accounts: [Accounts] = []
  62. let results = NCManageDatabase.shared.getAllAccount()
  63. accounts.append(Accounts(identifier: "active", display: NSLocalizedString("_account_active_", comment: "")))
  64. if results.isEmpty {
  65. return completion(nil, nil)
  66. } else if results.count == 1 {
  67. return completion(INObjectCollection(items: accounts), nil)
  68. }
  69. for result in results {
  70. let display = (result.alias.isEmpty) ? result.account : result.alias
  71. let account = Accounts(identifier: result.account, display: display)
  72. accounts.append(account)
  73. }
  74. completion(INObjectCollection(items: accounts), nil)
  75. }
  76. func defaultAccounts(for intent: DashboardIntent) -> Accounts? {
  77. if NCManageDatabase.shared.getActiveAccount() == nil {
  78. return nil
  79. } else {
  80. return Accounts(identifier: "active", display: NSLocalizedString("_account_active_", comment: ""))
  81. }
  82. }
  83. }