NCManageDatabase+LayoutForView.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 UIKit
  25. import RealmSwift
  26. import NextcloudKit
  27. class NCDBLayoutForView: Object {
  28. @Persisted(primaryKey: true) var index = ""
  29. @Persisted var account = ""
  30. @Persisted var keyStore = ""
  31. @Persisted var layout: String = NCGlobal.shared.layoutList
  32. @Persisted var sort: String = "fileName"
  33. @Persisted var ascending: Bool = true
  34. @Persisted var groupBy: String = "none"
  35. @Persisted var directoryOnTop: Bool = true
  36. @Persisted var titleButtonHeader: String = "_sorted_by_name_a_z_"
  37. @Persisted var columnGrid: Int = 3
  38. @Persisted var columnPhoto: Int = 3
  39. }
  40. extension NCManageDatabase {
  41. @discardableResult
  42. func setLayoutForView(account: String,
  43. key: String,
  44. serverUrl: String,
  45. layout: String? = nil,
  46. sort: String? = nil,
  47. ascending: Bool? = nil,
  48. groupBy: String? = nil,
  49. directoryOnTop: Bool? = nil,
  50. titleButtonHeader: String? = nil,
  51. columnGrid: Int? = nil,
  52. columnPhoto: Int? = nil) -> NCDBLayoutForView? {
  53. var keyStore = key
  54. if !serverUrl.isEmpty { keyStore = serverUrl}
  55. let index = account + " " + keyStore
  56. var addObject = NCDBLayoutForView()
  57. do {
  58. let realm = try Realm()
  59. try realm.write {
  60. if let result = realm.objects(NCDBLayoutForView.self).filter("index == %@", index).first {
  61. addObject = result
  62. } else {
  63. addObject.index = index
  64. }
  65. addObject.account = account
  66. addObject.keyStore = keyStore
  67. if let layout {
  68. addObject.layout = layout
  69. }
  70. if let sort {
  71. addObject.sort = sort
  72. }
  73. if let sort {
  74. addObject.sort = sort
  75. }
  76. if let ascending {
  77. addObject.ascending = ascending
  78. }
  79. if let groupBy {
  80. addObject.groupBy = groupBy
  81. }
  82. if let directoryOnTop {
  83. addObject.directoryOnTop = directoryOnTop
  84. }
  85. if let titleButtonHeader {
  86. addObject.titleButtonHeader = titleButtonHeader
  87. }
  88. if let columnGrid {
  89. addObject.columnGrid = columnGrid
  90. }
  91. if let columnPhoto {
  92. addObject.columnPhoto = columnPhoto
  93. }
  94. realm.add(addObject, update: .all)
  95. }
  96. } catch let error {
  97. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  98. }
  99. return NCDBLayoutForView(value: addObject)
  100. }
  101. @discardableResult
  102. func setLayoutForView(layoutForView: NCDBLayoutForView) -> NCDBLayoutForView? {
  103. let result = NCDBLayoutForView(value: layoutForView)
  104. do {
  105. let realm = try Realm()
  106. try realm.write {
  107. realm.add(result, update: .all)
  108. }
  109. } catch let error {
  110. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  111. return nil
  112. }
  113. return NCDBLayoutForView(value: result)
  114. }
  115. func getLayoutForView(account: String, key: String, serverUrl: String) -> NCDBLayoutForView? {
  116. var keyStore = key
  117. if !serverUrl.isEmpty { keyStore = serverUrl}
  118. let index = account + " " + keyStore
  119. do {
  120. let realm = try Realm()
  121. realm.refresh()
  122. if let result = realm.objects(NCDBLayoutForView.self).filter("index == %@", index).first {
  123. return NCDBLayoutForView(value: result)
  124. } else {
  125. return setLayoutForView(account: account, key: key, serverUrl: serverUrl)
  126. }
  127. } catch let error as NSError {
  128. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  129. }
  130. return setLayoutForView(account: account, key: key, serverUrl: serverUrl)
  131. }
  132. }