NCManageDatabase+DashboardWidget.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // NCManageDatabase+DashboardWidget.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 09/09/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 Foundation
  24. import UIKit
  25. import RealmSwift
  26. import NextcloudKit
  27. class tableDashboardWidget: Object {
  28. @Persisted(primaryKey: true) var index = ""
  29. @Persisted var account = ""
  30. @Persisted var id = ""
  31. @Persisted var title = ""
  32. @Persisted var order: Int = 0
  33. @Persisted var iconClass: String?
  34. @Persisted var iconUrl: String?
  35. @Persisted var widgetUrl: String?
  36. @Persisted var itemIconsRound: Bool = false
  37. }
  38. class tableDashboardWidgetButton: Object {
  39. @Persisted(primaryKey: true) var index = ""
  40. @Persisted var account = ""
  41. @Persisted var id = ""
  42. @Persisted var type = ""
  43. @Persisted var text = ""
  44. @Persisted var link = ""
  45. }
  46. extension NCManageDatabase {
  47. func getDashboardWidget(account: String, id: String) -> (tableDashboardWidget?, [tableDashboardWidgetButton]?) {
  48. do {
  49. let realm = try Realm()
  50. realm.refresh()
  51. guard let resultDashboard = realm.objects(tableDashboardWidget.self).filter("account == %@ AND id == %@", account, id).first else { return (nil, nil) }
  52. let resultsButton = realm.objects(tableDashboardWidgetButton.self).filter("account == %@ AND id == %@", account, id).sorted(byKeyPath: "type", ascending: true)
  53. return (tableDashboardWidget.init(value: resultDashboard), Array(resultsButton.map { tableDashboardWidgetButton.init(value: $0) }))
  54. } catch let error as NSError {
  55. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  56. }
  57. return (nil, nil)
  58. }
  59. func getDashboardWidgetApplications(account: String) -> [tableDashboardWidget] {
  60. do {
  61. let realm = try Realm()
  62. realm.refresh()
  63. let sortProperties = [SortDescriptor(keyPath: "order", ascending: true), SortDescriptor(keyPath: "title", ascending: true)]
  64. let results = realm.objects(tableDashboardWidget.self).filter("account == %@", account).sorted(by: sortProperties)
  65. return Array(results.map { tableDashboardWidget.init(value: $0) })
  66. } catch let error as NSError {
  67. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  68. }
  69. return []
  70. }
  71. func addDashboardWidget(account: String, dashboardWidgets: [NCCDashboardWidget]) {
  72. do {
  73. let realm = try Realm()
  74. try realm.write {
  75. let resultDashboard = realm.objects(tableDashboardWidget.self).filter("account == %@", account)
  76. realm.delete(resultDashboard)
  77. let resultDashboardButton = realm.objects(tableDashboardWidgetButton.self).filter("account == %@", account)
  78. realm.delete(resultDashboardButton)
  79. for widget in dashboardWidgets {
  80. let addObject = tableDashboardWidget()
  81. addObject.index = account + " " + widget.id
  82. addObject.account = account
  83. addObject.id = widget.id
  84. addObject.title = widget.title
  85. addObject.order = widget.order
  86. addObject.iconClass = widget.iconClass
  87. addObject.iconUrl = widget.iconUrl
  88. addObject.widgetUrl = widget.widgetUrl
  89. addObject.itemIconsRound = widget.itemIconsRound
  90. if let buttons = widget.button {
  91. for button in buttons {
  92. let addObject = tableDashboardWidgetButton()
  93. addObject.account = account
  94. addObject.id = widget.id
  95. addObject.type = button.type
  96. addObject.text = button.text
  97. addObject.link = button.link
  98. addObject.index = account + " " + widget.id + " " + button.type
  99. realm.add(addObject, update: .all)
  100. }
  101. }
  102. realm.add(addObject, update: .all)
  103. }
  104. }
  105. } catch let error {
  106. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  107. }
  108. }
  109. }