Device battery indicators on your Lock Screen
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.6KB

  1. #import "KAIBattery.h"
  2. KAIBattery *instance;
  3. //NSMutableArray *showingCells = [[NSMutableArray alloc] init];
  4. @implementation KAIBattery
  5. -(instancetype)init {
  6. self = [super init];
  7. instance = self;
  8. if (self) {
  9. self.displayingDevices = [[NSMutableArray alloc] init];
  10. [self updateBattery];
  11. self.userInteractionEnabled = NO;
  12. }
  13. return self;
  14. }
  15. long long batteryPercentage;
  16. long long lastPercentage;
  17. -(void)updateBattery {
  18. dispatch_async(dispatch_get_main_queue(), ^{
  19. //NSLog(@"kai: battery platter called to update");
  20. if(!self.isUpdating) {
  21. NSLog(@"kai: IS Updating");
  22. self.isUpdating = YES;
  23. //self.number = 0;
  24. float y = 0;
  25. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  26. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  27. if([devices count]!=0) {
  28. NSLog(@"kai: info is good, will proceed");
  29. for(KAIBatteryCell *cell in self.subviews) {
  30. if([cell respondsToSelector:@selector(updateInfo)] && ![devices containsObject:cell.device]) { //to confirm is a cell and battery device does not exist
  31. [cell removeFromSuperview];
  32. } else if([cell respondsToSelector:@selector(updateInfo)]) {
  33. [cell updateInfo];
  34. }
  35. }
  36. for (BCBatteryDevice *device in devices) {
  37. NSString *deviceName = MSHookIvar<NSString *>(device, "_name");
  38. //double batteryPercentage = MSHookIvar<long long>(device, "_percentCharge");
  39. BOOL charging = MSHookIvar<long long>(device, "_charging");
  40. //BOOL LPM = MSHookIvar<BOOL>(device, "_batterySaverModeActive");
  41. BOOL shouldAdd = NO;
  42. if(showAll) {
  43. shouldAdd = YES;
  44. //NSLog(@"Kai: SHOULD ADD");
  45. } else if(!showAll && charging) {
  46. shouldAdd = YES;
  47. //NSLog(@"Kai: SHOULD ADD");
  48. }
  49. KAIBatteryCell *cell = [KAIBatteryCell cellForDeviceIfExists:device frameToCreateNew:CGRectMake(0, y, self.frame.size.width, bannerHeight)];
  50. if(cell) {
  51. cell.device = device;
  52. cell.frame = cell.frame = CGRectMake(0, y, self.frame.size.width, bannerHeight); //bro im like creating my own stack view
  53. //[cell updateInfo];
  54. }
  55. if(shouldAdd && [deviceName length]!=0) {
  56. if(![self.subviews containsObject:cell]) {
  57. cell.frame = CGRectMake(0, y, self.frame.size.width, bannerHeight);
  58. [self addSubview:cell];
  59. }
  60. y+=bannerHeight + spacing;
  61. } else if(!shouldAdd) {
  62. [cell removeFromSuperview];
  63. }
  64. }
  65. //[self.heightAnchor constraintEqualToConstant:(self.number * 85)].active = YES;
  66. self.number = [self.subviews count];
  67. [(CSAdjunctListView *)self.superview.superview KaiUpdate];
  68. }
  69. self.isUpdating = NO;
  70. NSLog(@"kai: finished update");
  71. }
  72. });
  73. }
  74. -(void)removeAllAndRefresh {
  75. for( UIView *view in self.subviews ) {
  76. @try {
  77. [view removeFromSuperview];
  78. } @catch (NSException *exception) {
  79. //Panik
  80. }
  81. }
  82. [KAIBatteryCell resetArray];
  83. //self.displayingDevices = [[NSMutableArray alloc] init];
  84. //addedCells = nil;
  85. [self updateBattery];
  86. }
  87. +(KAIBattery *)sharedInstance {
  88. return instance;
  89. }
  90. @end