diff options
author | NicoleYarroch <nicole@livio.io> | 2020-05-22 11:26:20 -0400 |
---|---|---|
committer | NicoleYarroch <nicole@livio.io> | 2020-05-22 11:26:20 -0400 |
commit | 0119d13be92ae2d21cef603ed46b4e0af330384e (patch) | |
tree | c987eaa02c26ed4a6118b5ede082e89774e36bf5 | |
parent | 9a0bed42219748d9edae4a7b9397129a8bd2be08 (diff) | |
download | sdl_ios-0119d13be92ae2d21cef603ed46b4e0af330384e.tar.gz |
Added unsubscribe logic
-rw-r--r-- | SmartDeviceLink/SDLScreenManager.h | 4 | ||||
-rw-r--r-- | SmartDeviceLink/SDLScreenManager.m | 8 | ||||
-rw-r--r-- | SmartDeviceLink/SDLSubscribeButtonManager.h | 4 | ||||
-rw-r--r-- | SmartDeviceLink/SDLSubscribeButtonManager.m | 29 |
4 files changed, 34 insertions, 11 deletions
diff --git a/SmartDeviceLink/SDLScreenManager.h b/SmartDeviceLink/SDLScreenManager.h index a0431cd53..99101459b 100644 --- a/SmartDeviceLink/SDLScreenManager.h +++ b/SmartDeviceLink/SDLScreenManager.h @@ -260,9 +260,9 @@ If set to `SDLDynamicMenuUpdatesModeForceOff`, menu updates will work the legacy #pragma mark Subscribe Buttons -- (nullable id<NSObject>)subscribeButton:(SDLButtonName)buttonName withBlock:(nullable SDLSubscribeButtonHandler)block; +- (nullable id<NSObject>)subscribeButton:(SDLButtonName)buttonName withUpdateHandler:(nullable SDLSubscribeButtonHandler)updateHandler; - (BOOL)subscribeButton:(SDLButtonName)buttonName withObserver:(id<NSObject>)observer selector:(SEL)selector; -- (void)unsubscribeButton:(SDLButtonName)buttonName withObserver:(id<NSObject>)observer withCompletionHandler:(nullable SDLScreenManagerUpdateCompletionHandler)block; +- (void)unsubscribeButton:(SDLButtonName)buttonName withObserver:(id<NSObject>)observer withCompletionHandler:(nullable SDLScreenManagerUpdateCompletionHandler)completionHandler; #pragma mark Choice Sets diff --git a/SmartDeviceLink/SDLScreenManager.m b/SmartDeviceLink/SDLScreenManager.m index 2a364214a..8d7828059 100644 --- a/SmartDeviceLink/SDLScreenManager.m +++ b/SmartDeviceLink/SDLScreenManager.m @@ -263,16 +263,16 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark Subscribe Buttons -- (nullable id<NSObject>)subscribeButton:(SDLButtonName)buttonName withBlock:(nullable SDLSubscribeButtonHandler)block { - return [self.subscribeButtonManager subscribeButton:buttonName withBlock:block]; +- (nullable id<NSObject>)subscribeButton:(SDLButtonName)buttonName withUpdateHandler:(nullable SDLSubscribeButtonHandler)updateHandler { + return [self.subscribeButtonManager subscribeButton:buttonName withUpdateHandler:updateHandler]; } - (BOOL)subscribeButton:(SDLButtonName)buttonName withObserver:(id<NSObject>)observer selector:(SEL)selector { return [self.subscribeButtonManager subscribeButton:buttonName withObserver:observer selector:selector]; } -- (void)unsubscribeButton:(SDLButtonName)buttonName withObserver:(id<NSObject>)observer withCompletionHandler:(nullable SDLScreenManagerUpdateCompletionHandler)block { - [self.subscribeButtonManager unsubscribeButton:buttonName withObserver:observer withCompletionHandler:block]; +- (void)unsubscribeButton:(SDLButtonName)buttonName withObserver:(id<NSObject>)observer withCompletionHandler:(nullable SDLScreenManagerUpdateCompletionHandler)completionHandler { + [self.subscribeButtonManager unsubscribeButton:buttonName withObserver:observer withCompletionHandler:completionHandler]; } #pragma mark - Choice Sets diff --git a/SmartDeviceLink/SDLSubscribeButtonManager.h b/SmartDeviceLink/SDLSubscribeButtonManager.h index 02a444da6..d7a12a464 100644 --- a/SmartDeviceLink/SDLSubscribeButtonManager.h +++ b/SmartDeviceLink/SDLSubscribeButtonManager.h @@ -42,7 +42,7 @@ typedef void(^SDLSubscribeButtonUpdateCompletionHandler)(NSError *__nullable err /// @param buttonName The name of the subscribe button /// @param block The block run when the subscribe button is selected /// @return An object that can be used to unsubscribe the block using `unsubscribeButtonWithObserver:withCompletionHandler:`. If `nil` the manager was not able attempt the subscription for some reason (such as the app being in HMI_NONE). -- (nullable id<NSObject>)subscribeButton:(SDLButtonName)buttonName withBlock:(nullable SDLSubscribeButtonUpdateHandler)block; +- (nullable id<NSObject>)subscribeButton:(SDLButtonName)buttonName withUpdateHandler:(nullable SDLSubscribeButtonUpdateHandler)block; /// Subscribes to a subscribe button with the button name. /// @param buttonName The name of the subscribe button @@ -54,7 +54,7 @@ typedef void(^SDLSubscribeButtonUpdateCompletionHandler)(NSError *__nullable err /// @param buttonName The name of the subscribe button /// @param observer The object that will be unsubscribed. If a block was subscribed, the value returned by the subscription method should be passed. If a selector was subscribed, the observer object should be passed. /// @param block The block run when the subscribe button is unsubscribed -- (void)unsubscribeButton:(SDLButtonName)buttonName withObserver:(id<NSObject>)observer withCompletionHandler:(nullable SDLSubscribeButtonUpdateCompletionHandler)block; +- (void)unsubscribeButton:(SDLButtonName)buttonName withObserver:(id<NSObject>)observer withCompletionHandler:(nullable SDLSubscribeButtonUpdateCompletionHandler)completionHandler; @end diff --git a/SmartDeviceLink/SDLSubscribeButtonManager.m b/SmartDeviceLink/SDLSubscribeButtonManager.m index d09c49144..01dc0ccb2 100644 --- a/SmartDeviceLink/SDLSubscribeButtonManager.m +++ b/SmartDeviceLink/SDLSubscribeButtonManager.m @@ -15,6 +15,8 @@ #import "SDLRPCNotificationNotification.h" #import "SDLSubscribeButton.h" #import "SDLSubscribeButtonObserver.h" +#import "SDLUnsubscribeButton.h" +#import "SDLUnsubscribeButtonResponse.h" NS_ASSUME_NONNULL_BEGIN @@ -57,7 +59,7 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - Subscriptions -- (nullable id<NSObject>)subscribeButton:(SDLButtonName)buttonName withBlock:(nullable SDLSubscribeButtonUpdateHandler)block { +- (nullable id<NSObject>)subscribeButton:(SDLButtonName)buttonName withUpdateHandler:(nullable SDLSubscribeButtonUpdateHandler)block { return nil; } @@ -76,9 +78,13 @@ NS_ASSUME_NONNULL_BEGIN } SDLSubscribeButtonObserver *observerObject = [[SDLSubscribeButtonObserver alloc] initWithObserver:observer selector:selector]; - [self.subscribeButtonObservers[buttonName] addObject:observerObject]; + if (self.subscribeButtonObservers[buttonName] != nil) { + // This button was already subscribed + return YES; + } + SDLSubscribeButton *subscribeButton = [[SDLSubscribeButton alloc] initWithButtonName:buttonName handler:^(SDLOnButtonPress * _Nullable buttonPress, SDLOnButtonEvent * _Nullable buttonEvent) { for (SDLSubscribeButtonObserver *subscribeButtonObserver in self.subscribeButtonObservers[buttonName]) { [self sdl_invokeObserver:subscribeButtonObserver withButtonName:buttonName buttonPress:buttonPress buttonEvent:buttonEvent error:nil]; @@ -94,8 +100,25 @@ NS_ASSUME_NONNULL_BEGIN return YES; } -- (void)unsubscribeButton:(SDLButtonName)buttonName withObserver:(id<NSObject>)observer withCompletionHandler:(nullable SDLSubscribeButtonUpdateCompletionHandler)block { +- (void)unsubscribeButton:(SDLButtonName)buttonName withObserver:(id<NSObject>)observer withCompletionHandler:(nullable SDLSubscribeButtonUpdateCompletionHandler)completionHandler { + if (self.subscribeButtonObservers[buttonName] == nil) { + SDLLogE(@"Attempting to unsubscribe to subscribe button, %@, that is not currently subscribed", buttonName); + // TODO return custom error in the completion handler + return; + } + + SDLUnsubscribeButton *unsubscribeButton = [[SDLUnsubscribeButton alloc] initWithButtonName:buttonName]; + [self.connectionManager sendConnectionRequest:unsubscribeButton withResponseHandler:^(__kindof SDLRPCRequest * _Nullable request, __kindof SDLRPCResponse * _Nullable response, NSError * _Nullable error) { + SDLUnsubscribeButtonResponse *unsubscribeButtonResponse = (SDLUnsubscribeButtonResponse *)response; + if (unsubscribeButtonResponse == nil || unsubscribeButtonResponse.success == false) { + SDLLogE(@"Attempt to unsubscribe to subscribe button, %@, failed", buttonName); + return completionHandler(error); + } + + SDLLogD(@"Successfully unsubscribed to subscribe button: %@", buttonName); + self.subscribeButtonObservers[buttonName] = nil; + }]; } #pragma mark - Notifications |