diff options
author | Joel Fischer <joeljfischer@gmail.com> | 2019-05-23 16:24:08 -0400 |
---|---|---|
committer | Joel Fischer <joeljfischer@gmail.com> | 2019-05-23 16:24:08 -0400 |
commit | 3fce4d2ccd98ce1afc4476947aa756622f26f8dd (patch) | |
tree | decaaddbcf831b92e84df8586d860fee21ea7e8b | |
parent | c3c67cad6707c4958b9140869e3e5a1aa52f5439 (diff) | |
download | sdl_ios-3fce4d2ccd98ce1afc4476947aa756622f26f8dd.tar.gz |
Pass the capability instead of the manager into the callback
* Add documentation
-rw-r--r-- | SmartDeviceLink/SDLSystemCapabilityManager.h | 8 | ||||
-rw-r--r-- | SmartDeviceLink/SDLSystemCapabilityManager.m | 23 | ||||
-rw-r--r-- | SmartDeviceLink/SDLSystemCapabilityObserver.h | 36 |
3 files changed, 47 insertions, 20 deletions
diff --git a/SmartDeviceLink/SDLSystemCapabilityManager.h b/SmartDeviceLink/SDLSystemCapabilityManager.h index ea5e6764b..7d89cd677 100644 --- a/SmartDeviceLink/SDLSystemCapabilityManager.h +++ b/SmartDeviceLink/SDLSystemCapabilityManager.h @@ -43,11 +43,13 @@ typedef void (^SDLUpdateCapabilityHandler)(NSError * _Nullable error, SDLSystemC /** An observer block whenever a subscription is called. - @param systemCapabilityManager This manager. The user of the handler can then use the manager to pull the newest data. + @param capability The capability that was updated. */ -typedef void (^SDLCapabilityUpdateHandler)(SDLSystemCapabilityManager *systemCapabilityManager); - +typedef void (^SDLCapabilityUpdateHandler)(SDLSystemCapability *capability); +/** + A manager that handles updating and subscribing to SDL capabilities. + */ @interface SDLSystemCapabilityManager : NSObject /** diff --git a/SmartDeviceLink/SDLSystemCapabilityManager.m b/SmartDeviceLink/SDLSystemCapabilityManager.m index bf1f2b168..a7ffa785a 100644 --- a/SmartDeviceLink/SDLSystemCapabilityManager.m +++ b/SmartDeviceLink/SDLSystemCapabilityManager.m @@ -289,27 +289,28 @@ typedef NSString * SDLServiceID; */ - (BOOL)sdl_saveSystemCapability:(SDLSystemCapability *)systemCapability completionHandler:(nullable SDLUpdateCapabilityHandler)handler { if ([self.lastReceivedCapability isEqual:systemCapability]) { - return [self sdl_callSaveHandlerForCapabilityType:systemCapability.systemCapabilityType andReturnWithValue:NO handler:handler]; + return [self sdl_callSaveHandlerForCapability:systemCapability andReturnWithValue:NO handler:handler]; } self.lastReceivedCapability = systemCapability; SDLSystemCapabilityType systemCapabilityType = systemCapability.systemCapabilityType; if ([systemCapabilityType isEqualToEnum:SDLSystemCapabilityTypePhoneCall]) { - if ([self.phoneCapability isEqual:systemCapability.phoneCapability]) { return [self sdl_callSaveHandlerForCapabilityType:systemCapabilityType andReturnWithValue:NO handler:handler]; } + if ([self.phoneCapability isEqual:systemCapability.phoneCapability]) { return [self sdl_callSaveHandlerForCapability:systemCapability andReturnWithValue:NO handler:handler]; } self.phoneCapability = systemCapability.phoneCapability; } else if ([systemCapabilityType isEqualToEnum:SDLSystemCapabilityTypeNavigation]) { - if ([self.navigationCapability isEqual:systemCapability.navigationCapability]) { return [self sdl_callSaveHandlerForCapabilityType:systemCapabilityType andReturnWithValue:NO handler:handler]; } + if ([self.navigationCapability isEqual:systemCapability.navigationCapability]) { return [self sdl_callSaveHandlerForCapability:systemCapability andReturnWithValue:NO handler:handler]; } self.navigationCapability = systemCapability.navigationCapability; } else if ([systemCapabilityType isEqualToEnum:SDLSystemCapabilityTypeRemoteControl]) { - if ([self.remoteControlCapability isEqual:systemCapability.remoteControlCapability]) { return [self sdl_callSaveHandlerForCapabilityType:systemCapabilityType andReturnWithValue:NO handler:handler]; } + if ([self.remoteControlCapability isEqual:systemCapability.remoteControlCapability]) { return [self sdl_callSaveHandlerForCapability:systemCapability andReturnWithValue:NO handler:handler]; } self.remoteControlCapability = systemCapability.remoteControlCapability; } else if ([systemCapabilityType isEqualToEnum:SDLSystemCapabilityTypeVideoStreaming]) { - if ([self.videoStreamingCapability isEqual:systemCapability.videoStreamingCapability]) { return [self sdl_callSaveHandlerForCapabilityType:systemCapabilityType andReturnWithValue:NO handler:handler]; } + if ([self.videoStreamingCapability isEqual:systemCapability.videoStreamingCapability]) { return [self sdl_callSaveHandlerForCapability:systemCapability andReturnWithValue:NO handler:handler]; } self.videoStreamingCapability = systemCapability.videoStreamingCapability; } else if ([systemCapabilityType isEqualToEnum:SDLSystemCapabilityTypeAppServices]) { - if ([self.appServicesCapabilities isEqual:systemCapability.appServicesCapabilities]) { return [self sdl_callSaveHandlerForCapabilityType:systemCapabilityType andReturnWithValue:NO handler:handler]; } + if ([self.appServicesCapabilities isEqual:systemCapability.appServicesCapabilities]) { return [self sdl_callSaveHandlerForCapability:systemCapability andReturnWithValue:NO handler:handler]; } [self sdl_saveAppServicesCapabilitiesUpdate:systemCapability.appServicesCapabilities]; + systemCapability = [[SDLSystemCapability alloc] initWithAppServicesCapabilities:self.appServicesCapabilities]; } else { SDLLogW(@"Received response for unknown System Capability Type: %@", systemCapabilityType); return NO; @@ -317,13 +318,13 @@ typedef NSString * SDLServiceID; SDLLogD(@"Updated system capability manager with new data: %@", systemCapability); - return [self sdl_callSaveHandlerForCapabilityType:systemCapabilityType andReturnWithValue:YES handler:handler]; + return [self sdl_callSaveHandlerForCapability:systemCapability andReturnWithValue:YES handler:handler]; } -- (BOOL)sdl_callSaveHandlerForCapabilityType:(SDLSystemCapabilityType)type andReturnWithValue:(BOOL)value handler:(nullable SDLUpdateCapabilityHandler)handler { - for (SDLSystemCapabilityObserver *observer in self.capabilityObservers[type]) { +- (BOOL)sdl_callSaveHandlerForCapability:(SDLSystemCapability *)capability andReturnWithValue:(BOOL)value handler:(nullable SDLUpdateCapabilityHandler)handler { + for (SDLSystemCapabilityObserver *observer in self.capabilityObservers[capability.systemCapabilityType]) { if (observer.block != nil) { - observer.block(self); + observer.block(capability); } else { NSUInteger numberOfParametersInSelector = [NSStringFromSelector(observer.selector) componentsSeparatedByString:@":"].count - 1; #pragma clang diagnostic push @@ -334,7 +335,7 @@ typedef NSString * SDLServiceID; } } else if (numberOfParametersInSelector == 1) { if ([observer.observer respondsToSelector:observer.selector]) { - [observer.observer performSelector:observer.selector withObject:self]; + [observer.observer performSelector:observer.selector withObject:capability]; } } else { @throw [NSException sdl_invalidSelectorExceptionWithSelector:observer.selector]; diff --git a/SmartDeviceLink/SDLSystemCapabilityObserver.h b/SmartDeviceLink/SDLSystemCapabilityObserver.h index 79f8590c4..f5450d60e 100644 --- a/SmartDeviceLink/SDLSystemCapabilityObserver.h +++ b/SmartDeviceLink/SDLSystemCapabilityObserver.h @@ -8,24 +8,48 @@ #import <Foundation/Foundation.h> -@class SDLSystemCapabilityManager; +@class SDLSystemCapability; NS_ASSUME_NONNULL_BEGIN -/** - An observer block whenever a subscription is called. +typedef void (^SDLCapabilityUpdateHandler)(SDLSystemCapability *capability); - @param systemCapabilityManager This manager. The user of the handler can then use the manager to pull the newest data. +/** + An observer object for SDLSystemCapabilityManager */ -typedef void (^SDLCapabilityUpdateHandler)(SDLSystemCapabilityManager *systemCapabilityManager); - @interface SDLSystemCapabilityObserver : NSObject +/** + The object that will be used to call the selector if available, and to unsubscribe this observer + */ @property (strong, nonatomic) id<NSObject> observer; + +/** + A selector called when the observer is triggered + */ @property (assign, nonatomic) SEL selector; + +/** + A block called when the observer is triggered + */ @property (copy, nonatomic) SDLCapabilityUpdateHandler block; +/** + Create an observer using an object and a selector on that object + + @param observer The object to be called when the subscription triggers + @param selector The selector to be called when the subscription triggers + @return The observer + */ - (instancetype)initWithObserver:(id<NSObject>)observer selector:(SEL)selector; + +/** + Create an observer using an object and a callback block + + @param observer The object that can be used to unsubscribe the block + @param block The block that will be called when the subscription triggers + @return The observer + */ - (instancetype)initWithObserver:(id<NSObject>)observer block:(SDLCapabilityUpdateHandler)block; @end |