Skip to content

Commit

Permalink
Fix pluggable extension delegate for watchOS
Browse files Browse the repository at this point in the history
  • Loading branch information
basememara committed Oct 19, 2019
1 parent 7e64ed6 commit b7e7b11
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions Sources/ZamzamCore/Application/ExtensionPluggableDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ import WatchKit

/// Subclassed by the `ExtensionDelegate` to pass lifecycle events to loaded plugins.
///
/// The application plugins will be processed in sequence.
/// The application plugins will be processed in sequence after calling `application() -> [ExtensionPlugin]`.
///
/// class ExtensionDelegate: ExtensionPluggableDelegate {
///
/// override func plugins() -> [ExtensionPlugin] {
/// return [
/// LoggerPlugin(),
/// LocationPlugin()
/// ]
/// }
/// override func application() -> [ExtensionPlugin] {[
/// LoggerPlugin(),
/// LocationPlugin()
/// ]}
/// }
///
/// Each application module has access to the `ExtensionDelegate` lifecycle events:
Expand Down Expand Up @@ -50,42 +48,40 @@ import WatchKit
/// }
open class ExtensionPluggableDelegate: NSObject, WKExtensionDelegate {

/// Lazy implementation of application modules list
public private(set) lazy var lazyPlugins: [ExtensionPlugin] = plugins()
/// List of application plugins for binding to `ExtensionDelegate` events
public private(set) lazy var plugins: [ExtensionPlugin] = { application() }()

/// List of application plugins for binding to `ExtensionDelegate` events
open func plugins() -> [ExtensionPlugin] {
[ /* Populated from sub-class */ ]
}
open func application() -> [ExtensionPlugin] {[]} // Override
}

public extension ExtensionPluggableDelegate {

func applicationDidFinishLaunching() {
lazyPlugins.forEach { $0.applicationDidFinishLaunching(.shared()) }
plugins.forEach { $0.applicationDidFinishLaunching(.shared()) }
}
}

public extension ExtensionPluggableDelegate {

func applicationDidBecomeActive() {
lazyPlugins.forEach { $0.applicationDidBecomeActive(.shared()) }
plugins.forEach { $0.applicationDidBecomeActive(.shared()) }
}

func applicationWillResignActive() {
lazyPlugins.forEach { $0.applicationWillResignActive(.shared()) }
plugins.forEach { $0.applicationWillResignActive(.shared()) }
}

func applicationWillEnterForeground() {
lazyPlugins.forEach { $0.applicationWillEnterForeground(.shared()) }
plugins.forEach { $0.applicationWillEnterForeground(.shared()) }
}

func applicationDidEnterBackground() {
lazyPlugins.forEach { $0.applicationDidEnterBackground(.shared()) }
plugins.forEach { $0.applicationDidEnterBackground(.shared()) }
}
}

/// Conforming to an app module and added to `ExtensionDelegate.plugins()` will trigger events.
/// Conforming to an app module and added to `ExtensionDelegate.application()` will trigger events.
public protocol ExtensionPlugin {
func applicationDidFinishLaunching(_ application: WKExtension)

Expand Down

0 comments on commit b7e7b11

Please sign in to comment.