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.

190 lines
5.8KB

  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. //send the adjusted stackview as arg1
  32. %orig(arg1);
  33. }
  34. }
  35. %new
  36. -(void)KaiUpdate {
  37. KAIBatteryStack *battery = [KAIBatteryStack sharedInstance];
  38. battery.number = [battery.subviews count];
  39. [UIView animateWithDuration:0.3 animations:^{
  40. if(!battery.heightConstraint) {
  41. battery.heightConstraint.active = NO;
  42. battery.heightConstraint = [battery.heightAnchor constraintEqualToConstant:85];
  43. //set an initial constraint
  44. battery.heightConstraint.active = YES;
  45. } else {
  46. int height = (battery.number * (bannerHeight + spacing)); //big brain math
  47. battery.heightConstraint.active = NO; //deactivation
  48. battery.heightConstraint.constant = height;
  49. battery.heightConstraint.active = YES; //forcing reactivation
  50. UIStackView *s = [self stackView];
  51. s.frame = CGRectMake(s.frame.origin.x, s.frame.origin.y, s.frame.size.width, (s.frame.size.height - 1));
  52. //literally does nothing but makes the stack view lay itself out (doesnt adjust frame because translatesAutoreszingMaskIntoConstraints = NO on stack views)
  53. }
  54. }];
  55. }
  56. %new
  57. -(void)KaiInfo {
  58. if(!isUpdating) {
  59. isUpdating = YES;
  60. //NSLog(@"kai: kai info will update");
  61. dispatch_async(dispatch_get_main_queue(), ^{
  62. [[KAIBatteryStack sharedInstance] updateBattery];
  63. [self KaiUpdate];
  64. isUpdating = NO;
  65. });
  66. }
  67. }
  68. %end
  69. %hook BCBatteryDevice
  70. %property (nonatomic, strong) KAIBatteryCell *kaiCell;
  71. - (id)initWithIdentifier:(id)arg1 vendor:(long long)arg2 productIdentifier:(long long)arg3 parts:(unsigned long long)arg4 matchIdentifier:(id)arg5 {
  72. //Posts a notification to self when these keys change
  73. [self addObserver:self forKeyPath:@"charging" options:NSKeyValueObservingOptionNew context:nil];
  74. [self addObserver:self forKeyPath:@"batterySaverModeActive" options:NSKeyValueObservingOptionNew context:nil];
  75. [self addObserver:self forKeyPath:@"percentCharge" options:NSKeyValueObservingOptionNew context:nil];
  76. return %orig;
  77. }
  78. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
  79. @try {
  80. if([self isMemberOfClass:[objc_getClass("BCBatteryDevice") class]] && [self respondsToSelector:@selector(_kaiCell)]) {
  81. dispatch_async(dispatch_get_main_queue(), ^{
  82. if(self && self.kaiCell == nil) {
  83. self.kaiCell = [[KAIBatteryCell alloc] initWithFrame:CGRectMake(0,0,[KAIBatteryStack sharedInstance].frame.size.width,0) device:self]; }
  84. ((KAIBatteryCell *)self.kaiCell).translatesAutoresizingMaskIntoConstraints = NO;
  85. [((KAIBatteryCell *)self.kaiCell).heightAnchor constraintEqualToConstant:bannerHeight + spacing].active = YES;
  86. //sends the noti to update battery info
  87. [[NSNotificationCenter defaultCenter] postNotificationName:@"KaiInfoChanged" object:nil userInfo:nil];
  88. [(KAIBatteryCell *)self.kaiCell updateInfo];
  89. BOOL shouldAdd = NO;
  90. if(showAll) {
  91. shouldAdd = YES;
  92. } else if(!showAll && self.charging) {
  93. shouldAdd = YES;
  94. }
  95. if(![[KAIBatteryStack sharedInstance].subviews containsObject:self.kaiCell] && shouldAdd) {
  96. [[KAIBatteryStack sharedInstance] addArrangedSubview:self.kaiCell];
  97. } else if([[KAIBatteryStack sharedInstance].subviews containsObject:self.kaiCell] && !shouldAdd) {
  98. [[KAIBatteryStack sharedInstance] removeArrangedSubview:self.kaiCell];
  99. }
  100. });
  101. }
  102. } @catch (NSException *exc) {}
  103. }
  104. %new
  105. -(id)_kaiCell {
  106. return self.kaiCell;
  107. }
  108. %end
  109. %hook KAICSTarget //Again, not a class
  110. -(void)_transitionChargingViewToVisible:(BOOL)arg1 showBattery:(BOOL)arg2 animated:(BOOL)arg3 {
  111. if(hideChargingAnimation) {
  112. //Yeah bro this just makes the method never call to show the charging thing
  113. %orig(NO,NO,NO);
  114. }
  115. }
  116. -(void)_transitionChargingViewToVisible:(BOOL)arg1 showBattery:(BOOL)arg2 animated:(BOOL)arg3 force:(BOOL)arg4 { //might just be ios12
  117. if(hideChargingAnimation) {
  118. //Same idea
  119. %orig(NO,NO,NO,NO);
  120. }
  121. }
  122. %end
  123. %ctor {
  124. preferencesChanged();
  125. CFNotificationCenterAddObserver(
  126. CFNotificationCenterGetDarwinNotifyCenter(),
  127. &observer,
  128. (CFNotificationCallback)applyPrefs,
  129. kSettingsChangedNotification,
  130. NULL,
  131. CFNotificationSuspensionBehaviorDeliverImmediately
  132. );
  133. //Bro Muirey helped me figure out a logical way to do this because iOS 12-13 classes have changed
  134. Class cls = kCFCoreFoundationVersionNumber > 1600 ? ([objc_getClass("CSAdjunctListView") class]) : ([objc_getClass("SBDashBoardAdjunctListView") class]);
  135. Class CSCls = kCFCoreFoundationVersionNumber > 1600 ? ([objc_getClass("CSCoverSheetViewController") class]) : ([objc_getClass("SBDashBoardViewController") class]);
  136. if(enabled) {
  137. %init(KAITarget = cls, KAICSTarget = CSCls); //BIG BRAIN BRO!!
  138. }
  139. }