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.

218 lines
7.6KB

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