GithubHelp home page GithubHelp logo

programmer5000 / pluggableapplicationdelegate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fmo91/pluggableapplicationdelegate

0.0 1.0 0.0 54 KB

Smallest AppDelegate ever by using a decoupled-services based architecture. ๐Ÿ› 

License: MIT License

Swift 53.95% Ruby 4.55% Objective-C 3.54% Shell 37.97%

pluggableapplicationdelegate's Introduction

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, [email protected]

License

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

pluggableapplicationdelegate's People

Contributors

fmo91 avatar mariuszwisniewski avatar simonfairbairn avatar stephanecopin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.