PluggableApplicationDelegate

Smallest AppDelegate ever by using a decoupled-services based architecture. ?

Github星跟踪图

PluggableApplicationDelegate

CI Status
Version
License
Platform

Introduction

AppDelegate is a traditional example of bad code. Lots of line of code that makes so much different things are put together in methods that are called within the application life cycle. But all of those concerns are over.
Using PluggableApplicationDelegate you decouple AppDelegate from the services that you plug to it. Each ApplicationService has its own life cycle that is shared with AppDelegate.

At a glance

Let see some code.
Here is how a ApplicationService is like:

import Foundation
import PluggableApplicationDelegate

final class LoggerApplicationService: NSObject, ApplicationService {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
        
        print("It has started!")
        
        return true
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        print("It has entered background")
    }
}

That's all. It is exactly the same as a AppDelegate. Think of ApplicationService as sub-AppDelegates.

In AppDelegate you just have to inherit from PluggableApplicationDelegate to register the services.

import UIKit
import PluggableApplicationDelegate

@UIApplicationMain
class AppDelegate: PluggableApplicationDelegate {
    
    override var services: [ApplicationService] {
        return [
            LoggerApplicationService()
        ]
    }
}

Yes. That's all. Simple.

How does this work?

You may want to read my Medium post about Pluggable App Delegate.
Basically, you do an inversion of control. Instead of let AppDelegate instantiate your dependencies, perform actions at every step of its life cycle, you create objects that share the AppDelegate life cycle and plug them into your AppDelegate.
Those objects are observers of the AppDelegate. Your AppDelegate has the only responsibility of notify them about its life cycle events.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

PluggableApplicationDelegate requires Swift 3.0 or above.

Installation

PluggableApplicationDelegate is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'PluggableApplicationDelegate'

Author

fmo91, ortizfernandomartin@gmail.com

License

PluggableApplicationDelegate is available under the MIT license. See the LICENSE file for more info.

主要指标

概览
名称与所有者fmo91/PluggableApplicationDelegate
主编程语言Swift
编程语言Swift (语言数: 4)
平台
许可证MIT License
所有者活动
创建于2017-02-26 23:39:58
推送于2022-07-21 13:56:46
最后一次提交2017-11-08 19:18:25
发布数4
最新版本名称0.2.0 (发布于 )
第一版名称0.1.0 (发布于 )
用户参与
星数537
关注者数9
派生数77
提交数33
已启用问题?
问题数14
打开的问题数10
拉请求数10
打开的拉请求数5
关闭的拉请求数4
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?