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.

209 lines
7.1KB

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