123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- import Foundation
- import CoreLocation
- import NCCommunication
- class NCAutoUpload: NSObject {
- @objc static let shared: NCAutoUpload = {
- let instance = NCAutoUpload()
- return instance
- }()
-
- func initStateAutoUpload(viewController: UIViewController?) {
-
- if let account = NCManageDatabase.shared.getAccountActive() {
- if account.autoUpload {
-
-
- if account.autoUploadBackground {
- NCAskAuthorization.shared.askAuthorizationLocationManager(viewController: viewController) { (hasPermissions) in
- if hasPermissions {
- NCManageLocation.shared.startSignificantChangeUpdates()
- }
- }
- }
- }
- } else {
- NCManageLocation.shared.stopSignificantChangeUpdates()
- }
- }
-
- @objc func changeLocation() {
-
- if let account = NCManageDatabase.shared.getAccountActive() {
- if account.autoUpload && account.autoUploadBackground && UIApplication.shared.applicationState == UIApplication.State.background {
- }
- }
- }
- }
- class NCManageLocation: NSObject, CLLocationManagerDelegate {
- @objc static let shared: NCManageLocation = {
- let instance = NCManageLocation()
- return instance
- }()
-
- public var locationManager: CLLocationManager?
- @objc public var firstChangeAuthorizationDone: Bool = false
-
- @objc public func startSignificantChangeUpdates() {
-
- if locationManager == nil {
-
- locationManager = CLLocationManager.init()
- locationManager?.delegate = self
- locationManager?.requestAlwaysAuthorization()
- }
-
- locationManager?.startMonitoringSignificantLocationChanges()
- }
-
- @objc public func stopSignificantChangeUpdates() {
-
- locationManager?.stopMonitoringSignificantLocationChanges()
- }
-
- func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
-
- let location = locations.last
- let latitude = String(describing: location?.coordinate.latitude)
- let longitude = String(describing: location?.coordinate.longitude)
-
- NCCommunicationCommon.shared.writeLog("update location manager: latitude " + latitude + ", longitude " + longitude)
-
- NotificationCenter.default.postOnMainThread(name: NCBrandGlobal.shared.notificationCenterChangedLocation)
- }
-
- func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
-
- NotificationCenter.default.postOnMainThread(name: NCBrandGlobal.shared.notificationStatusAuthorizationChangedLocation)
- }
-
- func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
-
- NotificationCenter.default.postOnMainThread(name: NCBrandGlobal.shared.notificationStatusAuthorizationChangedLocation)
- }
- }
|