NCAutoUpload.swift 3.4 KB

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