NCManageDatabase+LocalFile.swift 9.0 KB

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