NCManageDatabase+LocalFile.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // NCManageDatabase+LocalFile.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 01/08/23.
  6. // Copyright © 2023 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 tableLocalFile: Object {
  27. @objc dynamic var account = ""
  28. @objc dynamic var etag = ""
  29. @objc dynamic var exifDate: NSDate?
  30. @objc dynamic var exifLatitude = ""
  31. @objc dynamic var exifLongitude = ""
  32. @objc dynamic var exifLensModel: String?
  33. @objc dynamic var favorite: Bool = false
  34. @objc dynamic var fileName = ""
  35. @objc dynamic var ocId = ""
  36. @objc dynamic var offline: Bool = false
  37. @objc dynamic var lastOpeningDate = NSDate()
  38. override static func primaryKey() -> String {
  39. return "ocId"
  40. }
  41. }
  42. extension NCManageDatabase {
  43. // MARK: -
  44. // MARK: Table LocalFile - return RESULT
  45. func getTableLocalFile(ocId: String) -> tableLocalFile? {
  46. do {
  47. let realm = try Realm()
  48. return realm.objects(tableLocalFile.self).filter("ocId == %@", ocId).first
  49. } catch let error as NSError {
  50. NextcloudKit.shared.nkCommonInstance.writeLog("Could not access database: \(error)")
  51. }
  52. return nil
  53. }
  54. // MARK: -
  55. // MARK: Table LocalFile
  56. func addLocalFile(metadata: tableMetadata) {
  57. do {
  58. let realm = try Realm()
  59. try realm.write {
  60. let addObject = getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) ?? tableLocalFile()
  61. addObject.account = metadata.account
  62. addObject.etag = metadata.etag
  63. addObject.exifDate = NSDate()
  64. addObject.exifLatitude = "-1"
  65. addObject.exifLongitude = "-1"
  66. addObject.ocId = metadata.ocId
  67. addObject.fileName = metadata.fileName
  68. realm.add(addObject, update: .all)
  69. }
  70. } catch let error {
  71. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  72. }
  73. }
  74. func addLocalFile(account: String, etag: String, ocId: String, fileName: String) {
  75. do {
  76. let realm = try Realm()
  77. try realm.write {
  78. let addObject = tableLocalFile()
  79. addObject.account = account
  80. addObject.etag = etag
  81. addObject.exifDate = NSDate()
  82. addObject.exifLatitude = "-1"
  83. addObject.exifLongitude = "-1"
  84. addObject.ocId = ocId
  85. addObject.fileName = fileName
  86. realm.add(addObject, update: .all)
  87. }
  88. } catch let error {
  89. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  90. }
  91. }
  92. func deleteLocalFile(predicate: NSPredicate) {
  93. do {
  94. let realm = try Realm()
  95. try realm.write {
  96. let results = realm.objects(tableLocalFile.self).filter(predicate)
  97. realm.delete(results)
  98. }
  99. } catch let error {
  100. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  101. }
  102. }
  103. func setLocalFile(ocId: String, fileName: String?, etag: String?) {
  104. do {
  105. let realm = try Realm()
  106. try realm.write {
  107. let result = realm.objects(tableLocalFile.self).filter("ocId == %@", ocId).first
  108. if let fileName = fileName {
  109. result?.fileName = fileName
  110. }
  111. if let etag = etag {
  112. result?.etag = etag
  113. }
  114. }
  115. } catch let error {
  116. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  117. }
  118. }
  119. @objc func setLocalFile(ocId: String, exifDate: NSDate?, exifLatitude: String, exifLongitude: String, exifLensModel: String?) {
  120. do {
  121. let realm = try Realm()
  122. try realm.write {
  123. if let result = realm.objects(tableLocalFile.self).filter("ocId == %@", ocId).first {
  124. result.exifDate = exifDate
  125. result.exifLatitude = exifLatitude
  126. result.exifLongitude = exifLongitude
  127. if exifLensModel?.count ?? 0 > 0 {
  128. result.exifLensModel = exifLensModel
  129. }
  130. }
  131. }
  132. } catch let error {
  133. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  134. }
  135. }
  136. func setLocalFile(ocId: String, offline: Bool) {
  137. do {
  138. let realm = try Realm()
  139. try realm.write {
  140. let result = realm.objects(tableLocalFile.self).filter("ocId == %@", ocId).first
  141. result?.offline = offline
  142. }
  143. } catch let error {
  144. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  145. }
  146. }
  147. func getTableLocalFile(account: String) -> [tableLocalFile] {
  148. do {
  149. let realm = try Realm()
  150. let results = realm.objects(tableLocalFile.self).filter("account == %@", account)
  151. return Array(results.map { tableLocalFile.init(value: $0) })
  152. } catch let error as NSError {
  153. NextcloudKit.shared.nkCommonInstance.writeLog("Could not access database: \(error)")
  154. }
  155. return []
  156. }
  157. func getTableLocalFile(predicate: NSPredicate) -> tableLocalFile? {
  158. do {
  159. let realm = try Realm()
  160. guard let result = realm.objects(tableLocalFile.self).filter(predicate).first else { return nil }
  161. return tableLocalFile.init(value: result)
  162. } catch let error as NSError {
  163. NextcloudKit.shared.nkCommonInstance.writeLog("Could not access database: \(error)")
  164. }
  165. return nil
  166. }
  167. func getResultsTableLocalFile(predicate: NSPredicate) -> Results<tableLocalFile>? {
  168. do {
  169. let realm = try Realm()
  170. return realm.objects(tableLocalFile.self).filter(predicate)
  171. } catch let error as NSError {
  172. NextcloudKit.shared.nkCommonInstance.writeLog("Could not access database: \(error)")
  173. }
  174. return nil
  175. }
  176. func getTableLocalFiles(predicate: NSPredicate, sorted: String, ascending: Bool) -> [tableLocalFile] {
  177. do {
  178. let realm = try Realm()
  179. let results = realm.objects(tableLocalFile.self).filter(predicate).sorted(byKeyPath: sorted, ascending: ascending)
  180. return Array(results.map { tableLocalFile.init(value: $0) })
  181. } catch let error as NSError {
  182. NextcloudKit.shared.nkCommonInstance.writeLog("Could not access database: \(error)")
  183. }
  184. return []
  185. }
  186. func getResultsTableLocalFile(predicate: NSPredicate, sorted: String, ascending: Bool) -> Results<tableLocalFile>? {
  187. do {
  188. let realm = try Realm()
  189. return realm.objects(tableLocalFile.self).filter(predicate).sorted(byKeyPath: sorted, ascending: ascending)
  190. } catch let error as NSError {
  191. NextcloudKit.shared.nkCommonInstance.writeLog("Could not access database: \(error)")
  192. }
  193. return nil
  194. }
  195. func setLastOpeningDate(metadata: tableMetadata) {
  196. do {
  197. let realm = try Realm()
  198. try realm.write {
  199. if let result = realm.objects(tableLocalFile.self).filter("ocId == %@", metadata.ocId).first {
  200. result.lastOpeningDate = NSDate()
  201. } else {
  202. let addObject = tableLocalFile()
  203. addObject.account = metadata.account
  204. addObject.etag = metadata.etag
  205. addObject.exifDate = NSDate()
  206. addObject.exifLatitude = "-1"
  207. addObject.exifLongitude = "-1"
  208. addObject.ocId = metadata.ocId
  209. addObject.fileName = metadata.fileName
  210. realm.add(addObject, update: .all)
  211. }
  212. }
  213. } catch let error {
  214. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  215. }
  216. }
  217. }