summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLSubscribeButtonManager.m
blob: d09c49144b52708e4a833a803e501d2ca1e22f98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
//  SDLSDLSubscribeButtonManager.m
//  SmartDeviceLink
//
//  Created by Nicole on 5/21/20.
//  Copyright © 2020 smartdevicelink. All rights reserved.
//

#import "SDLSubscribeButtonManager.h"

#import "SDLError.h"
#import "SDLLogMacros.h"
#import "SDLOnHMIStatus.h"
#import "SDLPredefinedWindows.h"
#import "SDLRPCNotificationNotification.h"
#import "SDLSubscribeButton.h"
#import "SDLSubscribeButtonObserver.h"

NS_ASSUME_NONNULL_BEGIN

@interface SDLSubscribeButtonManager()

@property (weak, nonatomic) id<SDLConnectionManagerType> connectionManager;
@property (weak, nonatomic) SDLSystemCapabilityManager *systemCapabilityManager;

@property (strong, nonatomic) NSMutableDictionary<SDLButtonName, NSMutableArray<SDLSubscribeButtonObserver *> *> *subscribeButtonObservers;
@property (copy, nonatomic, nullable) SDLHMILevel currentHMILevel;

@end

@implementation SDLSubscribeButtonManager

#pragma mark - Lifecycle

- (instancetype)initWithConnectionManager:(id<SDLConnectionManagerType>)connectionManager systemCapabilityManager:(SDLSystemCapabilityManager *)systemCapabilityManager {
    self = [super init];
    if (!self) { return nil; }

    _connectionManager = connectionManager;
    _systemCapabilityManager = systemCapabilityManager;

    _subscribeButtonObservers = [NSMutableDictionary dictionary];
    _currentHMILevel = nil;

    [self sdl_registerForNotifications];

    return self;
}

- (void)start { }

- (void)stop {
    [_subscribeButtonObservers removeAllObjects];

    _currentHMILevel = nil;
}

#pragma mark - Subscriptions

- (nullable id<NSObject>)subscribeButton:(SDLButtonName)buttonName withBlock:(nullable SDLSubscribeButtonUpdateHandler)block {
    return nil;
}

- (BOOL)subscribeButton:(SDLButtonName)buttonName withObserver:(id<NSObject>)observer selector:(SEL)selector; {
    SDLLogD(@"Subscribing to subscribe button with name: %@, with observer: %@, selector: %@", buttonName, observer, NSStringFromSelector(selector));

    NSUInteger numberOfParametersInSelector = [NSStringFromSelector(selector) componentsSeparatedByString:@":"].count - 1;
    if (numberOfParametersInSelector > 4) {
        SDLLogE(@"Attempted to subscribe to a subscribe button using a selector that contains more than 4 parameters.");
        return NO;
    }

    if (observer == nil) {
        SDLLogE(@"Attempted to subscribe to subscribe button with name: %@ with a selector on a *nil* observer, which will always fail.", buttonName);
        return NO;
    }

    SDLSubscribeButtonObserver *observerObject = [[SDLSubscribeButtonObserver alloc] initWithObserver:observer selector:selector];

    [self.subscribeButtonObservers[buttonName] addObject:observerObject];

    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];
        }
    }];

    [self.connectionManager sendConnectionRequest:subscribeButton withResponseHandler:^(__kindof SDLRPCRequest * _Nullable request, __kindof SDLRPCResponse * _Nullable response, NSError * _Nullable error) {
        if (error == nil) { return; }
        // If there was an error during the subscription attempt, return the error message.
        [self sdl_invokeObserver:observerObject withButtonName:buttonName buttonPress:nil buttonEvent:nil error:error];
    }];

    return YES;
}

- (void)unsubscribeButton:(SDLButtonName)buttonName withObserver:(id<NSObject>)observer withCompletionHandler:(nullable SDLSubscribeButtonUpdateCompletionHandler)block {

}

#pragma mark - Notifications

/// Registers for notifications and responses from Core
- (void)sdl_registerForNotifications {
    SDLLogV(@"Registering for notifications");
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sdl_hmiStatusNotification:) name:SDLDidChangeHMIStatusNotification object:nil];
}

- (void)sdl_hmiStatusNotification:(SDLRPCNotificationNotification *)notification {
    SDLOnHMIStatus *hmiStatus = (SDLOnHMIStatus *)notification.notification;

    if (hmiStatus.windowID != nil && hmiStatus.windowID.integerValue != SDLPredefinedWindowsDefaultWindow) {
        return;
    }

    self.currentHMILevel = hmiStatus.hmiLevel;
}

#pragma mark - Subscriptions

- (void)sdl_invokeObserver:(SDLSubscribeButtonObserver *)observer withButtonName:(SDLButtonName)buttonName buttonPress:(nullable SDLOnButtonPress *)buttonPress buttonEvent:(nullable SDLOnButtonEvent *)buttonEvent  error:(nullable NSError *)error {

    if (observer.updateBlock != nil) {
        observer.updateBlock(buttonPress, buttonEvent, error);
    } else {
        if ([observer.observer respondsToSelector:observer.selector]) {
            @throw [NSException sdl_invalidSelectorExceptionWithSelector:observer.selector];
        }

        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[(NSObject *)observer.observer methodSignatureForSelector:observer.selector]];
        [invocation setSelector:observer.selector];
        [invocation setTarget:observer.observer];

        NSUInteger numberOfParametersInSelector = [NSStringFromSelector(observer.selector) componentsSeparatedByString:@":"].count - 1;
        if (numberOfParametersInSelector >= 1) {
            [invocation setArgument:&buttonName atIndex:2];
        }
        if (numberOfParametersInSelector >= 2) {
            [invocation setArgument:&buttonPress atIndex:3];
        }
        if (numberOfParametersInSelector >= 3) {
            [invocation setArgument:&buttonEvent atIndex:4];
        }
        if (numberOfParametersInSelector >= 4) {
            [invocation setArgument:&error atIndex:5];
        }
        if (numberOfParametersInSelector >= 5) {
            @throw [NSException sdl_invalidSelectorExceptionWithSelector:observer.selector];
        }

        [invocation invoke];
    }
}

@end

NS_ASSUME_NONNULL_END