Skip to content
Permalink
Browse files

Added fixes for iOS and provider initialization in general

  • Loading branch information...
Michael Rademaker
Michael Rademaker committed May 15, 2019
1 parent 68b8228 commit 5ade96fae69d153556d05bbda55eaab9a66fb79b
@@ -185,7 +185,7 @@ protected void onNewIntent(Intent intent) {
protected void openIntentUrl(Intent intent) {
if (!intent.hasExtra("appUrl")) {
String url = getClientUrl();
LOG.fine("Loading web view: " + url);
LOG.fine("Loading web view: " + url);
loadUrl(url);
} else {
String url = getClientUrl();
@@ -39,6 +39,7 @@ public enum DefaultsKey {
public static let successKey = "success"
public static let dataKey = "data"
public static let enabledKey = "enabled"
public static let disabledKey = "disabled"
}

public enum Actions {
@@ -28,8 +28,8 @@ public class ErrorManager : NSObject {
NSLog("showing error %@",error as NSError)
let topWindow = UIWindow(frame: UIScreen.main.bounds)
topWindow.rootViewController = UIViewController()
topWindow.windowLevel = UIWindowLevelAlert + 1
let alertVC = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)
topWindow.windowLevel = UIWindow.Level.alert + 1
let alertVC = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: UIAlertController.Style.alert)
let alertAction = UIAlertAction(title: "Done", style: .cancel) { (action) in
topWindow.rootViewController?.presentedViewController?.dismiss(animated: true, completion: nil)
topWindow.isHidden = true
@@ -6,9 +6,10 @@ public class GeofenceProvider: NSObject, URLSessionDelegate {
static let baseUrlKey = "baseUrl"
static let consoleIdKey = "consoleId"
static let geoPostUrlsKey = "geoPostUrls"
static let geoDisabledKey = "geoDisabled"

let version = "ORConsole"
let geofenceFetchEndpoint = "api/rules/geofences/"
let geofenceFetchEndpoint = "rules/geofences/"
let locationManager = CLLocationManager()
let userdefaults = UserDefaults(suiteName: ORAppGroup.entitlement)
var geoPostUrls = [String:[String]]()
@@ -40,7 +41,8 @@ public class GeofenceProvider: NSObject, URLSessionDelegate {
DefaultsKey.requiresPermissionKey: true,
DefaultsKey.hasPermissionKey: checkPermission(),
DefaultsKey.successKey: true,
DefaultsKey.enabledKey: false
DefaultsKey.enabledKey: false,
DefaultsKey.disabledKey: userdefaults?.bool(forKey: GeofenceProvider.geoDisabledKey) ?? false
]
}

@@ -90,6 +92,7 @@ public class GeofenceProvider: NSObject, URLSessionDelegate {
self.consoleId = consoleId
userdefaults?.set(self.baseURL, forKey: GeofenceProvider.baseUrlKey)
userdefaults?.set(self.consoleId, forKey: GeofenceProvider.consoleIdKey)
userdefaults?.removeObject(forKey: GeofenceProvider.geoDisabledKey)
userdefaults?.synchronize()
enableCallback = callback
if checkPermission() {
@@ -116,6 +119,10 @@ public class GeofenceProvider: NSObject, URLSessionDelegate {

public func disable()-> [String: Any] {
locationManager.stopMonitoringSignificantLocationChanges()
userdefaults?.set(true, forKey: GeofenceProvider.geoDisabledKey)
userdefaults?.removeObject(forKey: GeofenceProvider.baseUrlKey)
userdefaults?.removeObject(forKey: GeofenceProvider.consoleIdKey)
userdefaults?.synchronize()
return [
DefaultsKey.actionKey: Actions.providerDisable,
DefaultsKey.providerKey: Providers.geofence
@@ -36,7 +36,7 @@ open class ORAppDelegate: UIResponder, UIApplicationDelegate, URLSessionDelegate

private var geofenceProvider : GeofenceProvider?

open func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
open func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
// if the app was launched because of geofencing
@@ -46,14 +46,14 @@ open class ORAppDelegate: UIResponder, UIApplicationDelegate, URLSessionDelegate
let logFilePath = (documentsDirectory as NSString).appendingPathComponent(fileName)
freopen(logFilePath.cString(using: String.Encoding.ascii)!, "a+", stderr)

if launchOptions?[UIApplicationLaunchOptionsKey.location] != nil {
if launchOptions?[UIApplication.LaunchOptionsKey.location] != nil {
NSLog("%@", "App started from location update")
// create new GeofenceProvider which creates a CLLocationManager that will receive the location update
geofenceProvider = GeofenceProvider()
if CLLocationManager.authorizationStatus() == .authorizedAlways {
geofenceProvider?.locationManager.startMonitoringSignificantLocationChanges()
}
} else if let remoteNotifcation = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {
} else if let remoteNotifcation = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {
NSLog("%@", "App started from remote notification")
if let action = remoteNotifcation[DefaultsKey.actionKey] as? String {
if action == Actions.geofenceRefresh {
@@ -186,7 +186,7 @@ open class ORAppDelegate: UIResponder, UIApplicationDelegate, URLSessionDelegate
if (!reachabilityAlertShown) {
let topWindow = UIWindow(frame: UIScreen.main.bounds)
topWindow.rootViewController = UIViewController()
topWindow.windowLevel = UIWindowLevelAlert + 1
topWindow.windowLevel = UIWindow.Level.alert + 1

reachabilityAlert = UIAlertController(title: "Network Error", message: "Your device seems to be offline", preferredStyle: .alert)
let reachabilityRetryAction = UIAlertAction(title: "Retry", style: .default, handler: { (action) in
@@ -65,7 +65,7 @@ open class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMess
})
}
} else if action == Actions.providerDisable {
if let disableData = pushProvider?.disbale() {
if let disableData = pushProvider?.disable() {
sendData(data: disableData)
}
}
@@ -78,7 +78,7 @@ open class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMess
if let consoleId = postMessageDict[GeofenceProvider.consoleIdKey] as? String {
geofenceProvider?.enable(baseUrl: "\(ORServer.baseUrl)api/\(ORServer.realm)", consoleId: consoleId, callback: { enableData in
self.sendData(data: enableData)
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3)) {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(5)) {
self.geofenceProvider?.fetchGeofences()
}
})
@@ -155,7 +155,7 @@ open class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMess
let app = UIApplication.shared
if navigationAction.targetFrame == nil, let url = navigationAction.request.url{
if app.canOpenURL(url) {
app.open(url, options: [:], completionHandler: nil)
app.open(url, options: convertToUIApplicationOpenExternalURLOptionsKeyDictionary([:]), completionHandler: nil)
decisionHandler(.cancel)
}
} else {
@@ -323,3 +323,8 @@ open class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMess
}
}
}

// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertToUIApplicationOpenExternalURLOptionsKeyDictionary(_ input: [String: Any]) -> [UIApplication.OpenExternalURLOptionsKey: Any] {
return Dictionary(uniqueKeysWithValues: input.map { key, value in (UIApplication.OpenExternalURLOptionsKey(rawValue: key), value)})
}
@@ -14,6 +14,7 @@ public class PushNotificationProvider: NSObject {
let userdefaults = UserDefaults(suiteName: ORAppGroup.entitlement)
let version = "fcm"
public var consoleId: String = ""
static let pushDisabledKey = "pushDisabled"

public override init() {
super.init()
@@ -31,7 +32,8 @@ public class PushNotificationProvider: NSObject {
DefaultsKey.hasPermissionKey: true,
DefaultsKey.requiresPermissionKey: true,
DefaultsKey.successKey: true,
DefaultsKey.enabledKey: false
DefaultsKey.enabledKey: false,
DefaultsKey.disabledKey: self.userdefaults?.bool(forKey: PushNotificationProvider.pushDisabledKey) ?? false
])

case .denied:
@@ -42,18 +44,20 @@ public class PushNotificationProvider: NSObject {
DefaultsKey.hasPermissionKey: false,
DefaultsKey.requiresPermissionKey: true,
DefaultsKey.successKey: true,
DefaultsKey.enabledKey: false
DefaultsKey.enabledKey: false,
DefaultsKey.disabledKey: self.userdefaults?.bool(forKey: PushNotificationProvider.pushDisabledKey) ?? false
])

case .notDetermined:
default:
callback( [
DefaultsKey.actionKey: Actions.providerInit,
DefaultsKey.providerKey: Providers.push,
DefaultsKey.versionKey: self.version,
DefaultsKey.hasPermissionKey: nil,
DefaultsKey.requiresPermissionKey: true,
DefaultsKey.successKey: true,
DefaultsKey.enabledKey: false
DefaultsKey.enabledKey: false,
DefaultsKey.disabledKey: self.userdefaults?.bool(forKey: PushNotificationProvider.pushDisabledKey) ?? false
])
}
}
@@ -62,6 +66,7 @@ public class PushNotificationProvider: NSObject {
public func enable(consoleId: String, callback:@escaping ([String: Any]) ->(Void)) {
self.consoleId = consoleId
userdefaults?.set(self.consoleId, forKey: GeofenceProvider.consoleIdKey)
userdefaults?.removeObject(forKey: PushNotificationProvider.pushDisabledKey)
userdefaults?.synchronize()
UNUserNotificationCenter.current().getNotificationSettings { (settings) in

@@ -87,7 +92,7 @@ public class PushNotificationProvider: NSObject {
DefaultsKey.dataKey: ["token": self.userdefaults?.string(forKey: DefaultsKey.fcmTokenKey)]
])

case .notDetermined:
default:
UNUserNotificationCenter.current().requestAuthorization(
options: [.alert, .badge, .sound],
completionHandler: {granted, _ in
@@ -110,7 +115,9 @@ public class PushNotificationProvider: NSObject {
}
}

public func disbale()-> [String: Any] {
public func disable()-> [String: Any] {
userdefaults?.set(true, forKey: PushNotificationProvider.pushDisabledKey)
userdefaults?.synchronize()
return [
DefaultsKey.actionKey: Actions.providerDisable,
DefaultsKey.providerKey: Providers.push
@@ -163,17 +163,17 @@ export class Console {
this._initialiseInProgress = true;

try {
// Get an ID for this console if it doesn't have one
if (!this._registration.id) {
await this.sendRegistration(0);
}

if (this._registration.providers) {
for (let providerName of Object.keys(this._registration.providers)) {
await this._initialiseProvider(providerName);
}
}

// Get an ID for this console if it doesn't have one
if (!this._registration.id) {
await this.sendRegistration(0);
}

this._initialised = true;
this._initialiseInProgress = false;

@@ -226,6 +226,7 @@ export class Console {
this._registration.providers![providerName].hasPermission = response.hasPermission;
this._registration.providers![providerName].success = response.success;
this._registration.providers![providerName].enabled = response.success;
this._registration.providers![providerName].data = response.data;

let index = this._pendingProviderEnables.indexOf(providerName);

@@ -513,6 +514,7 @@ export class Console {
this._registration.providers![providerName].success = initResponse.success;
this._registration.providers![providerName].enabled = initResponse.enabled;
this._registration.providers![providerName].disabled = initResponse.disabled;
this._registration.providers![providerName].data = initResponse.data;

if (!initResponse.success) {
console.debug("Provider initialisation failed: '" + providerName + "'");
@@ -184,7 +184,7 @@ export class OrMap extends LitElement {

stylesArr.forEach((styleItem) => {
const styleElem = document.createElement("style");
styleElem.textContent = styleItem.toString();
styleElem.textContent = String(styleItem.toString());
if (this._mapContainer!.children.length > 0) {
this._mapContainer!.insertBefore(styleElem, this._mapContainer!.children[0]);
} else {

0 comments on commit 5ade96f

Please sign in to comment.
You can’t perform that action at this time.