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.

232 lines
9.0KB

  1. #import "KAIClassHeaders.h"
  2. @implementation KAIBatteryCell
  3. - (instancetype)initWithFrame:(CGRect)arg1 device:(BCBatteryDevice *)device {
  4. self = [super initWithFrame:arg1];
  5. if (self && device != nil) {
  6. self.device = device;
  7. NSString *deviceName = device.name;
  8. double batteryPercentage = device.percentCharge;
  9. BOOL charging = [device isCharging];
  10. BOOL LPM = [device isBatterySaverModeActive];
  11. UIView *blur;
  12. UIView *blurPlatter = [[UIView alloc] init];
  13. if (bannerStyle == 1) {
  14. if (kCFCoreFoundationVersionNumber > 1600) {
  15. blur = [[[objc_getClass("MTMaterialView") class] alloc] _initWithRecipe:1 configuration:1 initialWeighting:1 scaleAdjustment:nil];
  16. } else if (kCFCoreFoundationVersionNumber < 1600) {
  17. blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  18. }
  19. } else if (bannerStyle == 2) {
  20. blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
  21. } else if (bannerStyle == 3) {
  22. blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  23. }
  24. blur.layer.masksToBounds = YES;
  25. blur.layer.continuousCorners = YES;
  26. blur.layer.cornerRadius = cornerRadius;
  27. blurPlatter.alpha = bannerAlpha;
  28. NSString *labelText = [NSString stringWithFormat:@"%@", deviceName];
  29. self.label = [[UILabel alloc] init];
  30. if (!hideDeviceLabel) {
  31. [self.label setFont:[UIFont systemFontOfSize:16]];
  32. } else if (hideDeviceLabel) {
  33. [self.label setFont:[UIFont systemFontOfSize:0]];
  34. }
  35. if (textColor == 1) {
  36. [self.label setTextColor:[UIColor whiteColor]];
  37. } else {
  38. [self.label setTextColor:[UIColor blackColor]];
  39. }
  40. self.label.lineBreakMode = NSLineBreakByWordWrapping;
  41. self.label.numberOfLines = 1;
  42. [self.label setText:labelText];
  43. self.battery = [[_UIBatteryView alloc] init];
  44. self.battery.chargePercent = (batteryPercentage * 0.01);
  45. self.percentLabel = [[UILabel alloc] init];
  46. self.battery.showsPercentage = NO;
  47. if (hidePercent) {
  48. [self.percentLabel setFont:[UIFont systemFontOfSize:0]];
  49. } else {
  50. [self.percentLabel setFont:[UIFont systemFontOfSize:14]];
  51. }
  52. if (textColor == 1) {
  53. [self.percentLabel setTextColor:[UIColor whiteColor]];
  54. } else {
  55. [self.percentLabel setTextColor:[UIColor blackColor]];
  56. }
  57. self.percentLabel.lineBreakMode = NSLineBreakByWordWrapping;
  58. [self.percentLabel setTextAlignment:NSTextAlignmentRight];
  59. self.percentLabel.numberOfLines = 1;
  60. [self.percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger)batteryPercentage)]];
  61. if (charging)
  62. self.battery.chargingState = 1;
  63. self.battery.showsInlineChargingIndicator = YES;
  64. if (LPM)
  65. self.battery.saverModeActive = YES;
  66. if (kCFCoreFoundationVersionNumber > 1600) {
  67. [self.battery setBodyColorAlpha:1.0];
  68. [self.battery setPinColorAlpha:1.0];
  69. }
  70. UIImage *glyph = ios13 ? [device glyph] : [device batteryWidgetGlyph];
  71. self.glyphView = [[UIImageView alloc] init];
  72. self.glyphView.contentMode = UIViewContentModeScaleAspectFit;
  73. [self.glyphView setImage:glyph];
  74. [self addSubview:blurPlatter];
  75. [blurPlatter addSubview:blur];
  76. [self addSubview:self.percentLabel];
  77. [self addSubview:self.label];
  78. [self addSubview:self.battery];
  79. [self addSubview:self.glyphView];
  80. // Blur Platter
  81. blurPlatter.translatesAutoresizingMaskIntoConstraints = NO;
  82. if (bannerAlign == 2) { //center
  83. [blurPlatter.centerXAnchor constraintEqualToAnchor:self.centerXAnchor].active = YES;
  84. } else if (bannerAlign == 1) { //left
  85. [blurPlatter.leftAnchor constraintEqualToAnchor:self.leftAnchor].active = YES;
  86. } else if (bannerAlign == 3) { //right
  87. [blurPlatter.rightAnchor constraintEqualToAnchor:self.rightAnchor].active = YES;
  88. }
  89. [NSLayoutConstraint activateConstraints:@[
  90. [blurPlatter.topAnchor constraintEqualToAnchor:self.topAnchor],
  91. [blurPlatter.widthAnchor constraintEqualToConstant:(([[[objc_getClass("CSAdjunctListView") class] sharedListViewForKai] stackView].frame.size.width - 16) + bannerWidthFactor)],
  92. [blurPlatter.heightAnchor constraintEqualToConstant:bannerHeight]
  93. ]];
  94. [self.widthAnchor constraintEqualToAnchor:blurPlatter.widthAnchor].active = YES;
  95. // Blur
  96. blur.translatesAutoresizingMaskIntoConstraints = NO;
  97. [NSLayoutConstraint activateConstraints:@[
  98. [blur.centerXAnchor constraintEqualToAnchor:blurPlatter.centerXAnchor],
  99. [blur.topAnchor constraintEqualToAnchor:blurPlatter.topAnchor],
  100. [blur.widthAnchor constraintEqualToAnchor:blurPlatter.widthAnchor],
  101. [blur.heightAnchor constraintEqualToAnchor:blurPlatter.heightAnchor]
  102. ]];
  103. // Percent label
  104. self.percentLabel.translatesAutoresizingMaskIntoConstraints = NO;
  105. [NSLayoutConstraint activateConstraints:@[
  106. [self.percentLabel.centerYAnchor constraintEqualToAnchor:blurPlatter.centerYAnchor],
  107. [self.percentLabel.widthAnchor constraintEqualToConstant:36],
  108. [self.percentLabel.heightAnchor constraintEqualToConstant:12]
  109. ]];
  110. // Label
  111. self.label.translatesAutoresizingMaskIntoConstraints = NO;
  112. [NSLayoutConstraint activateConstraints:@[
  113. [self.label.leftAnchor constraintEqualToAnchor:self.glyphView.rightAnchor
  114. constant:4.5],
  115. [self.label.centerYAnchor constraintEqualToAnchor:blurPlatter.centerYAnchor],
  116. [self.label.heightAnchor constraintEqualToConstant:25]
  117. ]];
  118. if (!hidePercent) {
  119. [self.label.rightAnchor constraintEqualToAnchor:self.percentLabel.leftAnchor constant:-4.5].active = YES;
  120. } else {
  121. [self.label.rightAnchor constraintEqualToAnchor:self.label.leftAnchor].active = YES;
  122. }
  123. // Glyph View
  124. self.glyphView.translatesAutoresizingMaskIntoConstraints = NO;
  125. [NSLayoutConstraint activateConstraints:@[
  126. [self.glyphView.leftAnchor constraintEqualToAnchor:blurPlatter.leftAnchor
  127. constant:20.5],
  128. [self.glyphView.centerYAnchor constraintEqualToAnchor:blurPlatter.centerYAnchor],
  129. [self.glyphView.widthAnchor constraintEqualToConstant:glyphSize],
  130. [self.glyphView.heightAnchor constraintEqualToConstant:glyphSize]
  131. ]];
  132. // Battery
  133. self.battery.translatesAutoresizingMaskIntoConstraints = NO;
  134. if (!hideBatteryIcon) {
  135. [self.battery.widthAnchor constraintEqualToConstant:20].active = YES;
  136. } else {
  137. [self.battery.widthAnchor constraintEqualToConstant:0].active = YES;
  138. self.battery.alpha = 0;
  139. }
  140. [NSLayoutConstraint activateConstraints:@[
  141. [self.battery.centerYAnchor constraintEqualToAnchor:blurPlatter.centerYAnchor],
  142. [self.battery.rightAnchor constraintEqualToAnchor:blurPlatter.rightAnchor
  143. constant:-20.5],
  144. [self.battery.heightAnchor constraintEqualToConstant:10]
  145. ]];
  146. if (!hideDeviceLabel) {
  147. [self.percentLabel.rightAnchor constraintEqualToAnchor:self.battery.leftAnchor constant:-4.5].active = YES;
  148. } else if (hideDeviceLabel) {
  149. [self.percentLabel.centerXAnchor constraintEqualToAnchor:blurPlatter.centerXAnchor].active = YES;
  150. }
  151. if (hidePercent) {
  152. [self.label.rightAnchor constraintEqualToAnchor:self.battery.leftAnchor constant:-4.5].active = YES;
  153. }
  154. [self.heightAnchor constraintEqualToConstant:(bannerHeight + spacing)];
  155. }
  156. return self;
  157. }
  158. - (void)traitCollectionDidChange:(id)arg1 {
  159. [super traitCollectionDidChange:arg1];
  160. if (textColor == 0) {
  161. if (@available(iOS 12.0, *)) {
  162. if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  163. [self.label setTextColor:[UIColor whiteColor]];
  164. [self.percentLabel setTextColor:[UIColor whiteColor]];
  165. } else if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
  166. [self.label setTextColor:[UIColor blackColor]];
  167. [self.percentLabel setTextColor:[UIColor blackColor]];
  168. }
  169. }
  170. }
  171. }
  172. - (void)updateInfo {
  173. if (self.device != nil) {
  174. NSString *deviceName = [self.device name];
  175. double batteryPercentage = [self.device percentCharge];
  176. BOOL charging = [self.device isCharging];
  177. BOOL LPM = [self.device isBatterySaverModeActive];
  178. self.label.text = [NSString stringWithFormat:@"%@", deviceName];
  179. [self.percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger)batteryPercentage)]];
  180. self.battery.chargePercent = (batteryPercentage * 0.01);
  181. if (charging) {
  182. self.battery.chargingState = 1;
  183. } else {
  184. self.battery.chargingState = 0;
  185. }
  186. self.battery.showsInlineChargingIndicator = YES;
  187. if (LPM) {
  188. self.battery.saverModeActive = YES;
  189. } else {
  190. self.battery.saverModeActive = NO;
  191. }
  192. if (kCFCoreFoundationVersionNumber > 1600) {
  193. [self.battery setBodyColorAlpha:1.0];
  194. [self.battery setPinColorAlpha:1.0];
  195. }
  196. [self.percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger)batteryPercentage)]];
  197. self.battery.chargePercent = (batteryPercentage * 0.01);
  198. [self.glyphView setImage:ios13 ? [self.device glyph] : [self.device batteryWidgetGlyph]];
  199. } else {
  200. }
  201. }
  202. - (void)removeFromSuperview {
  203. self.device.kaiCell = nil;
  204. [super removeFromSuperview];
  205. }
  206. @end