NCManageDatabase+PhotoLibrary.swift 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // NCManageDatabase+PhotoLibrary.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/11/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 Photos
  26. import RealmSwift
  27. import NextcloudKit
  28. class tablePhotoLibrary: Object {
  29. @objc dynamic var account = ""
  30. @objc dynamic var assetLocalIdentifier = ""
  31. @objc dynamic var creationDate: NSDate?
  32. @objc dynamic var idAsset = ""
  33. @objc dynamic var modificationDate: NSDate?
  34. @objc dynamic var mediaType: Int = 0
  35. override static func primaryKey() -> String {
  36. return "idAsset"
  37. }
  38. }
  39. extension NCManageDatabase {
  40. @discardableResult
  41. func addPhotoLibrary(_ assets: [PHAsset], account: String) -> Bool {
  42. do {
  43. let realm = try Realm()
  44. try realm.write {
  45. for asset in assets {
  46. var creationDateString = ""
  47. let addObject = tablePhotoLibrary()
  48. addObject.account = account
  49. addObject.assetLocalIdentifier = asset.localIdentifier
  50. addObject.mediaType = asset.mediaType.rawValue
  51. if let creationDate = asset.creationDate {
  52. addObject.creationDate = creationDate as NSDate
  53. creationDateString = String(describing: creationDate)
  54. }
  55. if let modificationDate = asset.modificationDate {
  56. addObject.modificationDate = modificationDate as NSDate
  57. }
  58. addObject.idAsset = account + asset.localIdentifier + creationDateString
  59. realm.add(addObject, update: .all)
  60. }
  61. }
  62. } catch let error {
  63. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  64. return false
  65. }
  66. return true
  67. }
  68. func getPhotoLibraryIdAsset(image: Bool, video: Bool, account: String) -> [String]? {
  69. var predicate = NSPredicate()
  70. if image && video {
  71. predicate = NSPredicate(format: "account == %@ AND (mediaType == %d OR mediaType == %d)", account, PHAssetMediaType.image.rawValue, PHAssetMediaType.video.rawValue)
  72. } else if image {
  73. predicate = NSPredicate(format: "account == %@ AND mediaType == %d", account, PHAssetMediaType.image.rawValue)
  74. } else if video {
  75. predicate = NSPredicate(format: "account == %@ AND mediaType == %d", account, PHAssetMediaType.video.rawValue)
  76. }
  77. do {
  78. let realm = try Realm()
  79. realm.refresh()
  80. let results = realm.objects(tablePhotoLibrary.self).filter(predicate)
  81. let idsAsset = results.map { $0.idAsset }
  82. return Array(idsAsset)
  83. } catch let error as NSError {
  84. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access to database: \(error)")
  85. }
  86. return nil
  87. }
  88. }