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.

127 lines
3.7KB

  1. #import "KAIBatteryStack.h"
  2. KAIBatteryStack *instance;
  3. NSTimer *queueTimer = nil;
  4. //NSMutableArray *showingCells = [[NSMutableArray alloc] init];
  5. @implementation KAIBatteryStack
  6. -(instancetype)init {
  7. self = [super init];
  8. instance = self;
  9. if (self) {
  10. self.displayingDevices = [[NSMutableArray alloc] init];
  11. self.axis = 1;
  12. self.distribution = 0;
  13. self.spacing = 0;
  14. self.alignment = 0;
  15. self.oldCountOfDevices = -100;
  16. self.queued = NO;
  17. [self updateBattery];
  18. //self.clipsToBounds = YES;
  19. self.userInteractionEnabled = NO;
  20. }
  21. return self;
  22. }
  23. long long batteryPercentage;
  24. long long lastPercentage;
  25. -(void)updateBattery {
  26. dispatch_async(dispatch_get_main_queue(), ^{
  27. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  28. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  29. if(self.oldCountOfDevices == -100) {
  30. self.oldCountOfDevices = [devices count] + 1;
  31. }
  32. self.oldCountOfDevices = [devices count];
  33. for (BCBatteryDevice *device in devices) {
  34. KAIBatteryCell *cell = [device kaiCellForDevice];
  35. [cell updateInfo];
  36. }
  37. if(!self.isUpdating && self.oldCountOfDevices != 0 && ([devices count] + 1 == self.oldCountOfDevices || [devices count] - 1 == self.oldCountOfDevices || [devices count] == self.oldCountOfDevices)) {
  38. self.isUpdating = YES;
  39. for (BCBatteryDevice *device in devices) {
  40. KAIBatteryCell *cell = [device kaiCellForDevice];
  41. BOOL charging = MSHookIvar<long long>(device, "_charging");
  42. BOOL shouldAdd = NO;
  43. if(showAll) {
  44. shouldAdd = YES;
  45. } else if(!showAll && charging) {
  46. shouldAdd = YES;
  47. }
  48. if(![self.subviews containsObject:cell] && shouldAdd && [devices containsObject:device]) {
  49. //[cell setFrame:CGRectMake(0,0,self.frame.size.width, bannerHeight)];
  50. cell.alpha = 0;
  51. [self addSubview:cell];
  52. [self addArrangedSubview:cell];
  53. [UIView animateWithDuration:0.3 animations:^{
  54. cell.alpha = 1;
  55. }];
  56. } else if([self.subviews containsObject:cell] && !shouldAdd){
  57. [UIView animateWithDuration:0.3 animations:^{
  58. cell.alpha = 0;
  59. } completion:^(BOOL finished) {
  60. [cell removeFromSuperview];
  61. [self removeArrangedSubview:cell];
  62. cell.alpha = 1;
  63. }];
  64. }
  65. }
  66. queueTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(dispatchQueue) userInfo:nil repeats:NO];
  67. } else if(self.isUpdating) {
  68. self.queued = YES;
  69. }
  70. self.number = [self.subviews count];
  71. [(CSAdjunctListView *)self.superview.superview KaiUpdate];
  72. });
  73. }
  74. -(void)refreshForPrefs {
  75. for( UIView *view in self.subviews ) {
  76. @try {
  77. [view removeFromSuperview];
  78. } @catch (NSException *exception) {
  79. //Panik
  80. }
  81. }
  82. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  83. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  84. for(BCBatteryDevice *device in devices) {
  85. [device resetKaiCellForNewPrefs];
  86. }
  87. [self updateBattery];
  88. }
  89. -(void)dispatchQueue {
  90. self.isUpdating = NO;
  91. if(self.queued) {
  92. [self updateBattery];
  93. self.queued = NO;
  94. }
  95. [queueTimer invalidate];
  96. queueTimer = nil;
  97. }
  98. +(KAIBatteryStack *)sharedInstance {
  99. return instance;
  100. }
  101. @end