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.

KAIBatteryPlatter.mm 8.1KB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. [self calculateHeight];
  104. }
  105. -(void)addSubview:(UIView *)view {
  106. [super addSubview:view];
  107. self.number = [self.stack.subviews count];
  108. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  109. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  110. }
  111. if(textColor==0 && [view respondsToSelector:@selector(updateInfo)]) {
  112. KAIBatteryCell *cell = (KAIBatteryCell *)view;
  113. if(@available(iOS 12.0, *)) {
  114. if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  115. [cell.label setTextColor:[UIColor whiteColor]];
  116. [cell.percentLabel setTextColor:[UIColor whiteColor]];
  117. } else if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
  118. [cell.label setTextColor:[UIColor blackColor]];
  119. [cell.percentLabel setTextColor:[UIColor blackColor]];
  120. }
  121. }
  122. }
  123. [self setContentSize:self.stack.frame.size];
  124. }
  125. -(void)layoutSubviews {
  126. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  127. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  128. }
  129. }
  130. -(void)calculateHeight {
  131. [UIView animateWithDuration:0.3 animations:^{
  132. if(!self.heightConstraint) {
  133. self.heightConstraint = [self.heightAnchor constraintEqualToConstant:(self.number * (bannerHeight + spacing))];
  134. self.stack.heightConstraint = [self.heightAnchor constraintEqualToConstant:(self.number * (bannerHeight + spacing))];
  135. self.heightConstraint.active = YES;
  136. self.stack.heightConstraint.active = YES;
  137. [self setContentSize:self.stack.frame.size];
  138. } else {
  139. int height = (self.number * (bannerHeight + spacing));
  140. if(kaiAlign==0) {
  141. height = (self.number * (bannerHeight + spacing));
  142. } else {
  143. height = bannerHeight + spacing;
  144. }
  145. if([self.superview.subviews count]>1) {
  146. height = height - spacing;
  147. }
  148. self.heightConstraint.constant = height;
  149. self.stack.heightConstraint.constant = height;
  150. UIStackView *s = (UIStackView *)(self.superview);
  151. s.frame = CGRectMake(s.frame.origin.x, s.frame.origin.y, s.frame.size.width, (s.frame.size.height - 1));
  152. //literally does nothing but makes the stack view lay itself out (doesnt adjust frame because translatesAutoreszingMaskIntoConstraints = NO on stack views)
  153. }
  154. }];
  155. if(self.number==0) {
  156. [(UIStackView *)(self.superview) removeArrangedSubview:self];
  157. } else if(self.number!=0 && !self.superview) {
  158. [[[objc_getClass("CSAdjunctListView") class] sharedListViewForKai] addArrangedSubview:self];
  159. }
  160. }
  161. -(void)refreshForPrefs {
  162. self.stack.spacing = kaiAlign==0 ? 0 : spacingHorizontal;
  163. [self setContentSize:self.stack.frame.size];
  164. for( UIView *view in self.stack.subviews ) {
  165. @try {
  166. [view removeFromSuperview];
  167. } @catch (NSException *exception) {
  168. //Panik
  169. }
  170. }
  171. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  172. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  173. for(BCBatteryDevice *device in devices) {
  174. [device resetKaiCellForNewPrefs];
  175. }
  176. //self.spacing = spacing;
  177. [self updateBattery];
  178. }
  179. -(void)dispatchQueue {
  180. self.isUpdating = NO;
  181. if(self.queued) {
  182. [self updateBattery];
  183. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  184. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  185. }
  186. self.queued = NO;
  187. }
  188. [queueTimer invalidate];
  189. queueTimer = nil;
  190. }
  191. +(KAIBatteryPlatter *)sharedInstance {
  192. return instance;
  193. }
  194. @end