Skip to content

Commit

Permalink
Merge pull request #2 from asowers1/master
Browse files Browse the repository at this point in the history
Adding support for setting custom RegionConfig
  • Loading branch information
yoman07 authored May 5, 2017
2 parents 9081c2d + ea793a6 commit c700925
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 105 deletions.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PODS:
- Nimble (5.1.1)
- Quick (1.0.0)
- SwiftBackgroundLocation (0.1.0)
- SwiftBackgroundLocation (0.1.1)

DEPENDENCIES:
- Nimble (~> 5.1.1)
Expand All @@ -15,7 +15,7 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
Nimble: 415e3aa3267e7bc2c96b05fa814ddea7bb686a29
Quick: 8024e4a47e6cc03a9d5245ef0948264fc6d27cff
SwiftBackgroundLocation: d19b816dad0f3ae7274a3150421f5e7eb2b257ed
SwiftBackgroundLocation: 2ab1b335c3eaffc70cfc7505bfaf84e5ed106e5d

PODFILE CHECKSUM: 8eff8db1401e1f58c77c892442a33f02662ede4d

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

144 changes: 74 additions & 70 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Example/SwiftBackgroundLocation/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var locationManager = TrackingHeadingLocationManager()
var backgroundLocationManager = BackgroundLocationManager()
var backgroundLocationManager = BackgroundLocationManager(regionConfig: RegionConfig(regionRadius: 100.0))

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

Expand Down
30 changes: 18 additions & 12 deletions Example/SwiftBackgroundLocation/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16A322" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16F60a" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand All @@ -23,41 +24,36 @@
<mapView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" mapType="standard" showsUserLocation="YES" translatesAutoresizingMaskIntoConstraints="NO" id="pb1-Re-U9G">
<rect key="frame" x="0.0" y="20" width="375" height="647"/>
</mapView>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="zpX-Rm-XPu">
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="zpX-Rm-XPu">
<rect key="frame" x="22" y="617" width="34" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Start"/>
<connections>
<action selector="start:" destination="BYZ-38-t0r" eventType="touchUpInside" id="ug5-1o-5VA"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="E6e-16-Nsx">
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="E6e-16-Nsx">
<rect key="frame" x="83" y="617" width="32" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Stop"/>
<connections>
<action selector="stop:" destination="BYZ-38-t0r" eventType="touchUpInside" id="A1Y-Pb-cue"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="SG2-q4-LSa">
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="SG2-q4-LSa">
<rect key="frame" x="127" y="617" width="70" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Clear map"/>
<connections>
<action selector="clear:" destination="BYZ-38-t0r" eventType="touchUpInside" id="rS6-RH-knv"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Mgf-R7-DSd">
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Mgf-R7-DSd">
<rect key="frame" x="289" y="617" width="61" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Clear log"/>
<connections>
<action selector="clearLog:" destination="BYZ-38-t0r" eventType="touchUpInside" id="CO6-jM-zKi"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="OU6-kz-f0e">
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="OU6-kz-f0e">
<rect key="frame" x="263" y="579" width="96" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Read from log"/>
<connections>
<action selector="readLog:" destination="BYZ-38-t0r" eventType="touchUpInside" id="eJ4-fm-GYy"/>
Expand All @@ -66,7 +62,17 @@
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="Mgf-R7-DSd" secondAttribute="bottom" constant="20" id="6hq-Du-P9A"/>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="pb1-Re-U9G" secondAttribute="bottom" id="9DF-Vj-hDc"/>
<constraint firstItem="OU6-kz-f0e" firstAttribute="trailing" secondItem="8bC-Xf-vdC" secondAttribute="trailingMargin" id="CyE-52-aCm"/>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="SG2-q4-LSa" secondAttribute="bottom" constant="20" id="Fc1-NZ-38M"/>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="E6e-16-Nsx" secondAttribute="bottom" constant="20" id="Krh-WW-lqD"/>
<constraint firstItem="SG2-q4-LSa" firstAttribute="leading" secondItem="E6e-16-Nsx" secondAttribute="trailing" constant="12" id="LnH-tx-E5I"/>
<constraint firstItem="Mgf-R7-DSd" firstAttribute="top" secondItem="OU6-kz-f0e" secondAttribute="bottom" constant="8" id="RIi-Do-6n3"/>
<constraint firstAttribute="trailingMargin" secondItem="Mgf-R7-DSd" secondAttribute="trailing" constant="9" id="Wjb-CF-sge"/>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="zpX-Rm-XPu" secondAttribute="bottom" constant="20" id="diI-io-7tN"/>
<constraint firstItem="zpX-Rm-XPu" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="6" id="f6Q-kh-HD2"/>
<constraint firstItem="E6e-16-Nsx" firstAttribute="leading" secondItem="zpX-Rm-XPu" secondAttribute="trailing" constant="27" id="hpy-9F-buf"/>
<constraint firstAttribute="trailing" secondItem="pb1-Re-U9G" secondAttribute="trailing" id="pHw-D8-G63"/>
<constraint firstItem="pb1-Re-U9G" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="sNk-17-1MJ"/>
<constraint firstItem="pb1-Re-U9G" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" id="tvK-wf-y4m"/>
Expand Down
2 changes: 1 addition & 1 deletion Example/SwiftBackgroundLocation/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class ViewController: UIViewController {
})

locations.forEach({ location in
let circle = MKCircle(center: location.coordinate, radius: BackgroundLocationManager.RegionConfig.regionRadius)
let circle = MKCircle(center: location.coordinate, radius: self.appDelagete().backgroundLocationManager.regionConfig.regionRadius)
circle.title = "regionPlanned"
self.mapView.add(circle)
self.circles.append(circle)
Expand Down
39 changes: 27 additions & 12 deletions SwiftBackgroundLocation/Classes/BackgroundLocationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,23 @@ import UIKit
public typealias BackgroundTrackingListener = (Result<CLLocation>) -> ()
public typealias RegionListener = (Result<[CLLocation]>) -> ()

public struct RegionConfig {
public var distanceToAroundRegions: Double {
return regionRadius*Double(maximumNumberOfRegions)/Double.pi
}
public let regionRadius: Double
public let maximumNumberOfRegions: Int

public init(regionRadius: Double = 100.0, maximumNumberOfRegions: Int = 20) {
self.regionRadius = regionRadius
self.maximumNumberOfRegions = maximumNumberOfRegions
}
}

final public class BackgroundLocationManager: NSObject {
public var addedRegionsListener: RegionListener?

public let regionConfig: RegionConfig

fileprivate var bgTaskIdentifier = "fetchLocation"

Expand All @@ -26,11 +41,17 @@ final public class BackgroundLocationManager: NSObject {

public convenience override init() {
guard let userDefaults = UserDefaults(suiteName: Constants.suitName) else { fatalError()}
self.init(regionCache: BackgroundLocationCache(defaults: userDefaults))
self.init(regionCache: BackgroundLocationCache(defaults: userDefaults), regionConfig: RegionConfig())
}

public convenience init(regionConfig: RegionConfig) {
guard let userDefaults = UserDefaults(suiteName: Constants.suitName) else { fatalError()}
self.init(regionCache: BackgroundLocationCache(defaults: userDefaults), regionConfig: regionConfig)
}

public init(regionCache: BackgroundLocationCacheable) {
public init(regionCache: BackgroundLocationCacheable, regionConfig: RegionConfig) {
self.regionCache = regionCache
self.regionConfig = regionConfig
}

public func start(backgroundTrackingListener: @escaping BackgroundTrackingListener) {
Expand All @@ -55,12 +76,6 @@ final public class BackgroundLocationManager: NSObject {
listener = nil
locationManager.delegate = nil
}

public struct RegionConfig {
public static let distanceToAroundRegions = regionRadius*Double(maximumNumberOfRegions)/Double.pi
public static let regionRadius = 100.0
public static let maximumNumberOfRegions = 20
}
}

extension BackgroundLocationManager {
Expand Down Expand Up @@ -88,23 +103,23 @@ extension BackgroundLocationManager {
fileprivate func startMonitoring(for coordinate: CLLocationCoordinate2D) {
clearRegions()

let val = 2*Double.pi/Double(RegionConfig.maximumNumberOfRegions)
let val = 2*Double.pi/Double(regionConfig.maximumNumberOfRegions)

let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)

var locations:[CLLocation] = []
locations.append(location)

var regions:[CLCircularRegion] = []
for i in 0..<RegionConfig.maximumNumberOfRegions {
for i in 0..<regionConfig.maximumNumberOfRegions {
let identifier = "\(Constants.suitName).regionIdentifier.\(i)"
let bearing = val*Double(i)

let coordinate = coordinate.location(for: bearing, and: RegionConfig.distanceToAroundRegions)
let coordinate = coordinate.location(for: bearing, and: regionConfig.distanceToAroundRegions)

let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: coordinate.latitude,
longitude: coordinate.longitude),
radius: RegionConfig.regionRadius,
radius: regionConfig.regionRadius,
identifier: identifier)


Expand Down

0 comments on commit c700925

Please sign in to comment.