NCAutoUpload.swift 4.0 KB

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