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.

230 lines
7.9KB

  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. if(kaiAlign==0) {
  131. [self.stack.widthAnchor constraintEqualToAnchor:self.widthAnchor].active = YES;
  132. } else {
  133. self.widthConstraint = [self.widthAnchor constraintEqualToConstant:(self.number * (self.frame.size.width + bannerWidthFactor))];
  134. self.widthConstraint.active = YES;
  135. }
  136. } else {
  137. int height = (self.number * (bannerHeight + spacing));
  138. if(kaiAlign!=0) {
  139. height = bannerHeight + spacing;
  140. self.widthConstraint.constant = (self.number * (self.frame.size.width + bannerWidthFactor));
  141. }
  142. if([self.superview.subviews count]>1) {
  143. height = (height - spacing) + 1;
  144. }
  145. self.heightConstraint.constant = height;
  146. self.stack.heightConstraint.constant = height;
  147. UIStackView *s = (UIStackView *)(self.superview);
  148. s.frame = CGRectMake(s.frame.origin.x, s.frame.origin.y, s.frame.size.width, (s.frame.size.height - 1));
  149. //literally does nothing but makes the stack view lay itself out (doesnt adjust frame because translatesAutoreszingMaskIntoConstraints = NO on stack views)
  150. }
  151. [self setContentSize:self.stack.frame.size];
  152. }];
  153. }
  154. -(void)refreshForPrefs {
  155. self.stack.spacing = kaiAlign==0 ? 0 : spacingHorizontal;
  156. [self setContentSize:self.stack.frame.size];
  157. for( UIView *view in self.stack.subviews ) {
  158. @try {
  159. [view removeFromSuperview];
  160. } @catch (NSException *exception) {
  161. //Panik
  162. }
  163. }
  164. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  165. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  166. for(BCBatteryDevice *device in devices) {
  167. [device resetKaiCellForNewPrefs];
  168. }
  169. //self.spacing = spacing;
  170. [self updateBattery];
  171. }
  172. -(void)dispatchQueue {
  173. self.isUpdating = NO;
  174. if(self.queued) {
  175. [self updateBattery];
  176. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  177. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  178. }
  179. self.queued = NO;
  180. }
  181. [queueTimer invalidate];
  182. queueTimer = nil;
  183. }
  184. +(KAIBatteryPlatter *)sharedInstance {
  185. return instance;
  186. }
  187. @end