NCManageDatabase+LayoutForView.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. }
  38. extension NCManageDatabase {
  39. @discardableResult
  40. 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? {
  41. var keyStore = key
  42. if !serverUrl.isEmpty { keyStore = serverUrl}
  43. let index = account + " " + keyStore
  44. var addObject = NCDBLayoutForView()
  45. do {
  46. let realm = try Realm()
  47. try realm.write {
  48. if let result = realm.objects(NCDBLayoutForView.self).filter("index == %@", index).first {
  49. addObject = result
  50. } else {
  51. addObject.index = index
  52. }
  53. addObject.account = account
  54. addObject.keyStore = keyStore
  55. if let layout = layout {
  56. addObject.layout = layout
  57. }
  58. if let sort = sort {
  59. addObject.sort = sort
  60. }
  61. if let sort = sort {
  62. addObject.sort = sort
  63. }
  64. if let ascending = ascending {
  65. addObject.ascending = ascending
  66. }
  67. if let groupBy = groupBy {
  68. addObject.groupBy = groupBy
  69. }
  70. if let directoryOnTop = directoryOnTop {
  71. addObject.directoryOnTop = directoryOnTop
  72. }
  73. if let titleButtonHeader = titleButtonHeader {
  74. addObject.titleButtonHeader = titleButtonHeader
  75. }
  76. if let itemForLine = itemForLine {
  77. addObject.itemForLine = itemForLine
  78. }
  79. realm.add(addObject, update: .all)
  80. }
  81. } catch let error {
  82. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  83. }
  84. return NCDBLayoutForView(value: addObject)
  85. }
  86. @discardableResult
  87. func setLayoutForView(layoutForView: NCDBLayoutForView) -> NCDBLayoutForView? {
  88. let result = NCDBLayoutForView(value: layoutForView)
  89. do {
  90. let realm = try Realm()
  91. try realm.write {
  92. realm.add(result, update: .all)
  93. }
  94. } catch let error {
  95. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  96. return nil
  97. }
  98. return NCDBLayoutForView(value: result)
  99. }
  100. func getLayoutForView(account: String, key: String, serverUrl: String) -> NCDBLayoutForView? {
  101. var keyStore = key
  102. if !serverUrl.isEmpty { keyStore = serverUrl}
  103. let index = account + " " + keyStore
  104. do {
  105. let realm = try Realm()
  106. if let result = realm.objects(NCDBLayoutForView.self).filter("index == %@", index).first {
  107. return NCDBLayoutForView(value: result)
  108. } else {
  109. return setLayoutForView(account: account, key: key, serverUrl: serverUrl)
  110. }
  111. } catch let error as NSError {
  112. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  113. }
  114. return setLayoutForView(account: account, key: key, serverUrl: serverUrl)
  115. }
  116. }