NCManageDatabase+Avatar.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // NCManageDatabase+Avatar.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 20/01/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 tableAvatar: Object {
  28. @objc dynamic var date = NSDate()
  29. @objc dynamic var etag = ""
  30. @objc dynamic var fileName = ""
  31. @objc dynamic var loaded: Bool = false
  32. override static func primaryKey() -> String {
  33. return "fileName"
  34. }
  35. }
  36. extension NCManageDatabase {
  37. func addAvatar(fileName: String, etag: String) {
  38. do {
  39. let realm = try Realm()
  40. try realm.write {
  41. let addObject = tableAvatar()
  42. addObject.date = NSDate()
  43. addObject.etag = etag
  44. addObject.fileName = fileName
  45. addObject.loaded = true
  46. realm.add(addObject, update: .all)
  47. }
  48. } catch let error {
  49. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  50. }
  51. }
  52. func getTableAvatar(fileName: String) -> tableAvatar? {
  53. do {
  54. let realm = try Realm()
  55. realm.refresh()
  56. guard let result = realm.objects(tableAvatar.self).filter("fileName == %@", fileName).first else { return nil }
  57. return tableAvatar.init(value: result)
  58. } catch let error as NSError {
  59. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  60. }
  61. return nil
  62. }
  63. func clearAllAvatarLoaded() {
  64. do {
  65. let realm = try Realm()
  66. try realm.write {
  67. let results = realm.objects(tableAvatar.self)
  68. for result in results {
  69. result.loaded = false
  70. realm.add(result, update: .all)
  71. }
  72. }
  73. } catch let error {
  74. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  75. }
  76. }
  77. @discardableResult
  78. func setAvatarLoaded(fileName: String) -> UIImage? {
  79. let fileNameLocalPath = utilityFileSystem.directoryUserData + "/" + fileName
  80. var image: UIImage?
  81. do {
  82. let realm = try Realm()
  83. try realm.write {
  84. if let result = realm.objects(tableAvatar.self).filter("fileName == %@", fileName).first {
  85. if let imageAvatar = UIImage(contentsOfFile: fileNameLocalPath) {
  86. result.loaded = true
  87. image = imageAvatar
  88. } else {
  89. realm.delete(result)
  90. }
  91. }
  92. }
  93. } catch let error {
  94. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  95. }
  96. return image
  97. }
  98. func getImageAvatarLoaded(fileName: String) -> (image: UIImage?, tableAvatar: tableAvatar?) {
  99. let fileNameLocalPath = utilityFileSystem.directoryUserData + "/" + fileName
  100. let image = UIImage(contentsOfFile: fileNameLocalPath)
  101. do {
  102. let realm = try Realm()
  103. realm.refresh()
  104. let result = realm.objects(tableAvatar.self).filter("fileName == %@", fileName).first
  105. if result == nil {
  106. utilityFileSystem.removeFile(atPath: fileNameLocalPath)
  107. }
  108. return (image, result)
  109. } catch let error as NSError {
  110. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  111. }
  112. utilityFileSystem.removeFile(atPath: fileNameLocalPath)
  113. return (nil, nil)
  114. }
  115. }