NCAutoUpload.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. public var locationManager: CLLocationManager?
  32. // MARK: -
  33. @objc public func startSignificantChangeUpdates() {
  34. if locationManager == nil {
  35. locationManager = CLLocationManager.init()
  36. locationManager?.delegate = self
  37. locationManager?.requestAlwaysAuthorization()
  38. }
  39. locationManager?.startMonitoringSignificantLocationChanges()
  40. }
  41. @objc public func stopSignificantChangeUpdates() {
  42. locationManager?.stopMonitoringSignificantLocationChanges()
  43. }
  44. func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  45. let location = locations.last
  46. let latitude = String(describing: location?.coordinate.latitude)
  47. let longitude = String(describing: location?.coordinate.longitude)
  48. NCCommunicationCommon.shared.writeLog("update location manager: latitude " + latitude + ", longitude " + longitude)
  49. changeLocation()
  50. }
  51. func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
  52. statusAuthorizationLocationChanged()
  53. }
  54. func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
  55. statusAuthorizationLocationChanged()
  56. }
  57. func changeLocation() {
  58. if let account = NCManageDatabase.shared.getAccountActive() {
  59. if account.autoUpload && account.autoUploadBackground && UIApplication.shared.applicationState == UIApplication.State.background {
  60. }
  61. }
  62. }
  63. func statusAuthorizationLocationChanged() {
  64. }
  65. // MARK: -
  66. @objc func initStateAutoUpload(viewController: UIViewController?) {
  67. if let account = NCManageDatabase.shared.getAccountActive() {
  68. if account.autoUpload {
  69. setupAutoUpload()
  70. if account.autoUploadBackground {
  71. NCAskAuthorization.shared.askAuthorizationLocationManager(viewController: viewController) { (hasPermissions) in
  72. if hasPermissions {
  73. self.startSignificantChangeUpdates()
  74. }
  75. }
  76. }
  77. }
  78. } else {
  79. stopSignificantChangeUpdates()
  80. }
  81. }
  82. @objc func setupAutoUpload() {
  83. }
  84. @objc func setupAutoUploadFull() {
  85. }
  86. @objc func alignPhotoLibrary() {
  87. }
  88. }