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.

133 lines
4.9KB

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