Device battery indicators on your Lock Screen
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

173 Zeilen
5.4KB

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