IntentHandler.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // Account
  28. func provideAccountsOptionsCollection(for intent: AccountIntent, with completion: @escaping (INObjectCollection<Accounts>?, Error?) -> Void) {
  29. var accounts: [Accounts] = []
  30. let results = NCManageDatabase.shared.getAllAccount()
  31. accounts.append(Accounts(identifier: "active", display: "Active account"))
  32. if results.isEmpty {
  33. return completion(nil, nil)
  34. } else if results.count == 1 {
  35. return completion(INObjectCollection(items: accounts), nil)
  36. }
  37. for result in results {
  38. let display = (result.alias.isEmpty) ? result.account : result.alias
  39. let account = Accounts(identifier: result.account, display: display)
  40. accounts.append(account)
  41. }
  42. completion(INObjectCollection(items: accounts), nil)
  43. }
  44. func defaultAccounts(for intent: AccountIntent) -> Accounts? {
  45. if NCManageDatabase.shared.getActiveAccount() == nil {
  46. return nil
  47. } else {
  48. return Accounts(identifier: "active", display: "Active account")
  49. }
  50. }
  51. // MARK: - Dashboard
  52. // Application
  53. func provideApplicationsOptionsCollection(for intent: DashboardIntent, with completion: @escaping (INObjectCollection<Applications>?, Error?) -> Void) {
  54. var applications: [Applications] = []
  55. var account: tableAccount?
  56. let accountIdentifier: String = intent.accounts?.identifier ?? "active"
  57. if accountIdentifier == "active" {
  58. account = NCManageDatabase.shared.getActiveAccount()
  59. } else {
  60. account = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", accountIdentifier))
  61. }
  62. guard let account = account else {
  63. return completion(nil, nil)
  64. }
  65. let results = NCManageDatabase.shared.getDashboardWidgetApplications(account: account.account)
  66. for result in results {
  67. let application = Applications(identifier: result.id, display: result.title)
  68. applications.append(application)
  69. }
  70. completion(INObjectCollection(items: applications), nil)
  71. }
  72. func defaultApplications(for intent: DashboardIntent) -> Applications? {
  73. guard let account = NCManageDatabase.shared.getActiveAccount() else {
  74. return nil
  75. }
  76. if let result = NCManageDatabase.shared.getDashboardWidgetApplications(account: account.account).first {
  77. return Applications(identifier: result.id, display: result.title)
  78. }
  79. return nil
  80. }
  81. // Account
  82. func provideAccountsOptionsCollection(for intent: DashboardIntent, with completion: @escaping (INObjectCollection<Accounts>?, Error?) -> Void) {
  83. var accounts: [Accounts] = []
  84. let results = NCManageDatabase.shared.getAllAccount()
  85. accounts.append(Accounts(identifier: "active", display: "Active account"))
  86. if results.isEmpty {
  87. return completion(nil, nil)
  88. } else if results.count == 1 {
  89. return completion(INObjectCollection(items: accounts), nil)
  90. }
  91. for result in results {
  92. let display = (result.alias.isEmpty) ? result.account : result.alias
  93. let account = Accounts(identifier: result.account, display: display)
  94. accounts.append(account)
  95. }
  96. completion(INObjectCollection(items: accounts), nil)
  97. }
  98. func defaultAccounts(for intent: DashboardIntent) -> Accounts? {
  99. if NCManageDatabase.shared.getActiveAccount() == nil {
  100. return nil
  101. } else {
  102. return Accounts(identifier: "active", display: "Active account")
  103. }
  104. }
  105. }