summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/private/SDLMenuReplaceUtilities.m
blob: b71488a9a2ab56cf856f1eb31a5421bf133c2426 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//
//  SDLMenuReplaceUtilities.m
//  SmartDeviceLink
//
//  Created by Joel Fischer on 1/22/21.
//  Copyright © 2021 smartdevicelink. All rights reserved.
//

#import "SDLMenuReplaceUtilities.h"

#import "SDLAddCommand.h"
#import "SDLAddSubMenu.h"
#import "SDLArtwork.h"
#import "SDLDeleteCommand.h"
#import "SDLDeleteSubMenu.h"
#import "SDLFileManager.h"
#import "SDLImage.h"
#import "SDLImageFieldName.h"
#import "SDLMenuCell.h"
#import "SDLMenuParams.h"
#import "SDLMenuManagerPrivateConstants.h"
#import "SDLRPCRequest.h"
#import "SDLWindowCapability.h"
#import "SDLWindowCapability+ScreenManagerExtensions.h"

@interface SDLMenuCell ()

@property (assign, nonatomic) UInt32 parentCellId;
@property (assign, nonatomic) UInt32 cellId;
@property (copy, nonatomic, readwrite, nullable) NSArray<SDLMenuCell *> *subCells;

@end

@interface SDLMenuReplaceUtilities ()

@property (class, assign, nonatomic) UInt32 nextMenuId;

@end

@implementation SDLMenuReplaceUtilities
static UInt32 _menuId = 0;

#pragma mark Ids

+ (void)setNextMenuId:(UInt32)nextMenuId {
    _menuId = nextMenuId;
}

+ (UInt32)nextMenuId {
    return ++_menuId;
}

/// Assign cell ids on an array of menu cells given a parent id (or no parent id)
/// @param menuCells The array of menu cells to update
/// @param parentId The parent id to assign if needed
+ (void)addIdsToMenuCells:(NSArray<SDLMenuCell *> *)menuCells parentId:(UInt32)parentId {
    for (SDLMenuCell *cell in menuCells) {
        cell.cellId = self.class.nextMenuId;
        if (parentId != ParentIdNotFound) {
            cell.parentCellId = parentId;
        }
        if (cell.subCells.count > 0) {
            [self addIdsToMenuCells:cell.subCells parentId:cell.cellId];
        }
    }
}

+ (void)transferCellIDsFromCells:(NSArray<SDLMenuCell *> *)fromCells toCells:(NSArray<SDLMenuCell *> *)toCells {
    if (fromCells.count == 0 || fromCells.count != toCells.count) { return; }
    for (NSUInteger i = 0; i < toCells.count; i++) {
        toCells[i].cellId = fromCells[i].cellId;
    }

    // Update parent ids
    for (SDLMenuCell *cell in toCells) {
        if (cell.subCells == nil) { continue; }

        for (SDLMenuCell *subCell in cell.subCells) {
            subCell.parentCellId = cell.cellId;
        }
    }
}

#pragma mark Artworks

+ (NSArray<SDLArtwork *> *)findAllArtworksToBeUploadedFromCells:(NSArray<SDLMenuCell *> *)cells fileManager:(SDLFileManager *)fileManager windowCapability:(SDLWindowCapability *)windowCapability {
    if (![windowCapability hasImageFieldOfName:SDLImageFieldNameCommandIcon]) { return @[]; }

    NSMutableSet<SDLArtwork *> *mutableArtworks = [NSMutableSet set];
    for (SDLMenuCell *cell in cells) {
        if ((cell.icon != nil) && [fileManager fileNeedsUpload:cell.icon]) {
            [mutableArtworks addObject:cell.icon];
        }

        if ((cell.secondaryArtwork != nil) && [fileManager fileNeedsUpload:cell.secondaryArtwork]) {
            [mutableArtworks addObject:cell.secondaryArtwork];
        }

        if (cell.subCells.count > 0) {
            [mutableArtworks addObjectsFromArray:[self findAllArtworksToBeUploadedFromCells:cell.subCells fileManager:fileManager windowCapability:windowCapability]];
        }
    }

    return [mutableArtworks allObjects];
}

/// If there is an icon and the icon has been uploaded, or if the icon is a static icon, it should include the image
+ (BOOL)sdl_shouldCellIncludePrimaryImageFromCell:(SDLMenuCell *)cell fileManager:(SDLFileManager *)fileManager windowCapability:(SDLWindowCapability *)windowCapability {
    BOOL supportsImage = (cell.subCells != nil) ? [windowCapability hasImageFieldOfName:SDLImageFieldNameSubMenuIcon] : [windowCapability hasImageFieldOfName:SDLImageFieldNameCommandIcon];
    return (cell.icon != nil) && supportsImage && ([fileManager hasUploadedFile:cell.icon] || cell.icon.isStaticIcon);
}

/// If there is an icon and the icon has been uploaded, or if the icon is a static icon, it should include the image
+ (BOOL)sdl_shouldCellIncludeSecondaryImageFromCell:(SDLMenuCell *)cell fileManager:(SDLFileManager *)fileManager windowCapability:(SDLWindowCapability *)windowCapability {
    BOOL supportsImage = (cell.subCells != nil) ? [windowCapability hasImageFieldOfName:SDLImageFieldNameMenuSubMenuSecondaryImage] : [windowCapability hasImageFieldOfName:SDLImageFieldNameMenuCommandSecondaryImage];
    return (cell.secondaryArtwork != nil) && supportsImage && ([fileManager hasUploadedFile:cell.secondaryArtwork] || cell.secondaryArtwork.isStaticIcon);
}

#pragma mark - RPC Commands
#pragma mark Retrieving Values

+ (UInt32)commandIdForRPCRequest:(SDLRPCRequest *)request {
    UInt32 commandId = 0;
    if ([request isMemberOfClass:[SDLAddCommand class]]) {
        commandId = ((SDLAddCommand *)request).cmdID.unsignedIntValue;
    } else if ([request isMemberOfClass:[SDLAddSubMenu class]]) {
        commandId = ((SDLAddSubMenu *)request).menuID.unsignedIntValue;
    } else if ([request isMemberOfClass:[SDLDeleteCommand class]]) {
        commandId = ((SDLDeleteCommand *)request).cmdID.unsignedIntValue;
    } else if ([request isMemberOfClass:[SDLDeleteSubMenu class]]) {
        commandId = ((SDLDeleteSubMenu *)request).menuID.unsignedIntValue;
    }

    return commandId;
}

+ (UInt16)positionForRPCRequest:(SDLRPCRequest *)request {
    UInt16 position = 0;
    if ([request isMemberOfClass:[SDLAddCommand class]]) {
        position = ((SDLAddCommand *)request).menuParams.position.unsignedShortValue;
    } else if ([request isMemberOfClass:[SDLAddSubMenu class]]) {
        position = ((SDLAddSubMenu *)request).position.unsignedShortValue;
    }

    return position;
}

#pragma mark Generating RPCs

+ (NSArray<SDLRPCRequest *> *)deleteCommandsForCells:(NSArray<SDLMenuCell *> *)cells {
    NSMutableArray<SDLRPCRequest *> *mutableDeletes = [NSMutableArray array];
    for (SDLMenuCell *cell in cells) {
        if (cell.subCells != nil) {
            SDLDeleteSubMenu *delete = [[SDLDeleteSubMenu alloc] initWithId:cell.cellId];
            [mutableDeletes addObject:delete];
        } else {
            SDLDeleteCommand *delete = [[SDLDeleteCommand alloc] initWithId:cell.cellId];
            [mutableDeletes addObject:delete];
        }
    }

    return [mutableDeletes copy];
}

+ (NSArray<SDLRPCRequest *> *)mainMenuCommandsForCells:(NSArray<SDLMenuCell *> *)cells fileManager:(SDLFileManager *)fileManager usingIndexesFrom:(NSArray<SDLMenuCell *> *)menu windowCapability:(SDLWindowCapability *)windowCapability defaultSubmenuLayout:(SDLMenuLayout)defaultSubmenuLayout {
    NSMutableArray<SDLRPCRequest *> *mutableCommands = [NSMutableArray array];
    for (NSUInteger menuInteger = 0; menuInteger < menu.count; menuInteger++) {
        for (NSUInteger updateCellsIndex = 0; updateCellsIndex < cells.count; updateCellsIndex++) {
            if ([menu[menuInteger] isEqual:cells[updateCellsIndex]]) {
                if (cells[updateCellsIndex].subCells != nil) {
                    [mutableCommands addObject:[self sdl_subMenuCommandForMenuCell:cells[updateCellsIndex] fileManager:fileManager position:(UInt16)menuInteger windowCapability:windowCapability defaultSubmenuLayout:defaultSubmenuLayout]];
                } else {
                    [mutableCommands addObject:[self sdl_commandForMenuCell:cells[updateCellsIndex] fileManager:fileManager windowCapability:windowCapability position:(UInt16)menuInteger]];
                }
                break;
            }
        }
    }

    return [mutableCommands copy];
}

+ (NSArray<SDLRPCRequest *> *)subMenuCommandsForCells:(NSArray<SDLMenuCell *> *)cells fileManager:(SDLFileManager *)fileManager windowCapability:(SDLWindowCapability *)windowCapability defaultSubmenuLayout:(SDLMenuLayout)defaultSubmenuLayout {
    NSMutableArray<SDLRPCRequest *> *mutableCommands = [NSMutableArray array];
    for (SDLMenuCell *cell in cells) {
        if (cell.subCells != nil) {
            [mutableCommands addObjectsFromArray:[self sdl_allCommandsForCells:cell.subCells fileManager:fileManager windowCapability:windowCapability defaultSubmenuLayout:defaultSubmenuLayout]];
        }
    }

    return [mutableCommands copy];
}

#pragma mark Private Helpers

+ (NSArray<SDLRPCRequest *> *)sdl_allCommandsForCells:(NSArray<SDLMenuCell *> *)cells fileManager:(SDLFileManager *)fileManager windowCapability:(SDLWindowCapability *)windowCapability defaultSubmenuLayout:(SDLMenuLayout)defaultSubmenuLayout {
    NSMutableArray<SDLRPCRequest *> *mutableCommands = [NSMutableArray array];

    for (NSUInteger cellIndex = 0; cellIndex < cells.count; cellIndex++) {
        if (cells[cellIndex].subCells != nil) {
            [mutableCommands addObject:[self sdl_subMenuCommandForMenuCell:cells[cellIndex] fileManager:fileManager position:(UInt16)cellIndex windowCapability:windowCapability defaultSubmenuLayout:defaultSubmenuLayout]];
            [mutableCommands addObjectsFromArray:[self sdl_allCommandsForCells:cells[cellIndex].subCells fileManager:fileManager windowCapability:windowCapability defaultSubmenuLayout:defaultSubmenuLayout]];
        } else {
            [mutableCommands addObject:[self sdl_commandForMenuCell:cells[cellIndex] fileManager:fileManager windowCapability:windowCapability position:(UInt16)cellIndex]];
        }
    }

    return [mutableCommands copy];
}

+ (SDLAddCommand *)sdl_commandForMenuCell:(SDLMenuCell *)cell fileManager:(SDLFileManager *)fileManager windowCapability:(SDLWindowCapability *)windowCapability position:(UInt16)position {
    SDLAddCommand *command = [[SDLAddCommand alloc] init];

    SDLMenuParams *params = [[SDLMenuParams alloc] init];
    params.menuName = cell.uniqueTitle;
    params.secondaryText = ([windowCapability hasTextFieldOfName:SDLTextFieldNameMenuCommandSecondaryText] && cell.secondaryText.length > 0) ? cell.secondaryText : nil;
    params.tertiaryText = ([windowCapability hasTextFieldOfName:SDLTextFieldNameMenuCommandTertiaryText]  && cell.tertiaryText.length > 0) ? cell.tertiaryText : nil;
    params.parentID = (cell.parentCellId != ParentIdNotFound) ? @(cell.parentCellId) : nil;
    params.position = @(position);

    command.menuParams = params;
    command.vrCommands = (cell.voiceCommands.count == 0) ? nil : cell.voiceCommands;
    command.cmdIcon = [self sdl_shouldCellIncludePrimaryImageFromCell:cell fileManager:fileManager windowCapability:windowCapability] ? cell.icon.imageRPC : nil;
    command.secondaryImage = [self sdl_shouldCellIncludeSecondaryImageFromCell:cell fileManager:fileManager windowCapability:windowCapability] ? cell.secondaryArtwork.imageRPC : nil;
    command.cmdID = @(cell.cellId);

    return command;
}

+ (SDLAddSubMenu *)sdl_subMenuCommandForMenuCell:(SDLMenuCell *)cell fileManager:(SDLFileManager *)fileManager position:(UInt16)position windowCapability:(SDLWindowCapability *)windowCapability defaultSubmenuLayout:(SDLMenuLayout)defaultSubmenuLayout {
    NSString *secondaryText = ([windowCapability hasTextFieldOfName:SDLTextFieldNameMenuCommandSecondaryText] && cell.secondaryText.length > 0) ? cell.secondaryText : nil;
    NSString *tertiaryText = ([windowCapability hasTextFieldOfName:SDLTextFieldNameMenuCommandTertiaryText]  && cell.tertiaryText.length > 0) ? cell.tertiaryText : nil;
    SDLImage *icon = [self sdl_shouldCellIncludePrimaryImageFromCell:cell fileManager:fileManager windowCapability:windowCapability] ? cell.icon.imageRPC : nil;
    SDLImage *secondaryIcon = [self sdl_shouldCellIncludeSecondaryImageFromCell:cell fileManager:fileManager windowCapability:windowCapability] ? cell.secondaryArtwork.imageRPC : nil;

    SDLMenuLayout submenuLayout = nil;
    if (cell.submenuLayout && [windowCapability.menuLayoutsAvailable containsObject:cell.submenuLayout]) {
        submenuLayout = cell.submenuLayout;
    } else {
        submenuLayout = defaultSubmenuLayout;
    }

    return [[SDLAddSubMenu alloc] initWithMenuID:cell.cellId menuName:cell.uniqueTitle position:@(position) menuIcon:icon menuLayout:submenuLayout parentID:nil secondaryText:secondaryText tertiaryText:tertiaryText secondaryImage:secondaryIcon];
}

#pragma mark - Updating Menu Cells In Lists

+ (BOOL)removeCellFromList:(NSMutableArray<SDLMenuCell *> *)menuCellList withCellId:(UInt32)commandId {
    for (SDLMenuCell *menuCell in menuCellList) {
        if (menuCell.cellId == commandId) {
            // If the cell id matches the command id, remove it from the list and return
            [menuCellList removeObject:menuCell];
            return YES;
        } else if (menuCell.subCells.count > 0) {
            // If the menu cell has subcells, we need to recurse and check the subcells
            NSMutableArray<SDLMenuCell *> *newList = [menuCell.subCells mutableCopy];
            BOOL foundAndRemovedItem = [self removeCellFromList:newList withCellId:commandId];
            if (foundAndRemovedItem) {
                menuCell.subCells = [newList copy];
                return YES;
            }
        }
    }

    return NO;
}

+ (BOOL)addCellWithCellId:(UInt32)cellId position:(UInt16)position fromNewMenuList:(NSArray<SDLMenuCell *> *)newMenuList toMainMenuList:(NSMutableArray <SDLMenuCell *> *)mainMenuList {
    SDLMenuCell *addedCell = nil;
    for (SDLMenuCell *cell in newMenuList) {
        if (cell.cellId == cellId) {
            addedCell = cell;
            break;
        } else if (cell.subCells.count > 0) {
            BOOL success = [self addCellWithCellId:cellId position:position fromNewMenuList:cell.subCells toMainMenuList:mainMenuList];
            if (success) { return YES; }
        }
    }

    if (addedCell != nil) {
        return [self sdl_addMenuCell:addedCell toList:mainMenuList atPosition:position];
    }

    return NO;
}

+ (BOOL)sdl_addMenuCell:(SDLMenuCell *)cell toList:(NSMutableArray<SDLMenuCell *> *)menuCellList atPosition:(UInt16)position {
    if (cell.parentCellId == ParentIdNotFound) {
        // The cell does not have a parent id, just insert it into the main menu
        [self sdl_insertMenuCell:cell intoList:menuCellList atPosition:position];
        return YES;
    }

    // If the cell has a parent id, we need to find the cell with a matching cell id and insert it into its submenu
    for (SDLMenuCell *menuCell in menuCellList) {
        if (menuCell.cellId == cell.parentCellId) {
            // If we found the correct submenu, insert it into that submenu
            NSMutableArray<SDLMenuCell *> *newList = nil;
            if (menuCell.subCells != nil) {
                newList = [menuCell.subCells mutableCopy];
            } else {
                newList = [NSMutableArray array];
            }

            [self sdl_insertMenuCell:cell intoList:newList atPosition:position];
            menuCell.subCells = [newList copy];
            return YES;
        } else if (menuCell.subCells.count > 0) {
            // Check the subcells of this cell to see if any of those have cell ids that match the parent cell id
            NSMutableArray<SDLMenuCell *> *newList = [menuCell.subCells mutableCopy];
            BOOL foundAndAddedItem = [self sdl_addMenuCell:cell toList:newList atPosition:position];
            if (foundAndAddedItem) {
                menuCell.subCells = [newList copy];
                return YES;
            }
        }
    }

    return NO;
}

+ (void)sdl_insertMenuCell:(SDLMenuCell *)cell intoList:(NSMutableArray<SDLMenuCell *> *)cellList atPosition:(UInt16)position {
    SDLMenuCell *cellToInsert = cell;
    if (cellToInsert.subCells != nil) {
        cellToInsert = [cell copy];
        cellToInsert.subCells = @[];
    }

    if (position > cellList.count) {
        [cellList addObject:cellToInsert];
    } else {
        [cellList insertObject:cellToInsert atIndex:position];
    }
}

@end