Coder Social home page Coder Social logo

fmo91 / pluggableapplicationdelegate Goto Github PK

View Code? Open in Web Editor NEW
538.0 11.0 79.0 57 KB

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

License: MIT License

Swift 56.37% Ruby 4.31% Objective-C 3.35% Shell 35.97%
swift ios appdelegate services architecture

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

davbeck avatar fmo91 avatar mariuszwisniewski avatar simonfairbairn avatar stephanecopin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pluggableapplicationdelegate's Issues

Variable 'result' was written to, but never read

From version 0.1.2:

    @available(iOS 3.0, *)
    public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
        var result = true
        
        for service in __services {
            if let serviceResult = service.application?(application, didFinishLaunchingWithOptions: launchOptions) {
                if serviceResult == false {
                    result = false
                }
            }
        }
        
        return true
    }
    @available(iOS, introduced: 2.0, deprecated: 9.0, message: "Please use application:openURL:options:")
    public func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
        var result = true
        
        for service in __services {
            if service.application?(application, handleOpen: url) == false {
                result = false
            }
        }
        
        return true
    }
    
    @available(iOS, introduced: 4.2, deprecated: 9.0, message: "Please use application:openURL:options:")
    public func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        var result = true
        
        for service in __services {
            if service.application?(application, open: url, sourceApplication: sourceApplication, annotation: annotation) == false {
                result = false
            }
        }
        
        return true
    }

screen shot 2017-04-17 at 09 57 36

result variable can be removed. These methods could just return false from within the for/if blocks.

Warnings in debug log

Great idea, really helps with organisation. I am seeing these warnings pop up in the debug log, however:

You've implemented -[<UIApplicationDelegate> application:performFetchWithCompletionHandler:], but you still need to add "fetch" to the list of your supported UIBackgroundModes in your Info.plist.

You've implemented -[<UIApplicationDelegate> application:didReceiveRemoteNotification:fetchCompletionHandler:], but you still need to add "remote-notification" to the list of your supported UIBackgroundModes in your Info.plist.

I'm pretty sure these messages can be safely ignored so this is more just FYI.

Issue with URL scheme handling

There is an issue with this implementation:

    @available(iOS 9.0, *)
    public func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return __services.reduce(true) { (previous, service) -> Bool in
            previous && (service.application?(app, open: url, options: options) ?? true)
        }
    }

The problem is when you have multiple application services. For instance we have a Facebook service and a payment service that can both be opened with a url. The issue is that the above code will start looping through until it gets to the first service that implements the url scheme check, in this case Facebook. As the url scheme is actually for the payment service, Facebook service correctly returns false. But this then causes no other service to be checked because previous is now false.

Access UIWindow

Where do i setup AppDelegate window and use it in the app where needed ?

Project is no longer supported

Hi guys.

It looks like this project is no longer supported (no updates, even no answers).
But it's a great idea and I use it on my own projects. ๐Ÿ‘๐Ÿป

And I took the liberty of creating a new repo for future support. ๐Ÿค“
I hope that the author will not object to this (I added him to Credits).

The new repo: https://github.com/pchelnikov/PluggableAppDelegate
I've already added some features from PRs and comments.

Please welcome for conversation and contribution. ๐Ÿฆ„

application(_:openUrl:options:) not working

In line 91 of PluggableApplicationDelegate.swift there is the method called open func application(_ app: UIApplication, public url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Boo but this should be open func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool. Not public url: but open url:.
So an app using the PluggableApplicationDelegate currently can not handle the url it was opened with.

[Question] Deprecated UIUserNotificationSettings and UILocalNotification

Hi,
first nice idea, I appreciate this solution.

for sure do you know that Xcode gives us warnings in PluggableApplicationDelegate-Swift.h due to UIUserNotificationSettings and UILocalNotification are deprecated since iOS 10.

You already have scheduled update of ApplicationServicesManager.swift?

I ask because probably I can do it, but if you're working on it, has no sense to open in the future a PR about it :)

Thanks!

Custom UIWindow

For my project, I need to have a custom UIWindow. Without the PluggableApplicationDelegate, I used to do it like this:

    var customWindow: MyCustomWindow?
    var window: UIWindow? {
        get {
            customWindow = customWindow ?? MyCustomWindow(frame: UIScreen.main.bounds)
            return customWindow
        }
    }

When I keep this code in my AppDelegate itself, I get the following error on the var Window:

Overriding non-open var outside of its defining module

And when I put this code in a specific ApplicationService, it says that self.window is unknown.

Any clue how I can still have this custom UIWindow? Thanks!

How to separate for remoteControlEvents

I tried this to separate remoteControlEvents and my code looks like

import Foundation
import PluggableApplicationDelegate

final class MusicControlsAppService: NSObject, ApplicationService {
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
        UIApplication.shared.beginReceivingRemoteControlEvents()
        print("It has started!")
        return true
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        print("It has entered background")
    }
    
    override func remoteControlReceived(with event: UIEvent?) {
        if event?.type == .remoteControl {
            switch event!.subtype {
            case .remoteControlPlay:
                PlayerManager.shared.playTapped()
            case .remoteControlPause:
                PlayerManager.shared.pauseTapped()
            case .remoteControlTogglePlayPause:
                if PlayerManager.shared.isPauseActive {
                    PlayerManager.shared.playTapped()
                } else {
                    PlayerManager.shared.pauseTapped()
                }
            case .remoteControlNextTrack:
                PlayerManager.shared.nextTapped()
            case .remoteControlPreviousTrack:
                PlayerManager.shared.previousTapped()
            default:
                break
            }
        }
    }
}

This gives error for the method Method does not override any method from its superclass , also when override keyword is removed it doesn't get calls.

Can you assist to solve this ? As of now I am keeping that method in the main App delegate which is subclass of PluggableApplicationDelegate which works.

Carthage support

I'm using Carthage as my dependency manager and the last tagged version 0.1.2 does not work but what's currently on master does. Can you tag or release what's on master? Thanks!

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.