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.

118 lines
4.1KB

  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. dispatch_async(dispatch_get_main_queue(), ^{
  32. [cell removeFromSuperview];
  33. });
  34. } else if([cell respondsToSelector:@selector(updateInfo)]) {
  35. dispatch_async(dispatch_get_main_queue(), ^{
  36. [cell updateInfo];
  37. });
  38. }
  39. }
  40. for (BCBatteryDevice *device in devices) {
  41. NSString *deviceName = MSHookIvar<NSString *>(device, "_name");
  42. //double batteryPercentage = MSHookIvar<long long>(device, "_percentCharge");
  43. BOOL charging = MSHookIvar<long long>(device, "_charging");
  44. //BOOL LPM = MSHookIvar<BOOL>(device, "_batterySaverModeActive");
  45. BOOL shouldAdd = NO;
  46. if(showAll) {
  47. shouldAdd = YES;
  48. //NSLog(@"Kai: SHOULD ADD");
  49. } else if(!showAll && charging) {
  50. shouldAdd = YES;
  51. //NSLog(@"Kai: SHOULD ADD");
  52. }
  53. KAIBatteryCell *cell = [KAIBatteryCell cellForDeviceIfExists:device frameToCreateNew:CGRectMake(0, y, self.frame.size.width, bannerHeight)];
  54. if(cell) {
  55. cell.device = device;
  56. cell.frame = cell.frame = CGRectMake(0, y, self.frame.size.width, bannerHeight); //bro im like creating my own stack view
  57. //[cell updateInfo];
  58. }
  59. if(shouldAdd && [deviceName length]!=0) {
  60. if(![self.subviews containsObject:cell]) {
  61. cell.frame = CGRectMake(0, y, self.frame.size.width, bannerHeight);
  62. dispatch_async(dispatch_get_main_queue(), ^{
  63. [self addSubview:cell];
  64. });
  65. }
  66. y+=bannerHeight + spacing;
  67. } else if(!shouldAdd) {
  68. dispatch_async(dispatch_get_main_queue(), ^{
  69. [cell removeFromSuperview];
  70. });
  71. }
  72. }
  73. //[self.heightAnchor constraintEqualToConstant:(self.number * 85)].active = YES;
  74. self.number = [self.subviews count];
  75. //[(CSAdjunctListView *)self.superview.superview KaiUpdate];
  76. }
  77. self.isUpdating = NO;
  78. //NSLog(@"kai: finished update");
  79. [(CSAdjunctListView *)self.superview.superview KaiUpdate];
  80. }
  81. });
  82. }
  83. -(void)removeAllAndRefresh {
  84. for( UIView *view in self.subviews ) {
  85. @try {
  86. [view removeFromSuperview];
  87. } @catch (NSException *exception) {
  88. //Panik
  89. }
  90. }
  91. [KAIBatteryCell resetArray];
  92. //self.displayingDevices = [[NSMutableArray alloc] init];
  93. //addedCells = nil;
  94. [self updateBattery];
  95. }
  96. +(KAIBattery *)sharedInstance {
  97. return instance;
  98. }
  99. @end