-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNactroCreditCell.m
More file actions
executable file
·188 lines (166 loc) · 9.41 KB
/
NactroCreditCell.m
File metadata and controls
executable file
·188 lines (166 loc) · 9.41 KB
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
//
// NactroCreditCell.m
// NactroCreditCell
//
// Copyright (c) 2017 Dynastic Development
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#import "NactroCreditCell.h"
#import "NactroCreditOption.h"
#import "NactroCreditService.h"
#define PREFERENCE_BUNDLE_PATH @"/Library/PreferenceBundles/Settings.bundle"
#define BUTTON_HEIGHT 20
#define BUTTON_SPACING 14
@implementation NactroCreditCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier specifier:(PSSpecifier *)specifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier specifier:specifier];
if (self) {
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
self.creditOptions = [NSArray array];
_avatarImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
[_avatarImageView setBackgroundColor:[UIColor colorWithWhite:0.847 alpha:1]];
[_avatarImageView.layer setMasksToBounds:YES];
[_avatarImageView setClipsToBounds:YES];
[_avatarImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addSubview:_avatarImageView];
[[_avatarImageView.heightAnchor constraintEqualToConstant:58] setActive:YES];
[[_avatarImageView.widthAnchor constraintEqualToAnchor:_avatarImageView.heightAnchor] setActive:YES];
[[_avatarImageView.centerYAnchor constraintEqualToAnchor:self.centerYAnchor] setActive:YES];
[[_avatarImageView.leadingAnchor constraintEqualToAnchor:self.layoutMarginsGuide.leadingAnchor] setActive:YES];
UIView *contentView = [[UIView alloc] initWithFrame:CGRectZero];
[contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addSubview:contentView];
[[contentView.leadingAnchor constraintEqualToAnchor:_avatarImageView.trailingAnchor constant:17] setActive:YES];
[[contentView.trailingAnchor constraintEqualToAnchor:self.layoutMarginsGuide.trailingAnchor] setActive:YES];
[[contentView.centerYAnchor constraintEqualToAnchor:self.centerYAnchor] setActive:YES];
_nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[_nameLabel setFont:[UIFont systemFontOfSize:18 weight:UIFontWeightMedium]];
[_nameLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[contentView addSubview:_nameLabel];
[[_nameLabel.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor] setActive:YES];
[[_nameLabel.trailingAnchor constraintEqualToAnchor:contentView.trailingAnchor] setActive:YES];
[[_nameLabel.topAnchor constraintEqualToAnchor:contentView.topAnchor] setActive:YES];
_positionLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[_positionLabel setFont:[UIFont systemFontOfSize:14]];
[_positionLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[contentView addSubview:_positionLabel];
[[_positionLabel.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor] setActive:YES];
[[_positionLabel.trailingAnchor constraintEqualToAnchor:contentView.trailingAnchor] setActive:YES];
[[_positionLabel.topAnchor constraintEqualToAnchor:_nameLabel.bottomAnchor] setActive:YES];
_optionScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
[_optionScrollView setAlwaysBounceHorizontal:YES];
[_optionScrollView setShowsVerticalScrollIndicator:NO];
[_optionScrollView setShowsHorizontalScrollIndicator:NO];
[_optionScrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
[contentView addSubview:_optionScrollView];
[[_optionScrollView.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor] setActive:YES];
[[_optionScrollView.trailingAnchor constraintEqualToAnchor:contentView.trailingAnchor] setActive:YES];
[[_optionScrollView.topAnchor constraintEqualToAnchor:_positionLabel.bottomAnchor constant:5] setActive:YES];
[[_optionScrollView.bottomAnchor constraintEqualToAnchor:contentView.bottomAnchor] setActive:YES];
[[_optionScrollView.heightAnchor constraintEqualToConstant:BUTTON_HEIGHT] setActive:YES];
_optionStackView = [[UIStackView alloc] initWithFrame:CGRectZero];
[_optionStackView setAlignment:UIStackViewAlignmentLeading];
[_optionStackView setAxis:UILayoutConstraintAxisHorizontal];
[_optionStackView setDistribution:UIStackViewDistributionFill];
[_optionStackView setSpacing:BUTTON_SPACING];
[_optionStackView setTranslatesAutoresizingMaskIntoConstraints:NO];
[_optionScrollView addSubview:_optionStackView];
[[_optionStackView.leadingAnchor constraintEqualToAnchor:_optionScrollView.leadingAnchor] setActive:YES];
[[_optionStackView.trailingAnchor constraintEqualToAnchor:_optionScrollView.trailingAnchor] setActive:YES];
[[_optionStackView.topAnchor constraintEqualToAnchor:_optionScrollView.topAnchor] setActive:YES];
[[_optionStackView.bottomAnchor constraintEqualToAnchor:_optionScrollView.bottomAnchor] setActive:YES];
[[_optionStackView.heightAnchor constraintEqualToAnchor:_optionScrollView.heightAnchor] setActive:YES];
[self parseSpecifiers:specifier];
}
return self;
}
- (instancetype)initWithSpecifier:(PSSpecifier *)specifier {
self = [self initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NecrtaExampleTableCell" specifier:specifier];
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGSize avatarSize = self.avatarImageView.bounds.size;
NSLog(@"avatarSize: %f", avatarSize.width);
[_avatarImageView.layer setCornerRadius:MIN(avatarSize.height, avatarSize.width) / 2];
}
- (void)setName:(NSString *)name {
[_nameLabel setText:name];
}
- (void)setPosition:(NSString *)position {
[_positionLabel setText:position];
}
- (void)setAvatarImage:(UIImage *)avatarImage {
[_avatarImageView setImage:avatarImage];
}
- (void)setCreditOptions:(NSArray *)creditOptions {
_creditOptions = creditOptions;
[self _doCreditOptions];
}
- (void)_doCreditOptions {
for (UIView *subview in _optionStackView.arrangedSubviews) {
[_optionStackView removeArrangedSubview:subview];
[subview removeFromSuperview];
}
[_creditOptions enumerateObjectsUsingBlock:^(id x, NSUInteger index, BOOL *stop) {
NactroCreditOption *creditOption = x;
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setImage:[self imageFromFilename:creditOption.service.imageName] forState:UIControlStateNormal];
[button setAccessibilityLabel:[creditOption getActionTitle]];
[button setTranslatesAutoresizingMaskIntoConstraints:NO];
[[button.heightAnchor constraintEqualToConstant:BUTTON_HEIGHT] setActive:YES];
[[button.widthAnchor constraintEqualToAnchor:button.heightAnchor] setActive:YES];
[button setTag:index];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[_optionStackView addArrangedSubview:button];
}];
}
- (void)buttonTapped:(UIButton *)button {
[_creditOptions[button.tag] open];
}
- (UIImage *)imageFromFilename:(NSString *)filename {
return [UIImage imageWithContentsOfFile:[PREFERENCE_BUNDLE_PATH stringByAppendingPathComponent:filename]];
}
- (void)parseSpecifiers:(PSSpecifier *)specifier {
NSDictionary *properties = specifier.properties;
if(properties[@"creditName"]) {
[self setName:properties[@"creditName"]];
}
if(properties[@"creditPosition"]) {
[self setPosition:properties[@"creditPosition"]];
}
if(properties[@"avatarImage"]) {
[self setAvatarImage:[self imageFromFilename:properties[@"avatarImage"]]];
}
NSMutableArray *newCreditOptions = [NSMutableArray array];
if(properties[@"creditOptions"]) {
NSArray *creditOptions = properties[@"creditOptions"];
for (NSDictionary *option in creditOptions) {
NSString *serviceName = option[@"service"];
NSString *forcedFormattedUsername = option[@"forcedFormattedUsername"];
NSString *username = option[@"username"];
NactroCreditService *service = [NactroCreditService serviceWithName:serviceName];
NactroCreditOption *option = [[NactroCreditOption alloc] initWithUsername:username service:service forcedFormattedUsername:forcedFormattedUsername];
[newCreditOptions addObject:option];
}
}
[self setCreditOptions:newCreditOptions];
}
@end