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.

226 lines
7.8KB

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