-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathCCSectionViewController.xm
407 lines (300 loc) · 12.5 KB
/
CCSectionViewController.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
//
// CCSectionViewController.xm
// CCLoader
//
// Created by Jonas Gessner on 04.01.2014.
// Copyright (c) 2014 Jonas Gessner. All rights reserved.
//
#import "CCSectionViewController.h"
#import "CCSectionView.h"
#import "ControlCenter/SBControlCenterController.h"
#import "ControlCenter/SBControlCenterViewController.h"
#import "CCLoaderSettings/SpringBoardUIServices/_SBUIWidgetViewController.h"
#import "CCLoaderSettings/BBWeeAppController-Protocol.h"
#import <objc/runtime.h>
#include <substrate.h>
@interface EKCalendarWidgetViewController : _SBUIWidgetViewController
- (void)_refreshDayView;
@end
@interface StocksWeeAppController : _SBUIWidgetViewController
@end
#define iPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define selfView ((CCSectionView *)self.view)
#define selfSection self._CCLoader_section
#define selfBundleType self._CCLoader_bundleType
@interface CCSectionViewController ()
- (void)_CCLoader_setBundle:(NSBundle *)bundle;
- (void)_CCLoader_setSection:(_SBUIWidgetViewController <CCSection, BBWeeAppController> *)section;
- (_SBUIWidgetViewController <CCSection, BBWeeAppController> *)_CCLoader_section;
- (CCBundleType)_CCLoader_bundleType;
- (void)showViewController:(UIViewController *)vc animated:(BOOL)animated modalPresentationStyle:(UIModalPresentationStyle)style completion:(void (^)(void))completion;
@end
%subclass CCSectionViewController : SBControlCenterSectionViewController <CCSectionDelegate, _SBUIWidgetHost>
%new
- (id)initWithCCLoaderBundle:(NSBundle *)bundle type:(CCBundleType)type {
self = [self init];
if (self) {
objc_setAssociatedObject(self, @selector(bundleType), @(type), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_CCLoader_controlCenterWillAppear) name:@"CCLoaderCCWillAppearNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_CCLoader_controlCenterDidAppear) name:@"CCLoaderCCDidAppearNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_CCLoader_controlCenterWillDisappear) name:@"CCLoaderCCWillDisappearNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_CCLoader_controlCenterDidDisappear) name:@"CCLoaderCCDidDisappearNotification" object:nil];
[self _CCLoader_setBundle:bundle];
Class principalClass = Nil;
if (type == CCBundleTypeWeeApp) {
principalClass = [bundle classNamed:[[[bundle objectForInfoDictionaryKey:@"SBUIWidgetViewControllers"] allValues] lastObject]];
}
else {
principalClass = [bundle principalClass];
}
_SBUIWidgetViewController <CCSection, BBWeeAppController> *section = [[principalClass alloc] init];
if (type == CCBundleTypeWeeApp) {
[section setWidgetHost:self];
}
else if (type == CCBundleTypeDefault && [selfSection respondsToSelector:@selector(setDelegate:)]) {
[section setDelegate:self];
}
[self _CCLoader_setSection:section];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"CCLoaderCCWillAppearNotification" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"CCLoaderCCDidAppearNotification" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"CCLoaderCCWillDisappearNotification" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"CCLoaderCCDidDisappearNotification" object:nil];
if (selfBundleType == CCBundleTypeWeeApp) {
[selfSection setWidgetHost:nil];
}
else if (selfBundleType == CCBundleTypeDefault && [selfSection respondsToSelector:@selector(setDelegate:)]) {
[selfSection setDelegate:nil];
}
[selfView _CCLoader_setContentView:nil];
[selfSection release];
[self _CCLoader_setSection:nil];
[self.view release];
self.view = nil;
if (selfBundleType == CCBundleTypeDefault) {
[self._CCLoader_bundle unload];
}
[self _CCLoader_setBundle:nil];
[self _CCLoader_setReplacingSectionViewController:nil];
%orig;
}
%new
- (void)showViewController:(UIViewController *)vc animated:(BOOL)animated modalPresentationStyle:(UIModalPresentationStyle)style completion:(void (^)(void))completion {
UIViewController *viewController = MSHookIvar<UIViewController *>([%c(SBControlCenterController) sharedInstance], "_viewController");
[viewController setModalPresentationStyle:style];
[viewController presentViewController:vc animated:animated completion:completion];
}
#pragma mark - _SBUIWidgetHost
%new
- (void)invalidatePreferredViewSize {
[[NSNotificationCenter defaultCenter] postNotificationName:@"CCLoaderReloadControlCenterHeight" object:nil];
}
%new
- (void)requestLaunchOfURL:(NSURL *)url {
[[UIApplication sharedApplication] openURL:url];
}
%new
- (void)requestPresentationOfViewController:(UIViewController *)present presentationStyle:(UIModalPresentationStyle)presentationStyle context:(NSDictionary *)info completion:(void (^)(void))completion {
[self showViewController:present animated:YES modalPresentationStyle:presentationStyle completion:completion];
}
#pragma mark - CCSectionDelegate
%new
- (void)showViewController:(UIViewController *)vc animated:(BOOL)animated completion:(void (^)(void))completion {
[self showViewController:vc animated:animated modalPresentationStyle:UIModalPresentationFullScreen completion:completion];
}
%new
- (void)updateStatusText:(NSString *)text {
[self.delegate section:self updateStatusText:text reason:@"de.j-gessner.ccloader.updatestatustext"];
}
%new
- (void)requestControlCenterDismissal {
if (!self.view.superview) {
NSLog(@"[CCLoader] ERROR: %@ was called too early", NSStringFromSelector(_cmd));
}
else {
[self.delegate sectionWantsControlCenterDismissal:self];
}
}
%new
- (void)sectionHeightChanged {
[[NSNotificationCenter defaultCenter] postNotificationName:@"CCLoaderReloadControlCenterHeight" object:nil];
}
#pragma mark -
%new
- (CCBundleType)_CCLoader_bundleType {
return (CCBundleType)[objc_getAssociatedObject(self, @selector(bundleType)) unsignedIntegerValue];
}
%new
- (void)_CCLoader_setReplacingSectionViewController:(SBControlCenterSectionViewController *)controller {
objc_setAssociatedObject(self, @selector(replacingSection), controller, OBJC_ASSOCIATION_ASSIGN);
}
%new
- (SBControlCenterSectionViewController *)_CCLoader_replacingSectionViewController {
return objc_getAssociatedObject(self, @selector(replacingSection));
}
%new
- (void)_CCLoader_setBundle:(NSBundle *)_bundle {
objc_setAssociatedObject(self, @selector(bundle), _bundle, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
%new
- (NSBundle *)_CCLoader_bundle {
return objc_getAssociatedObject(self, @selector(bundle));
}
%new
- (void)_CCLoader_setSection:(id <CCSection>)_section {
objc_setAssociatedObject(self, @selector(section), _section, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
%new
- (id <CCSection>)_CCLoader_section {
return objc_getAssociatedObject(self, @selector(section));
}
- (void)loadView {
CCSectionView *view = [[%c(CCSectionView) alloc] init];
if (selfBundleType == CCBundleTypeDefault) {
[view _CCLoader_setContentView:selfSection.view];
}
else if (selfBundleType == CCBundleTypeWeeApp) {
[self addChildViewController:selfSection];
[view _CCLoader_setContentView:selfSection.view];
[selfSection didMoveToParentViewController:self];
}
self.view = view;
}
%new
- (void)_CCLoader_controlCenterWillAppear {
if (selfBundleType == CCBundleTypeDefault && [selfSection respondsToSelector:@selector(controlCenterWillAppear)]) {
[selfSection controlCenterWillAppear];
}
else if (selfBundleType == CCBundleTypeBBWeeApp) {
if ([selfSection respondsToSelector:@selector(loadView)]) {
[selfSection loadView];
}
if ([selfSection respondsToSelector:@selector(loadPlaceholderView)]) {
[selfSection loadPlaceholderView];
}
if ([selfSection respondsToSelector:@selector(viewWillAppear)]) {
[selfSection viewWillAppear];
}
UIView *contentView = selfSection.view;
for (UIView *sub in contentView.subviews) {
if ([sub isKindOfClass:[UIImageView class]]) {
[sub removeFromSuperview];
break;
}
}
[selfView _CCLoader_setContentView:contentView];
}
else if (selfBundleType == CCBundleTypeWeeApp) {
[selfSection hostWillPresent];
}
}
%new
- (void)_CCLoader_controlCenterDidAppear {
if (selfBundleType == CCBundleTypeBBWeeApp) {
if ([selfSection respondsToSelector:@selector(loadFullView)]) {
[selfSection loadFullView];
}
if ([selfSection respondsToSelector:@selector(viewDidAppear)]) {
[selfSection viewDidAppear];
}
}
else if (selfBundleType == CCBundleTypeWeeApp) {
[selfSection hostDidPresent];
//For some reason this needs to be here or the Calendar Widget won't be displayed correctly after the first dismissal?
if ([selfSection isKindOfClass:%c(EKCalendarWidgetViewController)] && [selfSection respondsToSelector:@selector(_refreshDayView)]) {
[(EKCalendarWidgetViewController *)selfSection _refreshDayView];
}
}
}
%new
- (void)_CCLoader_controlCenterWillDisappear {
}
%new
- (void)_CCLoader_controlCenterDidDisappear {
if (selfBundleType == CCBundleTypeDefault && [selfSection respondsToSelector:@selector(controlCenterDidDisappear)]) {
[selfSection controlCenterDidDisappear];
}
else if (selfBundleType == CCBundleTypeBBWeeApp) {
if ([selfSection respondsToSelector:@selector(unloadView)]) {
[selfSection unloadView];
}
if ([selfSection respondsToSelector:@selector(viewWillDisappear)]) {
[selfSection viewWillDisappear];
}
if ([selfSection respondsToSelector:@selector(viewDidDisappear)]) {
[selfSection viewDidDisappear];
}
[selfView _CCLoader_setContentView:nil];
}
else if (selfBundleType == CCBundleTypeWeeApp) {
[selfSection hostWillDismiss];
BOOL stocksWeeApp = ([selfSection isKindOfClass:%c(StocksWeeAppController)]);
if (stocksWeeApp) {
[selfSection setWidgetHost:nil];
}
[selfSection hostDidDismiss];
if (stocksWeeApp) {
[selfSection setWidgetHost:self];
}
}
}
- (NSString *)sectionIdentifier {
return [self._CCLoader_bundle objectForInfoDictionaryKey:@"CFBundleIdentifier"];
}
%new
- (CGFloat)_CCLoader_height {
if (selfBundleType == CCBundleTypeDefault) {
return [selfSection sectionHeight];
}
else if (selfBundleType == CCBundleTypeBBWeeApp) {
if ([selfSection respondsToSelector:@selector(viewHeight)]) {
return [selfSection viewHeight];
}
else {
return 80.0f;
}
}
else if (selfBundleType == CCBundleTypeWeeApp) {
return [selfSection preferredViewSize].height;
}
else {
return 0.0f;
}
}
- (BOOL)enabledForOrientation:(UIInterfaceOrientation)orientation {
return YES;
}
- (CGSize)contentSizeForOrientation:(UIInterfaceOrientation)orientation {
SBControlCenterSectionViewController *replacingSection = self._CCLoader_replacingSectionViewController;
CGFloat height = self._CCLoader_height;
if (replacingSection) {
if (height != CGFLOAT_MIN && !UIInterfaceOrientationIsLandscape(orientation) && !iPad) {
return CGSizeMake(CGFLOAT_MAX, height);
}
else {
// NSLog(@"RETURNING SIZE %@", NSStringFromCGSize([replacingSection contentSizeForOrientation:orientation]));
return [replacingSection contentSizeForOrientation:orientation];
}
}
else {
return CGSizeMake(CGFLOAT_MAX, height);
}
}
- (NSUInteger)hash {
return [self.sectionIdentifier hash];
}
- (BOOL)isEqual:(id)other {
if ([other isKindOfClass:%c(SBControlCenterSectionViewController)] && [other hash] == self.hash) {
return YES;
}
return NO;
}
%end
%ctor {
@autoreleasepool {
%init();
}
}