Device battery indicators on your Lock Screen
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

197 lines
6.5KB

  1. #import "Kai.h"
  2. %hook KAITarget //This class is defined in %ctor, KAITarget is not a class name.
  3. %property (nonatomic, assign) BOOL hasKai;
  4. -(void)_layoutStackView {
  5. NSInteger lastSlot = [[self stackView].subviews count] -1;
  6. //this code is used to determine if kai is at the bottom of the stack view
  7. if([[self stackView].subviews objectAtIndex:lastSlot] != [KAIBatteryStack sharedInstance] && belowMusic) {
  8. //if it is not, but the option to have kai below music is on, i simply remove from it's current pos.
  9. //and insert into last slot.
  10. [[self stackView] removeArrangedSubview:[KAIBatteryStack sharedInstance]];
  11. [[self stackView] insertArrangedSubview:[KAIBatteryStack sharedInstance] atIndex:lastSlot];
  12. }
  13. //makes kai lay itself out when the stack does
  14. NSLog(@"kai: laying out stack view");
  15. [self KaiUpdate];
  16. %orig;
  17. }
  18. -(void)setStackView:(UIStackView *)arg1 {
  19. if(!KAISelf.hasKai) {
  20. KAIBatteryStack *battery = [[KAIBatteryStack alloc] init];
  21. //Add noti observer
  22. [[NSNotificationCenter defaultCenter] addObserver:self
  23. selector:@selector(KaiInfo)
  24. name:@"KaiInfoChanged"
  25. object:nil];
  26. KAISelf.hasKai = YES;
  27. if(![arg1.subviews containsObject:battery]) { //if not added
  28. //add kai to the stack view
  29. [arg1 addArrangedSubview:battery];
  30. }
  31. [battery updateBattery];
  32. //send the adjusted stackview as arg1
  33. %orig(arg1);
  34. }
  35. }
  36. %new
  37. -(void)KaiUpdate {
  38. KAIBatteryStack *battery = [KAIBatteryStack sharedInstance];
  39. //battery.number = [battery.subviews count];
  40. BCBatteryDeviceController *bcb = [BCBatteryDeviceController sharedInstance];
  41. NSArray *devices = MSHookIvar<NSArray *>(bcb, "_sortedDevices");
  42. for(KAIBatteryCell *cell in battery.subviews) {
  43. //BCBatteryDevice *device = cell.device;
  44. [cell updateInfo];
  45. if(![devices containsObject:cell.device]) {
  46. [UIView animateWithDuration:0.3 animations:^{
  47. cell.alpha = 0;
  48. } completion:^(BOOL finished) {
  49. [cell removeFromSuperview];
  50. [battery removeArrangedSubview:cell];
  51. cell.alpha = 1;
  52. }];
  53. }
  54. }
  55. [UIView animateWithDuration:0.3 animations:^{
  56. if(!battery.heightConstraint) {
  57. battery.heightConstraint.active = NO;
  58. battery.heightConstraint = [battery.heightAnchor constraintEqualToConstant:85];
  59. //set an initial constraint
  60. battery.heightConstraint.active = YES;
  61. } else {
  62. int height = (battery.number * (bannerHeight + spacing)); //big brain math
  63. //battery.heightConstraint.active = NO; //deactivation
  64. battery.heightConstraint.constant = height;
  65. //battery.heightConstraint.active = YES; //forcing reactivation
  66. UIStackView *s = [self stackView];
  67. s.frame = CGRectMake(s.frame.origin.x, s.frame.origin.y, s.frame.size.width, (s.frame.size.height - 1));
  68. //literally does nothing but makes the stack view lay itself out (doesnt adjust frame because translatesAutoreszingMaskIntoConstraints = NO on stack views)
  69. }
  70. }];
  71. }
  72. %new
  73. -(void)KaiInfo {
  74. if(!isUpdating) {
  75. isUpdating = YES;
  76. //NSLog(@"kai: kai info will update");
  77. dispatch_async(dispatch_get_main_queue(), ^{
  78. [[KAIBatteryStack sharedInstance] updateBattery];
  79. [self KaiUpdate];
  80. isUpdating = NO;
  81. });
  82. }
  83. }
  84. %end
  85. %hook BCBatteryDevice
  86. %property (nonatomic, strong) KAIBatteryCell *kaiCell;
  87. - (id)initWithIdentifier:(id)arg1 vendor:(long long)arg2 productIdentifier:(long long)arg3 parts:(unsigned long long)arg4 matchIdentifier:(id)arg5 {
  88. //Posts a notification to self when these keys change
  89. [self addObserver:self forKeyPath:@"charging" options:NSKeyValueObservingOptionNew context:nil];
  90. [self addObserver:self forKeyPath:@"batterySaverModeActive" options:NSKeyValueObservingOptionNew context:nil];
  91. [self addObserver:self forKeyPath:@"percentCharge" options:NSKeyValueObservingOptionNew context:nil];
  92. return %orig;
  93. }
  94. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
  95. //if([self isMemberOfClass:[objc_getClass("BCBatteryDevice") class]] && [self respondsToSelector:@selector(_kaiCell)] && object == self && ([keyPath isEqualToString:@"charging"] || [keyPath isEqualToString:@"percentCharge"] || [keyPath isEqualToString:@"batterySaverModeActive"])) {
  96. //sends the noti to update battery info
  97. [[NSNotificationCenter defaultCenter] postNotificationName:@"KaiInfoChanged" object:nil userInfo:nil];
  98. //}
  99. }
  100. %new
  101. -(id)kaiCellForDevice {
  102. if(self && self.kaiCell == nil) {
  103. self.kaiCell = [[KAIBatteryCell alloc] initWithFrame:CGRectMake(0,0,[KAIBatteryStack sharedInstance].frame.size.width,0) device:self]; }
  104. ((KAIBatteryCell *)self.kaiCell).translatesAutoresizingMaskIntoConstraints = NO;
  105. [((KAIBatteryCell *)self.kaiCell).heightAnchor constraintEqualToConstant:bannerHeight].active = YES;
  106. [(KAIBatteryCell *)self.kaiCell updateInfo];
  107. return self.kaiCell;
  108. }
  109. %new
  110. -(void)resetKaiCellForNewPrefs {
  111. self.kaiCell = [[KAIBatteryCell alloc] initWithFrame:CGRectMake(0,0,[KAIBatteryStack sharedInstance].frame.size.width,0) device:self];
  112. ((KAIBatteryCell *)self.kaiCell).translatesAutoresizingMaskIntoConstraints = NO;
  113. [((KAIBatteryCell *)self.kaiCell).heightAnchor constraintEqualToConstant:bannerHeight].active = YES;
  114. [(KAIBatteryCell *)self.kaiCell updateInfo];
  115. }
  116. %end
  117. %hook KAICSTarget //Again, not a class
  118. -(void)_transitionChargingViewToVisible:(BOOL)arg1 showBattery:(BOOL)arg2 animated:(BOOL)arg3 {
  119. if(hideChargingAnimation) {
  120. //Yeah bro this just makes the method never call to show the charging thing
  121. %orig(NO,NO,NO);
  122. }
  123. }
  124. -(void)_transitionChargingViewToVisible:(BOOL)arg1 showBattery:(BOOL)arg2 animated:(BOOL)arg3 force:(BOOL)arg4 { //might just be ios12
  125. if(hideChargingAnimation) {
  126. //Same idea
  127. %orig(NO,NO,NO,NO);
  128. }
  129. }
  130. %end
  131. %ctor {
  132. preferencesChanged();
  133. CFNotificationCenterAddObserver(
  134. CFNotificationCenterGetDarwinNotifyCenter(),
  135. &observer,
  136. (CFNotificationCallback)applyPrefs,
  137. kSettingsChangedNotification,
  138. NULL,
  139. CFNotificationSuspensionBehaviorDeliverImmediately
  140. );
  141. //Bro Muirey helped me figure out a logical way to do this because iOS 12-13 classes have changed
  142. Class cls = kCFCoreFoundationVersionNumber > 1600 ? ([objc_getClass("CSAdjunctListView") class]) : ([objc_getClass("SBDashBoardAdjunctListView") class]);
  143. Class CSCls = kCFCoreFoundationVersionNumber > 1600 ? ([objc_getClass("CSCoverSheetViewController") class]) : ([objc_getClass("SBDashBoardViewController") class]);
  144. if(enabled) {
  145. %init(KAITarget = cls, KAICSTarget = CSCls); //BIG BRAIN BRO!!
  146. }
  147. }