-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTweak.xm
411 lines (339 loc) · 14.6 KB
/
Tweak.xm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#import "Tweak.h"
%group ClockApp
// this is pretty much the same as the other hook, the only difference is that there is no iOS 13/12
// specific code since this view only exists on iOS 14
%hook MTASleepAlarmTableViewCell
-(void) didMoveToWindow{
%orig;
[self performSelector:@selector(didMoveToWindow) withObject:nil afterDelay:1];
if(!isEnabledForBedtime) return;
clockLabel = [self digitalClockLabel];
NSInteger fireHour = MSHookIvar<NSInteger>(clockLabel,"_hour");
NSInteger fireMinute = MSHookIvar<NSInteger>(clockLabel,"_minute");
NSDate *fireDate;
for (MTAlarm* alarm in [[MSHookIvar<MTAlarmManager*>([UIApplication sharedApplication], "_alarmManager") cache] sleepAlarms]){
if(([alarm hour] == fireHour) && ([alarm minute] == fireMinute)){
fireDate = [alarm nextFireDate];
if(![alarm isEnabled]){
return;
}
}
}
if(!fireDate) return;
NSString *remainingTimeString = remainingTime([NSDate date], fireDate, showSeconds, textStyle);
if(isNotiOS14){
for (id subview in [[[self subviews] objectAtIndex:0] subviews]){
if([subview isKindOfClass:%c(MTUIAlarmView)]){
for (id view in [subview subviews]){
if([view isKindOfClass:%c(UILabel)]){
[view setText:remainingTimeString];
}
}
}
}
} else {
[[self detailLabel] setText:remainingTimeString];
}
}
%end
// the meat and potatoes of this tweak, at least for the clock app
%hook MTAAlarmTableViewCell
-(id)initWithStyle:(NSInteger)arg1 reuseIdentifier:(id)arg2{
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(postNotification) userInfo:nil repeats:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateCountdown) name:@"NappyTimeUpdateCountdownNotification" object:nil];
return %orig;
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
%new
-(void)postNotification{
[[NSNotificationCenter defaultCenter] postNotificationName:@"NappyTimeUpdateCountdownNotification" object:self];
}
%new
-(void)updateCountdown{
// check if the alarm is enabled
UISwitch *alarmSwitch = [self enabledSwitch];
BOOL alarmState = MSHookIvar<BOOL>(alarmSwitch,"_on");
// if the tweak is disabled for the clock app or it is a sleep alarm and the tweak is disabled for the bedtime alarm, exit
if((!isEnabled) || ([self isSleepAlarm] && !isEnabledForBedtime)) return;
// if the tweak is disabled for disabled alarms, exit
if((!showDisabled) && !(alarmState)) return;
// iterate through the subviews of the cell to find the clock label on iOS 13 and below
if(isNotiOS14){
for (UIView *tableCell in [self subviews]){
if([tableCell isKindOfClass: %c(UITableViewCellContentView)]){
for(UIView *alarmView in [tableCell subviews]){
if([alarmView isKindOfClass: %c(MTUIAlarmView)]){
for(UIView *labelView in [alarmView subviews]){
if([labelView isKindOfClass: %c(MTUIDigitalClockLabel)]){
clockLabel = (MTUIDigitalClockLabel *) labelView;
}
}
}
}
}
}
}
// access the clock label property on iOS 14
else{
clockLabel = [self digitalClockLabel];
}
NSDate* fireDate;
NSTimeInterval timeUntilFire;
// get the time the alarm will fire from the clock label
NSInteger fireHour = MSHookIvar<NSInteger>(clockLabel,"_hour");
NSInteger fireMinute = MSHookIvar<NSInteger>(clockLabel,"_minute");
if([self isSleepAlarm]){
fireDate = [[[MSHookIvar<MTAlarmManager*>([UIApplication sharedApplication], "_alarmManager") cache] sleepAlarm] nextFireDate];
timeUntilFire = [fireDate timeIntervalSinceDate:[NSDate date]];
}
else{
for(MTAlarm* alarm in [[MSHookIvar<MTAlarmManager*>([UIApplication sharedApplication], "_alarmManager") cache] orderedAlarms]){
if(([alarm hour] == fireHour) && ([alarm minute] == fireMinute)){
fireDate = [alarm nextFireDate];
timeUntilFire = [fireDate timeIntervalSinceDate:[NSDate date]];
}
}
}
if( ![self isSleepAlarm] && (!shouldIgnoreThresholdClockApp) && (timeUntilFire>clockAppCountdownThreshold) ) return;
// get the string that will be displayed based on the date and the user's preferences
NSString *remainingTimeString = remainingTime([NSDate date], fireDate, showSeconds, textStyle);
// once again iterate through subviews to find what we're looking for
if (isNotiOS14){
for (id subview in [[[self subviews] objectAtIndex: 0] subviews]){
if ([subview isKindOfClass: %c(MTUIAlarmView)]){
for (id view in [subview subviews]){
if ([view isKindOfClass:%c(UILabel)]){
[view setText:remainingTimeString];
}
}
}
}
}
else{
[[self detailLabel] setText:remainingTimeString];
}
}
%end
CGPoint lastOffset;
NSTimeInterval lastOffsetCapture;
%hook UITableView
- (void)setContentOffset:(CGPoint)contentOffset{
%orig;
if([[self nextResponder] isKindOfClass:%c(MTAAlarmTableViewController)]){
CGPoint currentOffset = contentOffset;
NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval timeDiff = currentTime - lastOffsetCapture;
if(timeDiff > 0.01){
CGFloat distance = currentOffset.y - lastOffset.y;
CGFloat scrollSpeedNotAbs = distance / timeDiff;
CGFloat scrollSpeed = fabs(scrollSpeedNotAbs);
if(!(scrollSpeed > 2000)){
[[NSNotificationCenter defaultCenter] postNotificationName:@"NappyTimeUpdateCountdownNotification" object:self];
}
lastOffset = currentOffset;
lastOffsetCapture = currentTime;
}
}
}
%end
%end
%group SpringBoardCustomLabel
// meat and potatoes of the tweak for SpringBoard
%hook SBFLockScreenDateSubtitleDateView
%property (nonatomic, strong) UILabel *nappyTimeCountdown;
// remove the notification observer once the object is deallocated
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
%orig;
}
// add the notification observer as the object is initialized
-(id)initWithFrame:(CGRect)arg1{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toggleCountdown) name:@"NappyTimeToggleLSCountdown" object:nil];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown:) userInfo:nil repeats:YES];
if (![self nappyTimeCountdown] && !customPosition){
self.nappyTimeCountdown = [[UILabel alloc] initWithFrame:self.bounds];
[[self nappyTimeCountdown] setTranslatesAutoresizingMaskIntoConstraints:NO];
}
else if (![self nappyTimeCountdown] && customPosition) {
self.nappyTimeCountdown = [[UILabel alloc] initWithFrame:CGRectMake(labelCustomXPos, labelCustomYPos, 300, 200)];
[[self nappyTimeCountdown] sizeToFit];
}
return %orig;
}
// meat and potatoes of the SpringBoard Section of this tweak
%new
-(void)updateCountdown:(BOOL)willToggle{
if(customPosition){
[[self nappyTimeCountdown] setFrame:CGRectMake(labelCustomXPos, labelCustomYPos, 300, 200)];
}
[self addSubview:[self nappyTimeCountdown]];
if(jellyfishEnabled){
[[self nappyTimeCountdown] setTextColor:[[[%c(JPWallpaperColourManager) sharedManager] settings] primaryColor]];
[[self nappyTimeCountdown] setFont:[UIFont systemFontOfSize:17 weight:UIFontWeightBlack]];
}
else{
[[self nappyTimeCountdown].centerXAnchor constraintEqualToAnchor: [self centerXAnchor]].active = YES;
[[self nappyTimeCountdown].centerYAnchor constraintEqualToAnchor: [self centerYAnchor] constant:30].active = YES;
[[self nappyTimeCountdown].heightAnchor constraintEqualToConstant:50].active = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:@"NappyTimeGetCSTint" object:nil];
[[self nappyTimeCountdown] setTextColor:CSTintColor];
}
// get the firing date of the next alarm using SBScheduledAlarmObserver's sharedInstance
// get the string to display based on the fire date and current date
NSDate *fireDate = [[[MSHookIvar<MTAlarmManager *>([%c(SBScheduledAlarmObserver) sharedInstance], "_alarmManager") cache] nextAlarm] nextFireDate];
NSString *remainingTimeString = remainingTime([NSDate date], fireDate, showSecondsOnLS, LSTextStyle);
// check if fireDate exists, if it doesn't, you have no upcoming alarms
if([[MSHookIvar<MTAlarmManager *>([%c(SBScheduledAlarmObserver) sharedInstance], "_alarmManager") cache] nextAlarm]){
// if the tweak is enabled on the LS and the user wants to hide the clock based on the
// time to fire, check time remaining and respond accordingly
if(isEnabledOnLS && !alwaysShowLSCountdown && !isTempHiding){
// get the number of seconds remaining until the next alarm
NSTimeInterval timeUntilFire = [fireDate timeIntervalSinceDate:[NSDate date]];
if(timeUntilFire<LSCountdownThreshold){ // show countdown
moveNCList = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:@"NappyTimeMoveNC" object:nil];
if(!willToggle) [[self nappyTimeCountdown] setText:remainingTimeString];
else {
[UIView transitionWithView:[self nappyTimeCountdown] duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
[[self nappyTimeCountdown] setText:remainingTimeString];
} completion:nil];
}
}
else{ // show stock date
moveNCList = NO;
[[NSNotificationCenter defaultCenter] postNotificationName:@"NappyTimeMoveNC" object:nil];
if(!willToggle) [[self nappyTimeCountdown] setText:@""];
[UIView transitionWithView:[self nappyTimeCountdown] duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
[[self nappyTimeCountdown] setText:@""];
} completion:nil];
}
}
// this case is triggered when the user always to show the countdown, the tweak is enabled, and
// the countdown is not hiding
else if(isEnabledOnLS && !isTempHiding){
moveNCList = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:@"NappyTimeMoveNC" object:nil];
if(!willToggle) [[self nappyTimeCountdown] setText:remainingTimeString];
else {
[UIView transitionWithView:[self nappyTimeCountdown] duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
[[self nappyTimeCountdown] setText:remainingTimeString];
} completion:nil];
}
}
// this case is triggered when the countdown is always enabled but temporarily
// hidden by the double tap gesture
else{
moveNCList = NO;
[[NSNotificationCenter defaultCenter] postNotificationName:@"NappyTimeMoveNC" object:nil];
if(!willToggle) [[self nappyTimeCountdown] setText:@""];
[UIView transitionWithView:[self nappyTimeCountdown] duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
[[self nappyTimeCountdown] setText:@""];
} completion:nil];
}
}
else if(!isTempHiding && [[MSHookIvar<MTAlarmManager *>([%c(SBScheduledAlarmObserver) sharedInstance], "_alarmManager") cache] nextAlarm]){
// reserved for a future feature
moveNCList = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:@"NappyTimeMoveNC" object:nil];
}
else{
moveNCList = NO;
[[NSNotificationCenter defaultCenter] postNotificationName:@"NappyTimeMoveNC" object:nil];
if(!willToggle) [[self nappyTimeCountdown] setText:@""];
[UIView transitionWithView:[self nappyTimeCountdown] duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
[[self nappyTimeCountdown] setText:@""];
} completion:nil];
}
}
%new
// this method does what it says
- (void) toggleCountdown{
// toggle the value of isTempHiding
isTempHiding = !isTempHiding;
// call setString so the clock updates immediately
// the argument doesn't matter
[self updateCountdown:YES];
}
%end
%hook CSCoverSheetViewController
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
%orig;
}
// add an observer for the tint color and get the tint color
-(void)viewDidLoad{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTintColor) name:@"NappyTimeGetCSTint" object:nil];
CSTintColor = [[self legibilitySettings] primaryColor];
%orig;
}
%new
// does what it says
-(void)updateTintColor{
CSTintColor = [[self legibilitySettings] primaryColor];
}
%end
// add the view that will capture the double tap gesture
%hook NCNotificationStructuredListViewController
-(void)viewDidLoad{
%orig;
// make the view and gesture recognizer
UIView *gestureView = [[UIView alloc] initWithFrame:[self view].frame];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(NappyTimeToggleLSCountdown)];
// make the gesture recognizer listen for double taps and attach that to the view we just made
[doubleTap setNumberOfTapsRequired: 2];
[gestureView addGestureRecognizer: doubleTap];
// autolayout stuff
// Note: We're using the masterListView here since we want this view to scroll with the notifcations
// if we don't do this, the view blocks the scorlling of the notifications
[gestureView setTranslatesAutoresizingMaskIntoConstraints:NO];
[[self masterListView] addSubview: gestureView];
[gestureView.centerXAnchor constraintEqualToAnchor: [[self masterListView] centerXAnchor]].active = YES;
[gestureView.widthAnchor constraintEqualToConstant:[UIScreen mainScreen].bounds.size.width].active = YES;
if(jellyfishEnabled){
[gestureView.heightAnchor constraintEqualToConstant:120].active = YES;
[gestureView.topAnchor constraintEqualToAnchor: [[self masterListView] topAnchor] constant:-160].active = YES;
}
else{
[gestureView.topAnchor constraintEqualToAnchor: [[self masterListView] topAnchor] constant:-130].active = YES;
[gestureView.heightAnchor constraintEqualToConstant:100].active = YES;
}
[gestureView setUserInteractionEnabled: YES];
}
%new
// send a notification to toggle the countown once the view is double tapped
-(void)NappyTimeToggleLSCountdown{
[[NSNotificationCenter defaultCenter] postNotificationName:@"NappyTimeToggleLSCountdown" object:nil];
}
%end
// This is from Litten's Dress
// https://github.com/schneelittchen/Dress/blob/62b71b9d65f3919623d320ab12ae372634a43aa4/Tweak/Tweak.xm#L823
%hook CSCombinedListViewController
- (id)initWithNibName:(id)arg1 bundle:(id)arg2{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification) name:@"NappyTimeMoveNC" object:nil];
return %orig;
}
- (UIEdgeInsets)_listViewDefaultContentInsets { // adjust notification list position depending on style
if (!customPosition && !moveNCList) return %orig;
UIEdgeInsets originalInsets = %orig;
if(jellyfishEnabled) originalInsets.top += 17;
else originalInsets.top+=10;
return originalInsets;
}
%new
- (void) receiveTestNotification{
[self viewWillAppear: YES];
}
%end
%end
%ctor {
preferences = [[HBPreferences alloc] initWithIdentifier:@"com.arya06.nappytime14prefs"];
loadPrefs();
if([[[NSBundle mainBundle] bundleIdentifier] isEqual:@"com.apple.springboard"] && isEnabledOnLS){
%init(SpringBoardCustomLabel);
}
if([[[NSBundle mainBundle] bundleIdentifier] isEqual:@"com.apple.mobiletimer"]){
%init(ClockApp);
}
}