diff --git a/KAIBattery.h b/KAIBattery.h index aff1ca6..d19c76e 100644 --- a/KAIBattery.h +++ b/KAIBattery.h @@ -5,5 +5,6 @@ @property (nonatomic, assign) BOOL isUpdating; +(KAIBattery *)sharedInstance; -(instancetype)init; +-(void)removeAllAndRefresh; -(void)updateBattery; @end \ No newline at end of file diff --git a/KAIBattery.mm b/KAIBattery.mm index dfc3241..989e588 100644 --- a/KAIBattery.mm +++ b/KAIBattery.mm @@ -1,6 +1,8 @@ #import "KAIBattery.h" KAIBattery *instance; +NSMutableArray *addedCells = [[NSMutableArray alloc] init]; + @implementation KAIBattery -(instancetype)init { @@ -19,6 +21,7 @@ long long lastPercentage; -(void)updateBattery { dispatch_async(dispatch_get_main_queue(), ^{ + NSLog(@"kai: battery platter called to update"); if(!self.isUpdating) { self.isUpdating = YES; self.number = 0; @@ -34,11 +37,21 @@ long long lastPercentage; } }*/ + for(KAIBatteryCell *cell in addedCells) { + if(![devices containsObject:cell.device]) { + cell.device = nil; + [cell removeFromSuperview]; + [self.displayingDevices removeObject:cell.label.text]; //lmaoo + } else { + [cell updateInfo]; + } + } + for (BCBatteryDevice *device in devices) { NSString *deviceName = MSHookIvar(device, "_name"); - double batteryPercentage = MSHookIvar(device, "_percentCharge"); + //double batteryPercentage = MSHookIvar(device, "_percentCharge"); BOOL charging = MSHookIvar(device, "_charging"); - BOOL LPM = MSHookIvar(device, "_batterySaverModeActive"); + //BOOL LPM = MSHookIvar(device, "_batterySaverModeActive"); BOOL shouldAdd = NO; @@ -50,8 +63,6 @@ long long lastPercentage; NSLog(@"Kai: SHOULD ADD"); } - BOOL shouldRefresh = NO; - KAIBatteryCell *cell = [KAIBatteryCell cellForDeviceIfExists:device]; /* @@ -59,43 +70,50 @@ long long lastPercentage; @property (nonatomic, assign) BOOL lastLPM; @property (nonatomic, assign) double lastPercent; */ - if(cell.lastChargingState != charging || cell.lastLPM != LPM || cell.lastPercent != batteryPercentage) { - shouldRefresh = YES; - NSLog(@"Kai: SHOULD REFRESH"); - } if(shouldAdd && [deviceName length]!=0) { - if([self.displayingDevices containsObject:device] && shouldRefresh) { - NSLog(@"Kai: Updating cell: %@ for device:%@", cell, device); - } else if(![self.displayingDevices containsObject:deviceName]) { + if(cell==nil && ![self.displayingDevices containsObject:deviceName]) { KAIBatteryCell *newCell = [[KAIBatteryCell alloc] initWithFrame:CGRectMake(0, y, self.frame.size.width, bannerHeight) device:device]; [self addSubview:newCell]; [self.displayingDevices addObject:deviceName]; + [addedCells addObject:newCell]; //y+=bannerHeight + spacing; - NSLog(@"Kai: Added cell: %@ for device:%@", cell, device); } - self.number +=1; + //self.number +=1; y+=bannerHeight + spacing; - NSLog(@"Kai: incremented y, so now it is %f", y); - [cell updateInfo]; } else if(!shouldAdd) { - if([self.displayingDevices containsObject:device]) { + if([self.displayingDevices containsObject:deviceName]) { [cell removeFromSuperview]; - [self.displayingDevices removeObject:device]; - NSLog(@"Kai: Removed cell: %@ for device: %@", cell, device); + [self.displayingDevices removeObject:deviceName]; } } } //[self.heightAnchor constraintEqualToConstant:(self.number * 85)].active = YES; self.isUpdating = NO; + self.number = [self.subviews count]; } }); } +-(void)removeAllAndRefresh { + for( UIView *view in self.subviews ) { + @try { + [view removeFromSuperview]; + } @catch (NSException *exception) { + //Panik + } + } + + self.displayingDevices = [[NSMutableArray alloc] init]; + + addedCells = nil; + [self updateBattery]; +} + +(KAIBattery *)sharedInstance { return instance; } diff --git a/KAIBatteryCell.h b/KAIBatteryCell.h index c045e0a..3248217 100644 --- a/KAIBatteryCell.h +++ b/KAIBatteryCell.h @@ -33,11 +33,6 @@ @property (nonatomic, strong) UILabel *percentLabel; @property (nonatomic, strong) UIImageView *glyphView; @property (nonatomic, strong) _UIBatteryView *battery; - -@property (nonatomic, assign) BOOL lastChargingState; -@property (nonatomic, assign) BOOL lastLPM; -@property (nonatomic, assign) double lastPercent; - -(instancetype)initWithFrame:(CGRect)arg1 device:(BCBatteryDevice *)device; -(void)updateInfo; +(instancetype)cellForDeviceIfExists:(BCBatteryDevice *)device; diff --git a/KAIBatteryCell.mm b/KAIBatteryCell.mm index bac1c08..566bca1 100644 --- a/KAIBatteryCell.mm +++ b/KAIBatteryCell.mm @@ -1,6 +1,6 @@ #import "KAIBatteryCell.h" -NSMutableArray *deviceInstances; +NSMutableArray *deviceInstances = [[NSMutableArray alloc] init]; @implementation KAIBatteryCell @@ -113,15 +113,14 @@ NSMutableArray *deviceInstances; [self.battery.widthAnchor constraintEqualToConstant:20].active = YES; [self.battery.heightAnchor constraintEqualToConstant:10].active = YES; - self.lastChargingState = charging; - self.lastLPM = LPM; - self.lastPercent = batteryPercentage; + [deviceInstances addObject:self]; } return self; } -(void)updateInfo { + NSLog(@"kai: updating cell info"); NSString *deviceName = MSHookIvar(self.device, "_name"); double batteryPercentage = MSHookIvar(self.device, "_percentCharge"); @@ -133,7 +132,7 @@ NSMutableArray *deviceInstances; self.battery.chargePercent = (batteryPercentage*0.01); if(charging) { self.battery.chargingState = 1; } else { self.battery.chargingState = 0; } self.battery.showsInlineChargingIndicator = YES; - if(LPM) self.battery.saverModeActive = YES; + if(LPM) { self.battery.saverModeActive = YES; } else { self.battery.saverModeActive = NO; } if(kCFCoreFoundationVersionNumber > 1600) { [self.battery setBodyColorAlpha:1.0]; [self.battery setPinColorAlpha:1.0]; @@ -143,9 +142,7 @@ NSMutableArray *deviceInstances; [self.glyphView setImage:[self.device glyph]]; - self.lastChargingState = charging; - self.lastLPM = LPM; - self.lastPercent = batteryPercentage; + [deviceInstances addObject:self]; } @@ -157,14 +154,17 @@ NSMutableArray *deviceInstances; +(instancetype)cellForDeviceIfExists:(BCBatteryDevice *)device { KAIBatteryCell *foundCell; + NSString *deviceName = MSHookIvar(device, "_name"); + for(KAIBatteryCell *cell in deviceInstances) { - if(cell.device == device) { + if(cell.label.text == deviceName) { foundCell = cell; break; } } return foundCell; + //return deviceInstances; } @end \ No newline at end of file diff --git a/Kai.h b/Kai.h index 59a547d..8a8165f 100644 --- a/Kai.h +++ b/Kai.h @@ -114,7 +114,7 @@ static void applyPrefs() isUpdating = YES; - [[KAIBattery sharedInstance] updateBattery]; + [[KAIBattery sharedInstance] removeAllAndRefresh]; [(CSAdjunctListView *)([KAIBattery sharedInstance].superview.superview) KaiUpdate]; isUpdating = NO; diff --git a/Kai.xm b/Kai.xm index 5fc44e2..dffeafc 100644 --- a/Kai.xm +++ b/Kai.xm @@ -6,16 +6,17 @@ -(void)_layoutStackView { - //NSInteger lastSlot = [[self stackView].subviews count] -1; + 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] != [KAIBattery 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:[KAIBattery sharedInstance]]; + [[self stackView] insertArrangedSubview:[KAIBattery sharedInstance] atIndex:lastSlot]; + } //makes kai lay itself out when the stack does + NSLog(@"kai: laying out stack view"); [self KaiUpdate]; %orig; @@ -79,6 +80,8 @@ isUpdating = YES; + NSLog(@"kai: kai info will update"); + [[KAIBattery sharedInstance] updateBattery]; [self KaiUpdate];