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.

235 lines
9.3KB

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