NCAutoUpload.swift 3.0 KB

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