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.

140 lines
6.0KB

  1. #import "KAIBattery.h"
  2. KAIBattery *instance;
  3. @implementation KAIBattery
  4. -(instancetype)init {
  5. self = [super init];
  6. instance = self;
  7. if (self) {
  8. /*self.translatesAutoresizingMaskIntoConstraints = NO;
  9. [self.leftAnchor constraintEqualToAnchor:self.leftAnchor constant:8].active = YES;
  10. [self.topAnchor constraintEqualToAnchor:self.topAnchor constant:arg1.origin.y].active = YES;
  11. [self.widthAnchor constraintEqualToConstant:UIScreen.mainScreen.bounds.size.width - 16].active = YES;
  12. [self.heightAnchor constraintEqualToConstant:(self.number * 85)].active = YES;*/
  13. [self updateBattery];
  14. [self darkLightMode];
  15. self.userInteractionEnabled = NO;
  16. //[self addSubview:self.batteryLabel];
  17. }
  18. return self;
  19. }
  20. long long batteryPercentage;
  21. long long lastPercentage;
  22. -(void)updateBattery {
  23. dispatch_async(dispatch_get_main_queue(), ^{
  24. if(!self.isUpdating) {
  25. self.isUpdating = YES;
  26. self.number = 0;
  27. float y = 0;
  28. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  29. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  30. for( UIView *view in self.subviews ) {
  31. @try {
  32. [view removeFromSuperview];
  33. } @catch (NSException *exception) {
  34. //Panik
  35. }
  36. }
  37. for (BCBatteryDevice *device in devices) {
  38. NSString *deviceName = MSHookIvar<NSString *>(device, "_name");
  39. double batteryPercentage = MSHookIvar<long long>(device, "_percentCharge");
  40. BOOL charging = MSHookIvar<long long>(device, "_charging");
  41. BOOL LPM = MSHookIvar<BOOL>(device, "_batterySaverModeActive");
  42. if(charging) {
  43. /*UIVisualEffectView *blank;
  44. if(@available(iOS 12.0, *)) {
  45. if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  46. blank = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
  47. } else {
  48. blank = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  49. }
  50. } else {
  51. blank = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
  52. }*/
  53. MTMaterialView *blank = [[[objc_getClass("MTMaterialView") class] alloc] _initWithRecipe:1 configuration:1 initialWeighting:1 scaleAdjustment:nil];
  54. //blank.recipeDynamic = NO; //makes it stay light
  55. blank.frame = CGRectMake(0, 0 + y, self.superview.bounds.size.width - 16, 80);
  56. blank.layer.masksToBounds = YES;
  57. blank.layer.cornerRadius = 13;
  58. //[blank setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1]];
  59. NSString *labelText = [NSString stringWithFormat:@"%@", deviceName];
  60. UILabel *label = [[UILabel alloc] init];
  61. [label setFont:[UIFont systemFontOfSize:16]];
  62. [label setTextColor:[UIColor whiteColor]];
  63. label.lineBreakMode = NSLineBreakByWordWrapping;
  64. label.numberOfLines = 0;
  65. [label setText:labelText];
  66. _UIBatteryView *battery = [[_UIBatteryView alloc] init];
  67. battery.chargePercent = (batteryPercentage*0.01);
  68. UILabel *percentLabel = [[UILabel alloc] init];
  69. battery.showsPercentage = NO;
  70. [percentLabel setFont:[UIFont systemFontOfSize:14]];
  71. [percentLabel setTextColor:[UIColor whiteColor]];
  72. percentLabel.lineBreakMode = NSLineBreakByWordWrapping;
  73. [percentLabel setTextAlignment:NSTextAlignmentRight];
  74. percentLabel.numberOfLines = 0;
  75. [percentLabel setText:[NSString stringWithFormat:@"%ld%%", (long)((NSInteger) batteryPercentage)]];
  76. if(charging) battery.chargingState = 1;
  77. battery.showsInlineChargingIndicator = YES;
  78. if(LPM) battery.saverModeActive = YES;
  79. if(kCFCoreFoundationVersionNumber > 1600) {
  80. [battery setBodyColorAlpha:1.0];
  81. [battery setPinColorAlpha:1.0];
  82. }
  83. UIImage *glyph = [device glyph];
  84. UIImageView *glyphView = [[UIImageView alloc] init];
  85. glyphView.contentMode = UIViewContentModeScaleAspectFit;
  86. [glyphView setImage:glyph];
  87. label.frame = CGRectMake(65.5,27.5 + y,275,25);
  88. glyphView.frame = CGRectMake(20.5,18.5 + y,40,40);
  89. battery.frame = CGRectMake(self.superview.bounds.size.width - 16 - 49,35 + y,20,10);
  90. percentLabel.frame = CGRectMake(self.superview.bounds.size.width - 16 - 94,35 + y,36,12);
  91. y+=85;
  92. self.number +=1;
  93. [self addSubview:blank];
  94. [self addSubview:percentLabel];
  95. [self addSubview:label];
  96. [self addSubview:battery];
  97. [self addSubview:glyphView];
  98. //blank.alpha = 0.8;
  99. }
  100. }
  101. //[self.heightAnchor constraintEqualToConstant:(self.number * 85)].active = YES;
  102. self.isUpdating = NO;
  103. [self darkLightMode];
  104. }
  105. });
  106. }
  107. +(KAIBattery *)sharedInstance {
  108. return instance;
  109. }
  110. -(void)darkLightMode {
  111. /*for(UIVisualEffectView *view in self.subviews) {
  112. if(@available(iOS 12.0, *)) {
  113. if(self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  114. if([view respondsToSelector:@selector(setEffect:)]) view.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
  115. }
  116. else {
  117. if([view respondsToSelector:@selector(setEffect:)]) view.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
  118. }
  119. }
  120. }*/
  121. }
  122. @end