Skip to content

Commit

Permalink
Merge pull request #1271 from smartdevicelink/feature/issue_1257_sdlm…
Browse files Browse the repository at this point in the history
…anager_rpc_subscriptions

Implement SDL-0226: SDLManager RPC Subscription Helper Methods
  • Loading branch information
joeljfischer committed May 24, 2019
2 parents c080f3e + 7292417 commit 24adfae
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Example Apps/Example ObjC/VehicleDataManager.m
Expand Up @@ -37,7 +37,7 @@ - (instancetype)initWithManager:(SDLManager *)manager refreshUIHandler:(RefreshU
_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;
Expand Down Expand Up @@ -112,7 +112,7 @@ - (void)vehicleDataNotification:(SDLRPCNotificationNotification *)notification {
}

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();
Expand Down
2 changes: 1 addition & 1 deletion Example Apps/Example Swift/VehicleDataManager.swift
Expand Up @@ -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(_:)))
}
}

Expand Down
40 changes: 40 additions & 0 deletions SmartDeviceLink/SDLManager.h
Expand Up @@ -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
21 changes: 21 additions & 0 deletions SmartDeviceLink/SDLManager.m
Expand Up @@ -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"
Expand Down Expand Up @@ -143,6 +146,24 @@ - (void)sendSequentialRequests:(NSArray<SDLRPCRequest *> *)requests progressHand
[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

0 comments on commit 24adfae

Please sign in to comment.