summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/private/SDLTextAndGraphicUpdateOperation.m
blob: a623ad8ad0902146ca8b2d5bce923c3b928f2d45 (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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//
//  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 "SDLSetDisplayLayout.h"
#import "SDLShow.h"
#import "SDLTemplateConfiguration.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:(SDLTextAndGraphicState *)currentData newState:(SDLTextAndGraphicState *)newState currentScreenDataUpdatedHandler:(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 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];
    fullShow = [self sdl_assembleLayout:fullShow];

    __weak typeof(self) weakSelf = self;
    if ([self sdl_showSupportsTemplateConfiguration]) {
        // Everything (template, text, and images) can be updated using a single Show request
        [self sdl_updateGraphicsAndShow:fullShow];
    } else if (self.sdl_shouldUpdateTemplateConfig) {
        // The template must first be updated using SetDisplayLayout request. Then a Show request is sent. 
        [self sdl_sendSetDisplayLayoutWithTemplateConfiguration:self.updatedState.templateConfig completionHandler:^(NSError * _Nullable error) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (strongSelf.isCancelled) {
                [strongSelf finishOperation];
                return;
            } else if (error != nil) {
                strongSelf.internalError = error;
                [strongSelf finishOperation];
                return;
            }

            [strongSelf sdl_updateGraphicsAndShow:fullShow];
        }];
    } else {
        // The template does not need to be updated. Just send the show.
        [self sdl_updateGraphicsAndShow:fullShow];
    }
}

- (void)updateStateDataWithErrorData:(SDLTextAndGraphicState *)errorData {
    self.updatedState.textField1 = errorData.textField1 == self.updatedState.textField1 ? self.currentScreenData.textField1 : self.updatedState.textField1;
    self.updatedState.textField2 = errorData.textField2 == self.updatedState.textField2 ? self.currentScreenData.textField2 : self.updatedState.textField2;
    self.updatedState.textField3 = errorData.textField3 == self.updatedState.textField3 ? self.currentScreenData.textField3 : self.updatedState.textField3;
    self.updatedState.textField4 = errorData.textField4 == self.updatedState.textField4 ? self.currentScreenData.textField4 : self.updatedState.textField4;
    self.updatedState.mediaTrackTextField = errorData.mediaTrackTextField == self.updatedState.mediaTrackTextField ? self.currentScreenData.mediaTrackTextField : self.updatedState.mediaTrackTextField;
    self.updatedState.title = errorData.title == self.updatedState.title ? self.currentScreenData.title : self.updatedState.title;
    self.updatedState.primaryGraphic = errorData.primaryGraphic == self.updatedState.primaryGraphic ? self.currentScreenData.primaryGraphic : self.updatedState.primaryGraphic;
    self.updatedState.secondaryGraphic = errorData.secondaryGraphic == self.updatedState.secondaryGraphic ? self.currentScreenData.secondaryGraphic : self.updatedState.secondaryGraphic;
    self.updatedState.alignment = errorData.alignment == self.updatedState.alignment ? self.currentScreenData.alignment : self.updatedState.alignment;
    self.updatedState.textField1Type = errorData.textField1Type == self.updatedState.textField1Type ? self.currentScreenData.textField1Type : self.updatedState.textField1Type;
    self.updatedState.textField2Type = errorData.textField2Type == self.updatedState.textField2Type ? self.currentScreenData.textField2Type : self.updatedState.textField2Type;
    self.updatedState.textField3Type = errorData.textField3Type == self.updatedState.textField3Type ? self.currentScreenData.textField3Type : self.updatedState.textField3Type;
    self.updatedState.textField4Type = errorData.textField4Type == self.updatedState.textField4Type ? self.currentScreenData.textField4Type : self.updatedState.textField4Type;
    self.updatedState.templateConfig = errorData.templateConfig == self.updatedState.templateConfig ? self.currentScreenData.templateConfig : self.updatedState.templateConfig;
}

#pragma mark - Send Show / Set Display Layout

- (void)sdl_updateGraphicsAndShow:(SDLShow *)show {
    __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_extractTextAndLayoutFromShow:show] withHandler:^(NSError * _Nullable error) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (error != nil) {
                strongSelf.internalError = error;
            }
            [strongSelf finishOperation];
        }];
    } else if (![self.fileManager fileNeedsUpload:self.updatedState.primaryGraphic] && ![self.fileManager fileNeedsUpload: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:show 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");

        // Send the text immediately
        [self sdl_sendShow:[self sdl_extractTextAndLayoutFromShow:show] withHandler:^(NSError * _Nullable error) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (self.cancelled) {
                [strongSelf finishOperation];
                return;
            } else if (error != nil) {
                // The show to change the text / layout failed, so fail the operation
                self.internalError = error;
                [strongSelf finishOperation];
                return;
            }

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

- (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;
        SDLLogV(@"Text and Graphic Show completed. Request: %@, response: %@", request, response);

        if (response.success.boolValue) {
            SDLLogD(@"Text and Graphic Show completed successfully");
            [strongSelf sdl_updateCurrentScreenDataFromShow:request];
        } else {
            SDLLogD(@"Text and Graphic Show failed");
            self.currentDataUpdatedHandler(nil, error, self.updatedState);
        }

        handler(error);
    }];
}

- (void)sdl_sendSetDisplayLayoutWithTemplateConfiguration:(SDLTemplateConfiguration *)configuration completionHandler:(void (^)(NSError *_Nullable error))handler {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    SDLSetDisplayLayout *setLayout = [[SDLSetDisplayLayout alloc] initWithLayout:configuration.template dayColorScheme:configuration.dayColorScheme nightColorScheme:configuration.nightColorScheme];
#pragma clang diagnostic pop

    __weak typeof(self)weakSelf = self;
    [self.connectionManager sendConnectionRequest:setLayout withResponseHandler:^(__kindof SDLRPCRequest * _Nullable request, __kindof SDLRPCResponse * _Nullable response, NSError * _Nullable error) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        SDLLogV(@"Text and Graphic SetDisplayLayout completed. Request: %@, response: %@", request, response);

        if (response.success.boolValue) {
            SDLLogD(@"Text and Graphic SetDisplayLayout succeeded. New layout: %@", setLayout.displayLayout);
            [strongSelf sdl_updateCurrentScreenDataFromSetDisplayLayout:request];
        } else {
            SDLLogD(@"Text and Graphic SetDisplayLayout failed to change to new layout: %@", setLayout.displayLayout);
            self.currentDataUpdatedHandler(nil, error, nil);
        }

        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_shouldRPCIncludeImage:primaryArtwork] ? primaryArtwork.imageRPC : nil;
    newShow.secondaryGraphic = [self sdl_shouldRPCIncludeImage: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;
    }

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

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

    // If the template is updating, we don't yet know it's capabilities. Just assume the template supports the max number of textfields. 
    NSUInteger numberOfLines = ![self sdl_shouldUpdateTemplateConfig] ? self.currentCapabilities.maxNumberOfMainFieldLines : 4;
    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 Layout

- (SDLShow *)sdl_assembleLayout:(SDLShow *)show {
    if (![self sdl_showSupportsTemplateConfiguration] || !self.sdl_shouldUpdateTemplateConfig) { return show; }

    show.templateConfiguration = self.updatedState.templateConfig;
    return show;
}

#pragma mark - Extraction

- (SDLShow *)sdl_extractTextAndLayoutFromShow:(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;

    if ([self sdl_showSupportsTemplateConfiguration]) {
        newShow.templateConfiguration = show.templateConfiguration;
    }

    return newShow;
}

#pragma mark - Update Current Screen Data

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

    // This is intentionally checking `mainField1` because the fields may be in different places based on the capabilities, then check it's own field in case that's the only field thats being used.
    self.currentScreenData.textField1 = show.mainField1 ? self.updatedState.textField1 : self.currentScreenData.textField1;
    self.currentScreenData.textField2 = (show.mainField1 || show.mainField2) ? self.updatedState.textField2 : self.currentScreenData.textField2;
    self.currentScreenData.textField3 = (show.mainField1 || show.mainField3) ? self.updatedState.textField3 : self.currentScreenData.textField3;
    self.currentScreenData.textField4 = (show.mainField1 || show.mainField4) ? self.updatedState.textField4 : self.currentScreenData.textField4;

    // This is intentionally checking show.metadataTags.mainField1 because the tags may be in different places based on the capabilities, then check its own field in case that's the only field that's being used.
    self.currentScreenData.textField1Type = show.metadataTags.mainField1 ? self.updatedState.textField1Type : self.currentScreenData.textField1Type;
    self.currentScreenData.textField2Type = (show.metadataTags.mainField1 || show.metadataTags.mainField2) ? self.updatedState.textField2Type : self.currentScreenData.textField2Type;
    self.currentScreenData.textField3Type = (show.metadataTags.mainField1 || show.metadataTags.mainField3) ? self.updatedState.textField3Type : self.currentScreenData.textField3Type;
    self.currentScreenData.textField4Type = (show.metadataTags.mainField1 || show.metadataTags.mainField4) ? self.updatedState.textField4Type : self.currentScreenData.textField4Type;

    self.currentScreenData.templateConfig = show.templateConfiguration ? self.updatedState.templateConfig : self.currentScreenData.templateConfig;

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

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (void)sdl_updateCurrentScreenDataFromSetDisplayLayout:(SDLSetDisplayLayout *)setDisplayLayout {
#pragma clang diagnostic pop
    self.currentScreenData.templateConfig = [[SDLTemplateConfiguration alloc] initWithTemplate:setDisplayLayout.displayLayout dayColorScheme:setDisplayLayout.dayColorScheme nightColorScheme:setDisplayLayout.nightColorScheme];

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

#pragma mark - Should Update

- (BOOL)sdl_shouldRPCIncludeImage:(nullable SDLArtwork *)artwork {
    if (artwork == nil) { return NO; }

    return (artwork.isStaticIcon || [self.fileManager hasUploadedFile:artwork]);
}

- (BOOL)sdl_shouldUpdatePrimaryImage {
    // If the template is updating, we don't yet know it's capabilities. Just assume the template supports the primary image. 
    BOOL templateSupportsPrimaryArtwork = [self.currentCapabilities hasImageFieldOfName:SDLImageFieldNameGraphic] || [self sdl_shouldUpdateTemplateConfig];
    BOOL graphicNameMatchesExisting = [self.currentScreenData.primaryGraphic.name isEqualToString:self.updatedState.primaryGraphic.name];
    BOOL shouldOverwriteGraphic = self.updatedState.primaryGraphic.overwrite;
    BOOL graphicExists = (self.updatedState.primaryGraphic != nil);

    return (templateSupportsPrimaryArtwork && (shouldOverwriteGraphic || !graphicNameMatchesExisting) && graphicExists);
}

- (BOOL)sdl_shouldUpdateSecondaryImage {
    // If the template is updating, we don't yet know it's capabilities. Just assume the template supports the secondary image. 
    BOOL templateSupportsSecondaryArtwork = [self.currentCapabilities hasImageFieldOfName:SDLImageFieldNameSecondaryGraphic] || [self sdl_shouldUpdateTemplateConfig];
    BOOL graphicNameMatchesExisting = [self.currentScreenData.secondaryGraphic.name isEqualToString:self.updatedState.secondaryGraphic.name];
    BOOL shouldOverwriteGraphic = self.updatedState.secondaryGraphic != nil && self.updatedState.secondaryGraphic.overwrite;
    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 && (shouldOverwriteGraphic || !graphicNameMatchesExisting) && graphicExists);
    } else {
        return ([self.currentCapabilities hasImageFieldOfName:SDLImageFieldNameGraphic] && (shouldOverwriteGraphic || !graphicNameMatchesExisting) && graphicExists);
    }
}

- (BOOL)sdl_shouldUpdateMediaTextField {
    // If the template is updating, we don't yet know it's capabilities. Just assume the template supports the media text field. 
    return [self.currentCapabilities hasTextFieldOfName:SDLTextFieldNameMediaTrack] || [self sdl_shouldUpdateTemplateConfig];
}

- (BOOL)sdl_shouldUpdateTitleField {
    // If the template is updating, we don't yet know it's capabilities. Just assume the template supports the template title. 
    return [self.currentCapabilities hasTextFieldOfName:SDLTextFieldNameTemplateTitle] || [self sdl_shouldUpdateTemplateConfig];
}

- (BOOL)sdl_shouldUpdateTemplateConfig {
    return (_updatedState.templateConfig != nil) && ![_updatedState.templateConfig isEqual:_currentScreenData.templateConfig];
}

- (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 - Version Check

- (BOOL)sdl_showSupportsTemplateConfiguration {
    return [[SDLGlobals sharedGlobals].rpcVersion isGreaterThanOrEqualToVersion:[SDLVersion versionWithMajor:6 minor:0 patch:0]];
}

#pragma mark - Operation Overrides

- (void)finishOperation {
    SDLLogV(@"Finishing text and graphic update operation");
    if (self.isCancelled) {
        self.internalError = [NSError sdl_textAndGraphicManager_pendingUpdateSuperseded];
    }

    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