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.

94 lines
3.1KB

  1. #import "KAIBattery.h"
  2. KAIBattery *instance;
  3. @implementation KAIBattery
  4. -(instancetype)init {
  5. self = [super init];
  6. instance = self;
  7. if (self) {
  8. [self updateBattery];
  9. self.userInteractionEnabled = NO;
  10. }
  11. return self;
  12. }
  13. long long batteryPercentage;
  14. long long lastPercentage;
  15. -(void)updateBattery {
  16. dispatch_async(dispatch_get_main_queue(), ^{
  17. if(!self.isUpdating) {
  18. self.isUpdating = YES;
  19. self.number = 0;
  20. float y = 0;
  21. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  22. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  23. for( UIView *view in self.subviews ) {
  24. @try {
  25. [view removeFromSuperview];
  26. } @catch (NSException *exception) {
  27. //Panik
  28. }
  29. }
  30. for (BCBatteryDevice *device in devices) {
  31. //NSString *deviceName = MSHookIvar<NSString *>(device, "_name");
  32. double batteryPercentage = MSHookIvar<long long>(device, "_percentCharge");
  33. BOOL charging = MSHookIvar<long long>(device, "_charging");
  34. BOOL LPM = MSHookIvar<BOOL>(device, "_batterySaverModeActive");
  35. BOOL shouldAdd = NO;
  36. if(showAll) {
  37. shouldAdd = YES;
  38. } else if(!showAll && charging) {
  39. shouldAdd = YES;
  40. }
  41. BOOL shouldRefresh = NO;
  42. KAIBatteryCell *cell = [KAIBatteryCell cellForDeviceIfExists:device];
  43. /*
  44. @property (nonatomic, assign) BOOL lastChargingState;
  45. @property (nonatomic, assign) BOOL lastLPM;
  46. @property (nonatomic, assign) double lastPercent;
  47. */
  48. if(cell.lastChargingState != charging || cell.lastLPM != LPM || cell.lastPercent != batteryPercentage) {
  49. shouldRefresh = YES;
  50. }
  51. if(shouldAdd) {
  52. if([self.displayingDevices containsObject:device] && shouldRefresh) {
  53. [cell updateInfo];
  54. } else if(![self.displayingDevices containsObject:device]) {
  55. KAIBatteryCell *newCell = [[KAIBatteryCell alloc] initWithFrame:CGRectMake(0, y, self.frame.size.width, self.frame.size.width)];
  56. [self addSubview:newCell];
  57. [self.displayingDevices addObject:device];
  58. y+=bannerHeight + spacing;
  59. self.number +=1;
  60. }
  61. } else if(!shouldAdd) {
  62. if([self.displayingDevices containsObject:device]) {
  63. [cell removeFromSuperview];
  64. [self.displayingDevices removeObject:device];
  65. }
  66. }
  67. }
  68. //[self.heightAnchor constraintEqualToConstant:(self.number * 85)].active = YES;
  69. self.isUpdating = NO;
  70. }
  71. });
  72. }
  73. +(KAIBattery *)sharedInstance {
  74. return instance;
  75. }
  76. @end