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.

120 lines
3.7KB

  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 = 0;
  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. NSLog(@"kai: devices are %@", devices);
  34. for(KAIBatteryCell *cell in self.subviews) {
  35. //BCBatteryDevice *device = cell.device;
  36. [cell updateInfo];
  37. }
  38. for (BCBatteryDevice *device in devices) {
  39. KAIBatteryCell *cell = [device kaiCellForDevice];
  40. [cell updateInfo];
  41. BOOL shouldAdd = NO;
  42. if(showAll) {
  43. shouldAdd = YES;
  44. } else if(!showAll && device.charging) {
  45. shouldAdd = YES;
  46. }
  47. if(![self.subviews containsObject:cell] && shouldAdd && [devices containsObject:device]) {
  48. [cell setFrame:CGRectMake(0,0,self.frame.size.width, bannerHeight + spacing)];
  49. [self addArrangedSubview:cell];
  50. } else {
  51. [self removeArrangedSubview:cell];
  52. }
  53. if(!cell.height) {
  54. cell.height.active = NO;
  55. cell.height = [cell.heightAnchor constraintEqualToConstant:(bannerHeight + spacing)];
  56. cell.height.active = YES;
  57. } else {
  58. int height = (bannerHeight + spacing);
  59. cell.height.constant = height;
  60. UIStackView *s = self;
  61. s.frame = CGRectMake(s.frame.origin.x, s.frame.origin.y, s.frame.size.width, (s.frame.size.height - 1));
  62. }
  63. if(!cell.width) {
  64. cell.width.active = NO;
  65. cell.width = [cell.widthAnchor constraintEqualToConstant:(self.frame.size.width)];
  66. cell.width.active = YES;
  67. } else {
  68. int width = self.frame.size.width;
  69. cell.width.constant = width;
  70. UIStackView *s = self;
  71. s.frame = CGRectMake(s.frame.origin.x, s.frame.origin.y, s.frame.size.width, (s.frame.size.height - 1));
  72. }
  73. }
  74. self.number = [self.subviews count];
  75. }
  76. self.isUpdating = NO;
  77. //NSLog(@"kai: finished update");
  78. //[(CSAdjunctListView *)self.superview.superview KaiUpdate];
  79. [(CSAdjunctListView *)self.superview.superview performSelector:@selector(KaiUpdate) withObject:(CSAdjunctListView *)self.superview.superview afterDelay:0.2];
  80. });
  81. self.number = [self.subviews count];
  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 updateBattery];
  93. }
  94. +(KAIBatteryStack *)sharedInstance {
  95. return instance;
  96. }
  97. @end