Coder Social home page Coder Social logo

ramblerappdelegateproxy's Introduction

Overview

RamblerAppDelegateProxy is your best cure against massive AppDelegate class. It provides a nice and clean way to divide all of the AppDelegate responsibilities between multiple helper classes.

Picture

Just imagine, that instead of 2k lines of code monster you have multiple simple classes:

  • StartAppDelegate - configures application on start up,
  • AnalyticsAppDelegate - configures analytics services,
  • RemoteNotificationAppDelegate - handles push-notifications,
  • SpotlightAppDelegate - handles start from spotlight results,
  • HandoffAppDelegate - handles start from handoff,
  • DeepLinkAppDelegate - incapsulates deep linking logic,
  • and anything else.

Key features

  • Provides all the infrastructure - you should only create your own mini-AppDelegate classes with single responsibilities.
  • Has simple dependency injection system for injecting your mini-instances,
  • Battle-tested into a lot of Rambler&Co projects.

Installation

pod RamblerAppDelegateProxy

Usage

Create a RemoteNotificationAppDelegate class which conforms to UIApplicationDelegate protocol.

@interface RemoteNotificationAppDelegate : NSObject <UIApplicationDelegate>

@end
...
@implementation RemoteNotificationAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (notification) {
        NSLog(@"Launching from push %@", notification);
    }
    return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"Did register for remote notifications");
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
   NSLog(@"Did receive remote notification");}
}

@end

Change the main.m file implementation:

int main(int argc, char * argv[]) {
    @autoreleasepool {
        [[RamblerAppDelegateProxy injector] addAppDelegates:@[[RemoteNotificationAppDelegate new]]];

        return UIApplicationMain(argc, argv, nil, NSStringFromClass([RamblerAppDelegateProxy class]));
    }
}

Use your RemoteNotificationAppDelegate in the same way as the standart AppDelegate class - just remember to not mess its responsibilities.

Troubleshooting

Based on the concept, the proxy forwards only the methods defined in the protocol UIApplicationDelegate. If you need a additional method that must be implemented in AppDelegate, you need to create a subclass and implement this method in this class.

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

This subclass should be set as a default.

[[RamblerAppDelegateProxy injector] setDefaultAppDelegate:[AppDelegate new]];

This is necessary for example if you are using Typhoon. For injection depending in AppDelegates need to write a definition for RamblerAppDelegateProxy.

@implementation ApplicationAssembly

- (RamblerAppDelegateProxy *)applicationDelegateProxy {
    return [TyphoonDefinition withClass:[RamblerAppDelegateProxy class]
                          configuration:^(TyphoonDefinition *definition) {
                                [definition injectMethod:@selector(addAppDelegates:)
                                              parameters:^(TyphoonMethod *method) {
                                                    NSArray *appDelegates = @[
                                                        [self remoteNotificationAppDelegate]
                                                    ];
                                                    [method injectParameterWith:appDelegates];
                                                }];
                          }];
}

- (id<UIApplicationDelegate>)remoteNotificationAppDelegate {
    return [TyphoonDefinition withClass:[RCMLaunchingAppDelegate class]
                          configuration:^(TyphoonDefinition *definition) {
                          }];
}

@end

Authors

Vadim Smal

License

MIT

ramblerappdelegateproxy's People

Contributors

cognitivedisson avatar etolstoy avatar ybabenko avatar valeriyvan avatar

Watchers

James Cloos avatar yulong 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.