diff options
author | Joel Fischer <joeljfischer@gmail.com> | 2019-05-24 10:00:22 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-24 10:00:22 -0400 |
commit | 24adfae46f9fe883ee5fe59d7a02a494b40f9662 (patch) | |
tree | 1676c208bcd0d614352d2699477382c32e63f493 | |
parent | c080f3e0084ea502930c22749e18eb1e0f41f524 (diff) | |
parent | 7292417f285b42bcf62538f7bac4ca09e70e6ee1 (diff) | |
download | sdl_ios-24adfae46f9fe883ee5fe59d7a02a494b40f9662.tar.gz |
Merge pull request #1271 from smartdevicelink/feature/issue_1257_sdlmanager_rpc_subscriptions
Implement SDL-0226: SDLManager RPC Subscription Helper Methods
-rw-r--r-- | Example Apps/Example ObjC/VehicleDataManager.m | 4 | ||||
-rw-r--r-- | Example Apps/Example Swift/VehicleDataManager.swift | 2 | ||||
-rw-r--r-- | SmartDeviceLink/SDLManager.h | 40 | ||||
-rw-r--r-- | SmartDeviceLink/SDLManager.m | 21 |
4 files changed, 64 insertions, 3 deletions
diff --git a/Example Apps/Example ObjC/VehicleDataManager.m b/Example Apps/Example ObjC/VehicleDataManager.m index 999a22616..cc2ee46fa 100644 --- a/Example Apps/Example ObjC/VehicleDataManager.m +++ b/Example Apps/Example ObjC/VehicleDataManager.m @@ -37,7 +37,7 @@ NS_ASSUME_NONNULL_BEGIN _refreshUIHandler = refreshUIHandler; _vehicleOdometerData = @""; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(vehicleDataNotification:) name:SDLDidReceiveVehicleDataNotification object:nil]; + [_sdlManager subscribeToRPC:SDLDidReceiveVehicleDataNotification withObserver:self selector:@selector(vehicleDataNotification:)]; [self sdlex_resetOdometer]; return self; @@ -112,7 +112,7 @@ NS_ASSUME_NONNULL_BEGIN } SDLOnVehicleData *onVehicleData = (SDLOnVehicleData *)notification.notification; - self.vehicleOdometerData = [NSString stringWithFormat:@"%@: %@ kph", VehicleDataOdometerName, onVehicleData.odometer]; + self.vehicleOdometerData = [NSString stringWithFormat:@"%@: %@ km", VehicleDataOdometerName, onVehicleData.odometer]; if (!self.refreshUIHandler) { return; } self.refreshUIHandler(); diff --git a/Example Apps/Example Swift/VehicleDataManager.swift b/Example Apps/Example Swift/VehicleDataManager.swift index c5e2b187e..1d78edd42 100644 --- a/Example Apps/Example Swift/VehicleDataManager.swift +++ b/Example Apps/Example Swift/VehicleDataManager.swift @@ -23,7 +23,7 @@ class VehicleDataManager: NSObject { super.init() resetOdometer() - NotificationCenter.default.addObserver(self, selector: #selector(vehicleDataNotification(_:)), name: .SDLDidReceiveVehicleData, object: nil) + self.sdlManager.subscribe(to: .SDLDidReceiveVehicleData, observer: self, selector: #selector(vehicleDataNotification(_:))) } } diff --git a/SmartDeviceLink/SDLManager.h b/SmartDeviceLink/SDLManager.h index 91aece5e5..58a234923 100644 --- a/SmartDeviceLink/SDLManager.h +++ b/SmartDeviceLink/SDLManager.h @@ -178,6 +178,46 @@ typedef void (^SDLManagerReadyBlock)(BOOL success, NSError *_Nullable error); */ - (void)sendSequentialRequests:(NSArray<SDLRPCRequest *> *)requests progressHandler:(nullable SDLMultipleSequentialRequestProgressHandler)progressHandler completionHandler:(nullable SDLMultipleRequestCompletionHandler)completionHandler NS_SWIFT_NAME(sendSequential(requests:progressHandler:completionHandler:)); + +#pragma mark - RPC Subscriptions + +typedef void (^SDLRPCUpdatedBlock) (__kindof SDLRPCMessage *); + +/** + * Subscribe to callbacks about a particular RPC request, notification, or response with a block callback. + * + * @param rpcName The name of the RPC request, response, or notification to subscribe to. + * @param block The block that will be called every time an RPC of the name and type specified is received. + * @return An object that can be passed to `unsubscribeFromRPC:ofType:withObserver:` to unsubscribe the block. + */ +- (id)subscribeToRPC:(SDLNotificationName)rpcName withBlock:(SDLRPCUpdatedBlock)block NS_SWIFT_NAME(subscribe(to:block:)); + +/** + * Subscribe to callbacks about a particular RPC request, notification, or response with a selector callback. + * + * The selector supports the following parameters: + * + * 1. One parameter e.g. `- (void)registerAppInterfaceResponse:(SDLRegisterAppInterfaceResponse *)response;` + * 2. Two parameters e.g. `- (void)registerAppInterfaceResponse:(SDLRegisterAppInterfaceResponse *)response error:(nullable NSError *)error;` + * + * Note that using this method to get a response instead of the `sendRequest:withResponseHandler:` method of getting a response, you will not be notifed of any `SDLGenericResponse` errors where the head unit doesn't understand the request. + * + * The error will be called if `response.success` is `NO` and will be filled with the info field. + * + * @param rpcName The name of the RPC request, response, or notification to subscribe to. + * @param observer The object that will have its selector called every time an RPC of the name and type specified is received. + * @param selector The selector on `observer` that will be called every time an RPC of the name and type specified is received. + */ +- (void)subscribeToRPC:(SDLNotificationName)rpcName withObserver:(id)observer selector:(SEL)selector NS_SWIFT_NAME(subscribe(to:observer:selector:)); + +/** + * Unsubscribe to callbacks about a particular RPC request, notification, or response. + * + * @param rpcName The name of the RPC request, response, or notification to unsubscribe from. + * @param observer The object representing a block callback or selector callback to be unsubscribed + */ +- (void)unsubscribeFromRPC:(SDLNotificationName)rpcName withObserver:(id)observer NS_SWIFT_NAME(unsubscribe(from:observer:)); + @end NS_ASSUME_NONNULL_END diff --git a/SmartDeviceLink/SDLManager.m b/SmartDeviceLink/SDLManager.m index 0cf027401..b198f7e52 100644 --- a/SmartDeviceLink/SDLManager.m +++ b/SmartDeviceLink/SDLManager.m @@ -17,6 +17,9 @@ #import "SDLManagerDelegate.h" #import "SDLNotificationDispatcher.h" #import "SDLResponseDispatcher.h" +#import "SDLRPCRequestNotification.h" +#import "SDLRPCResponseNotification.h" +#import "SDLRPCNotificationNotification.h" #import "SDLSoftButtonManager.h" #import "SDLStateMachine.h" #import "SDLTextAndGraphicManager.h" @@ -143,6 +146,24 @@ NS_ASSUME_NONNULL_BEGIN [self.lifecycleManager sendSequentialRequests:requests progressHandler:progressHandler completionHandler:completionHandler]; } + +#pragma mark - RPC Subscriptions + +- (id)subscribeToRPC:(SDLNotificationName)rpcName withBlock:(SDLRPCUpdatedBlock)block { + return [[NSNotificationCenter defaultCenter] addObserverForName:rpcName object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) { + SDLRPCMessage *message = note.userInfo[SDLNotificationUserInfoObject]; + block(message); + }]; +} + +- (void)subscribeToRPC:(SDLNotificationName)rpcName withObserver:(id)observer selector:(SEL)selector { + [[NSNotificationCenter defaultCenter] addObserver:observer selector:selector name:rpcName object:nil]; +} + +- (void)unsubscribeFromRPC:(SDLNotificationName)rpcName withObserver:(id)observer { + [[NSNotificationCenter defaultCenter] removeObserver:observer name:rpcName object:nil]; +} + @end NS_ASSUME_NONNULL_END |