Device battery indicators on your Lock Screen
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

232 lines
7.7KB

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