-
Notifications
You must be signed in to change notification settings - Fork 3
/
Tweak.xm
303 lines (261 loc) · 23.7 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
/**
* @Author: Dana Buehre <creaturesurvive>
* @Date: 06-08-2017 2:08:52
* @Email: [email protected]
* @Filename: Tweak.xm
* @Last modified by: creaturesurvive
* @Last modified time: 25-09-2017 11:29:38
* @Copyright: Copyright © 2014-2017 CreatureSurvive
*/
#include "headers.h"
//
// ─── NCNOTIFICATIONLISTVIEWCONTROLLER ───────────────────────────────────────────
//
%hook NCNotificationListViewController
// ─── PROPERTIES ─────────────────────────────────────────────────────────────────
%property(nonatomic, retain) UILabel *refreshLabel;
%property(nonatomic, retain) UIColor *refreshColor;
%property(nonatomic, retain) NSString *pullString;
%property(nonatomic, retain) NSString *releaseString;
%property(nonatomic, assign) BOOL isRefreshing;
%property(nonatomic, assign) BOOL isClearing;
// ────────────────────────────────────────────────────────────────────────────────
// ideally we will initialize our label here, however NCLink10 used AutoHook for
// all its hooking and that prevents these from being invoked. i attempted to fix
// this by hooking -(void)hook_viewDidLoad or -(void)original_viewDidLoad but that
// did not work. if anyone knows a fix, let me know.
// ────────────────────────────────────────────────────────────────────────────────
- (void)viewDidLoad {
%orig;
[self addRefreshLabelIfNecessary];
}
// ────────────────────────────────────────────────────────────────────────────────
// if we started scrolling we should configure our label according to the scroll
// distance
// ────────────────────────────────────────────────────────────────────────────────
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
%orig;
if (![[PTCProvider sharedProvider] boolForKey:@"kPTCEnabled"]) return;
[self refreshForCurrentOffset:scrollView.contentOffset.y];
}
// ────────────────────────────────────────────────────────────────────────────────
// if our drag ended while we were in the refreshing state, we should invoke our
// haptic feedback, hide the label and prepair to clear notifications
// ────────────────────────────────────────────────────────────────────────────────
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
%orig;
if (![[PTCProvider sharedProvider] boolForKey:@"kPTCEnabled"]) return;
if (self.isRefreshing) {
AudioServicesPlaySystemSound(1520);
self.refreshLabel.textColor = [UIColor clearColor];
self.refreshLabel.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0, 0);
self.isClearing = YES;
}
}
%new - (void)refreshForCurrentOffset: (CGFloat)offset {
// ────────────────────────────────────────────────────────────────────────────────
// a hackish solution to NCLink10 preventing the label from appearing
// ────────────────────────────────────────────────────────────────────────────────
if (!self.refreshLabel) {
[self addRefreshLabelIfNecessary];
}
// ────────────────────────────────────────────────────────────────────────────────
// calculate the pull and release percentages based on our scroll offset
// ────────────────────────────────────────────────────────────────────────────────
CGFloat height = [[PTCProvider sharedProvider] floatForKey:@"kPTCActivationHeight"];
CGFloat clearHeight = [[PTCProvider sharedProvider] floatForKey:@"kPTCClearHeight"];
CGFloat alpha = -offset/height;
CGFloat bravo = -offset/clearHeight;
// ────────────────────────────────────────────────────────────────────────────────
// if the scroll percentage is less than 1/5 our pull height then we can go ahead
// and hide our label its to small to see anyway, we also return control to sender.
// if we are already in the clearing state, we should also clear the notifications
// ────────────────────────────────────────────────────────────────────────────────
if (alpha <= 0.2) {
[self hideRefreshLabel];
if (self.isClearing) {
self.isClearing = NO;
[self clearNotifications];
}
return;
}
// ────────────────────────────────────────────────────────────────────────────────
// at this point there is no reason for our label to be hidden
// ────────────────────────────────────────────────────────────────────────────────
if (self.refreshLabel.hidden) {
self.refreshLabel.hidden = NO;
}
// ────────────────────────────────────────────────────────────────────────────────
// if our lebel is clearing (drag is past our puul height) then we need to start
// lowering the alpha of the notification view until it pops, just some asthetics
// ────────────────────────────────────────────────────────────────────────────────
if (self.isClearing) {
self.collectionView.alpha = alpha-0.2;
}
// ────────────────────────────────────────────────────────────────────────────────
// we've made it this far now time to configure our labels transparancy, scale, &
// position based on our percentages calculated above
// ───────────────────────────────────────────���────────────────────────────────────
self.refreshLabel.textColor = [self.refreshColor colorWithAlphaComponent:alpha];
self.refreshLabel.transform = CGAffineTransformScale(CGAffineTransformIdentity, alpha < 0.8 ? alpha : 0.8, alpha < 0.8 ? alpha : 0.8);
self.refreshLabel.frame = CGRectMake(0, offset, self.collectionView.frame.size.width, height);
// ────────────────────────────────────────────────────────────────────────────────
// if weve scroled past our clear height, we should clear our notifications
// ────────────────────────────────────────────────────────────────────────────────
if (bravo >= 1) {
[self clearNotifications];
// ────────────────────────────────────────────────────────────────────────────────
// if we've scrolled past 4/5 of our pull height, and we're not already in the
// release state pop our release string with a little spring animation. we also
// invoke haptic feedback for the pop and start lowering the alpha for the
// notification view.
// ────────────────────────────────────────────────────────────────────────────────
} else if (alpha >= 0.8f) {
if (!self.isRefreshing) {
self.isRefreshing = YES;
AudioServicesPlaySystemSound(1520);
self.refreshLabel.text = self.releaseString;
[UIView animateWithDuration:0.7 delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:8 options:UIViewAnimationOptionAllowUserInteraction animations:^{
[self updateRefreshLabel];
self.refreshLabel.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
} completion:nil];
}
self.collectionView.alpha = 1 - (alpha - 0.8);
// ────────────────────────────────────────────────────────────────────────────────
// if our scroll percentage is less than 4/5 our release pull threshold, and it's
// in the refresh state, we should change it back to the pull state.
// ────────────────────────────────────────────────────────────────────────────────
} else {
if (self.isRefreshing) {
self.isRefreshing = NO;
AudioServicesPlaySystemSound(1519);
self.refreshLabel.text = self.pullString;
}
}
}
// ────────────────────────────────────────────────────────────────────────────────
// sets our refresh label with the appropriate string for its current scroll
// percentage.
// ────────────────────────────────────────────────────────────────────────────────
%new -(void)updateRefreshLabel {
NSMutableParagraphStyle *attributedStringParagraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
attributedStringParagraphStyle.alignment = NSTextAlignmentCenter;
// ────────────────────────────────────────────────────────────────────────────────
// due to tweaks such as NCLink10 breaking thing and preventing properties from
// being set, we should catch the exception and display it rather than just crashing
// ────────────────────────────────────────────────────────────────────────────────
@try {
self.refreshLabel.attributedText = [[NSAttributedString alloc] initWithString:self.refreshLabel.text
attributes:@{NSForegroundColorAttributeName:self.refreshLabel.textColor,
NSParagraphStyleAttributeName:attributedStringParagraphStyle,
NSFontAttributeName:self.refreshLabel.font}];
} @catch (NSException *error) {
CSAlertLog(@"PTC ERROR, failed to set attributedText %@", error.description);
}
}
// ────────────────────────────────────────────────────────────────────────────────
// hides the refresh label
// ────────────────────────────────────────────────────────────────────────────────
%new - (void)hideRefreshLabel {
self.refreshLabel.textColor = [UIColor clearColor];
self.refreshLabel.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0, 0);
self.refreshLabel.hidden = YES;
}
// ────────────────────────────────────────────────────────────────────────────────
// clears notifications in the appropriate manor according to what class we are in.
// ────────────────────────────────────────────────────────────────────────────────
%new - (void)clearNotifications {
if ([self class] == NSClassFromString(@"NCNotificationSectionListViewController")) {
[(NCNotificationSectionListViewController *) self sectionHeaderViewDidReceiveClearAllAction:nil];
} else if ([self class] == NSClassFromString(@"NCNotificationPriorityListViewController")) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"kPTCClear" object:nil userInfo:nil];
}
// ────────────────────────────────────────────────────────────────────────────────
// we should prepair our label to be reused now that the notification view has been
// dismissed.
// ────────────────────────────────────────────────────────────────────────────────
[self prepairForRefresh];
}
// ────────────────────────────────────────────────────────────────────────────────
// resets values back to default so our label is ready next time we need it
// ───────────────────────────────────────────────────────────────────────────���────
%new - (void)prepairForRefresh {
self.isClearing = NO;
self.isRefreshing = NO;
[self performSelector:@selector(prepairCollectionViewForReuse) withObject:nil afterDelay:0.5];
}
// ────────────────────────────────────────────────────────────────────────────────
// adds the refresh label to our notification scroll view if it doesnt already
// exitst and PTC is enabled
// ────────────────────────────────────────────────────────────────────────────────
%new - (void)addRefreshLabelIfNecessary {
if (![[PTCProvider sharedProvider] boolForKey:@"kPTCEnabled"]) return;
if (!self.refreshLabel) {
CGFloat fontSize = [[PTCProvider sharedProvider] floatForKey:@"kPTCFontSize"];
NSString *fontName = [[PTCProvider sharedProvider] stringForKey:@"kPTCFontName"];
self.refreshLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.collectionView.frame.size.width, [[PTCProvider sharedProvider] floatForKey:@"kPTCActivationHeight"])];
self.refreshLabel.textAlignment = NSTextAlignmentCenter;
self.refreshLabel.font = [UIFont loadFontWithName:fontName size:fontSize];
[self.collectionView addSubview:self.refreshLabel];
}
// ────────────────────────────────────────────────────────────────────────────────
// we need to make sure all properties are properly set after adding the label
// ────────────────────────────────────────────────────────────────────────────────
[self prepairRefreshLabelForReuse];
}
// ────────────────────────────────────────────────────────────────────────────────
// ensures all properties are setup befor we try and access any of them. this also
// ensures that the notification view will not be transparent when appearing.
// ────────────────────────────────────────────────────────────────────────────────
%new - (void)prepairRefreshLabelForReuse {
if (![[PTCProvider sharedProvider] boolForKey:@"kPTCEnabled"]) return;
CGFloat fontSize = [[PTCProvider sharedProvider] floatForKey:@"kPTCFontSize"];
NSString *fontName = [[PTCProvider sharedProvider] stringForKey:@"kPTCFontName"];
self.pullString = [[PTCProvider sharedProvider] objectForKey:@"kPTCPullString"];
self.releaseString = [[PTCProvider sharedProvider] objectForKey:@"kPTCReleaseString"];
self.refreshLabel.font = [UIFont loadFontWithName:fontName size:fontSize];
self.refreshColor = [[PTCProvider sharedProvider] colorForKey:@"kPTCFontColor"];
self.refreshLabel.text = self.pullString;
// ────────────────────────────────────────────────────────────────────────────────
// ensure that our notification view is not transparent when it comes into view
// ────────────────────────────────────────────────────────────────────────────────
[self prepairCollectionViewForReuse];
// ────────────────────────────────────────────────────────────────────────────────
// we only just added the label and no scroll should be detected yet, we can go
// ahead and hide our label until we need it
// ────────────────────────────────────────────────────────────────────────────────
[self hideRefreshLabel];
}
// ────────────────────────────────────────────────────────────────────────────────
// resets the alpha for our notification view
// ────────────────────────────────────────────────────────────────────────────────
%new - (void)prepairCollectionViewForReuse {
self.collectionView.alpha = 1;
}
%end
//
// ─── SBDASHBOARDNOTIFICATIONLISTVIEWCONTROLLER ──────────────────────────────────
//
%hook SBDashBoardNotificationListViewController
// ────────────────────────────────────────────────────────────────────────────────
// register for CLEAR notifications when we load our view, this will be resopnsible
// for clearing notifications on the lockscreen when notified
// ────────────────────────────────────────────────────────────────────────────────
- (void)viewDidLoad {
%orig;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clear) name:@"kPTCClear" object:nil];
}
// ────────────────────────────────────────────────────────────────────────────────
// we never want to leave a stray notification observer, remove it durring dealloc
// ────────────────────────────────────────────────────────────────────────────────
- (void)dealloc {
%orig;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
// ────────────────────────────────────────────────────────────────────────────────
// clears all lockscreen notifications
// ────────────────────────────────────────────────────────────────────────────────
%new - (void)clear {
[self _clearContentIncludingPersistent:YES];
}
%end