Device battery indicators on your Lock Screen
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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