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.

154 lines
4.3KB

  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] != [KAIBattery 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:[KAIBattery sharedInstance]];
  11. [[self stackView] insertArrangedSubview:[KAIBattery sharedInstance] atIndex:lastSlot];
  12. }
  13. //makes kai lay itself out when the stack does
  14. [self KaiUpdate];
  15. %orig;
  16. }
  17. -(void)setStackView:(UIStackView *)arg1 {
  18. if(!KAISelf.hasKai) {
  19. KAIBattery *battery = [[KAIBattery alloc] init];
  20. //Add noti observer
  21. [[NSNotificationCenter defaultCenter] addObserver:self
  22. selector:@selector(KaiInfo)
  23. name:@"KaiInfoChanged"
  24. object:nil];
  25. KAISelf.hasKai = YES;
  26. if(![arg1.subviews containsObject:battery]) { //if not added
  27. //add kai to the stack view
  28. [arg1 addArrangedSubview:battery];
  29. }
  30. //send the adjusted stackview as arg1
  31. %orig(arg1);
  32. }
  33. }
  34. %new
  35. -(void)KaiUpdate {
  36. KAIBattery *battery = [KAIBattery sharedInstance];
  37. [UIView animateWithDuration:0.3 animations:^{
  38. if(!battery.heightConstraint) {
  39. battery.heightConstraint.active = NO;
  40. battery.heightConstraint = [battery.heightAnchor constraintEqualToConstant:85];
  41. //set an initial constraint
  42. battery.heightConstraint.active = YES;
  43. } else {
  44. int height = ((battery.number * (bannerHeight + spacing)) - spacing + 5); //big brain math
  45. battery.heightConstraint.active = NO;
  46. battery.heightConstraint.constant = height;
  47. battery.heightConstraint.active = YES;
  48. UIStackView *s = [self stackView];
  49. s.frame = CGRectMake(s.frame.origin.x, s.frame.origin.y, s.frame.size.width, (s.frame.size.height - 1));
  50. //literally does nothing but makes the stack view lay itself out (doesnt adjust frame because translatesAutoreszingMaskIntoConstraints = NO on stack views)
  51. }
  52. }];
  53. }
  54. %new
  55. -(void)KaiInfo {
  56. if(!isUpdating) {
  57. isUpdating = YES;
  58. [UIView animateWithDuration:0.3 animations:^{
  59. //nice fade out
  60. [KAIBattery sharedInstance].alpha = 0;
  61. } completion:^(BOOL finished){
  62. [[KAIBattery sharedInstance] updateBattery];
  63. [self KaiUpdate];
  64. [UIView animateWithDuration:0.35 animations:^{
  65. //fade back in
  66. [KAIBattery sharedInstance].alpha = 1;
  67. } completion:^(BOOL finished){
  68. isUpdating = NO;
  69. }];
  70. }];
  71. }
  72. }
  73. %end
  74. %hook BCBatteryDevice
  75. - (id)initWithIdentifier:(id)arg1 vendor:(long long)arg2 productIdentifier:(long long)arg3 parts:(unsigned long long)arg4 matchIdentifier:(id)arg5 {
  76. //Posts a notification to self when these keys change
  77. [self addObserver:self forKeyPath:@"charging" options:NSKeyValueObservingOptionNew context:nil];
  78. [self addObserver:self forKeyPath:@"batterySaverModeActive" options:NSKeyValueObservingOptionNew context:nil];
  79. [self addObserver:self forKeyPath:@"percentCharge" options:NSKeyValueObservingOptionNew context:nil];
  80. return %orig;
  81. }
  82. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
  83. dispatch_async(dispatch_get_main_queue(), ^{
  84. //sends the noti to update battery info
  85. [[NSNotificationCenter defaultCenter] postNotificationName:@"KaiInfoChanged" object:nil userInfo:nil];
  86. });
  87. }
  88. %end
  89. %hook CSCoverSheetViewController
  90. -(void)_transitionChargingViewToVisible:(BOOL)arg1 showBattery:(BOOL)arg2 animated:(BOOL)arg3 {
  91. if(hideChargingAnimation) {
  92. //Yeah bro this just makes the method never call to show the charging thing
  93. %orig(NO,NO,NO);
  94. }
  95. }
  96. %end
  97. %ctor {
  98. preferencesChanged();
  99. CFNotificationCenterAddObserver(
  100. CFNotificationCenterGetDarwinNotifyCenter(),
  101. &observer,
  102. (CFNotificationCallback)applyPrefs,
  103. kSettingsChangedNotification,
  104. NULL,
  105. CFNotificationSuspensionBehaviorDeliverImmediately
  106. );
  107. //Bro Muirey helped me figure out a logical way to do this because iOS 12-13 classes have changed
  108. Class cls = kCFCoreFoundationVersionNumber > 1600 ? ([objc_getClass("CSAdjunctListView") class]) : ([objc_getClass("SBDashBoardAdjunctListView") class]);
  109. if(enabled) {
  110. %init(KAITarget = cls);
  111. }
  112. }