-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Using regions instead of significant location change * Setup a trigger of 1 second for the notification * Removing the history guard to show notifications * Correct version of the library * Adding suggestions
- Loading branch information
Showing
10 changed files
with
138 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
github "stephencelis/SQLite.swift" "swift-2.3" | ||
github "mozilla-magnet/magnet-scanner-ios" ~> 0.3.4 | ||
github "mozilla-magnet/magnet-scanner-ios" ~> 0.3.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
github "Alamofire/Alamofire" "3.5.1" | ||
github "stephencelis/SQLite.swift" "8b95a0c4a884f35e5102cd306f634680255473bb" | ||
github "SwiftyJSON/SwiftyJSON" "54017d514a87b2b855b407131292c510cdeb65f8" | ||
github "mozilla-magnet/magnet-scanner-ios" "0.3.4" | ||
github "mozilla-magnet/magnet-scanner-ios" "0.3.6" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// | ||
// RegionManager.swift | ||
// Magnet | ||
// | ||
// Created by Francisco Jordano on 09/11/2016. | ||
// Copyright © 2016 Mozilla. All rights reserved. | ||
// | ||
// This class takes care of waking up in background and | ||
// checking the current location to know if there are interesting | ||
// things around. | ||
// In order to do this we use the region functionality in iOS. | ||
// We setup circular regions centered in the current location of the user, | ||
// when the OS detect that we abandon that region, we get a callback and have | ||
// some time in the background to get a meassure of the new location, check | ||
// if there are interesting things around and setup the new region. | ||
// | ||
// This process will happen, creation of the region, detection of the region | ||
// being abandoned, checking things around, and again we start. | ||
import Foundation | ||
import CoreLocation | ||
import MagnetScannerIOS | ||
|
||
@objc(RegionManager) class RegionManager: NSObject, CLLocationManagerDelegate { | ||
let locationManager:CLLocationManager = CLLocationManager() | ||
private static let REGION_NAME = "region.magnet.mozilla.org" | ||
private static let REGION_RADIUS: Double = 50 | ||
var scanner: MagnetScanner? = nil | ||
var isEnabled = true | ||
var currentRegion: CLRegion? | ||
let locationResolver: LocationHelper = LocationHelper() | ||
|
||
@objc override init() { | ||
super.init() | ||
|
||
isEnabled = CLLocationManager.isMonitoringAvailableForClass(CLRegion.self) | ||
|
||
guard isEnabled else { | ||
Log.w("Region monitoring not enabled for this device") | ||
return; | ||
} | ||
|
||
scanner = MagnetScanner(callback: self.onItemFound) | ||
} | ||
|
||
private func onItemFound(item: Dictionary<String, AnyObject>) { | ||
Log.l("Found item on leaving region \(item)") | ||
let url = item["url"] as! String | ||
var channel: String? = nil | ||
if item["channel_id"] != nil { | ||
channel = item["channel_id"] as! String | ||
} | ||
NotificationsHelper.notifyUser(url, channel: channel) | ||
} | ||
|
||
@objc func startListeningToRegionChange() { | ||
guard isEnabled else { | ||
return | ||
} | ||
Log.l("Start listening to region changes") | ||
self.locationManager.delegate = self | ||
setupRegion() | ||
} | ||
|
||
@objc func stopListeningToRegionChange() { | ||
guard isEnabled && self.currentRegion != nil else { | ||
return | ||
} | ||
Log.l("Stoping listening to region changes") | ||
|
||
self.locationManager.stopMonitoringForRegion(self.currentRegion!) | ||
} | ||
|
||
private func setupRegion() { | ||
locationResolver.start { location in | ||
let region = CLCircularRegion(center: location.coordinate, radius: RegionManager.REGION_RADIUS, identifier: RegionManager.REGION_NAME) | ||
self.currentRegion = region | ||
Log.l("Setting up region \(region)") | ||
self.locationManager.startMonitoringForRegion(region) | ||
} | ||
} | ||
|
||
func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) { | ||
setupRegion() | ||
Log.l("Waking up case region abandoned") | ||
self.scanner!.start() | ||
} | ||
} |