Skip to content

Instantly share code, notes, and snippets.

@manishkkatoch
Created December 6, 2017 17:42
Show Gist options
  • Save manishkkatoch/1702cb6019a2a025f832d9f74c4c55f1 to your computer and use it in GitHub Desktop.
Save manishkkatoch/1702cb6019a2a025f832d9f74c4c55f1 to your computer and use it in GitHub Desktop.
Mocking CLLocationManager
//
// MockCLLocationManager.swift
//
// Created by Manish Katoch on 11/28/17.
// Copyright © 2017 Manish Katoch. All rights reserved.
//
import Foundation
import CoreLocation
struct MockLocationConfiguration {
static var updateInterval = 0.5
static var GpxFileName: String?
}
class MockCLLocationManager: CLLocationManager {
private var parser: GpxParser?
private var timer: Timer?
private var locations: Queue<CLLocation>?
private var _isRunning:Bool = false
var updateInterval: TimeInterval = 0.5
var isRunning: Bool {
get {
return _isRunning
}
}
static let shared = MockCLLocationManager()
private override init() {
locations = Queue<CLLocation>()
}
func startMocks(usingGpx fileName: String) {
if let fileName = MockLocationConfiguration.GpxFileName {
parser = GpxParser(forResource: fileName, ofType: "gpx")
parser?.delegate = self
parser?.parse()
}
}
func stopMocking() {
self.stopUpdatingLocation()
}
private func updateLocation() {
if let location = locations?.dequeue() {
_isRunning = true
delegate?.locationManager?(self, didUpdateLocations: [location])
if let isEmpty = locations?.isEmpty(), isEmpty {
print("stopping at: \(location.coordinate)")
stopUpdatingLocation()
}
}
}
override func startUpdatingLocation() {
timer = Timer(timeInterval: updateInterval, repeats: true, block: {
[unowned self](_) in
self.updateLocation()
})
if let timer = timer {
RunLoop.main.add(timer, forMode: .defaultRunLoopMode)
}
}
override func stopUpdatingLocation() {
timer?.invalidate()
_isRunning = false
}
override func requestLocation() {
if let location = locations?.peek() {
delegate?.locationManager?(self, didUpdateLocations: [location])
}
}
}
extension MockCLLocationManager: GpxParsing {
func parser(_ parser: GpxParser, didCompleteParsing locations: Queue<CLLocation>) {
self.locations = locations
self.startUpdatingLocation()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment