NCManageDatabase+DashboardWidget.swift 3.2 KB

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