Device battery indicators on your Lock Screen
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

232 lines
9.1KB

  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. glyph= [glyph imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  72. self.glyphView = [[UIImageView alloc] init];
  73. self.glyphView.contentMode = UIViewContentModeScaleAspectFit;
  74. [self.glyphView setImage:glyph];
  75. self.glyphView.tintColor = [UIColor whiteColor];
  76. [self addSubview:blurPlatter];
  77. [blurPlatter addSubview:blur];
  78. [self addSubview:self.percentLabel];
  79. [self addSubview:self.label];
  80. [self addSubview:self.battery];
  81. [self addSubview:self.glyphView];
  82. // Blur Platter
  83. blurPlatter.translatesAutoresizingMaskIntoConstraints = NO;
  84. if (bannerAlign == 2) { //center
  85. [blurPlatter.centerXAnchor constraintEqualToAnchor:self.centerXAnchor].active = YES;
  86. } else if (bannerAlign == 1) { //left
  87. [blurPlatter.leftAnchor constraintEqualToAnchor:self.leftAnchor].active = YES;
  88. } else if (bannerAlign == 3) { //right
  89. [blurPlatter.rightAnchor constraintEqualToAnchor:self.rightAnchor].active = YES;
  90. }
  91. [NSLayoutConstraint activateConstraints:@[
  92. [blurPlatter.topAnchor constraintEqualToAnchor:self.topAnchor],
  93. [blurPlatter.widthAnchor constraintEqualToConstant:(([[[objc_getClass("CSAdjunctListView") class] sharedListViewForKai] stackView].frame.size.width - 16) + bannerWidthFactor)],
  94. [blurPlatter.heightAnchor constraintEqualToConstant:bannerHeight]
  95. ]];
  96. [self.widthAnchor constraintEqualToAnchor:blurPlatter.widthAnchor].active = YES;
  97. // Blur
  98. blur.translatesAutoresizingMaskIntoConstraints = NO;
  99. [NSLayoutConstraint activateConstraints:@[
  100. [blur.centerXAnchor constraintEqualToAnchor:blurPlatter.centerXAnchor],
  101. [blur.topAnchor constraintEqualToAnchor:blurPlatter.topAnchor],
  102. [blur.widthAnchor constraintEqualToAnchor:blurPlatter.widthAnchor],
  103. [blur.heightAnchor constraintEqualToAnchor:blurPlatter.heightAnchor]
  104. ]];
  105. // Percent label
  106. self.percentLabel.translatesAutoresizingMaskIntoConstraints = NO;
  107. [NSLayoutConstraint activateConstraints:@[
  108. [self.percentLabel.centerYAnchor constraintEqualToAnchor:blurPlatter.centerYAnchor],
  109. [self.percentLabel.widthAnchor constraintEqualToConstant:36],
  110. [self.percentLabel.heightAnchor constraintEqualToConstant:12]
  111. ]];
  112. // Label
  113. self.label.translatesAutoresizingMaskIntoConstraints = NO;
  114. [NSLayoutConstraint activateConstraints:@[
  115. [self.label.leftAnchor constraintEqualToAnchor:self.glyphView.rightAnchor
  116. constant:4.5],
  117. [self.label.centerYAnchor constraintEqualToAnchor:blurPlatter.centerYAnchor],
  118. [self.label.heightAnchor constraintEqualToConstant:25]
  119. ]];
  120. if (!hidePercent) {
  121. [self.label.rightAnchor constraintEqualToAnchor:self.percentLabel.leftAnchor constant:-4.5].active = YES;
  122. } else {
  123. [self.label.rightAnchor constraintEqualToAnchor:self.label.leftAnchor].active = YES;
  124. }
  125. // Glyph View
  126. self.glyphView.translatesAutoresizingMaskIntoConstraints = NO;
  127. [NSLayoutConstraint activateConstraints:@[
  128. [self.glyphView.leftAnchor constraintEqualToAnchor:blurPlatter.leftAnchor
  129. constant:20.5],
  130. [self.glyphView.centerYAnchor constraintEqualToAnchor:blurPlatter.centerYAnchor],
  131. [self.glyphView.widthAnchor constraintEqualToConstant:glyphSize],
  132. [self.glyphView.heightAnchor constraintEqualToConstant:glyphSize]
  133. ]];
  134. // Battery
  135. self.battery.translatesAutoresizingMaskIntoConstraints = NO;
  136. if (!hideBatteryIcon) {
  137. [self.battery.widthAnchor constraintEqualToConstant:20].active = YES;
  138. } else {
  139. [self.battery.widthAnchor constraintEqualToConstant:0].active = YES;
  140. self.battery.alpha = 0;
  141. }
  142. [NSLayoutConstraint activateConstraints:@[
  143. [self.battery.centerYAnchor constraintEqualToAnchor:blurPlatter.centerYAnchor],
  144. [self.battery.rightAnchor constraintEqualToAnchor:blurPlatter.rightAnchor
  145. constant:-20.5],
  146. [self.battery.heightAnchor constraintEqualToConstant:10]
  147. ]];
  148. if (!hideDeviceLabel) {
  149. [self.percentLabel.rightAnchor constraintEqualToAnchor:self.battery.leftAnchor constant:-4.5].active = YES;
  150. } else if (hideDeviceLabel) {
  151. [self.percentLabel.centerXAnchor constraintEqualToAnchor:blurPlatter.centerXAnchor].active = YES;
  152. }
  153. if (hidePercent) {
  154. [self.label.rightAnchor constraintEqualToAnchor:self.battery.leftAnchor constant:-4.5].active = YES;
  155. }
  156. [self.heightAnchor constraintEqualToConstant:(bannerHeight + spacing)];
  157. }
  158. return self;
  159. }
  160. - (void)traitCollectionDidChange:(id)arg1 {
  161. [super traitCollectionDidChange:arg1];
  162. if (textColor == 0) {
  163. if (@available(iOS 12.0, *)) {
  164. if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  165. [self.label setTextColor:[UIColor whiteColor]];
  166. [self.percentLabel setTextColor:[UIColor whiteColor]];
  167. } else if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
  168. [self.label setTextColor:[UIColor blackColor]];
  169. [self.percentLabel setTextColor:[UIColor blackColor]];
  170. }
  171. }
  172. }
  173. }
  174. - (void)updateInfo {
  175. if (self.device != nil) {
  176. NSString *deviceName = [self.device name];
  177. double batteryPercentage = [self.device percentCharge];
  178. BOOL charging = [self.device isCharging];
  179. BOOL LPM = [self.device isBatterySaverModeActive];
  180. self.label.text = [NSString stringWithFormat:@"%@", deviceName];
  181. [self.percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger)batteryPercentage)]];
  182. self.battery.chargePercent = (batteryPercentage * 0.01);
  183. if (charging) {
  184. self.battery.chargingState = 1;
  185. } else {
  186. self.battery.chargingState = 0;
  187. }
  188. self.battery.showsInlineChargingIndicator = YES;
  189. if (LPM) {
  190. self.battery.saverModeActive = YES;
  191. } else {
  192. self.battery.saverModeActive = NO;
  193. }
  194. if (kCFCoreFoundationVersionNumber > 1600) {
  195. [self.battery setBodyColorAlpha:1.0];
  196. [self.battery setPinColorAlpha:1.0];
  197. }
  198. [self.percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger)batteryPercentage)]];
  199. self.battery.chargePercent = (batteryPercentage * 0.01);
  200. }
  201. }
  202. - (void)removeFromSuperview {
  203. self.device.kaiCell = nil;
  204. [super removeFromSuperview];
  205. }
  206. @end