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.

260 lines
9.4KB

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