RNNRouter.m 4.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
//
//  NRNNManager.m
//  NRNNOtifications
//
//  Created by Muhammad Abed El Razek on 09/01/2019.
//  Copyright © 2019 Wix. All rights reserved.
//

#import "RNNRouter.h"
#import "RNNotifications.h"
//RNNNotifications's router (delegater) :::  singleton which routes all the static, system functions delegate calls to RNNNotifications insatnce ; can't have and RNNNotifications instance from outside of it's class




@interface RNNRouter()

@property(nonatomic,strong) NSMutableArray* pendingDelegateCalls;

@end

@implementation RNNRouter

+ (nonnull instancetype)sharedInstance
{
    static RNNRouter* sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [self new];
    });
    return sharedInstance;
}



- (instancetype)init
{
    self = [super init];
    if(self)
    {
        _pendingDelegateCalls = [NSMutableArray new];
        
    }
    return self;
}


- (void)setDelegate:(id<RNNRerouterDelegate,UNUserNotificationCenterDelegate>)delegate
{
    _delegate = delegate;
    
    while (self.pendingDelegateCalls.count > 0)
    {
        void(^block)(void) = _pendingDelegateCalls.lastObject;
        block();
        [_pendingDelegateCalls removeLastObject];
    }
    
    
}


/*
 ______           ______            _   _
 | ___ \          | ___ \          | | (_)
 | |_/ /___ ______| |_/ /___  _   _| |_ _ _ __   __ _
 |    // _ \______|    // _ \| | | | __| | '_ \ / _` |
 | |\ \  __/      | |\ \ (_) | |_| | |_| | | | | (_| |
 \_| \_\___|      \_| \_\___/ \__,_|\__|_|_| |_|\__, |
                                                 __/ |
                                                 |___/
 */
/////////////////////////////////////////////////////////////////
#pragma mark static calls for RNNNotifications rerouting purpous functions
////////////////////////////////////////////////////////////////
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
    void(^callLaterBlock)(void) = ^{
        [self->_delegate userNotificationCenter:center willPresentNotification:notification withCompletionHandler:completionHandler];
    };
    
    if(_delegate)
    {
        callLaterBlock();
    }
    else // //called and _delegate is not set yet, need to store to call later
    {
        [_pendingDelegateCalls insertObject:callLaterBlock atIndex:0];
    }
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
    void(^callLaterBlock)(void) = ^{
        [self->_delegate userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
    };
    
    if(_delegate)
    {
        callLaterBlock();
    }
    else //called and _delegate is not set yet, need to store to call later
    {
        [_pendingDelegateCalls insertObject:callLaterBlock atIndex:0];
    }
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    if (self.delegate != nil)
    {
        [self.delegate application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
    }
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    if (self.delegate != nil)
    {
        [self.delegate application:application didFailToRegisterForRemoteNotificationsWithError:error];
    }
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    if (self.delegate != nil)
    {
        [self.delegate application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
    }
}


- (void)didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type
{
    if(self.delegate != nil)
    {
        [self.delegate handlePushKitRegistered:@{@"pushKitToken": [RNNotifications deviceTokenToString:credentials.token]}];
    }
}

- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type
{
    if (self.delegate != nil)
    {
        [self.delegate application:nil didReceiveRemoteNotification:payload.dictionaryPayload fetchCompletionHandler:nil];
    }
}

@end