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.

KAIBatteryCell.mm 9.2KB

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