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.

269 lines
9.8KB

  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. [self setMinimumZoomScale:1];
  17. [self setMaximumZoomScale:1];
  18. [self addSubview:self.stackHolder];
  19. [self.stackHolder addSubview:self.stack];
  20. [self setContentSize:self.stack.frame.size];
  21. [self setContentOffset:CGPointMake(0,0)];
  22. //Keeping this link here to leak...
  23. //https://cdn.discordapp.com/attachments/683698397634756646/718122118990266518/unknown.png
  24. self.stackHolder.translatesAutoresizingMaskIntoConstraints = NO;
  25. if(bannerAlign==2) { //center
  26. self.subviewAligner = [self.stackHolder.centerXAnchor constraintEqualToAnchor:self.centerXAnchor];
  27. } else if(bannerAlign==1) { //left
  28. self.subviewAligner = [self.stackHolder.leftAnchor constraintEqualToAnchor:self.leftAnchor];
  29. } else if(bannerAlign==3) { //right
  30. self.subviewAligner = [self.stackHolder.rightAnchor constraintEqualToAnchor:self.rightAnchor];
  31. }
  32. [self.stackHolder.heightAnchor constraintEqualToAnchor:self.heightAnchor].active = YES;
  33. [self.stackHolder.widthAnchor constraintEqualToAnchor:self.stack.widthAnchor].active = YES;
  34. [self.stackHolder.centerYAnchor constraintEqualToAnchor:self.centerYAnchor].active = YES;
  35. self.subviewAligner.active = YES;
  36. [self updateBattery];
  37. }
  38. return self;
  39. }
  40. long long batteryPercentage;
  41. long long lastPercentage;
  42. -(void)updateBattery {
  43. if(!self.stack.widthAnchor) {
  44. [self.stack.widthAnchor constraintEqualToAnchor:self.widthAnchor].active = YES;
  45. }
  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.subviewAligner.active = NO;
  189. [self.stackHolder removeConstraint:self.subviewAligner];
  190. if(bannerAlign==2) { //center
  191. self.subviewAligner = [self.stackHolder.centerXAnchor constraintEqualToAnchor:self.centerXAnchor];
  192. } else if(bannerAlign==1) { //left
  193. self.subviewAligner = [self.stackHolder.leftAnchor constraintEqualToAnchor:self.leftAnchor];
  194. } else if(bannerAlign==3) { //right
  195. self.subviewAligner = [self.stackHolder.rightAnchor constraintEqualToAnchor:self.rightAnchor];
  196. }
  197. self.subviewAligner.active = YES;
  198. [self updateBattery];
  199. }
  200. -(void)dispatchQueue {
  201. self.isUpdating = NO;
  202. if(self.queued) {
  203. [self updateBattery];
  204. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  205. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  206. }
  207. self.queued = NO;
  208. }
  209. [queueTimer invalidate];
  210. queueTimer = nil;
  211. }
  212. +(KAIBatteryPlatter *)sharedInstance {
  213. return instance;
  214. }
  215. @end