-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDelegate.swift
executable file
·84 lines (64 loc) · 2.4 KB
/
AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//
// AppDelegate.swift
// Limits
//
// Created by Michael Epstein on 3/13/16.
// Copyright © 2016 Michael Epstein. All rights reserved.
//
import UIKit
import SwiftlySalesforce
import PromiseKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, LoginViewPresentable {
var window: UIWindow?
let consumerKey = "<YOUR CONSUMER KEY HERE>"
let redirectURL = NSURL(string: "limits://authorized")!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Swiftly Salesforce config
OAuth2Manager.sharedInstance.configureWithConsumerKey(consumerKey, redirectURL: redirectURL)
OAuth2Manager.sharedInstance.authenticationDelegate = self
// Background fetch config
application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
let settings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
return true
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
handleRedirectURL(url)
return true
}
func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
guard let _ = application.currentUserNotificationSettings()?.types.contains([.Badge]) else {
application.applicationIconBadgeNumber = 0
completionHandler(.Failed)
return
}
// Refresh model and update badge
when(Model.sharedInstance.limitCache.refresh(), Model.sharedInstance.siteCache.refresh()).then {
() -> () in
guard let orgLimits = Model.sharedInstance.limitCache.data, sites = Model.sharedInstance.siteCache.data else {
application.applicationIconBadgeNumber = 0
completionHandler(.Failed)
return
}
// Count low org limits
var n = (orgLimits.filter { $0.state == Limit.LimitState.Low }).count
// Add low Site limits
for site in sites {
if site.dailyBandwidthLimit.state == .Low {
n += 1
}
if site.dailyRequestTimeLimit.state == .Low {
n += 1
}
}
// Update badge
application.applicationIconBadgeNumber = n
completionHandler(.NewData)
}.error {
(error) -> () in
application.applicationIconBadgeNumber = 0
completionHandler(.Failed)
}
}
}