NCAutoUpload.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // NCAutoUpload.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 27/01/21.
  6. // Copyright © 2021 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 CoreLocation
  25. import NCCommunication
  26. class NCAutoUpload: NSObject, CLLocationManagerDelegate {
  27. @objc static let shared: NCAutoUpload = {
  28. let instance = NCAutoUpload()
  29. return instance
  30. }()
  31. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  32. public var locationManager: CLLocationManager?
  33. // MARK: -
  34. @objc public func startSignificantChangeUpdates() {
  35. if locationManager == nil {
  36. locationManager = CLLocationManager.init()
  37. locationManager?.delegate = self
  38. locationManager?.requestAlwaysAuthorization()
  39. }
  40. locationManager?.startMonitoringSignificantLocationChanges()
  41. }
  42. @objc public func stopSignificantChangeUpdates() {
  43. locationManager?.stopMonitoringSignificantLocationChanges()
  44. }
  45. func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  46. let location = locations.last
  47. let latitude = String(describing: location?.coordinate.latitude)
  48. let longitude = String(describing: location?.coordinate.longitude)
  49. NCCommunicationCommon.shared.writeLog("update location manager: latitude " + latitude + ", longitude " + longitude)
  50. if let account = NCManageDatabase.shared.getAccountActive() {
  51. if account.autoUpload && account.autoUploadBackground && UIApplication.shared.applicationState == UIApplication.State.background {
  52. self.uploadNewAssets()
  53. }
  54. }
  55. }
  56. func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
  57. statusAuthorizationLocationChanged()
  58. }
  59. func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
  60. statusAuthorizationLocationChanged()
  61. }
  62. func statusAuthorizationLocationChanged() {
  63. NCManageDatabase.shared.setAccountAutoUploadProperty("autoUploadBackground", state: false)
  64. self.stopSignificantChangeUpdates()
  65. }
  66. // MARK: -
  67. @objc func initStateAutoUpload(viewController: UIViewController?) {
  68. if let account = NCManageDatabase.shared.getAccountActive() {
  69. if account.autoUpload {
  70. setupAutoUpload()
  71. if account.autoUploadBackground {
  72. NCAskAuthorization.shared.askAuthorizationLocationManager(viewController: appDelegate.window.rootViewController) { (hasPermissions) in
  73. if hasPermissions {
  74. self.startSignificantChangeUpdates()
  75. } else {
  76. NCManageDatabase.shared.setAccountAutoUploadProperty("autoUploadBackground", state: false)
  77. self.stopSignificantChangeUpdates()
  78. }
  79. }
  80. }
  81. }
  82. } else {
  83. stopSignificantChangeUpdates()
  84. }
  85. }
  86. @objc func setupAutoUpload() {
  87. NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: appDelegate.window.rootViewController) { (hasPermission) in
  88. if hasPermission {
  89. self.uploadNewAssets()
  90. } else {
  91. NCManageDatabase.shared.setAccountAutoUploadProperty("autoUpload", state: false)
  92. self.stopSignificantChangeUpdates()
  93. }
  94. }
  95. }
  96. @objc func uploadNewAssets() {
  97. }
  98. @objc func setupAutoUploadFull() {
  99. }
  100. @objc func alignPhotoLibrary() {
  101. if let account = NCManageDatabase.shared.getAccountActive() {
  102. getCameraRollAssets(account: account, selector: NCBrandGlobal.shared.selectorUploadAutoUploadAll, alignPhotoLibrary: true) { (assets) in
  103. NCManageDatabase.shared.clearTable(tablePhotoLibrary.self, account: account.account)
  104. if let assets = assets {
  105. NCManageDatabase.shared.addPhotoLibrary(assets, account: account.account)
  106. NCCommunicationCommon.shared.writeLog("Align Photo Library \(assets.count)")
  107. }
  108. }
  109. }
  110. }
  111. func getCameraRollAssets(account: tableAccount, selector: String, alignPhotoLibrary: Bool, completion: @escaping (_ assets: [PHAsset]?)->()) {
  112. NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: appDelegate.window.rootViewController) { (hasPermission) in
  113. if hasPermission {
  114. let assetCollection = PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.smartAlbum, subtype: PHAssetCollectionSubtype.smartAlbumUserLibrary, options: nil)
  115. if assetCollection.count > 0 {
  116. let predicateImage = NSPredicate(format: "mediaType == %i", PHAssetMediaType.image as! CVarArg)
  117. let predicateVideo = NSPredicate(format: "mediaType == %i", PHAssetMediaType.video as! CVarArg)
  118. var predicate: NSPredicate?
  119. let fetchOptions = PHFetchOptions()
  120. if alignPhotoLibrary || (account.autoUploadImage && account.autoUploadVideo) {
  121. predicate = NSCompoundPredicate(orPredicateWithSubpredicates: [predicateImage, predicateVideo])
  122. } else if account.autoUploadImage {
  123. predicate = predicateImage
  124. } else if account.autoUploadVideo {
  125. predicate = predicateVideo
  126. }
  127. fetchOptions.predicate = predicate
  128. let assets: PHFetchResult<PHAsset> = PHAsset.fetchAssets(in: assetCollection.firstObject!, options: fetchOptions)
  129. if selector == NCBrandGlobal.shared.selectorUploadAutoUpload {
  130. var newAssets: [PHAsset] = []
  131. var creationDate = ""
  132. var idAsset = ""
  133. let idsAsset = NCManageDatabase.shared.getPhotoLibraryIdAsset(image: account.autoUploadImage, video: account.autoUploadVideo, account: account.account)
  134. assets.enumerateObjects { (asset, count, stop) in
  135. if asset.creationDate != nil { creationDate = String(describing: asset.creationDate) }
  136. idAsset = account.account + asset.localIdentifier + creationDate
  137. if !(idsAsset?.contains(idAsset) ?? false) {
  138. newAssets.append(asset)
  139. }
  140. completion(newAssets)
  141. }
  142. } else {
  143. completion(assets.copy() as? [PHAsset])
  144. }
  145. }
  146. }
  147. completion(nil)
  148. }
  149. }
  150. }