1+ #include < Foundation/Foundation.h>
12#import < objc/objc.h>
3+ #import < theos/IOSMacros.h>
24#import < UIKit/UIColor+Private.h>
35#import " TLRootViewController.h"
46
@@ -22,6 +24,10 @@ @interface TLRootViewController () {
2224@property (nonatomic , strong ) NSMutableArray <NSLayoutConstraint *> *lockSwitchConstraints;
2325@property (nonatomic , strong ) UILabel *lockSwitchLabel;
2426@property (nonatomic , strong ) NSMutableArray <NSLayoutConstraint *> *lockSwitchLabelConstraints;
27+ @property (nonatomic , strong ) UISegmentedControl *ledCount;
28+ @property (nonatomic , strong ) NSMutableArray <NSLayoutConstraint *> *ledCountConstraints;
29+ @property (nonatomic , strong ) UILabel *ledCountLabel;
30+ @property (nonatomic , strong ) NSMutableArray <NSLayoutConstraint *> *ledCountLabelConstraints;
2531@end
2632
2733@implementation TLRootViewController
@@ -38,6 +44,8 @@ @implementation TLRootViewController
3844@synthesize lockSwitchConstraints = _lockSwitchConstraints;
3945@synthesize lockSwitchLabel = _lockSwitchLabel;
4046@synthesize lockSwitchLabelConstraints = _lockSwitchLabelConstraints;
47+ @synthesize ledCount = _ledCount;
48+ @synthesize ledCountConstraints = _ledCountConstraints;
4149
4250- (instancetype )init {
4351 self = [super init ];
@@ -62,9 +70,13 @@ - (void)releaseStream {
6270 [_deviceManager releaseStream ];
6371}
6472
73+ - (BOOL )isQuadLEDs {
74+ return [[NSUserDefaults standardUserDefaults ] boolForKey: @" TLQuadLEDs" ];
75+ }
76+
6577- (void )handleShortcutAction : (NSString *)shortcutType {
6678 BOOL legacy = [_deviceManager isLegacyLEDs ];
67- BOOL quad = [_deviceManager isQuadLEDs ];
79+ BOOL quad = [self isQuadLEDs ];
6880 if ([shortcutType isEqualToString: @" com.ps.TrollLEDs.AmberOn" ]) {
6981 if (legacy) {
7082 LEDLevel = 100 ;
@@ -152,31 +164,16 @@ - (NSString *)switchLabel {
152164 return locked ? @" On: Only TrollLEDs can control the LEDs" : @" Off: Release the LEDs to other apps (this may take few seconds)" ;
153165}
154166
155- - (void )viewDidLoad {
156- [super viewDidLoad ];
157-
158- NSString *currentError = _deviceManager.currentError ;
159- if (currentError) {
160- [self printError: currentError];
161- return ;
162- }
163-
164- BOOL isLegacyLEDs = [_deviceManager isLegacyLEDs ];
165-
167+ - (void )configureTableView {
166168 UITableView *tableView = (UITableView *)self.view ;
167169 tableView.scrollEnabled = NO ;
168170 tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
169171 tableView.backgroundColor = [UIColor blackColor ];
172+ }
170173
171- _sliders = [[NSMutableArray alloc ] init ];
172- _sliderConstraints = [[NSMutableArray alloc ] init ];
173- _sliderLabels = [[NSMutableArray alloc ] init ];
174- _sliderLabelConstraints = [[NSMutableArray alloc ] init ];
175- _sliderValueLabels = [[NSMutableArray alloc ] init ];
176- _sliderValueLabelConstraints = [[NSMutableArray alloc ] init ];
174+ - (void )configureLockSwitch {
177175 _lockSwitchConstraints = [[NSMutableArray alloc ] init ];
178176 _lockSwitchLabelConstraints = [[NSMutableArray alloc ] init ];
179- int sliderCount = isLegacyLEDs ? 2 : 4 ;
180177
181178 _lockSwitch = [[UISwitch alloc ] init ];
182179 _lockSwitch.translatesAutoresizingMaskIntoConstraints = NO ;
@@ -194,14 +191,44 @@ - (void)viewDidLoad {
194191 _lockSwitchLabel.font = [UIFont systemFontOfSize: 14 ];
195192
196193 [self .view addSubview: _lockSwitchLabel];
194+ }
195+
196+ - (void )configureLEDCount {
197+ _ledCountConstraints = [[NSMutableArray alloc ] init ];
198+ _ledCountLabelConstraints = [[NSMutableArray alloc ] init ];
199+
200+ _ledCount = [[UISegmentedControl alloc ] initWithItems: @[@" Dual" , @" Quad" ]];
201+ _ledCount.translatesAutoresizingMaskIntoConstraints = NO ;
202+ _ledCount.selectedSegmentIndex = [self isQuadLEDs ] ? 1 : 0 ;
203+ [_ledCount addTarget: self action: @selector (ledCountChanged: ) forControlEvents: UIControlEventValueChanged];
204+
205+ [self .view addSubview: _ledCount];
206+
207+ _ledCountLabel = [[UILabel alloc ] init ];
208+ _ledCountLabel.translatesAutoresizingMaskIntoConstraints = NO ;
209+ _ledCountLabel.text = @" Physical LED Count" ;
210+ _ledCountLabel.textColor = [UIColor systemGrayColor ];
211+ _ledCountLabel.textAlignment = NSTextAlignmentCenter;
212+ _ledCountLabel.font = [UIFont systemFontOfSize: 14 ];
213+
214+ [self .view addSubview: _ledCountLabel];
215+ }
216+
217+ - (void )configureLEDSliders : (int )sliderCount maximumValue : (int )maximumValue {
218+ _sliders = [[NSMutableArray alloc ] init ];
219+ _sliderConstraints = [[NSMutableArray alloc ] init ];
220+ _sliderLabels = [[NSMutableArray alloc ] init ];
221+ _sliderLabelConstraints = [[NSMutableArray alloc ] init ];
222+ _sliderValueLabels = [[NSMutableArray alloc ] init ];
223+ _sliderValueLabelConstraints = [[NSMutableArray alloc ] init ];
197224
198225 for (NSInteger i = 0 ; i < sliderCount; i++) {
199226 UIColor *color = [self color: i];
200227
201228 UISlider *slider = [[UISlider alloc ] init ];
202229 slider.translatesAutoresizingMaskIntoConstraints = NO ;
203230 slider.minimumValue = 0 ;
204- slider.maximumValue = isLegacyLEDs ? 100 : 255 ;
231+ slider.maximumValue = maximumValue ;
205232 slider.value = 0 ;
206233 slider.minimumTrackTintColor = color;
207234 slider.transform = CGAffineTransformMakeRotation (-M_PI_2);
@@ -242,6 +269,25 @@ - (void)viewDidLoad {
242269 [self .view addSubview: valueLabel];
243270 [_sliderValueLabels addObject: valueLabel];
244271 }
272+ }
273+
274+ - (void )viewDidLoad {
275+ [super viewDidLoad ];
276+
277+ NSString *currentError = _deviceManager.currentError ;
278+ if (currentError) {
279+ [self printError: currentError];
280+ return ;
281+ }
282+
283+ BOOL isLegacyLEDs = [_deviceManager isLegacyLEDs ];
284+ int sliderCount = isLegacyLEDs ? 2 : 4 ;
285+
286+ [self configureTableView ];
287+ [self configureLockSwitch ];
288+ if (!isLegacyLEDs)
289+ [self configureLEDCount ];
290+ [self configureLEDSliders: sliderCount maximumValue: isLegacyLEDs ? 100 : 255 ];
245291
246292 if (_shortcutAction) {
247293 [self handleShortcutAction: _shortcutAction];
@@ -252,36 +298,56 @@ - (void)viewDidLoad {
252298- (void )viewWillLayoutSubviews {
253299 [super viewWillLayoutSubviews ];
254300
301+ BOOL isLegacyLEDs = [_deviceManager isLegacyLEDs ];
302+
255303 [NSLayoutConstraint deactivateConstraints: _sliderConstraints];
256304 [NSLayoutConstraint deactivateConstraints: _sliderLabelConstraints];
257305 [NSLayoutConstraint deactivateConstraints: _sliderValueLabelConstraints];
258306 [NSLayoutConstraint deactivateConstraints: _lockSwitchConstraints];
259307 [NSLayoutConstraint deactivateConstraints: _lockSwitchLabelConstraints];
308+
260309 [_sliderConstraints removeAllObjects ];
261310 [_sliderLabelConstraints removeAllObjects ];
262311 [_sliderValueLabelConstraints removeAllObjects ];
263312 [_lockSwitchConstraints removeAllObjects ];
264313 [_lockSwitchLabelConstraints removeAllObjects ];
265314
266- UISwitch *lockSwitch = _lockSwitch;
315+ if (!isLegacyLEDs) {
316+ [NSLayoutConstraint deactivateConstraints: _ledCountConstraints];
317+ [NSLayoutConstraint deactivateConstraints: _ledCountLabelConstraints];
318+ [_ledCountConstraints removeAllObjects ];
319+ [_ledCountLabelConstraints removeAllObjects ];
320+ }
267321
268- NSLayoutConstraint *lockSwitchCenterX = [lockSwitch .centerXAnchor constraintEqualToAnchor: self .view.centerXAnchor];
269- NSLayoutConstraint *lockSwitchTop = [lockSwitch .topAnchor constraintEqualToAnchor: self .view.topAnchor constant: 20 ];
322+ NSLayoutConstraint *lockSwitchCenterX = [_lockSwitch .centerXAnchor constraintEqualToAnchor: self .view.centerXAnchor];
323+ NSLayoutConstraint *lockSwitchTop = [_lockSwitch .topAnchor constraintEqualToAnchor: self .view.topAnchor constant: 20 ];
270324
271325 [_lockSwitchConstraints addObjectsFromArray: @[lockSwitchCenterX, lockSwitchTop]];
272326 [NSLayoutConstraint activateConstraints: _lockSwitchConstraints];
273327
274- UILabel *lockSwitchLabel = _lockSwitchLabel;
275-
276- NSLayoutConstraint *lockSwitchLabelCenterX = [lockSwitchLabel.centerXAnchor constraintEqualToAnchor: self .view.centerXAnchor];
277- NSLayoutConstraint *lockSwitchLabelTop = [lockSwitchLabel.topAnchor constraintEqualToAnchor: lockSwitch.bottomAnchor constant: 10 ];
278- NSLayoutConstraint *lockSwitchLabelWidth = [lockSwitchLabel.widthAnchor constraintEqualToAnchor: self .view.widthAnchor multiplier: 0.9 ];
328+ NSLayoutConstraint *lockSwitchLabelCenterX = [_lockSwitchLabel.centerXAnchor constraintEqualToAnchor: self .view.centerXAnchor];
329+ NSLayoutConstraint *lockSwitchLabelTop = [_lockSwitchLabel.topAnchor constraintEqualToAnchor: _lockSwitch.bottomAnchor constant: 10 ];
330+ NSLayoutConstraint *lockSwitchLabelWidth = [_lockSwitchLabel.widthAnchor constraintEqualToAnchor: self .view.widthAnchor multiplier: 0.9 ];
279331
280332 [_lockSwitchLabelConstraints addObjectsFromArray: @[lockSwitchLabelCenterX, lockSwitchLabelTop, lockSwitchLabelWidth]];
281333 [NSLayoutConstraint activateConstraints: _lockSwitchLabelConstraints];
282334
335+ if (!isLegacyLEDs) {
336+ NSLayoutConstraint *ledCountCenterX = [_ledCount.centerXAnchor constraintEqualToAnchor: self .view.centerXAnchor];
337+ NSLayoutConstraint *ledCountBottom = [_ledCount.bottomAnchor constraintEqualToAnchor: self .view.safeAreaLayoutGuide.bottomAnchor constant: -20 ];
338+
339+ [_ledCountConstraints addObjectsFromArray: @[ledCountCenterX, ledCountBottom]];
340+ [NSLayoutConstraint activateConstraints: _ledCountConstraints];
341+
342+ NSLayoutConstraint *ledCountLabelCenterX = [_ledCountLabel.centerXAnchor constraintEqualToAnchor: self .view.centerXAnchor];
343+ NSLayoutConstraint *ledCountLabelBottom = [_ledCountLabel.bottomAnchor constraintEqualToAnchor: _ledCount.topAnchor constant: -10 ];
344+
345+ [_ledCountLabelConstraints addObjectsFromArray: @[ledCountLabelCenterX, ledCountLabelBottom]];
346+ [NSLayoutConstraint activateConstraints: _ledCountLabelConstraints];
347+ }
348+
283349 CGFloat sliderWidth = 30.0 ;
284- CGFloat sliderHeight = self.view .bounds .size .height * 0.4 ;
350+ CGFloat sliderHeight = self.view .bounds .size .height * (IS_IPAD || isLegacyLEDs ? 0.4 : 0.3 ) ;
285351 CGFloat totalSlidersWidth = _sliders.count * sliderWidth;
286352 CGFloat spacing = (self.view .bounds .size .width - totalSlidersWidth) / (_sliders.count + 1 );
287353
@@ -315,6 +381,10 @@ - (void)viewWillLayoutSubviews {
315381 [NSLayoutConstraint activateConstraints: _sliderValueLabelConstraints];
316382}
317383
384+ - (void )ledCountChanged : (UISegmentedControl *)sender {
385+ [[NSUserDefaults standardUserDefaults ] setBool: sender.selectedSegmentIndex == 1 forKey: @" TLQuadLEDs" ];
386+ }
387+
318388- (void )sliderValueTapped : (UITapGestureRecognizer *)sender {
319389 if (!locked) return ;
320390 UILabel *label = (UILabel *)sender.view ;
0 commit comments