summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/public/SDLMenuCell.m
blob: b9f0a7be4130115b1ecede24bd81de826d493ca1 (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
//
//  SDLMenuCell.m
//  SmartDeviceLink
//
//  Created by Joel Fischer on 4/9/18.
//  Copyright © 2018 smartdevicelink. All rights reserved.
//

#import "SDLMenuCell.h"

#import "SDLArtwork.h"
#import "NSArray+Extensions.h"

NS_ASSUME_NONNULL_BEGIN

@interface SDLMenuCell()

@property (assign, nonatomic) UInt32 parentCellId;
@property (assign, nonatomic) UInt32 cellId;
@property (strong, nonatomic, readwrite) NSString *uniqueTitle;

@end

@implementation SDLMenuCell

- (instancetype)initWithTitle:(NSString *)title icon:(nullable SDLArtwork *)icon voiceCommands:(nullable NSArray<NSString *> *)voiceCommands handler:(SDLMenuCellSelectionHandler)handler {
    return [self initWithTitle:title secondaryText:nil tertiaryText:nil icon:icon secondaryArtwork:nil voiceCommands:voiceCommands handler:handler];
}

- (instancetype)initWithTitle:(NSString *)title icon:(nullable SDLArtwork *)icon submenuLayout:(nullable SDLMenuLayout)layout subCells:(NSArray<SDLMenuCell *> *)subCells {
    return [self initWithTitle:title secondaryText:nil tertiaryText:nil icon:icon secondaryArtwork:nil submenuLayout:layout subCells:subCells];
}

- (instancetype)initWithTitle:(NSString *)title secondaryText:(nullable NSString *)secondaryText tertiaryText:(nullable NSString *)tertiaryText icon:(nullable SDLArtwork *)icon secondaryArtwork:(nullable SDLArtwork *)secondaryArtwork voiceCommands:(nullable NSArray<NSString *> *)voiceCommands handler:(SDLMenuCellSelectionHandler)handler {
    self = [super init];
    if (!self) { return nil; }

    _title = title;
    _icon = icon;
    _voiceCommands = voiceCommands;
    _handler = handler;
    _uniqueTitle = title;

    _cellId = UINT32_MAX;
    _parentCellId = UINT32_MAX;

    _secondaryText = secondaryText;
    _tertiaryText = tertiaryText;
    _secondaryArtwork = secondaryArtwork;

    return self;
}

- (instancetype)initWithTitle:(NSString *)title secondaryText:(nullable NSString *)secondaryText tertiaryText:(nullable NSString *)tertiaryText icon:(nullable SDLArtwork *)icon secondaryArtwork:(nullable SDLArtwork *)secondaryArtwork submenuLayout:(nullable SDLMenuLayout)layout subCells:(NSArray<SDLMenuCell *> *)subCells {

    self = [super init];
    if (!self) { return nil; }

    _title = title;
    _submenuLayout = layout;
    _icon = icon;
    _subCells = subCells;
    _uniqueTitle = title;

    _cellId = UINT32_MAX;
    _parentCellId = UINT32_MAX;

    _secondaryText = secondaryText;
    _tertiaryText = tertiaryText;
    _secondaryArtwork = secondaryArtwork;

    return self;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"SDLMenuCell: %u-\"%@\", unique title: %@, artworkName: %@, voice commands: %lu, isSubcell: %@, hasSubcells: %@, submenuLayout: %@", (unsigned int)_cellId, _title, ([_title isEqualToString:_uniqueTitle] ? @"NO" : _uniqueTitle),  _icon.name, (unsigned long)_voiceCommands.count, (_parentCellId != UINT32_MAX ? @"YES" : @"NO"), (_subCells.count > 0 ? @"YES" : @"NO"), _submenuLayout];
}

#pragma mark - Object Equality

- (NSUInteger)hash {
    return NSUIntRotateCell(self.title.hash, NSUIntBitCell / 2)
    ^ NSUIntRotateCell(self.icon.name.hash, NSUIntBitCell / 3)
    ^ NSUIntRotateCell(self.voiceCommands.dynamicHash, NSUIntBitCell / 4)
    ^ NSUIntRotateCell((self.subCells.count != 0), NSUIntBitCell  / 5)
    ^ NSUIntRotateCell(self.secondaryText.hash, NSUIntBitCell  / 6)
    ^ NSUIntRotateCell(self.tertiaryText.hash, NSUIntBitCell  / 7)
    ^ NSUIntRotateCell(self.secondaryArtwork.name.hash, NSUIntBitCell  / 8)
    ^ NSUIntRotateCell(self.submenuLayout.hash, NSUIntBitCell / 9);
}

- (BOOL)isEqual:(id)object {
    if (self == object) { return YES; }
    if (![object isMemberOfClass:[self class]]) { return NO; }

    return [self isEqualToChoice:(SDLMenuCell *)object];
}

- (BOOL)isEqualToChoice:(SDLMenuCell *)choice {
    if (choice == nil) { return NO; }

    return (self.hash == choice.hash);
}

@end

NS_ASSUME_NONNULL_END