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.

206 lines
6.5KB

  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 shouldAdd = NO;
  44. if(showAll) {
  45. shouldAdd = YES;
  46. } else if(!showAll && charging) {
  47. shouldAdd = YES;
  48. }
  49. if(![self.subviews containsObject:cell] && shouldAdd && [devices containsObject:device]) {
  50. //[cell setFrame:CGRectMake(0,0,self.frame.size.width, bannerHeight)];
  51. cell.alpha = 0;
  52. [self addSubview:cell];
  53. [self addArrangedSubview:cell];
  54. [UIView animateWithDuration:0.3 animations:^{
  55. cell.alpha = 1;
  56. }];
  57. } else if([self.subviews containsObject:cell] && !shouldAdd){
  58. [UIView animateWithDuration:0.3 animations:^{
  59. cell.alpha = 0;
  60. } completion:^(BOOL finished) {
  61. [cell removeFromSuperview];
  62. [self removeArrangedSubview:cell];
  63. cell.alpha = 1;
  64. }];
  65. }
  66. }
  67. for(KAIBatteryCell *cell in self.subviews) {
  68. if(![devices containsObject:cell.device]) {
  69. [UIView animateWithDuration:0.3 animations:^{
  70. cell.alpha = 0;
  71. } completion:^(BOOL finished) {
  72. [cell removeFromSuperview];
  73. [self removeArrangedSubview:cell];
  74. cell.alpha = 1;
  75. }];
  76. }
  77. }
  78. //queueTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(dispatchQueue) userInfo:nil repeats:NO];
  79. self.isUpdating = NO;
  80. } else if(self.isUpdating) {
  81. self.queued = YES;
  82. }
  83. });
  84. }
  85. -(void)addArrangedSubview:(UIView *)view {
  86. [super addArrangedSubview:view];
  87. self.number = [self.subviews count];
  88. [UIView animateWithDuration:0.3 animations:^{
  89. if(!self.heightConstraint) {
  90. self.heightConstraint.active = NO;
  91. self.heightConstraint = [self.heightAnchor constraintEqualToConstant:85];
  92. //set an initial constraint
  93. self.heightConstraint.active = YES;
  94. } else {
  95. int height = (self.number * (bannerHeight + spacing)); //big brain math
  96. //self.heightConstraint.active = NO; //deactivation
  97. self.heightConstraint.constant = height;
  98. //self.heightConstraint.active = YES; //forcing reactivation
  99. UIStackView *s = (UIStackView *)(self.superview);
  100. s.frame = CGRectMake(s.frame.origin.x, s.frame.origin.y, s.frame.size.width, (s.frame.size.height - 1));
  101. //literally does nothing but makes the stack view lay itself out (doesnt adjust frame because translatesAutoreszingMaskIntoConstraints = NO on stack views)
  102. }
  103. }];
  104. if(textColor==0) {
  105. KAIBatteryCell *cell = (KAIBatteryCell *)view;
  106. if(@available(iOS 12.0, *)) {
  107. if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  108. [cell.label setTextColor:[UIColor whiteColor]];
  109. [cell.percentLabel setTextColor:[UIColor whiteColor]];
  110. } else if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
  111. [cell.label setTextColor:[UIColor blackColor]];
  112. [cell.percentLabel setTextColor:[UIColor blackColor]];
  113. }
  114. }
  115. }
  116. }
  117. -(void)removeArrangedSubview:(UIView *)view {
  118. [super removeArrangedSubview:view];
  119. self.number = [self.subviews count];
  120. [UIView animateWithDuration:0.3 animations:^{
  121. if(!self.heightConstraint) {
  122. self.heightConstraint.active = NO;
  123. self.heightConstraint = [self.heightAnchor constraintEqualToConstant:85];
  124. //set an initial constraint
  125. self.heightConstraint.active = YES;
  126. } else {
  127. int height = (self.number * (bannerHeight + spacing)); //big brain math
  128. //self.heightConstraint.active = NO; //deactivation
  129. self.heightConstraint.constant = height;
  130. //self.heightConstraint.active = YES; //forcing reactivation
  131. UIStackView *s = (UIStackView *)(self.superview);
  132. s.frame = CGRectMake(s.frame.origin.x, s.frame.origin.y, s.frame.size.width, (s.frame.size.height - 1));
  133. //literally does nothing but makes the stack view lay itself out (doesnt adjust frame because translatesAutoreszingMaskIntoConstraints = NO on stack views)
  134. }
  135. }];
  136. }
  137. -(void)refreshForPrefs {
  138. for( UIView *view in self.subviews ) {
  139. @try {
  140. [view removeFromSuperview];
  141. } @catch (NSException *exception) {
  142. //Panik
  143. }
  144. }
  145. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  146. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  147. for(BCBatteryDevice *device in devices) {
  148. [device resetKaiCellForNewPrefs];
  149. }
  150. [self updateBattery];
  151. }
  152. -(void)dispatchQueue {
  153. self.isUpdating = NO;
  154. if(self.queued) {
  155. [self updateBattery];
  156. self.queued = NO;
  157. }
  158. [queueTimer invalidate];
  159. queueTimer = nil;
  160. }
  161. +(KAIBatteryStack *)sharedInstance {
  162. return instance;
  163. }
  164. @end