Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SDL-0226: SDLManager RPC Subscription Helper Methods #1271

Merged
merged 3 commits into from May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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