IntentHandler.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Intents
  24. import RealmSwift
  25. class IntentHandler: INExtension, DashboardIntentHandling, AccountIntentHandling {
  26. // MARK: - Account
  27. func provideAccountsOptionsCollection(for intent: AccountIntent, with completion: @escaping (INObjectCollection<Accounts>?, Error?) -> Void) {
  28. var accounts: [Accounts] = []
  29. let results = NCManageDatabase.shared.getAllTableAccount()
  30. accounts.append(Accounts(identifier: "active", display: "Active account"))
  31. if results.isEmpty {
  32. return completion(nil, nil)
  33. } else if results.count == 1 {
  34. return completion(INObjectCollection(items: accounts), nil)
  35. }
  36. for result in results {
  37. let display = (result.alias.isEmpty) ? result.account : result.alias
  38. let account = Accounts(identifier: result.account, display: display)
  39. accounts.append(account)
  40. }
  41. completion(INObjectCollection(items: accounts), nil)
  42. }
  43. func defaultAccounts(for intent: AccountIntent) -> Accounts? {
  44. if NCManageDatabase.shared.getActiveTableAccount() == nil {
  45. return nil
  46. } else {
  47. return Accounts(identifier: "active", display: "Active account")
  48. }
  49. }
  50. // MARK: - Dashboard
  51. // Application
  52. func provideApplicationsOptionsCollection(for intent: DashboardIntent, with completion: @escaping (INObjectCollection<Applications>?, Error?) -> Void) {
  53. var applications: [Applications] = []
  54. var activeTableAccount: tableAccount?
  55. let accountIdentifier: String = intent.accounts?.identifier ?? "active"
  56. if accountIdentifier == "active" {
  57. activeTableAccount = NCManageDatabase.shared.getActiveTableAccount()
  58. } else {
  59. activeTableAccount = NCManageDatabase.shared.getTableAccount(predicate: NSPredicate(format: "account == %@", accountIdentifier))
  60. }
  61. guard let activeTableAccount else {
  62. return completion(nil, nil)
  63. }
  64. let results = NCManageDatabase.shared.getDashboardWidgetApplications(account: activeTableAccount.account)
  65. for result in results {
  66. let application = Applications(identifier: result.id, display: result.title)
  67. applications.append(application)
  68. }
  69. completion(INObjectCollection(items: applications), nil)
  70. }
  71. func defaultApplications(for intent: DashboardIntent) -> Applications? {
  72. guard let account = NCManageDatabase.shared.getActiveTableAccount() else {
  73. return nil
  74. }
  75. if let result = NCManageDatabase.shared.getDashboardWidgetApplications(account: account.account).first {
  76. return Applications(identifier: result.id, display: result.title)
  77. }
  78. return nil
  79. }
  80. // Account
  81. func provideAccountsOptionsCollection(for intent: DashboardIntent, with completion: @escaping (INObjectCollection<Accounts>?, Error?) -> Void) {
  82. var accounts: [Accounts] = []
  83. let results = NCManageDatabase.shared.getAllTableAccount()
  84. accounts.append(Accounts(identifier: "active", display: "Active account"))
  85. if results.isEmpty {
  86. return completion(nil, nil)
  87. } else if results.count == 1 {
  88. return completion(INObjectCollection(items: accounts), nil)
  89. }
  90. for result in results {
  91. let display = (result.alias.isEmpty) ? result.account : result.alias
  92. let account = Accounts(identifier: result.account, display: display)
  93. accounts.append(account)
  94. }
  95. completion(INObjectCollection(items: accounts), nil)
  96. }
  97. func defaultAccounts(for intent: DashboardIntent) -> Accounts? {
  98. if NCManageDatabase.shared.getActiveTableAccount() == nil {
  99. return nil
  100. } else {
  101. return Accounts(identifier: "active", display: "Active account")
  102. }
  103. }
  104. }