Device battery indicators on your Lock Screen
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

192 rindas
6.3KB

  1. #import "KAIBatteryStack.h"
  2. KAIBatteryStack *instance;
  3. NSTimer *queueTimer = nil;
  4. @implementation KAIBatteryStack
  5. -(instancetype)init {
  6. self = [super init];
  7. instance = self;
  8. if (self) {
  9. self.axis = 1;
  10. self.distribution = 0;
  11. self.spacing = 0;
  12. self.alignment = 0;
  13. self.oldCountOfDevices = -100;
  14. self.queued = NO;
  15. [self updateBattery];
  16. self.userInteractionEnabled = NO;
  17. }
  18. return self;
  19. }
  20. long long batteryPercentage;
  21. long long lastPercentage;
  22. -(void)updateBattery {
  23. dispatch_async(dispatch_get_main_queue(), ^{
  24. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  25. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  26. if(self.oldCountOfDevices == -100) {
  27. self.oldCountOfDevices = [devices count] + 1;
  28. }
  29. if(!self.isUpdating && self.oldCountOfDevices != 0 && ([devices count] + 1 == self.oldCountOfDevices || [devices count] - 1 == self.oldCountOfDevices || [devices count] == self.oldCountOfDevices)) {
  30. //if(!self.isUpdating) {
  31. self.isUpdating = YES;
  32. for (BCBatteryDevice *device in devices) {
  33. KAIBatteryCell *cell = [device kaiCellForDevice];
  34. BOOL charging = MSHookIvar<long long>(device, "_charging");
  35. BOOL internal = MSHookIvar<BOOL>(device, "_internal");
  36. BOOL shouldAdd = NO;
  37. if(showAll) {
  38. shouldAdd = YES;
  39. } else if(showAllMinusInternal && !internal) {
  40. shouldAdd = YES;
  41. } else if(!showAll && charging) {
  42. shouldAdd = YES;
  43. }
  44. if(![self.subviews containsObject:cell] && shouldAdd && [devices containsObject:device]) {
  45. //[cell setFrame:CGRectMake(0,0,self.frame.size.width, bannerHeight)];
  46. cell.alpha = 0;
  47. [self addSubview:cell];
  48. [self addArrangedSubview:cell];
  49. [UIView animateWithDuration:0.3 animations:^{
  50. cell.alpha = 1;
  51. }];
  52. } else if([self.subviews containsObject:cell] && !shouldAdd){
  53. [UIView animateWithDuration:0.3 animations:^{
  54. cell.alpha = 0;
  55. } completion:^(BOOL finished) {
  56. [cell removeFromSuperview];
  57. [self removeArrangedSubview:cell];
  58. cell.alpha = 1;
  59. }];
  60. }
  61. }
  62. for(KAIBatteryCell *cell in self.subviews) {
  63. if(![devices containsObject:cell.device]) {
  64. [UIView animateWithDuration:0.3 animations:^{
  65. cell.alpha = 0;
  66. } completion:^(BOOL finished) {
  67. [cell removeFromSuperview];
  68. [self removeArrangedSubview:cell];
  69. cell.alpha = 1;
  70. }];
  71. }
  72. }
  73. queueTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(dispatchQueue) userInfo:nil repeats:NO];
  74. //self.isUpdating = NO;
  75. } else if(self.isUpdating) {
  76. self.queued = YES;
  77. }
  78. self.oldCountOfDevices = [devices count];
  79. self.number = [self.subviews count];
  80. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  81. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  82. }
  83. });
  84. }
  85. -(void)setNumber:(NSInteger)arg1 {
  86. _number = arg1;
  87. [UIView animateWithDuration:0.3 animations:^{
  88. if(!self.heightConstraint) {
  89. self.heightConstraint = [self.heightAnchor constraintEqualToConstant:(self.number * (bannerHeight + spacing))];
  90. self.heightConstraint.active = YES;
  91. } else {
  92. int height = (self.number * (bannerHeight + spacing));
  93. self.heightConstraint.constant = height;
  94. UIStackView *s = (UIStackView *)(self.superview);
  95. s.frame = CGRectMake(s.frame.origin.x, s.frame.origin.y, s.frame.size.width, (s.frame.size.height - 1));
  96. //literally does nothing but makes the stack view lay itself out (doesnt adjust frame because translatesAutoreszingMaskIntoConstraints = NO on stack views)
  97. }
  98. }];
  99. }
  100. -(void)addArrangedSubview:(UIView *)view {
  101. [super addArrangedSubview:view];
  102. self.number = [self.subviews count];
  103. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  104. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  105. }
  106. if(textColor==0) {
  107. KAIBatteryCell *cell = (KAIBatteryCell *)view;
  108. if(@available(iOS 12.0, *)) {
  109. if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  110. [cell.label setTextColor:[UIColor whiteColor]];
  111. [cell.percentLabel setTextColor:[UIColor whiteColor]];
  112. } else if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
  113. [cell.label setTextColor:[UIColor blackColor]];
  114. [cell.percentLabel setTextColor:[UIColor blackColor]];
  115. }
  116. }
  117. }
  118. }
  119. -(void)removeArrangedSubview:(UIView *)view {
  120. [super removeArrangedSubview:view];
  121. self.number = [self.subviews count];
  122. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  123. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  124. }
  125. }
  126. -(void)refreshForPrefs {
  127. for( UIView *view in self.subviews ) {
  128. @try {
  129. [view removeFromSuperview];
  130. } @catch (NSException *exception) {
  131. //Panik
  132. }
  133. }
  134. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  135. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  136. for(BCBatteryDevice *device in devices) {
  137. [device resetKaiCellForNewPrefs];
  138. }
  139. //self.spacing = spacing;
  140. [self updateBattery];
  141. }
  142. -(void)dispatchQueue {
  143. self.isUpdating = NO;
  144. if(self.queued) {
  145. [self updateBattery];
  146. if([self.superview.superview.superview respondsToSelector:@selector(fixComplicationsViewFrame)]) {
  147. [(NCNotificationListView *)(self.superview.superview.superview) fixComplicationsViewFrame];
  148. }
  149. self.queued = NO;
  150. }
  151. [queueTimer invalidate];
  152. queueTimer = nil;
  153. }
  154. +(KAIBatteryStack *)sharedInstance {
  155. return instance;
  156. }
  157. @end