NCManageDatabase+LayoutForView.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // NCManageDatabase+LayoutForView.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 28/11/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. class NCDBLayoutForView: Object {
  27. @Persisted(primaryKey: true) var index = ""
  28. @Persisted var account = ""
  29. @Persisted var keyStore = ""
  30. @Persisted var layout: String = NCGlobal.shared.layoutList
  31. @Persisted var sort: String = "fileName"
  32. @Persisted var ascending: Bool = true
  33. @Persisted var groupBy: String = "none"
  34. @Persisted var directoryOnTop: Bool = true
  35. @Persisted var titleButtonHeader: String = "_sorted_by_name_a_z_"
  36. @Persisted var itemForLine: Int = 3
  37. @discardableResult
  38. func setLayoutForView(account: String, key: String, serverUrl: String, layout: String? = nil, sort: String? = nil, ascending: Bool? = nil, groupBy: String? = nil, directoryOnTop: Bool? = nil, titleButtonHeader: String? = nil, itemForLine: Int? = nil) -> NCDBLayoutForView? {
  39. let realm = try! Realm()
  40. var keyStore = key
  41. if !serverUrl.isEmpty { keyStore = serverUrl}
  42. let index = account + " " + keyStore
  43. var addObject = NCDBLayoutForView()
  44. do {
  45. try realm.write {
  46. if let result = realm.objects(NCDBLayoutForView.self).filter("index == %@", index).first {
  47. addObject = result
  48. } else {
  49. addObject.index = index
  50. }
  51. addObject.account = account
  52. addObject.keyStore = keyStore
  53. if let layout = layout {
  54. addObject.layout = layout
  55. }
  56. if let sort = sort {
  57. addObject.sort = sort
  58. }
  59. if let sort = sort {
  60. addObject.sort = sort
  61. }
  62. if let ascending = ascending {
  63. addObject.ascending = ascending
  64. }
  65. if let groupBy = groupBy {
  66. addObject.groupBy = groupBy
  67. }
  68. if let directoryOnTop = directoryOnTop {
  69. addObject.directoryOnTop = directoryOnTop
  70. }
  71. if let titleButtonHeader = titleButtonHeader {
  72. addObject.titleButtonHeader = titleButtonHeader
  73. }
  74. if let itemForLine = itemForLine {
  75. addObject.itemForLine = itemForLine
  76. }
  77. realm.add(addObject, update: .all)
  78. }
  79. } catch let error {
  80. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  81. }
  82. return NCDBLayoutForView.init(value: addObject)
  83. }
  84. @discardableResult
  85. func setLayoutForView(layoutForView: NCDBLayoutForView) -> NCDBLayoutForView? {
  86. let realm = try! Realm()
  87. let result = NCDBLayoutForView.init(value: layoutForView)
  88. do {
  89. try realm.write {
  90. realm.add(result, update: .all)
  91. }
  92. } catch let error {
  93. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  94. return nil
  95. }
  96. return NCDBLayoutForView.init(value: result)
  97. }
  98. func getLayoutForView(account: String, key: String, serverUrl: String) -> NCDBLayoutForView? {
  99. let realm = try! Realm()
  100. var keyStore = key
  101. if !serverUrl.isEmpty { keyStore = serverUrl}
  102. let index = account + " " + keyStore
  103. if let result = realm.objects(NCDBLayoutForView.self).filter("index == %@", index).first {
  104. return NCDBLayoutForView.init(value: result)
  105. } else {
  106. return setLayoutForView(account: account, key: key, serverUrl: serverUrl)
  107. }
  108. }
  109. }