Device battery indicators on your Lock Screen
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

236 lines
8.2KB

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