Skip to content

Commit

Permalink
Merge pull request #16 from ZamzamInc/develop
Browse files Browse the repository at this point in the history
Create release
  • Loading branch information
basememara authored Nov 2, 2019
2 parents c7f8749 + 73dc053 commit 77d4407
Show file tree
Hide file tree
Showing 11 changed files with 483 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//___FILEHEADER___

import ZamzamKit
import ZamzamCore

public struct ___VARIABLE_productName:identifier___NetworkStore: ___VARIABLE_productName:identifier___Store, Loggable {
public struct ___VARIABLE_productName:identifier___NetworkStore: ___VARIABLE_productName:identifier___Store {
private let log: LogWorkerType

public init() {

public init(log: LogWorkerType) {
self.log = log
}
}

Expand All @@ -16,7 +17,7 @@ public extension ___VARIABLE_productName:identifier___NetworkStore {
// Handle errors if applicable
guard let value = $0.value, $0.isSuccess else {
let error = DataError(from: $0.error)
self.Log(error: "An error occured while fetching ___VARIABLE_productName:identifier___: \($0.error?.serverDescription ?? "unknown")")
self.log.error("An error occured while fetching ___VARIABLE_productName:identifier___: \($0.error?.serverDescription ?? "unknown")")
return completion(.failure(error))
}

Expand All @@ -34,15 +35,15 @@ public extension ___VARIABLE_productName:identifier___NetworkStore {
)

guard let data = payload.data, payload.status == .success else {
self.Log(error: "An error occured while fetching ___VARIABLE_productName:identifier___, data nil or server status error: \(String(describing: payload.errors)).")
self.log.error("An error occured while fetching ___VARIABLE_productName:identifier___, data nil or server status error: \(String(describing: payload.errors)).")
return DispatchQueue.main.async { completion(.failure(.unknownReason(nil))) }
}

DispatchQueue.main.async {
completion(.success(data.objects))
}
} catch {
self.Log(error: "An error occured while parsing ___VARIABLE_productName:identifier___: \(error).")
self.log.error("An error occured while parsing ___VARIABLE_productName:identifier___: \(error).")
return DispatchQueue.main.async { completion(.failure(.parseFailure(error))) }
}
}
Expand Down
66 changes: 54 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -622,21 +622,16 @@ someStruct.isRunningOnSimulator -> false
```swift
// Subclass and install to pass lifecycle events to loaded plugins
@UIApplicationMain
class AppDelegate: ApplicationPluginDelegate {
class AppDelegate: ApplicationPluggableDelegate {

private(set) lazy var plugins: [ApplicationPlugin] = [
override func plugins() -> [ApplicationPlugin] {[
LoggerPlugin(),
NotificationPlugin()
]

override init() {
super.init()
install(plugins)
}
]}
}
```
```swift
// Each application plugin has access to the AppDelegate lifecycle events
// Each application plugin has access to the `AppDelegate` lifecycle events
final class LoggerPlugin: ApplicationPlugin {
private let log = Logger()

Expand All @@ -659,6 +654,32 @@ final class LoggerPlugin: ApplicationPlugin {
}
}
```

> Split up `SceneDelegate` into plugins:
```swift
// Subclass and install to pass lifecycle events to loaded plugins
class SceneDelegate: ScenePluggableDelegate {

override func plugins() -> [ScenePlugin] {[
LoggerPlugin(),
NotificationPlugin()
]}
}
```
```swift
// Each application plugin has access to the `SceneDelegate` lifecycle events
final class LoggerPlugin: ScenePlugin {
private let log = Logger()

func sceneWillEnterForeground() {
log.info("Scene will enter foreground.")
}

func sceneDidEnterBackground() {
log.info("Scene did enter background.")
}
}
```
</details>

<details>
Expand Down Expand Up @@ -761,6 +782,27 @@ myLabel3.text = .localized(.next)
```
</details>

<details>
<summary>Logger</summary>

> Create loggers that conform to `LogStore` and add to `LogWorker` (console and `os_log` are included):
```swift
let log: LogWorkerType = LogWorker(
stores: [
LogConsoleStore(minLevel: .debug),
LogOSStore(
minLevel: .warning,
subsystem: "io.zamzam.Basem-Emara",
category: "Application"
),
MyCustomLogger()
]
)

log.error("There was an error.")
```
</details>

<details>
<summary>SystemConfiguration</summary>

Expand Down Expand Up @@ -999,8 +1041,8 @@ UNUserNotificationCenter.current().register(
authorizations: [.alert, .badge, .sound],
completion: { granted in
granted
? log(debug: "Authorization for notification succeeded.")
: log(warn: "Authorization for notification not given.")
? log.debug("Authorization for notification succeeded.")
: log.warn("Authorization for notification not given.")
}
)
```
Expand Down Expand Up @@ -1085,7 +1127,7 @@ UNUserNotificationCenter.current().add(
```swift
UNNotificationAttachment.download(from: urlString) {
guard $0.isSuccess, let attachment = $0.value else {
return log(error: "Could not download the remote resource (\(urlString)): \($0.error.debugDescription).")
return log.error("Could not download the remote resource (\(urlString)): \($0.error.debugDescription).")
}

UNUserNotificationCenter.current().add(
Expand Down
48 changes: 23 additions & 25 deletions Sources/ZamzamCore/Application/ApplicationPluggableDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import UIKit
/// @UIApplicationMain
/// class AppDelegate: ApplicationPluggableDelegate {
///
/// override func application() -> [ApplicationPlugin] {[
/// override func plugins() -> [ApplicationPlugin] {[
/// LoggerPlugin(),
/// NotificationPlugin()
/// ]}
Expand Down Expand Up @@ -46,37 +46,37 @@ import UIKit
/// log.warn("App will terminate.")
/// }
/// }
open class ApplicationPluggableDelegate: UIResponder, UIApplicationDelegate {
open class ApplicationPluggableDelegate: UIResponder, UIApplicationDelegate, WindowDelegate {
public var window: UIWindow?

/// List of application plugins for binding to `AppDelegate` events
public private(set) lazy var plugins: [ApplicationPlugin] = { application() }()
public private(set) lazy var pluginInstances: [ApplicationPlugin] = { plugins() }()

public override init() {
super.init()

// Load lazy property early
_ = plugins
_ = pluginInstances
}

/// List of application plugins for binding to `AppDelegate` events
open func application() -> [ApplicationPlugin] {[]} // Override
open func plugins() -> [ApplicationPlugin] {[]} // Override
}

extension ApplicationPluggableDelegate {

open func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
// Ensure all delegates called even if condition fails early
//swiftlint:disable reduce_boolean
plugins.reduce(true) {
pluginInstances.reduce(true) {
$0 && $1.application(application, willFinishLaunchingWithOptions: launchOptions)
}
}

open func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Ensure all delegates called even if condition fails early
//swiftlint:disable reduce_boolean
plugins.reduce(true) {
pluginInstances.reduce(true) {
$0 && $1.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Expand All @@ -85,41 +85,49 @@ extension ApplicationPluggableDelegate {
extension ApplicationPluggableDelegate {

open func applicationWillEnterForeground(_ application: UIApplication) {
plugins.forEach { $0.applicationWillEnterForeground(application) }
pluginInstances
.compactMap { $0 as? ScenePlugin }
.forEach { $0.sceneWillEnterForeground() }
}

open func applicationDidEnterBackground(_ application: UIApplication) {
plugins.forEach { $0.applicationDidEnterBackground(application) }
pluginInstances
.compactMap { $0 as? ScenePlugin }
.forEach { $0.sceneDidEnterBackground() }
}

open func applicationDidBecomeActive(_ application: UIApplication) {
plugins.forEach { $0.applicationDidBecomeActive(application) }
pluginInstances
.compactMap { $0 as? ScenePlugin }
.forEach { $0.sceneDidBecomeActive() }
}

open func applicationWillResignActive(_ application: UIApplication) {
plugins.forEach { $0.applicationWillResignActive(application) }
pluginInstances
.compactMap { $0 as? ScenePlugin }
.forEach { $0.sceneWillResignActive() }
}
}

extension ApplicationPluggableDelegate {

open func applicationProtectedDataWillBecomeUnavailable(_ application: UIApplication) {
plugins.forEach { $0.applicationProtectedDataWillBecomeUnavailable(application) }
pluginInstances.forEach { $0.applicationProtectedDataWillBecomeUnavailable(application) }
}

open func applicationProtectedDataDidBecomeAvailable(_ application: UIApplication) {
plugins.forEach { $0.applicationProtectedDataDidBecomeAvailable(application) }
pluginInstances.forEach { $0.applicationProtectedDataDidBecomeAvailable(application) }
}
}

extension ApplicationPluggableDelegate {

open func applicationWillTerminate(_ application: UIApplication) {
plugins.forEach { $0.applicationWillTerminate(application) }
pluginInstances.forEach { $0.applicationWillTerminate(application) }
}

open func applicationDidReceiveMemoryWarning(_ application: UIApplication) {
plugins.forEach { $0.applicationDidReceiveMemoryWarning(application) }
pluginInstances.forEach { $0.applicationDidReceiveMemoryWarning(application) }
}
}

Expand All @@ -128,11 +136,6 @@ public protocol ApplicationPlugin {
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool

func applicationWillEnterForeground(_ application: UIApplication)
func applicationDidEnterBackground(_ application: UIApplication)
func applicationDidBecomeActive(_ application: UIApplication)
func applicationWillResignActive(_ application: UIApplication)

func applicationProtectedDataWillBecomeUnavailable(_ application: UIApplication)
func applicationProtectedDataDidBecomeAvailable(_ application: UIApplication)

Expand All @@ -146,11 +149,6 @@ public extension ApplicationPlugin {
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { return true }
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { return true }

func applicationWillEnterForeground(_ application: UIApplication) {}
func applicationDidEnterBackground(_ application: UIApplication) {}
func applicationDidBecomeActive(_ application: UIApplication) {}
func applicationWillResignActive(_ application: UIApplication) {}

func applicationProtectedDataWillBecomeUnavailable(_ application: UIApplication) {}
func applicationProtectedDataDidBecomeAvailable(_ application: UIApplication) {}

Expand Down
18 changes: 9 additions & 9 deletions Sources/ZamzamCore/Application/ExtensionPluggableDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import WatchKit
///
/// class ExtensionDelegate: ExtensionPluggableDelegate {
///
/// override func application() -> [ExtensionPlugin] {[
/// override func plugins() -> [ExtensionPlugin] {[
/// LoggerPlugin(),
/// LocationPlugin()
/// ]}
Expand Down Expand Up @@ -49,42 +49,42 @@ import WatchKit
open class ExtensionPluggableDelegate: NSObject, WKExtensionDelegate {

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

public override init() {
super.init()

// Load lazy property early
_ = plugins
_ = pluginInstances
}

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

public extension ExtensionPluggableDelegate {

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

public extension ExtensionPluggableDelegate {

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

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

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

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

Expand Down
Loading

0 comments on commit 77d4407

Please sign in to comment.