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.

240 lines
8.3KB

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