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.

277 lines
10KB

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