Device battery indicators on your Lock Screen
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

136 rindas
5.5KB

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