Device battery indicators on your Lock Screen
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

261 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 updateBattery];
  36. }
  37. return self;
  38. }
  39. long long batteryPercentage;
  40. long long lastPercentage;
  41. -(void)updateBattery {
  42. if(!self.stack.widthAnchor) {
  43. [self.stack.widthAnchor constraintEqualToAnchor:self.widthAnchor].active = YES;
  44. [self.stack.heightAnchor constraintEqualToAnchor:self.heightAnchor].active = YES;
  45. [self.stack.widthAnchor constraintEqualToAnchor:self.widthAnchor].active = YES;
  46. [self.stack.centerYAnchor constraintEqualToAnchor:self.centerYAnchor].active = YES;
  47. }
  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. self.stackHolder.frame = self.frame;
  174. }
  175. -(void)refreshForPrefs {
  176. self.stack.spacing = kaiAlign==0 ? 0 : spacingHorizontal;
  177. [self setContentSize:self.stack.frame.size];
  178. for( UIView *view in self.stack.subviews ) {
  179. @try {
  180. [view removeFromSuperview];
  181. } @catch (NSException *exception) {
  182. //Panik
  183. }
  184. }
  185. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  186. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  187. for(BCBatteryDevice *device in devices) {
  188. [device resetKaiCellForNewPrefs];
  189. }
  190. [self updateBattery];
  191. }
  192. -(void)dispatchQueue {
  193. self.isUpdating = NO;
  194. if(self.queued) {
  195. [self updateBattery];
  196. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  197. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  198. }
  199. self.queued = NO;
  200. }
  201. [queueTimer invalidate];
  202. queueTimer = nil;
  203. }
  204. +(KAIBatteryPlatter *)sharedInstance {
  205. return instance;
  206. }
  207. //This is for compatibility (did i spell that right?)
  208. -(void)setSizeToMimic:(CGSize)arg1 {}
  209. @end