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.

138 lines
5.1KB

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