NCManageDatabase+DashboardWidget.swift 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 RealmSwift
  25. import NextcloudKit
  26. extension NCManageDatabase {
  27. func getDashboardWidget(account: String, id: String) -> (tableDashboardWidget?, Results<tableDashboardWidgetButton>?) {
  28. let realm = try! Realm()
  29. guard let resultDashboard = realm.objects(tableDashboardWidget.self).filter("account == %@ AND id == %@", account, id).first else {
  30. return (nil, nil)
  31. }
  32. let resultsButton = realm.objects(tableDashboardWidgetButton.self).filter("account == %@ AND id == %@", account, id).sorted(byKeyPath: "type", ascending: true)
  33. return (resultDashboard, resultsButton)
  34. }
  35. func addDashboardWidget(account: String, dashboardWidgets: [NCCDashboardWidget]) {
  36. let realm = try! Realm()
  37. do {
  38. try realm.safeWrite {
  39. let resultDashboard = realm.objects(tableDashboardWidget.self).filter("account == %@", account)
  40. realm.delete(resultDashboard)
  41. let resultDashboardButton = realm.objects(tableDashboardWidgetButton.self).filter("account == %@", account)
  42. realm.delete(resultDashboardButton)
  43. for widget in dashboardWidgets {
  44. let addObject = tableDashboardWidget()
  45. addObject.index = account + " " + widget.id
  46. addObject.account = account
  47. addObject.id = widget.id
  48. addObject.title = widget.title
  49. addObject.order = widget.order
  50. addObject.iconClass = widget.iconClass
  51. addObject.iconUrl = widget.iconUrl
  52. addObject.widgetUrl = widget.widgetUrl
  53. addObject.itemIconsRound = widget.itemIconsRound
  54. if let buttons = widget.button {
  55. for button in buttons {
  56. let addObject = tableDashboardWidgetButton()
  57. addObject.account = account
  58. addObject.id = widget.id
  59. addObject.type = button.type
  60. addObject.text = button.text
  61. addObject.link = button.link
  62. addObject.index = account + " " + widget.id + " " + button.type
  63. realm.add(addObject, update: .all)
  64. }
  65. }
  66. realm.add(addObject, update: .all)
  67. }
  68. }
  69. } catch let error {
  70. NKCommon.shared.writeLog("Could not write to database: \(error)")
  71. }
  72. }
  73. }