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.

KAIBatteryStack.mm 6.5KB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #import "KAIBatteryStack.h"
  2. KAIBatteryStack *instance;
  3. NSTimer *queueTimer = nil;
  4. @implementation KAIBatteryStack
  5. -(instancetype)init {
  6. self = [super init];
  7. instance = self;
  8. if (self) {
  9. self.axis = 1;
  10. self.distribution = 0;
  11. self.spacing = 0;
  12. self.alignment = 0;
  13. self.oldCountOfDevices = -100;
  14. self.queued = NO;
  15. [self updateBattery];
  16. self.userInteractionEnabled = NO;
  17. }
  18. return self;
  19. }
  20. long long batteryPercentage;
  21. long long lastPercentage;
  22. -(void)updateBattery {
  23. dispatch_async(dispatch_get_main_queue(), ^{
  24. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  25. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  26. if(self.oldCountOfDevices == -100) {
  27. self.oldCountOfDevices = [devices count] + 1;
  28. }
  29. if(!self.isUpdating && self.oldCountOfDevices != 0 && ([devices count] + 1 == self.oldCountOfDevices || [devices count] - 1 == self.oldCountOfDevices || [devices count] == self.oldCountOfDevices)) {
  30. //if(!self.isUpdating) {
  31. self.isUpdating = YES;
  32. for (BCBatteryDevice *device in devices) {
  33. KAIBatteryCell *cell = [device kaiCellForDevice];
  34. BOOL charging = MSHookIvar<long long>(device, "_charging");
  35. BOOL internal = MSHookIvar<BOOL>(device, "_internal");
  36. BOOL shouldAdd = NO;
  37. if(showAll) {
  38. shouldAdd = YES;
  39. } else if(showAllMinusInternal && !internal) {
  40. shouldAdd = YES;
  41. } else if(!showAll && charging) {
  42. shouldAdd = YES;
  43. }
  44. if(![self.subviews containsObject:cell] && shouldAdd && [devices containsObject:device]) {
  45. //[cell setFrame:CGRectMake(0,0,self.frame.size.width, bannerHeight)];
  46. cell.alpha = 0;
  47. [self addSubview:cell];
  48. [self addArrangedSubview:cell];
  49. [UIView animateWithDuration:0.3 animations:^{
  50. cell.alpha = 1;
  51. }];
  52. } else if([self.subviews containsObject:cell] && !shouldAdd){
  53. [UIView animateWithDuration:0.3 animations:^{
  54. cell.alpha = 0;
  55. } completion:^(BOOL finished) {
  56. [cell removeFromSuperview];
  57. [self removeArrangedSubview:cell];
  58. cell.alpha = 1;
  59. }];
  60. }
  61. }
  62. for(KAIBatteryCell *cell in self.subviews) {
  63. if(![devices containsObject:cell.device]) {
  64. [UIView animateWithDuration:0.3 animations:^{
  65. cell.alpha = 0;
  66. } completion:^(BOOL finished) {
  67. [cell removeFromSuperview];
  68. [self removeArrangedSubview:cell];
  69. cell.alpha = 1;
  70. }];
  71. }
  72. }
  73. queueTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(dispatchQueue) userInfo:nil repeats:NO];
  74. //self.isUpdating = NO;
  75. } else if(self.isUpdating) {
  76. self.queued = YES;
  77. }
  78. self.oldCountOfDevices = [devices count];
  79. self.number = [self.subviews count];
  80. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  81. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  82. }
  83. });
  84. }
  85. -(void)setNumber:(NSInteger)arg1 {
  86. _number = arg1;
  87. [UIView animateWithDuration:0.3 animations:^{
  88. if(!self.heightConstraint) {
  89. self.heightConstraint = [self.heightAnchor constraintEqualToConstant:(self.number * (bannerHeight + spacing))];
  90. self.heightConstraint.active = YES;
  91. } else {
  92. int height = (self.number * (bannerHeight + spacing));
  93. //if([self.superview.subviews count]>1) {
  94. // height = (self.number * (bannerHeight + spacing)) - spacing;
  95. //}
  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. if(textColor==0) {
  110. KAIBatteryCell *cell = (KAIBatteryCell *)view;
  111. if(@available(iOS 12.0, *)) {
  112. if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  113. [cell.label setTextColor:[UIColor whiteColor]];
  114. [cell.percentLabel setTextColor:[UIColor whiteColor]];
  115. } else if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
  116. [cell.label setTextColor:[UIColor blackColor]];
  117. [cell.percentLabel setTextColor:[UIColor blackColor]];
  118. }
  119. }
  120. }
  121. }
  122. -(void)removeArrangedSubview:(UIView *)view {
  123. [super removeArrangedSubview:view];
  124. self.number = [self.subviews count];
  125. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  126. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  127. }
  128. }
  129. -(void)refreshForPrefs {
  130. for( UIView *view in self.subviews ) {
  131. @try {
  132. [view removeFromSuperview];
  133. } @catch (NSException *exception) {
  134. //Panik
  135. }
  136. }
  137. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  138. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  139. for(BCBatteryDevice *device in devices) {
  140. [device resetKaiCellForNewPrefs];
  141. }
  142. //self.spacing = spacing;
  143. [self updateBattery];
  144. }
  145. -(void)dispatchQueue {
  146. self.isUpdating = NO;
  147. if(self.queued) {
  148. [self updateBattery];
  149. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  150. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  151. }
  152. self.queued = NO;
  153. }
  154. [queueTimer invalidate];
  155. queueTimer = nil;
  156. }
  157. +(KAIBatteryStack *)sharedInstance {
  158. return instance;
  159. }
  160. @end