summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLTextAndGraphicUpdateOperation.m
blob: dd70bda0fd692d98474704aaef49f93ee3890543 (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//
//  SDLTextAndGraphicUpdateOperation.m
//  SmartDeviceLink
//
//  Created by Joel Fischer on 8/13/20.
//  Copyright © 2020 smartdevicelink. All rights reserved.
//

#import "SDLTextAndGraphicUpdateOperation.h"

#import "SDLArtwork.h"
#import "SDLConnectionManagerType.h"
#import "SDLError.h"
#import "SDLFileManager.h"
#import "SDLGlobals.h"
#import "SDLImage.h"
#import "SDLLogMacros.h"
#import "SDLMetadataTags.h"
#import "SDLShow.h"
#import "SDLTextAndGraphicState.h"
#import "SDLVersion.h"
#import "SDLWindowCapability.h"
#import "SDLWindowCapability+ScreenManagerExtensions.h"

NS_ASSUME_NONNULL_BEGIN

@interface SDLTextAndGraphicUpdateOperation ()

@property (weak, nonatomic) id<SDLConnectionManagerType> connectionManager;
@property (weak, nonatomic) SDLFileManager *fileManager;
@property (strong, nonatomic) SDLWindowCapability *currentCapabilities;
@property (strong, nonatomic) SDLTextAndGraphicState *updatedState;

@property (copy, nonatomic, nullable) CurrentDataUpdatedHandler currentDataUpdatedHandler;
@property (copy, nonatomic, nullable) SDLTextAndGraphicUpdateCompletionHandler updateCompletionHandler;

@property (copy, nonatomic, nullable) NSError *internalError;

@end

@implementation SDLTextAndGraphicUpdateOperation

- (instancetype)initWithConnectionManager:(id<SDLConnectionManagerType>)connectionManager fileManager:(SDLFileManager *)fileManager currentCapabilities:(SDLWindowCapability *)currentCapabilities currentScreenData:(SDLShow *)currentData newState:(nonnull SDLTextAndGraphicState *)newState currentScreenDataUpdatedHandler:(nullable CurrentDataUpdatedHandler)currentDataUpdatedHandler updateCompletionHandler:(nullable SDLTextAndGraphicUpdateCompletionHandler)updateCompletionHandler {
    self = [self init];
    if (!self) { return nil; }

    _connectionManager = connectionManager;
    _fileManager = fileManager;
    _currentCapabilities = currentCapabilities;
    _currentScreenData = currentData;
    _updatedState = newState;
    _currentDataUpdatedHandler = currentDataUpdatedHandler;
    _updateCompletionHandler = updateCompletionHandler;

    return self;
}

- (void)start {
    [super start];
    if (self.cancelled) {
        // Make sure the update handler is called
        self.internalError = [NSError sdl_textAndGraphicManager_pendingUpdateSuperseded];
        [self finishOperation];
        return;
    }

    // Build a show with everything from `self.newState`, we'll pull things out later if we can.
    SDLShow *fullShow = [[SDLShow alloc] init];
    fullShow.alignment = self.updatedState.alignment;
    fullShow = [self sdl_assembleShowText:fullShow];
    fullShow = [self sdl_assembleShowImages:fullShow];

    __weak typeof(self) weakSelf = self;
    if (!([self sdl_shouldUpdatePrimaryImage] || [self sdl_shouldUpdateSecondaryImage])) {
        SDLLogV(@"No images to send, sending text");
        // If there are no images to update, just send the text
        [self sdl_sendShow:[self sdl_extractTextFromShow:fullShow] withHandler:^(NSError * _Nullable error) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (error != nil) {
                strongSelf.internalError = error;
            }
            [strongSelf finishOperation];
        }];
    } else if (![self sdl_artworkNeedsUpload:self.updatedState.primaryGraphic] && ![self sdl_artworkNeedsUpload:self.updatedState.secondaryGraphic]) {
        SDLLogV(@"Images already uploaded, sending full update");
        // The files to be updated are already uploaded, send the full show immediately
        [self sdl_sendShow:fullShow withHandler:^(NSError * _Nullable error) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (error != nil) {
                strongSelf.internalError = error;
            }
            [strongSelf finishOperation];
        }];
    } else {
        SDLLogV(@"Images need to be uploaded, sending text and uploading images");

        // We need to upload or queue the upload of the images
        // Send the text immediately
        [self sdl_sendShow:[self sdl_extractTextFromShow:fullShow] withHandler:^(NSError * _Nullable error) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (self.cancelled) {
                [strongSelf finishOperation];
            }

            [strongSelf sdl_uploadImagesAndSendWhenDone:^(NSError * _Nullable error) {
                __strong typeof(weakSelf) strongSelf = weakSelf;
                if (error != nil) {
                    strongSelf.internalError = error;
                }
                [strongSelf finishOperation];
            }];
        }];
    }
}

#pragma mark - Send Show

- (void)sdl_sendShow:(SDLShow *)show withHandler:(void (^)(NSError *_Nullable error))handler {
    __weak typeof(self)weakSelf = self;
    [self.connectionManager sendConnectionRequest:show withResponseHandler:^(__kindof SDLRPCRequest * _Nullable request, __kindof SDLRPCResponse * _Nullable response, NSError * _Nullable error) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        SDLLogD(@"Text and Graphic update completed");

        if (response.success) {
            [strongSelf sdl_updateCurrentScreenDataFromShow:(SDLShow *)request];
        }

        handler(error);
    }];
}

#pragma mark - Uploading Images

- (void)sdl_uploadImagesAndSendWhenDone:(void (^)(NSError *_Nullable error))handler {
    __weak typeof(self)weakSelf = self;
    [self sdl_uploadImagesWithCompletionHandler:^(NSError *_Nullable error) {
        __strong typeof(weakSelf) strongSelf = weakSelf;

        SDLShow *showWithGraphics = [self sdl_createImageOnlyShowWithPrimaryArtwork:self.updatedState.primaryGraphic secondaryArtwork:self.updatedState.secondaryGraphic];
        if (showWithGraphics != nil) {
            SDLLogD(@"Sending update with the successfully uploaded images");
            [strongSelf sdl_sendShow:showWithGraphics withHandler:^(NSError * _Nullable error) {
                return handler(error);
            }];
        } else {
            SDLLogW(@"All images failed to upload. No graphics to show, skipping update.");
            return handler(error);
        }
    }];
}

- (void)sdl_uploadImagesWithCompletionHandler:(void (^)(NSError *_Nullable error))handler {
    NSMutableArray<SDLArtwork *> *artworksToUpload = [NSMutableArray array];
    if ([self sdl_shouldUpdatePrimaryImage] && !self.updatedState.primaryGraphic.isStaticIcon) {
        [artworksToUpload addObject:self.updatedState.primaryGraphic];
    }
    if ([self sdl_shouldUpdateSecondaryImage] && !self.updatedState.secondaryGraphic.isStaticIcon) {
        [artworksToUpload addObject:self.updatedState.secondaryGraphic];
    }

    if (artworksToUpload.count == 0) {
        SDLLogD(@"No artworks need an upload, sending them without upload instead");
        return handler(nil);
    }

    __weak typeof(self) weakSelf = self;
    [self.fileManager uploadArtworks:artworksToUpload progressHandler:^BOOL(NSString * _Nonnull artworkName, float uploadPercentage, NSError * _Nullable error) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if (strongSelf.isCancelled) {
            [strongSelf finishOperation];
            return NO;
        }

        return YES;
    } completionHandler:^(NSArray<NSString *> * _Nonnull artworkNames, NSError * _Nullable error) {
        if (error != nil) {
            SDLLogW(@"Text and graphic manager artwork failed to upload with error: %@", error.localizedDescription);
        }

        handler(error);
    }];
}


#pragma mark - Assembly of Shows

#pragma mark Images

- (SDLShow *)sdl_assembleShowImages:(SDLShow *)show {
    if (![self sdl_shouldUpdatePrimaryImage] && ![self sdl_shouldUpdateSecondaryImage]) {
        return show;
    }

    if ([self sdl_shouldUpdatePrimaryImage]) {
        show.graphic = self.updatedState.primaryGraphic.imageRPC;
    }
    if ([self sdl_shouldUpdateSecondaryImage]) {
        show.secondaryGraphic = self.updatedState.secondaryGraphic.imageRPC;
    }

    return show;
}

- (nullable SDLShow *)sdl_createImageOnlyShowWithPrimaryArtwork:(nullable SDLArtwork *)primaryArtwork secondaryArtwork:(nullable SDLArtwork *)secondaryArtwork  {
    SDLShow *newShow = [[SDLShow alloc] init];
    newShow.graphic = ![self sdl_artworkNeedsUpload:primaryArtwork] ? primaryArtwork.imageRPC : nil;
    newShow.secondaryGraphic = ![self sdl_artworkNeedsUpload:secondaryArtwork] ? secondaryArtwork.imageRPC : nil;

    if (newShow.graphic == nil && newShow.secondaryGraphic == nil) {
        SDLLogV(@"No graphics to upload");
        return nil;
    }

    return newShow;
}

#pragma mark Text

- (SDLShow *)sdl_assembleShowText:(SDLShow *)show {
    [self sdl_setBlankTextFieldsWithShow:show];

    if (self.updatedState.mediaTrackTextField != nil && [self sdl_shouldUpdateMediaTextField]) {
        show.mediaTrack = self.updatedState.mediaTrackTextField;
    } else {
        show.mediaTrack = @"";
    }

    if (self.updatedState.title != nil && [self sdl_shouldUpdateTitleField]) {
        show.templateTitle = self.updatedState.title;
    } else {
        show.templateTitle = @"";
    }

    NSArray *nonNilFields = [self sdl_findNonNilTextFields];
    if (nonNilFields.count == 0) { return show; }

    NSUInteger numberOfLines = self.currentCapabilities.maxNumberOfMainFieldLines;
    if (numberOfLines == 1) {
        show = [self sdl_assembleOneLineShowText:show withShowFields:nonNilFields];
    } else if (numberOfLines == 2) {
        show = [self sdl_assembleTwoLineShowText:show];
    } else if (numberOfLines == 3) {
        show = [self sdl_assembleThreeLineShowText:show];
    } else if (numberOfLines == 4) {
        show = [self sdl_assembleFourLineShowText:show];
    }

    return show;
}

- (SDLShow *)sdl_assembleOneLineShowText:(SDLShow *)show withShowFields:(NSArray<NSString *> *)fields {
    NSMutableString *showString1 = [NSMutableString stringWithString:fields[0]];
    for (NSUInteger i = 1; i < fields.count; i++) {
        [showString1 appendFormat:@" - %@", fields[i]];
    }
    show.mainField1 = showString1.copy;

    NSMutableArray<SDLMetadataType> *metadataArray = [NSMutableArray array];
    self.updatedState.textField1Type ? [metadataArray addObject:self.updatedState.textField1Type] : nil;
    self.updatedState.textField2Type ? [metadataArray addObject:self.updatedState.textField2Type] : nil;
    self.updatedState.textField3Type ? [metadataArray addObject:self.updatedState.textField3Type] : nil;
    self.updatedState.textField4Type ? [metadataArray addObject:self.updatedState.textField4Type] : nil;
    show.metadataTags.mainField1 = [metadataArray copy];

    return show;
}

- (SDLShow *)sdl_assembleTwoLineShowText:(SDLShow *)show {
    NSMutableString *tempString = [NSMutableString string];
    if (self.updatedState.textField1.length > 0) {
        // If text 1 exists, put it in slot 1
        [tempString appendString:self.updatedState.textField1];
        show.metadataTags.mainField1 = self.updatedState.textField1Type.length > 0 ? @[self.updatedState.textField1Type] : @[];
    }

    if (self.updatedState.textField2.length > 0) {
        if (!(self.updatedState.textField3.length > 0 || self.updatedState.textField4.length > 0)) {
            // If text 3 & 4 do not exist, put it in slot 2
            show.mainField2 = self.updatedState.textField2;
            show.metadataTags.mainField2 = self.updatedState.textField2Type.length > 0 ? @[self.updatedState.textField2Type] : @[];
        } else if (self.updatedState.textField1.length > 0) {
            // If text 1 exists, put it in slot 1 formatted
            [tempString appendFormat:@" - %@", self.updatedState.textField2];
            show.metadataTags.mainField1 = self.updatedState.textField2Type.length > 0 ? [show.metadataTags.mainField1 arrayByAddingObjectsFromArray:@[self.updatedState.textField2Type]] : show.metadataTags.mainField1;
        } else {
            // If text 1 does not exist, put it in slot 1 unformatted
            [tempString appendString:self.updatedState.textField2];
            show.metadataTags.mainField1 = self.updatedState.textField2Type.length > 0 ? @[self.updatedState.textField2Type] : @[];
        }
    }

    show.mainField1 = [tempString copy];

    tempString = [NSMutableString string];
    if (self.updatedState.textField3.length > 0) {
        // If text 3 exists, put it in slot 2
        [tempString appendString:self.updatedState.textField3];
        show.metadataTags.mainField2 = self.updatedState.textField3Type.length > 0 ? @[self.updatedState.textField3Type] : @[];
    }

    if (self.updatedState.textField4.length > 0) {
        if (self.updatedState.textField3.length > 0) {
            // If text 3 exists, put it in slot 2 formatted
            [tempString appendFormat:@" - %@", self.updatedState.textField4];
            show.metadataTags.mainField2 = self.updatedState.textField4Type.length > 0 ? [show.metadataTags.mainField2 arrayByAddingObjectsFromArray:@[self.updatedState.textField4Type]] : show.metadataTags.mainField2;
        } else {
            // If text 3 does not exist, put it in slot 3 unformatted
            [tempString appendString:self.updatedState.textField4];
            show.metadataTags.mainField2 = self.updatedState.textField4Type.length > 0 ? @[self.updatedState.textField4Type] : @[];
        }
    }

    if (tempString.length > 0) {
        show.mainField2 = [tempString copy];
    }

    return show;
}

- (SDLShow *)sdl_assembleThreeLineShowText:(SDLShow *)show {
    if (self.updatedState.textField1.length > 0) {
        show.mainField1 = self.updatedState.textField1;
        show.metadataTags.mainField1 = self.updatedState.textField1Type.length > 0 ? @[self.updatedState.textField1Type] : @[];
    }

    if (self.updatedState.textField2.length > 0) {
        show.mainField2 = self.updatedState.textField2;
        show.metadataTags.mainField2 = self.updatedState.textField2Type.length > 0 ? @[self.updatedState.textField2Type] : @[];
    }

    NSMutableString *tempString = [NSMutableString string];
    if (self.updatedState.textField3.length > 0) {
        [tempString appendString:self.updatedState.textField3];
        show.metadataTags.mainField3 = self.updatedState.textField3Type.length > 0 ? @[self.updatedState.textField3Type] : @[];
    }

    if (self.updatedState.textField4.length > 0) {
        if (self.updatedState.textField3.length > 0) {
            // If text 3 exists, put it in slot 3 formatted
            [tempString appendFormat:@" - %@", self.updatedState.textField4];
            show.metadataTags.mainField3 = self.updatedState.textField4Type.length > 0 ? [show.metadataTags.mainField3 arrayByAddingObjectsFromArray:@[self.updatedState.textField4Type]] : show.metadataTags.mainField3;
        } else {
            // If text 3 does not exist, put it in slot 3 formatted
            [tempString appendString:self.updatedState.textField4];
            show.metadataTags.mainField3 = self.updatedState.textField4Type.length > 0 ? @[self.updatedState.textField4Type] : @[];
        }
    }

    show.mainField3 = [tempString copy];

    return show;
}

- (SDLShow *)sdl_assembleFourLineShowText:(SDLShow *)show {
    if (self.updatedState.textField1.length > 0) {
        show.mainField1 = self.updatedState.textField1;
        show.metadataTags.mainField1 = self.updatedState.textField1Type.length > 0 ? @[self.updatedState.textField1Type] : @[];
    }

    if (self.updatedState.textField2.length > 0) {
        show.mainField2 = self.updatedState.textField2;
        show.metadataTags.mainField2 = self.updatedState.textField2Type.length > 0 ? @[self.updatedState.textField2Type] : @[];
    }

    if (self.updatedState.textField3.length > 0) {
        show.mainField3 = self.updatedState.textField3;
        show.metadataTags.mainField3 = self.updatedState.textField3Type.length > 0 ? @[self.updatedState.textField3Type] : @[];
    }

    if (self.updatedState.textField4.length > 0) {
        show.mainField4 = self.updatedState.textField4;
        show.metadataTags.mainField4 = self.updatedState.textField4Type.length > 0 ? @[self.updatedState.textField4Type] : @[];
    }

    return show;
}

- (SDLShow *)sdl_setBlankTextFieldsWithShow:(SDLShow *)show {
    show.mainField1 = @"";
    show.mainField2 = @"";
    show.mainField3 = @"";
    show.mainField4 = @"";
    show.mediaTrack = @"";
    show.templateTitle = @"";
    show.metadataTags = [[SDLMetadataTags alloc] init];

    return show;
}

#pragma mark - Extraction

- (SDLShow *)sdl_extractTextFromShow:(SDLShow *)show {
    SDLShow *newShow = [[SDLShow alloc] init];
    newShow.mainField1 = show.mainField1;
    newShow.mainField2 = show.mainField2;
    newShow.mainField3 = show.mainField3;
    newShow.mainField4 = show.mainField4;
    newShow.mediaTrack = show.mediaTrack;
    newShow.templateTitle = show.templateTitle;
    newShow.metadataTags = show.metadataTags;
    newShow.alignment = show.alignment;

    return newShow;
}

- (void)sdl_updateCurrentScreenDataFromShow:(SDLShow *)show {
    // If the items are nil, they were not updated, so we can't just set it directly
    self.currentScreenData.mainField1 = show.mainField1 ?: self.currentScreenData.mainField1;
    self.currentScreenData.mainField2 = show.mainField2 ?: self.currentScreenData.mainField2;
    self.currentScreenData.mainField3 = show.mainField3 ?: self.currentScreenData.mainField3;
    self.currentScreenData.mainField4 = show.mainField4 ?: self.currentScreenData.mainField4;
    self.currentScreenData.mediaTrack = show.mediaTrack ?: self.currentScreenData.mediaTrack;
    self.currentScreenData.templateTitle = show.templateTitle ?: self.currentScreenData.templateTitle;
    self.currentScreenData.metadataTags = show.metadataTags ?: self.currentScreenData.metadataTags;
    self.currentScreenData.alignment = show.alignment ?: self.currentScreenData.alignment;
    self.currentScreenData.graphic = show.graphic ?: self.currentScreenData.graphic;
    self.currentScreenData.secondaryGraphic = show.secondaryGraphic ?: self.currentScreenData.secondaryGraphic;

    if (self.currentDataUpdatedHandler != nil) {
        self.currentDataUpdatedHandler(self.currentScreenData);
    }
}

#pragma mark - Should Update

- (BOOL)sdl_artworkNeedsUpload:(SDLArtwork *)artwork {
    return (artwork != nil && ![self.fileManager hasUploadedFile:artwork] && !artwork.isStaticIcon);
}

- (BOOL)sdl_shouldUpdatePrimaryImage {
    BOOL templateSupportsPrimaryArtwork = [self.currentCapabilities hasImageFieldOfName:SDLImageFieldNameGraphic];
    BOOL graphicMatchesExisting = [self.currentScreenData.graphic.value isEqualToString:self.updatedState.primaryGraphic.name];
    BOOL graphicExists = (self.updatedState.primaryGraphic != nil);

    return (templateSupportsPrimaryArtwork && !graphicMatchesExisting && graphicExists);
}

- (BOOL)sdl_shouldUpdateSecondaryImage {
    BOOL templateSupportsSecondaryArtwork = [self.currentCapabilities hasImageFieldOfName:SDLImageFieldNameSecondaryGraphic];
    BOOL graphicMatchesExisting = [self.currentScreenData.secondaryGraphic.value isEqualToString:self.updatedState.secondaryGraphic.name];
    BOOL graphicExists = (self.updatedState.secondaryGraphic != nil);

    // Cannot detect if there is a secondary image below v5.0, so we'll just try to detect if the primary image is allowed and allow the secondary image if it is.
    if ([[SDLGlobals sharedGlobals].rpcVersion isGreaterThanOrEqualToVersion:[SDLVersion versionWithMajor:5 minor:0 patch:0]]) {
        return (templateSupportsSecondaryArtwork && !graphicMatchesExisting && graphicExists);
    } else {
        return ([self.currentCapabilities hasImageFieldOfName:SDLImageFieldNameGraphic] && !graphicMatchesExisting && graphicExists);
    }
}

- (BOOL)sdl_shouldUpdateMediaTextField {
    return [self.currentCapabilities hasTextFieldOfName:SDLTextFieldNameMediaTrack];
}

- (BOOL)sdl_shouldUpdateTitleField {
    return [self.currentCapabilities hasTextFieldOfName:SDLTextFieldNameTemplateTitle];
}

- (NSArray<NSString *> *)sdl_findNonNilTextFields {
    NSMutableArray *array = [NSMutableArray array];
    (self.updatedState.textField1.length > 0) ? [array addObject:self.updatedState.textField1] : nil;
    (self.updatedState.textField2.length > 0) ? [array addObject:self.updatedState.textField2] : nil;
    (self.updatedState.textField3.length > 0) ? [array addObject:self.updatedState.textField3] : nil;
    (self.updatedState.textField4.length > 0) ? [array addObject:self.updatedState.textField4] : nil;

    return [array copy];
}

#pragma mark - Operation Overrides

- (void)finishOperation {
    SDLLogV(@"Finishing text and graphic update operation");
    if (self.updateCompletionHandler != nil) {
        self.updateCompletionHandler(self.error);
    }
    [super finishOperation];
}

- (nullable NSString *)name {
    return @"com.sdl.textandgraphic.update";
}

- (NSOperationQueuePriority)queuePriority {
    return NSOperationQueuePriorityNormal;
}

- (nullable NSError *)error {
    return self.internalError;
}

@end

NS_ASSUME_NONNULL_END