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.

234 lines
8.0KB

  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. }
  158. }
  159. -(void)refreshForPrefs {
  160. self.stack.spacing = kaiAlign==0 ? 0 : spacingHorizontal;
  161. [self setContentSize:self.stack.frame.size];
  162. for( UIView *view in self.stack.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. +(KAIBatteryPlatter *)sharedInstance {
  190. return instance;
  191. }
  192. @end