|
| 1 | +// |
| 2 | +// PluggableApplicationDelegate.swift |
| 3 | +// |
| 4 | +// Created by Basem Emara on 2018-04-07. |
| 5 | +// Copyright © 2018 Basem Emara. All rights reserved. |
| 6 | +// |
| 7 | + |
| 8 | +// NOTE: Maintained code in ZamzamKit framework: |
| 9 | +// https://github.com/ZamzamInc/ZamzamKit/blob/master/Sources/Models/iOS/PluggableApplicationDelegate.swift |
| 10 | + |
| 11 | +import UIKit |
| 12 | + |
| 13 | +public protocol ApplicationService { |
| 14 | + func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool |
| 15 | + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool |
| 16 | + |
| 17 | + func applicationWillEnterForeground(_ application: UIApplication) |
| 18 | + func applicationDidEnterBackground(_ application: UIApplication) |
| 19 | + func applicationDidBecomeActive(_ application: UIApplication) |
| 20 | + func applicationWillResignActive(_ application: UIApplication) |
| 21 | + |
| 22 | + func applicationProtectedDataWillBecomeUnavailable(_ application: UIApplication) |
| 23 | + func applicationProtectedDataDidBecomeAvailable(_ application: UIApplication) |
| 24 | + |
| 25 | + func applicationWillTerminate(_ application: UIApplication) |
| 26 | + func applicationDidReceiveMemoryWarning(_ application: UIApplication) |
| 27 | + |
| 28 | + func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) |
| 29 | + func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) |
| 30 | +} |
| 31 | + |
| 32 | +// MARK: - Optionals |
| 33 | + |
| 34 | +public extension ApplicationService { |
| 35 | + func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { return true } |
| 36 | + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { return true } |
| 37 | + |
| 38 | + func applicationWillEnterForeground(_ application: UIApplication) {} |
| 39 | + func applicationDidEnterBackground(_ application: UIApplication) {} |
| 40 | + func applicationDidBecomeActive(_ application: UIApplication) {} |
| 41 | + func applicationWillResignActive(_ application: UIApplication) {} |
| 42 | + |
| 43 | + func applicationProtectedDataWillBecomeUnavailable(_ application: UIApplication) {} |
| 44 | + func applicationProtectedDataDidBecomeAvailable(_ application: UIApplication) {} |
| 45 | + |
| 46 | + func applicationWillTerminate(_ application: UIApplication) {} |
| 47 | + func applicationDidReceiveMemoryWarning(_ application: UIApplication) {} |
| 48 | + |
| 49 | + func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {} |
| 50 | + func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {} |
| 51 | +} |
| 52 | + |
| 53 | +open class PluggableApplicationDelegate: UIResponder, UIApplicationDelegate { |
| 54 | + |
| 55 | + public var window: UIWindow? |
| 56 | + |
| 57 | + /// Lazy implementation of application services list |
| 58 | + public lazy var lazyServices: [ApplicationService] = { |
| 59 | + services() |
| 60 | + }() |
| 61 | + |
| 62 | + /// List of application services for binding to `AppDelegate` events |
| 63 | + open func services() -> [ApplicationService] { |
| 64 | + return [ /* Populated from sub-class */ ] |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +public extension PluggableApplicationDelegate { |
| 69 | + |
| 70 | + func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]? = nil) -> Bool { |
| 71 | + return lazyServices.reduce(true) { |
| 72 | + $0 && $1.application(application, willFinishLaunchingWithOptions: launchOptions) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { |
| 77 | + return lazyServices.reduce(true) { |
| 78 | + $0 && $1.application(application, didFinishLaunchingWithOptions: launchOptions) |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +public extension PluggableApplicationDelegate { |
| 84 | + |
| 85 | + func applicationWillEnterForeground(_ application: UIApplication) { |
| 86 | + lazyServices.forEach { $0.applicationWillEnterForeground(application) } |
| 87 | + } |
| 88 | + |
| 89 | + func applicationDidEnterBackground(_ application: UIApplication) { |
| 90 | + lazyServices.forEach { $0.applicationDidEnterBackground(application) } |
| 91 | + } |
| 92 | + |
| 93 | + func applicationDidBecomeActive(_ application: UIApplication) { |
| 94 | + lazyServices.forEach { $0.applicationDidBecomeActive(application) } |
| 95 | + } |
| 96 | + |
| 97 | + func applicationWillResignActive(_ application: UIApplication) { |
| 98 | + lazyServices.forEach { $0.applicationWillResignActive(application) } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +public extension PluggableApplicationDelegate { |
| 103 | + |
| 104 | + func applicationProtectedDataWillBecomeUnavailable(_ application: UIApplication) { |
| 105 | + lazyServices.forEach { $0.applicationProtectedDataWillBecomeUnavailable(application) } |
| 106 | + } |
| 107 | + |
| 108 | + func applicationProtectedDataDidBecomeAvailable(_ application: UIApplication) { |
| 109 | + lazyServices.forEach { $0.applicationProtectedDataDidBecomeAvailable(application) } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +public extension PluggableApplicationDelegate { |
| 114 | + |
| 115 | + func applicationWillTerminate(_ application: UIApplication) { |
| 116 | + lazyServices.forEach { $0.applicationWillTerminate(application) } |
| 117 | + } |
| 118 | + |
| 119 | + func applicationDidReceiveMemoryWarning(_ application: UIApplication) { |
| 120 | + lazyServices.forEach { $0.applicationDidReceiveMemoryWarning(application) } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +public extension PluggableApplicationDelegate { |
| 125 | + |
| 126 | + func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { |
| 127 | + lazyServices.forEach { $0.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken) } |
| 128 | + } |
| 129 | + |
| 130 | + func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { |
| 131 | + lazyServices.forEach { $0.application(application, didFailToRegisterForRemoteNotificationsWithError: error) } |
| 132 | + } |
| 133 | +} |
0 commit comments