Device battery indicators on your Lock Screen
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

279 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. 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. if(kaiAlign==0) {
  191. self.subviewAligner.active = NO;
  192. if(bannerAlign==2) { //center
  193. self.stack.alignment = UIStackViewAlignmentLeading;
  194. self.subviewAligner = [self.stack.centerXAnchor constraintEqualToAnchor:self.stackHolder.centerXAnchor];
  195. } else if(bannerAlign==1) { //left
  196. self.stack.alignment = UIStackViewAlignmentCenter;
  197. self.subviewAligner = [self.stack.leftAnchor constraintEqualToAnchor:self.stackHolder.leftAnchor];
  198. } else if(bannerAlign==3) { //right
  199. self.stack.alignment = UIStackViewAlignmentTrailing;
  200. self.subviewAligner = [self.stack.rightAnchor constraintEqualToAnchor:self.stackHolder.rightAnchor];
  201. }
  202. self.subviewAligner.active = YES;
  203. }
  204. [self updateBattery];
  205. }
  206. -(void)dispatchQueue {
  207. self.isUpdating = NO;
  208. if(self.queued) {
  209. [self updateBattery];
  210. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  211. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  212. }
  213. self.queued = NO;
  214. }
  215. [queueTimer invalidate];
  216. queueTimer = nil;
  217. }
  218. +(KAIBatteryPlatter *)sharedInstance {
  219. return instance;
  220. }
  221. //This is for compatibility (did i spell that right?)
  222. -(void)setSizeToMimic:(CGSize)arg1 {}
  223. @end