IntentHandler.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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: "Active account"))
  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: "Active account")
  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. var account: tableAccount?
  41. let accountIdentifier: String = intent.accounts?.identifier ?? "active"
  42. if accountIdentifier == "active" {
  43. account = NCManageDatabase.shared.getActiveAccount()
  44. } else {
  45. account = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", accountIdentifier))
  46. }
  47. guard let account = account else {
  48. return completion(nil, nil)
  49. }
  50. let results = NCManageDatabase.shared.getDashboardWidgetApplications(account: account.account)
  51. for result in results {
  52. let application = Applications(identifier: result.id, display: result.title)
  53. applications.append(application)
  54. }
  55. completion(INObjectCollection(items: applications), nil)
  56. }
  57. func defaultApplications(for intent: DashboardIntent) -> Applications? {
  58. guard let account = NCManageDatabase.shared.getActiveAccount() else {
  59. return nil
  60. }
  61. if let result = NCManageDatabase.shared.getDashboardWidgetApplications(account: account.account).first {
  62. return Applications(identifier: result.id, display: result.title)
  63. }
  64. return nil
  65. }
  66. // Account
  67. func provideAccountsOptionsCollection(for intent: DashboardIntent, with completion: @escaping (INObjectCollection<Accounts>?, Error?) -> Void) {
  68. var accounts: [Accounts] = []
  69. let results = NCManageDatabase.shared.getAllAccount()
  70. accounts.append(Accounts(identifier: "active", display: "Active account"))
  71. if results.isEmpty {
  72. return completion(nil, nil)
  73. } else if results.count == 1 {
  74. return completion(INObjectCollection(items: accounts), nil)
  75. }
  76. for result in results {
  77. let display = (result.alias.isEmpty) ? result.account : result.alias
  78. let account = Accounts(identifier: result.account, display: display)
  79. accounts.append(account)
  80. }
  81. completion(INObjectCollection(items: accounts), nil)
  82. }
  83. func defaultAccounts(for intent: DashboardIntent) -> Accounts? {
  84. if NCManageDatabase.shared.getActiveAccount() == nil {
  85. return nil
  86. } else {
  87. return Accounts(identifier: "active", display: "Active account")
  88. }
  89. }
  90. }