NCManageDatabase+Avatar.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 RealmSwift
  25. import NextcloudKit
  26. class tableAvatar: Object {
  27. @objc dynamic var date = NSDate()
  28. @objc dynamic var etag = ""
  29. @objc dynamic var fileName = ""
  30. @objc dynamic var loaded: Bool = false
  31. override static func primaryKey() -> String {
  32. return "fileName"
  33. }
  34. }
  35. extension NCManageDatabase {
  36. func addAvatar(fileName: String, etag: String) {
  37. let realm = try! Realm()
  38. do {
  39. try realm.write {
  40. // Add new
  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("Could not write to database: \(error)")
  50. }
  51. }
  52. func getTableAvatar(fileName: String) -> tableAvatar? {
  53. let realm = try! Realm()
  54. guard let result = realm.objects(tableAvatar.self).filter("fileName == %@", fileName).first else {
  55. return nil
  56. }
  57. return tableAvatar.init(value: result)
  58. }
  59. func clearAllAvatarLoaded() {
  60. let realm = try! Realm()
  61. do {
  62. try realm.write {
  63. let results = realm.objects(tableAvatar.self)
  64. for result in results {
  65. result.loaded = false
  66. realm.add(result, update: .all)
  67. }
  68. }
  69. } catch let error {
  70. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  71. }
  72. }
  73. @discardableResult
  74. func setAvatarLoaded(fileName: String) -> UIImage? {
  75. let realm = try! Realm()
  76. let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
  77. var image: UIImage?
  78. do {
  79. try realm.write {
  80. if let result = realm.objects(tableAvatar.self).filter("fileName == %@", fileName).first {
  81. if let imageAvatar = UIImage(contentsOfFile: fileNameLocalPath) {
  82. result.loaded = true
  83. image = imageAvatar
  84. } else {
  85. realm.delete(result)
  86. }
  87. }
  88. }
  89. } catch let error {
  90. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  91. }
  92. return image
  93. }
  94. func getImageAvatarLoaded(fileName: String) -> UIImage? {
  95. let realm = try! Realm()
  96. let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
  97. let result = realm.objects(tableAvatar.self).filter("fileName == %@", fileName).first
  98. if result == nil {
  99. NCUtilityFileSystem.shared.deleteFile(filePath: fileNameLocalPath)
  100. return nil
  101. } else if result?.loaded == false {
  102. return nil
  103. }
  104. return UIImage(contentsOfFile: fileNameLocalPath)
  105. }
  106. }