From 80c9a3ed5b9ae3ad37b535cf7e4e620a333277c8 Mon Sep 17 00:00:00 2001 From: Burrit0z Date: Fri, 22 May 2020 19:25:07 -0400 Subject: [PATCH] ah yes, broken af --- KAIBatteryCell.h | 7 +- KAIBatteryCell.mm | 12 +-- KAIBatteryStack copy.mm | 138 ++++++++++++++++++++++++++++ KAIBattery.h => KAIBatteryStack.h | 4 +- KAIBattery.mm => KAIBatteryStack.mm | 19 ++-- Kai.h | 17 ++-- Kai.xm | 52 ++++++----- Layout/DEBIAN/control | 2 +- 8 files changed, 202 insertions(+), 49 deletions(-) create mode 100644 KAIBatteryStack copy.mm rename KAIBattery.h => KAIBatteryStack.h (78%) rename KAIBattery.mm => KAIBatteryStack.mm (93%) diff --git a/KAIBatteryCell.h b/KAIBatteryCell.h index 5dc1e0a..f2cb2f6 100644 --- a/KAIBatteryCell.h +++ b/KAIBatteryCell.h @@ -17,13 +17,18 @@ +(id)materialViewWithRecipe:(NSInteger)arg1 options:(NSInteger)arg2 initialWeighting:(CGFloat)arg3 scaleAdjustment:(id)arg4; @end -@interface BCBatteryDeviceController +@interface BCBatteryDeviceController : NSObject @property (nonatomic, strong) NSArray *sortedDevices; -(id)_sortedDevices; +(id)sharedInstance; @end @interface BCBatteryDevice : NSObject +@property (nonatomic, strong) id kaiCell; +@property (nonatomic, strong) NSString *name; +@property (nonatomic, assign) long long percentCharge; +@property (nonatomic, assign) BOOL charging; +@property (nonatomic, assign) BOOL batterySaverModeActive; @property (nonatomic, strong) NSString *identifier; -(id)glyph; @end diff --git a/KAIBatteryCell.mm b/KAIBatteryCell.mm index cc5ab2d..40a97f9 100644 --- a/KAIBatteryCell.mm +++ b/KAIBatteryCell.mm @@ -6,14 +6,14 @@ NSMutableArray *deviceInstances = [[NSMutableArray alloc] init]; -(instancetype)initWithFrame:(CGRect)arg1 device:(BCBatteryDevice *)device { self = [super initWithFrame:arg1]; - if(self) { + if(self && device!=nil) { self.device = device; - - NSString *deviceName = MSHookIvar(device, "_name"); - double batteryPercentage = MSHookIvar(device, "_percentCharge"); - BOOL charging = MSHookIvar(device, "_charging"); - BOOL LPM = MSHookIvar(device, "_batterySaverModeActive"); + + NSString *deviceName = device.name; + double batteryPercentage = device.percentCharge; + BOOL charging = device.charging; + BOOL LPM = device.batterySaverModeActive; UIView *blank; if(bannerStyle==1) { diff --git a/KAIBatteryStack copy.mm b/KAIBatteryStack copy.mm new file mode 100644 index 0000000..566f994 --- /dev/null +++ b/KAIBatteryStack copy.mm @@ -0,0 +1,138 @@ +#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/KAIBattery.h b/KAIBatteryStack.h similarity index 78% rename from KAIBattery.h rename to KAIBatteryStack.h index d19c76e..c978a9a 100644 --- a/KAIBattery.h +++ b/KAIBatteryStack.h @@ -1,9 +1,9 @@ -@interface KAIBattery : UIView +@interface KAIBatteryStack : UIStackView @property (nonatomic, strong) NSMutableArray *displayingDevices; @property (nonatomic, assign) NSInteger number; @property (nonatomic, strong) NSLayoutConstraint *heightConstraint; @property (nonatomic, assign) BOOL isUpdating; -+(KAIBattery *)sharedInstance; ++(KAIBatteryStack *)sharedInstance; -(instancetype)init; -(void)removeAllAndRefresh; -(void)updateBattery; diff --git a/KAIBattery.mm b/KAIBatteryStack.mm similarity index 93% rename from KAIBattery.mm rename to KAIBatteryStack.mm index 10bc59b..0b9999f 100644 --- a/KAIBattery.mm +++ b/KAIBatteryStack.mm @@ -1,17 +1,21 @@ -#import "KAIBattery.h" +#import "KAIBatteryStack.h" -KAIBattery *instance; +KAIBatteryStack *instance; //NSMutableArray *showingCells = [[NSMutableArray alloc] init]; -@implementation KAIBattery +@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.clipsToBounds = YES; self.userInteractionEnabled = NO; } return self; @@ -21,6 +25,8 @@ 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) { @@ -107,7 +113,8 @@ long long lastPercentage; //[(CSAdjunctListView *)self.superview.superview KaiUpdate]; [(CSAdjunctListView *)self.superview.superview performSelector:@selector(KaiUpdate) withObject:(CSAdjunctListView *)self.superview.superview afterDelay:0.2]; } - }); + });*/ + self.number = [self.subviews count]; } -(void)removeAllAndRefresh { @@ -126,7 +133,7 @@ long long lastPercentage; [self updateBattery]; } -+(KAIBattery *)sharedInstance { ++(KAIBatteryStack *)sharedInstance { return instance; } diff --git a/Kai.h b/Kai.h index 8a8165f..0584ab8 100644 --- a/Kai.h +++ b/Kai.h @@ -5,7 +5,8 @@ #import #define KAISelf ((CSAdjunctListView *)self) //for use when calling self in KAITarget -#define KAIBattery UHDUEIHGCEBCHYDEICVKEVSAGJKBCXAHJGKVXHAS //lmao +//#define KAIBatteryStack UHDUEIHGCEBCHYDEICVKEVSAGJKBCXAHJGKVXHAS //lmao +//#define KAIBatteryCell HDEIUOGEUBGUYOEXHNOPUSZIOJIGECEXIUSHXJXBE //very good @interface CSAdjunctListView : UIView @property (nonatomic, assign) BOOL hasKai; @@ -39,7 +40,7 @@ double horizontalOffset; //by importing here, I can use vars in the .mm files #import "KAIBatteryCell.mm" -#import "KAIBattery.mm" +#import "KAIBatteryStack.mm" #define PLIST_PATH @"/User/Library/Preferences/com.burritoz.kaiprefs.plist" #define kIdentifier @"com.burritoz.kaiprefs" @@ -114,20 +115,20 @@ static void applyPrefs() isUpdating = YES; - [[KAIBattery sharedInstance] removeAllAndRefresh]; - [(CSAdjunctListView *)([KAIBattery sharedInstance].superview.superview) KaiUpdate]; + [[KAIBatteryStack sharedInstance] removeAllAndRefresh]; + [(CSAdjunctListView *)([KAIBatteryStack sharedInstance].superview.superview) KaiUpdate]; isUpdating = NO; //here I remotely refresh the KAIView. /*isUpdating = YES; [UIView animateWithDuration:0.3 animations:^{ - [KAIBattery sharedInstance].alpha = 0; + [KAIBatteryStack sharedInstance].alpha = 0; } completion:^(BOOL finished){ - [[KAIBattery sharedInstance] updateBattery]; - [(CSAdjunctListView *)([KAIBattery sharedInstance].superview.superview) KaiUpdate]; + [[KAIBatteryStack sharedInstance] updateBattery]; + [(CSAdjunctListView *)([KAIBatteryStack sharedInstance].superview.superview) KaiUpdate]; [UIView animateWithDuration:0.35 animations:^{ - [KAIBattery sharedInstance].alpha = 1; + [KAIBatteryStack sharedInstance].alpha = 1; } completion:^(BOOL finished){ isUpdating = NO; }]; diff --git a/Kai.xm b/Kai.xm index 03265f5..b6e67cd 100644 --- a/Kai.xm +++ b/Kai.xm @@ -8,11 +8,11 @@ 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] != [KAIBattery sharedInstance] && belowMusic) { + if([[self stackView].subviews objectAtIndex:lastSlot] != [KAIBatteryStack 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:[KAIBattery sharedInstance]]; - [[self stackView] insertArrangedSubview:[KAIBattery sharedInstance] atIndex:lastSlot]; + [[self stackView] removeArrangedSubview:[KAIBatteryStack sharedInstance]]; + [[self stackView] insertArrangedSubview:[KAIBatteryStack sharedInstance] atIndex:lastSlot]; } //makes kai lay itself out when the stack does @@ -25,7 +25,7 @@ -(void)setStackView:(UIStackView *)arg1 { if(!KAISelf.hasKai) { - KAIBattery *battery = [[KAIBattery alloc] init]; + KAIBatteryStack *battery = [[KAIBatteryStack alloc] init]; //Add noti observer [[NSNotificationCenter defaultCenter] addObserver:self @@ -47,7 +47,7 @@ %new -(void)KaiUpdate { - KAIBattery *battery = [KAIBattery sharedInstance]; + KAIBatteryStack *battery = [KAIBatteryStack sharedInstance]; battery.number = [battery.subviews count]; [UIView animateWithDuration:0.3 animations:^{ @@ -84,31 +84,12 @@ //NSLog(@"kai: kai info will update"); dispatch_async(dispatch_get_main_queue(), ^{ - [[KAIBattery sharedInstance] updateBattery]; + [[KAIBatteryStack sharedInstance] updateBattery]; [self KaiUpdate]; isUpdating = NO; }); - /*isUpdating = YES; - [UIView animateWithDuration:0.3 animations:^{ - - //nice fade out - [KAIBattery sharedInstance].alpha = 0; - - } completion:^(BOOL finished){ - - [[KAIBattery sharedInstance] updateBattery]; - [self KaiUpdate]; - [UIView animateWithDuration:0.35 animations:^{ - //fade back in - [KAIBattery sharedInstance].alpha = 1; - } completion:^(BOOL finished){ - isUpdating = NO; - }]; - - }];*/ - } } @@ -116,6 +97,7 @@ %hook BCBatteryDevice +%property (nonatomic, strong) KAIBatteryCell *kaiCell; - (id)initWithIdentifier:(id)arg1 vendor:(long long)arg2 productIdentifier:(long long)arg3 parts:(unsigned long long)arg4 matchIdentifier:(id)arg5 { @@ -128,9 +110,29 @@ } -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ + if(self && self.kaiCell == nil) { + self.kaiCell = [[KAIBatteryCell alloc] initWithFrame:CGRectMake(0,0,0,0) device:self]; } + ((KAIBatteryCell *)self.kaiCell).translatesAutoresizingMaskIntoConstraints = NO; + [((KAIBatteryCell *)self.kaiCell).heightAnchor constraintEqualToConstant:bannerHeight].active = YES; dispatch_async(dispatch_get_main_queue(), ^{ //sends the noti to update battery info [[NSNotificationCenter defaultCenter] postNotificationName:@"KaiInfoChanged" object:nil userInfo:nil]; + [(KAIBatteryCell *)self.kaiCell updateInfo]; + + BOOL shouldAdd = NO; + + if(showAll) { + shouldAdd = YES; + } else if(!showAll && self.charging) { + shouldAdd = YES; + } + + if(![[KAIBatteryStack sharedInstance].subviews containsObject:self.kaiCell] && shouldAdd) { + [[KAIBatteryStack sharedInstance] addArrangedSubview:self.kaiCell]; + } else if([[KAIBatteryStack sharedInstance].subviews containsObject:self.kaiCell] && !shouldAdd) { + [[KAIBatteryStack sharedInstance] removeArrangedSubview:self.kaiCell]; + } + }); } diff --git a/Layout/DEBIAN/control b/Layout/DEBIAN/control index 2b0bf10..5472595 100644 --- a/Layout/DEBIAN/control +++ b/Layout/DEBIAN/control @@ -1,6 +1,6 @@ Package: com.burritoz.kai Name: Kai -Version: 0.2.0~alpha +Version: 0.2.5~alpha Architecture: iphoneos-arm Description: Show charging banners on your lock screen! Maintainer: burrit0z