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.

204 lines
7.1KB

  1. #import "KAIBatteryPlatter.h"
  2. KAIBatteryPlatter *instance;
  3. NSTimer *queueTimer = nil;
  4. @implementation KAIBatteryPlatter
  5. -(instancetype)init {
  6. self = [super init];
  7. instance = self;
  8. if (self) {
  9. self.stack = [[KAIStackView alloc] init];
  10. self.stack.axis = kaiAlign==0 ? 1 : 0;
  11. self.stack.distribution = 0;
  12. self.stack.spacing = kaiAlign==0 ? 0 : spacing;
  13. self.stack.alignment = 0;
  14. self.oldCountOfDevices = -100;
  15. self.queued = NO;
  16. [self addSubview:self.stack];
  17. [self updateBattery];
  18. }
  19. return self;
  20. }
  21. long long batteryPercentage;
  22. long long lastPercentage;
  23. -(void)updateBattery {
  24. if(!self.stack.widthAnchor) {
  25. [self.stack.widthAnchor constraintEqualToAnchor:self.widthAnchor].active = YES;
  26. }
  27. dispatch_async(dispatch_get_main_queue(), ^{
  28. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  29. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  30. if(self.oldCountOfDevices == -100) {
  31. self.oldCountOfDevices = [devices count] + 1;
  32. }
  33. if(!self.isUpdating && self.oldCountOfDevices != 0 && ([devices count] + 1 == self.oldCountOfDevices || [devices count] - 1 == self.oldCountOfDevices || [devices count] == self.oldCountOfDevices)) {
  34. //if(!self.isUpdating) {
  35. self.isUpdating = YES;
  36. for (BCBatteryDevice *device in devices) {
  37. KAIBatteryCell *cell = [device kaiCellForDevice];
  38. BOOL charging = MSHookIvar<long long>(device, "_charging");
  39. BOOL internal = MSHookIvar<BOOL>(device, "_internal");
  40. BOOL shouldAdd = NO;
  41. if(showAll) {
  42. shouldAdd = YES;
  43. } else if(showAllMinusInternal && !internal) {
  44. shouldAdd = YES;
  45. } else if(!showAll && charging) {
  46. shouldAdd = YES;
  47. }
  48. if(![self.stack.subviews containsObject:cell] && shouldAdd && [devices containsObject:device]) {
  49. //[cell setFrame:CGRectMake(0,0,self.frame.size.width, bannerHeight)];
  50. cell.alpha = 0;
  51. [self.stack addSubview:cell];
  52. [self.stack addArrangedSubview:cell];
  53. [UIView animateWithDuration:0.3 animations:^{
  54. cell.alpha = 1;
  55. }];
  56. } else if([self.stack.subviews containsObject:cell] && !shouldAdd){
  57. [UIView animateWithDuration:0.3 animations:^{
  58. cell.alpha = 0;
  59. } completion:^(BOOL finished) {
  60. [cell removeFromSuperview];
  61. [self.stack removeArrangedSubview:cell];
  62. cell.alpha = 1;
  63. }];
  64. }
  65. }
  66. for(KAIBatteryCell *cell in self.stack.subviews) {
  67. if(![devices containsObject:cell.device]) {
  68. [UIView animateWithDuration:0.3 animations:^{
  69. cell.alpha = 0;
  70. } completion:^(BOOL finished) {
  71. [cell removeFromSuperview];
  72. [self.stack removeArrangedSubview:cell];
  73. cell.alpha = 1;
  74. }];
  75. }
  76. }
  77. queueTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(dispatchQueue) userInfo:nil repeats:NO];
  78. //self.isUpdating = NO;
  79. } else if(self.isUpdating) {
  80. self.queued = YES;
  81. }
  82. self.oldCountOfDevices = [devices count];
  83. self.number = [self.stack.subviews count];
  84. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  85. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  86. }
  87. });
  88. }
  89. -(void)setNumber:(NSInteger)arg1 {
  90. _number = arg1;
  91. [UIView animateWithDuration:0.3 animations:^{
  92. if(!self.heightConstraint) {
  93. self.heightConstraint = [self.heightAnchor constraintEqualToConstant:(self.number * (bannerHeight + spacing))];
  94. self.stack.heightConstraint = [self.heightAnchor constraintEqualToConstant:(self.number * (bannerHeight + spacing))];
  95. self.heightConstraint.active = YES;
  96. self.stack.heightConstraint.active = YES;
  97. } else {
  98. int height = (self.number * (bannerHeight + spacing));
  99. if(kaiAlign==0 && [self.superview.subviews count]>1) {
  100. height = (self.number * (bannerHeight + spacing)) - spacing;
  101. } else {
  102. height = bannerHeight;
  103. }
  104. self.heightConstraint.constant = height;
  105. self.stack.heightConstraint.constant = height;
  106. UIStackView *s = (UIStackView *)(self.superview);
  107. s.frame = CGRectMake(s.frame.origin.x, s.frame.origin.y, s.frame.size.width, (s.frame.size.height - 1));
  108. //literally does nothing but makes the stack view lay itself out (doesnt adjust frame because translatesAutoreszingMaskIntoConstraints = NO on stack views)
  109. }
  110. }];
  111. }
  112. -(void)addArrangedSubview:(UIView *)view {
  113. [super addArrangedSubview:view];
  114. self.number = [self.stack.subviews count];
  115. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  116. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  117. }
  118. if(textColor==0) {
  119. KAIBatteryCell *cell = (KAIBatteryCell *)view;
  120. if(@available(iOS 12.0, *)) {
  121. if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  122. [cell.label setTextColor:[UIColor whiteColor]];
  123. [cell.percentLabel setTextColor:[UIColor whiteColor]];
  124. } else if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
  125. [cell.label setTextColor:[UIColor blackColor]];
  126. [cell.percentLabel setTextColor:[UIColor blackColor]];
  127. }
  128. }
  129. }
  130. }
  131. -(void)removeArrangedSubview:(UIView *)view {
  132. [super removeArrangedSubview:view];
  133. self.number = [self.stack.subviews count];
  134. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  135. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  136. }
  137. }
  138. -(void)refreshForPrefs {
  139. for( UIView *view in self.stack.subviews ) {
  140. @try {
  141. [view removeFromSuperview];
  142. } @catch (NSException *exception) {
  143. //Panik
  144. }
  145. }
  146. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  147. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  148. for(BCBatteryDevice *device in devices) {
  149. [device resetKaiCellForNewPrefs];
  150. }
  151. //self.spacing = spacing;
  152. [self updateBattery];
  153. }
  154. -(void)dispatchQueue {
  155. self.isUpdating = NO;
  156. if(self.queued) {
  157. [self updateBattery];
  158. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  159. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  160. }
  161. self.queued = NO;
  162. }
  163. [queueTimer invalidate];
  164. queueTimer = nil;
  165. }
  166. +(KAIBatteryPlatter *)sharedInstance {
  167. return instance;
  168. }
  169. @end