From b62508f1601bdecf10ec997fd2b7f000b38546dd Mon Sep 17 00:00:00 2001 From: Burrit0z Date: Tue, 2 Jun 2020 17:02:46 -0400 Subject: [PATCH] reworking --- KAIBatteryStack.h => KAIBatteryPlatter.h | 5 +- KAIBatteryStack.mm => KAIBatteryPlatter.mm | 53 ++++---- KAIBatteryStack copy.mm | 138 --------------------- KAIStackView.h | 3 + KAIStackView.mm | 11 ++ Kai.h | 9 +- Kai.xm | 28 ++--- 7 files changed, 66 insertions(+), 181 deletions(-) rename KAIBatteryStack.h => KAIBatteryPlatter.h (70%) rename KAIBatteryStack.mm => KAIBatteryPlatter.mm (78%) delete mode 100755 KAIBatteryStack copy.mm create mode 100644 KAIStackView.h create mode 100644 KAIStackView.mm diff --git a/KAIBatteryStack.h b/KAIBatteryPlatter.h similarity index 70% rename from KAIBatteryStack.h rename to KAIBatteryPlatter.h index 941d02d..81e662b 100755 --- a/KAIBatteryStack.h +++ b/KAIBatteryPlatter.h @@ -1,10 +1,11 @@ -@interface KAIBatteryStack : UIStackView +@interface KAIBatteryPlatter : UIStackView @property (nonatomic, assign) NSInteger number; @property (nonatomic, assign) NSInteger oldCountOfDevices; @property (nonatomic, strong) NSLayoutConstraint *heightConstraint; +@property (nonatomic, strong) KAIStackView *stack; @property (nonatomic, assign) BOOL isUpdating; @property (nonatomic, assign) BOOL queued; -+(KAIBatteryStack *)sharedInstance; ++(KAIBatteryPlatter *)sharedInstance; -(instancetype)init; -(void)refreshForPrefs; -(void)updateBattery; diff --git a/KAIBatteryStack.mm b/KAIBatteryPlatter.mm similarity index 78% rename from KAIBatteryStack.mm rename to KAIBatteryPlatter.mm index bc4a6e8..4b37947 100755 --- a/KAIBatteryStack.mm +++ b/KAIBatteryPlatter.mm @@ -1,22 +1,23 @@ -#import "KAIBatteryStack.h" +#import "KAIBatteryPlatter.h" -KAIBatteryStack *instance; +KAIBatteryPlatter *instance; NSTimer *queueTimer = nil; -@implementation KAIBatteryStack +@implementation KAIBatteryPlatter -(instancetype)init { self = [super init]; instance = self; if (self) { - self.axis = 1; - self.distribution = 0; - self.spacing = 0; - self.alignment = 0; + self.stack = [[KAIStackView alloc] init]; + self.stack.axis = 1; + self.stack.distribution = 0; + self.stack.spacing = 0; + self.stack.alignment = 0; self.oldCountOfDevices = -100; self.queued = NO; + [self addSubview:self.stack]; [self updateBattery]; - self.userInteractionEnabled = NO; } return self; } @@ -25,6 +26,9 @@ long long batteryPercentage; long long lastPercentage; -(void)updateBattery { + if(!self.stack.widthAnchor) { + [self.stack.widthAnchor constraintEqualToAnchor:self.widthAnchor].active = YES; + } dispatch_async(dispatch_get_main_queue(), ^{ BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance]; NSArray *devices = MSHookIvar(bcb, "_sortedDevices"); @@ -53,33 +57,33 @@ long long lastPercentage; shouldAdd = YES; } - if(![self.subviews containsObject:cell] && shouldAdd && [devices containsObject:device]) { + if(![self.stack.subviews containsObject:cell] && shouldAdd && [devices containsObject:device]) { //[cell setFrame:CGRectMake(0,0,self.frame.size.width, bannerHeight)]; cell.alpha = 0; - [self addSubview:cell]; - [self addArrangedSubview:cell]; + [self.stack addSubview:cell]; + [self.stack addArrangedSubview:cell]; [UIView animateWithDuration:0.3 animations:^{ cell.alpha = 1; }]; - } else if([self.subviews containsObject:cell] && !shouldAdd){ + } else if([self.stack.subviews containsObject:cell] && !shouldAdd){ [UIView animateWithDuration:0.3 animations:^{ cell.alpha = 0; } completion:^(BOOL finished) { [cell removeFromSuperview]; - [self removeArrangedSubview:cell]; + [self.stack removeArrangedSubview:cell]; cell.alpha = 1; }]; } } - for(KAIBatteryCell *cell in self.subviews) { + for(KAIBatteryCell *cell in self.stack.subviews) { if(![devices containsObject:cell.device]) { [UIView animateWithDuration:0.3 animations:^{ cell.alpha = 0; } completion:^(BOOL finished) { [cell removeFromSuperview]; - [self removeArrangedSubview:cell]; + [self.stack removeArrangedSubview:cell]; cell.alpha = 1; }]; } @@ -94,7 +98,7 @@ long long lastPercentage; self.oldCountOfDevices = [devices count]; - self.number = [self.subviews count]; + self.number = [self.stack.subviews count]; if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) { [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame]; @@ -110,14 +114,17 @@ long long lastPercentage; if(!self.heightConstraint) { self.heightConstraint = [self.heightAnchor constraintEqualToConstant:(self.number * (bannerHeight + spacing))]; + self.stack.heightConstraint = [self.heightAnchor constraintEqualToConstant:(self.number * (bannerHeight + spacing))]; self.heightConstraint.active = YES; + self.stack.heightConstraint.active = YES; } else { int height = (self.number * (bannerHeight + spacing)); - //if([self.superview.subviews count]>1) { - // height = (self.number * (bannerHeight + spacing)) - spacing; - //} + if([self.superview.subviews count]>1) { + height = (self.number * (bannerHeight + spacing)) - spacing; + } self.heightConstraint.constant = height; + self.stack.heightConstraint.constant = height; UIStackView *s = (UIStackView *)(self.superview); s.frame = CGRectMake(s.frame.origin.x, s.frame.origin.y, s.frame.size.width, (s.frame.size.height - 1)); @@ -129,7 +136,7 @@ long long lastPercentage; -(void)addArrangedSubview:(UIView *)view { [super addArrangedSubview:view]; - self.number = [self.subviews count]; + self.number = [self.stack.subviews count]; if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) { [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame]; } @@ -150,7 +157,7 @@ long long lastPercentage; -(void)removeArrangedSubview:(UIView *)view { [super removeArrangedSubview:view]; - self.number = [self.subviews count]; + self.number = [self.stack.subviews count]; if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) { [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame]; } @@ -158,7 +165,7 @@ long long lastPercentage; } -(void)refreshForPrefs { - for( UIView *view in self.subviews ) { + for( UIView *view in self.stack.subviews ) { @try { [view removeFromSuperview]; } @catch (NSException *exception) { @@ -188,7 +195,7 @@ long long lastPercentage; queueTimer = nil; } -+(KAIBatteryStack *)sharedInstance { ++(KAIBatteryPlatter *)sharedInstance { return instance; } diff --git a/KAIBatteryStack copy.mm b/KAIBatteryStack copy.mm deleted file mode 100755 index 566f994..0000000 --- a/KAIBatteryStack copy.mm +++ /dev/null @@ -1,138 +0,0 @@ -#import "KAIBatteryStack.h" - -KAIBatteryStack *instance; -//NSMutableArray *showingCells = [[NSMutableArray alloc] init]; - -@implementation KAIBatteryStack - --(instancetype)init { - self = [super init]; - instance = self; - if (self) { - self.displayingDevices = [[NSMutableArray alloc] init]; - self.axis = 1; - self.distribution = 0; - self.spacing = spacing; - self.alignment = 0; - [self updateBattery]; - self.clipsToBounds = YES; - self.userInteractionEnabled = NO; - } - return self; -} - -long long batteryPercentage; -long long lastPercentage; - --(void)updateBattery { - self.spacing = spacing; - dispatch_async(dispatch_get_main_queue(), ^{ - //NSLog(@"kai: battery platter called to update"); - if(!self.isUpdating) { - //NSLog(@"kai: IS Updating"); - self.isUpdating = YES; - //self.number = 0; - float y = 0; - BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance]; - NSArray *devices = MSHookIvar(bcb, "_sortedDevices"); - if([devices count]!=0) { - //NSLog(@"kai: info is good, will proceed"); - - float ytwo = 0; - - for(KAIBatteryStackCell *cell in self.subviews) { - if([cell respondsToSelector:@selector(updateInfo)] && ![devices containsObject:cell.device]) { //to confirm is a cell and battery device does not exist - //dispatch_async(dispatch_get_main_queue(), ^{ - [UIView animateWithDuration:0.2 animations:^{ - cell.alpha = 0; - } completion:^(BOOL finished){ - [cell removeFromSuperview]; - }]; - //}); - } else if([cell respondsToSelector:@selector(updateInfo)]) { - cell.frame = CGRectMake(0, y, self.frame.size.width, bannerHeight); - [cell updateInfo]; - ytwo+= bannerHeight + spacing; - } - } - - - for (BCBatteryDevice *device in devices) { - NSString *deviceName = MSHookIvar(device, "_name"); - //double batteryPercentage = MSHookIvar(device, "_percentCharge"); - BOOL charging = MSHookIvar(device, "_charging"); - //BOOL LPM = MSHookIvar(device, "_batterySaverModeActive"); - - BOOL shouldAdd = NO; - - if(showAll) { - shouldAdd = YES; - //NSLog(@"Kai: SHOULD ADD"); - } else if(!showAll && charging) { - shouldAdd = YES; - //NSLog(@"Kai: SHOULD ADD"); - } - - KAIBatteryStackCell *cell = [KAIBatteryStackCell cellForDeviceIfExists:device frameToCreateNew:CGRectMake(0, y, self.frame.size.width, bannerHeight)]; - cell.frame = CGRectMake(0, y, self.frame.size.width, bannerHeight); - - if(cell) { - cell.device = device; - //cell.frame = cell.frame = CGRectMake(0, y, self.frame.size.width, bannerHeight); //bro im like creating my own stack view - //[cell updateInfo]; - } - - if(shouldAdd && [deviceName length]!=0) { - if(![self.subviews containsObject:cell]) { - cell.frame = CGRectMake(0, y, self.frame.size.width, bannerHeight); - cell.alpha = 0; - [self addSubview:cell]; - [UIView animateWithDuration:0.3 animations:^{ - cell.alpha = 1; - }]; - } - y+=bannerHeight + spacing; - - } else if(!shouldAdd) { - //dispatch_async(dispatch_get_main_queue(), ^{ - [UIView animateWithDuration:0.2 animations:^{ - cell.alpha = 0; - } completion:^(BOOL finished){ - [cell removeFromSuperview]; - }]; - //}); - } - } - //[self.heightAnchor constraintEqualToConstant:(self.number * 85)].active = YES; - self.number = [self.subviews count]; - //[(CSAdjunctListView *)self.superview.superview KaiUpdate]; - } - self.isUpdating = NO; - //NSLog(@"kai: finished update"); - //[(CSAdjunctListView *)self.superview.superview KaiUpdate]; - [(CSAdjunctListView *)self.superview.superview performSelector:@selector(KaiUpdate) withObject:(CSAdjunctListView *)self.superview.superview afterDelay:0.2]; - } - }); -} - --(void)removeAllAndRefresh { - for( UIView *view in self.subviews ) { - @try { - [view removeFromSuperview]; - } @catch (NSException *exception) { - //Panik - } - } - [KAIBatteryStackCell resetArray]; - - //self.displayingDevices = [[NSMutableArray alloc] init]; - - //addedCells = nil; - [self updateBattery]; -} - -+(KAIBatteryStack *)sharedInstance { - return instance; -} - -@end \ No newline at end of file diff --git a/KAIStackView.h b/KAIStackView.h new file mode 100644 index 0000000..5b91e35 --- /dev/null +++ b/KAIStackView.h @@ -0,0 +1,3 @@ +@interface KAIStackView : UIStackView +@property (nonatomic, strong) NSLayoutConstraint *heightConstraint; +@end \ No newline at end of file diff --git a/KAIStackView.mm b/KAIStackView.mm new file mode 100644 index 0000000..e30ba24 --- /dev/null +++ b/KAIStackView.mm @@ -0,0 +1,11 @@ +#import "KAIStackView.h" + +@implementation KAIStackView + +-(id)initWithFrame:(CGRect)arg1 { + self = [super initWithFrame:arg1]; + self.translatesAutoresizingMaskIntoConstraints = NO; + return self; +} + +@end \ No newline at end of file diff --git a/Kai.h b/Kai.h index e5b52e6..e0e38a9 100755 --- a/Kai.h +++ b/Kai.h @@ -7,7 +7,7 @@ #import "NSTask.h" #define KAISelf ((CSAdjunctListView *)self) //for use when calling self in KAITarget -//#define KAIBatteryStack UHDUEIHGCEBCHYDEICVKEVSAGJKBCXAHJGKVXHAS //lmao +//#define KAIBatteryPlatter UHDUEIHGCEBCHYDEICVKEVSAGJKBCXAHJGKVXHAS //lmao //#define KAIBatteryCell HDEIUOGEUBGUYOEXHNOPUSZIOJIGECEXIUSHXJXBE //very good @interface CSAdjunctListView : UIView @@ -60,7 +60,8 @@ double bannerAlpha; //by importing here, I can use vars in the .mm files #import "KAIBatteryCell.mm" -#import "KAIBatteryStack.mm" +#import "KAIStackView.mm" +#import "KAIBatteryPlatter.mm" #define PLIST_PATH @"/User/Library/Preferences/com.burritoz.kaiprefs.plist" #define kIdentifier @"com.burritoz.kaiprefs" @@ -138,8 +139,8 @@ static void applyPrefs() isUpdating = YES; - [[KAIBatteryStack sharedInstance] refreshForPrefs]; //so hard (not) - [(CSAdjunctListView *)([KAIBatteryStack sharedInstance].superview.superview) _layoutStackView]; + [[KAIBatteryPlatter sharedInstance] refreshForPrefs]; //so hard (not) + [(CSAdjunctListView *)([KAIBatteryPlatter sharedInstance].superview.superview) _layoutStackView]; isUpdating = NO; diff --git a/Kai.xm b/Kai.xm index 2aa4ccd..a1b429e 100755 --- a/Kai.xm +++ b/Kai.xm @@ -10,18 +10,18 @@ NSInteger lastSlot = [[self stackView].subviews count] -1; //this code is used to determine if kai is at the bottom of the stack view - if([[self stackView].subviews objectAtIndex:lastSlot] != [KAIBatteryStack sharedInstance] && belowMusic) { + if([[self stackView].subviews objectAtIndex:lastSlot] != [KAIBatteryPlatter sharedInstance] && belowMusic) { //if it is not, but the option to have kai below music is on, i simply remove from it's current pos. //and insert into last slot. - [[self stackView] removeArrangedSubview:[KAIBatteryStack sharedInstance]]; - [[self stackView] insertArrangedSubview:[KAIBatteryStack sharedInstance] atIndex:lastSlot]; + [[self stackView] removeArrangedSubview:[KAIBatteryPlatter sharedInstance]]; + [[self stackView] insertArrangedSubview:[KAIBatteryPlatter sharedInstance] atIndex:lastSlot]; } if([KAISelf.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) { [(NCNotificationListView *)(KAISelf.superview) fixComplicationsViewFrame]; } - [[KAIBatteryStack sharedInstance] setNumber:[KAIBatteryStack sharedInstance].number]; + [[KAIBatteryPlatter sharedInstance] setNumber:[KAIBatteryPlatter sharedInstance].number]; %orig; } @@ -29,7 +29,7 @@ -(void)setStackView:(UIStackView *)arg1 { if(!KAISelf.hasKai) { - KAIBatteryStack *battery = [[KAIBatteryStack alloc] init]; + KAIBatteryPlatter *battery = [[KAIBatteryPlatter alloc] init]; //Add noti observer [[NSNotificationCenter defaultCenter] addObserver:self @@ -60,13 +60,13 @@ //NSLog(@"kai: kai info will update"); dispatch_async(dispatch_get_main_queue(), ^{ - [[KAIBatteryStack sharedInstance] updateBattery]; - if([KAIBatteryStack sharedInstance].number == 0) { - [[KAIBatteryStack sharedInstance] removeFromSuperview]; - [[self stackView] removeArrangedSubview:[KAIBatteryStack sharedInstance]]; - } else if(![[self stackView].subviews containsObject:[KAIBatteryStack sharedInstance]]) { - [[self stackView] addSubview:[KAIBatteryStack sharedInstance]]; - [[self stackView] addArrangedSubview:[KAIBatteryStack sharedInstance]]; + [[KAIBatteryPlatter sharedInstance] updateBattery]; + if([KAIBatteryPlatter sharedInstance].number == 0) { + [[KAIBatteryPlatter sharedInstance] removeFromSuperview]; + [[self stackView] removeArrangedSubview:[KAIBatteryPlatter sharedInstance]]; + } else if(![[self stackView].subviews containsObject:[KAIBatteryPlatter sharedInstance]]) { + [[self stackView] addSubview:[KAIBatteryPlatter sharedInstance]]; + [[self stackView] addArrangedSubview:[KAIBatteryPlatter sharedInstance]]; } if([KAISelf.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) { [KAISelf.superview performSelector:@selector(fixComplicationsViewFrame) withObject:KAISelf.superview afterDelay:0.35]; @@ -107,7 +107,7 @@ %new -(id)kaiCellForDevice { if(self && self.kaiCell == nil) { - self.kaiCell = [[KAIBatteryCell alloc] initWithFrame:CGRectMake(0,0,[KAIBatteryStack sharedInstance].frame.size.width,0) device:self]; } + self.kaiCell = [[KAIBatteryCell alloc] initWithFrame:CGRectMake(0,0,[KAIBatteryPlatter sharedInstance].frame.size.width,0) device:self]; } ((KAIBatteryCell *)self.kaiCell).translatesAutoresizingMaskIntoConstraints = NO; [(KAIBatteryCell *)self.kaiCell updateInfo]; @@ -116,7 +116,7 @@ %new -(void)resetKaiCellForNewPrefs { - self.kaiCell = [[KAIBatteryCell alloc] initWithFrame:CGRectMake(0,0,[KAIBatteryStack sharedInstance].frame.size.width,0) device:self]; + self.kaiCell = [[KAIBatteryCell alloc] initWithFrame:CGRectMake(0,0,[KAIBatteryPlatter sharedInstance].frame.size.width,0) device:self]; ((KAIBatteryCell *)self.kaiCell).translatesAutoresizingMaskIntoConstraints = NO; [(KAIBatteryCell *)self.kaiCell updateInfo]; }