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.

202 lines
7.0KB

  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 = 1;
  11. self.stack.distribution = 0;
  12. self.stack.spacing = 0;
  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([self.superview.subviews count]>1) {
  100. height = (self.number * (bannerHeight + spacing)) - spacing;
  101. }
  102. self.heightConstraint.constant = height;
  103. self.stack.heightConstraint.constant = height;
  104. UIStackView *s = (UIStackView *)(self.superview);
  105. s.frame = CGRectMake(s.frame.origin.x, s.frame.origin.y, s.frame.size.width, (s.frame.size.height - 1));
  106. //literally does nothing but makes the stack view lay itself out (doesnt adjust frame because translatesAutoreszingMaskIntoConstraints = NO on stack views)
  107. }
  108. }];
  109. }
  110. -(void)addArrangedSubview:(UIView *)view {
  111. [super addArrangedSubview:view];
  112. self.number = [self.stack.subviews count];
  113. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  114. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  115. }
  116. if(textColor==0) {
  117. KAIBatteryCell *cell = (KAIBatteryCell *)view;
  118. if(@available(iOS 12.0, *)) {
  119. if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  120. [cell.label setTextColor:[UIColor whiteColor]];
  121. [cell.percentLabel setTextColor:[UIColor whiteColor]];
  122. } else if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
  123. [cell.label setTextColor:[UIColor blackColor]];
  124. [cell.percentLabel setTextColor:[UIColor blackColor]];
  125. }
  126. }
  127. }
  128. }
  129. -(void)removeArrangedSubview:(UIView *)view {
  130. [super removeArrangedSubview:view];
  131. self.number = [self.stack.subviews count];
  132. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  133. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  134. }
  135. }
  136. -(void)refreshForPrefs {
  137. for( UIView *view in self.stack.subviews ) {
  138. @try {
  139. [view removeFromSuperview];
  140. } @catch (NSException *exception) {
  141. //Panik
  142. }
  143. }
  144. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  145. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  146. for(BCBatteryDevice *device in devices) {
  147. [device resetKaiCellForNewPrefs];
  148. }
  149. //self.spacing = spacing;
  150. [self updateBattery];
  151. }
  152. -(void)dispatchQueue {
  153. self.isUpdating = NO;
  154. if(self.queued) {
  155. [self updateBattery];
  156. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  157. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  158. }
  159. self.queued = NO;
  160. }
  161. [queueTimer invalidate];
  162. queueTimer = nil;
  163. }
  164. +(KAIBatteryPlatter *)sharedInstance {
  165. return instance;
  166. }
  167. @end