Device battery indicators on your Lock Screen
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

KAIBatteryPlatter.mm 7.5KB

4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 calculateHeight];
  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)layoutSubviews {
  101. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  102. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  103. }
  104. }
  105. -(void)calculateHeight {
  106. self.number = [self.stack.subviews count];
  107. if(self.number==0) {
  108. UIStackView *s = (UIStackView *)(self.superview);
  109. s.frame = CGRectMake(s.frame.origin.x, s.frame.origin.y, s.frame.size.width, (s.frame.size.height - 1));
  110. [s removeArrangedSubview:self];
  111. [self removeFromSuperview];
  112. } else if(self.number!=0 && self.superview == nil) {
  113. [[[[objc_getClass("CSAdjunctListView") class] sharedListViewForKai] stackView] addArrangedSubview:self];
  114. //[self performSelector:@selector(calculateHeight) withObject:self afterDelay:0.1];
  115. }
  116. [UIView animateWithDuration:0.3 animations:^{
  117. if(!self.heightConstraint) {
  118. int height = (self.number * (bannerHeight + spacing));
  119. if(kaiAlign!=0) {
  120. height = bannerHeight + spacing;
  121. }
  122. if([self.superview.subviews count]>1) {
  123. height = (height - spacing) + 1;
  124. }
  125. self.heightConstraint = [self.heightAnchor constraintEqualToConstant:height];
  126. self.stack.heightConstraint = [self.heightAnchor constraintEqualToConstant:height];
  127. self.heightConstraint.active = YES;
  128. self.stack.heightConstraint.active = YES;
  129. [self setContentSize:self.stack.frame.size];
  130. } else {
  131. int height = (self.number * (bannerHeight + spacing));
  132. if(kaiAlign!=0) {
  133. height = bannerHeight + spacing;
  134. }
  135. if([self.superview.subviews count]>1) {
  136. height = (height - spacing) + 1;
  137. }
  138. self.heightConstraint.constant = height;
  139. self.stack.heightConstraint.constant = height;
  140. UIStackView *s = (UIStackView *)(self.superview);
  141. s.frame = CGRectMake(s.frame.origin.x, s.frame.origin.y, s.frame.size.width, (s.frame.size.height - 1));
  142. //literally does nothing but makes the stack view lay itself out (doesnt adjust frame because translatesAutoreszingMaskIntoConstraints = NO on stack views)
  143. }
  144. [self setContentSize:self.stack.frame.size];
  145. }];
  146. }
  147. -(void)refreshForPrefs {
  148. self.stack.spacing = kaiAlign==0 ? 0 : spacingHorizontal;
  149. [self setContentSize:self.stack.frame.size];
  150. for( UIView *view in self.stack.subviews ) {
  151. @try {
  152. [view removeFromSuperview];
  153. } @catch (NSException *exception) {
  154. //Panik
  155. }
  156. }
  157. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  158. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  159. for(BCBatteryDevice *device in devices) {
  160. [device resetKaiCellForNewPrefs];
  161. }
  162. //self.spacing = spacing;
  163. [self updateBattery];
  164. }
  165. -(void)dispatchQueue {
  166. self.isUpdating = NO;
  167. if(self.queued) {
  168. [self updateBattery];
  169. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  170. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  171. }
  172. self.queued = NO;
  173. }
  174. [queueTimer invalidate];
  175. queueTimer = nil;
  176. }
  177. +(KAIBatteryPlatter *)sharedInstance {
  178. return instance;
  179. }
  180. @end