NCManageDatabase+Avatar.swift 3.9 KB

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