forked from a3tweaks/Flipswitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSLazySwitch.m
82 lines (68 loc) · 2.67 KB
/
FSLazySwitch.m
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
#import "FSLazySwitch.h"
#import "FSSwitchPanel.h"
@implementation _FSLazySwitch
- (id)initWithBundle:(NSBundle *)_bundle
{
if ((self = [super init])) {
bundle = [_bundle retain];
}
return self;
}
- (void)dealloc
{
[bundle release];
[super dealloc];
}
- (void)lazyLoadWithSwitchIdentifier:(NSString *)switchIdentifier
{
Class switchClass = [bundle principalClass] ?: NSClassFromString([bundle objectForInfoDictionaryKey:@"NSPrincipalClass"]);
id<FSSwitchDataSource> switchImplementation = [switchClass respondsToSelector:@selector(initWithBundle:)] ? [[switchClass alloc] initWithBundle:bundle] : [[switchClass alloc] init];
if (switchImplementation) {
[[self retain] autorelease];
[[FSSwitchPanel sharedPanel] registerDataSource:switchImplementation forSwitchIdentifier:switchIdentifier];
} else {
[[FSSwitchPanel sharedPanel] unregisterSwitchIdentifier:switchIdentifier];
NSLog(@"Flipswitch: Lazy switch with identifier '%@' was unregistered because it failed to load!", switchIdentifier);
}
[switchImplementation release];
}
- (NSBundle *)bundleForSwitchIdentifier:(NSString *)switchIdentifier
{
return bundle;
}
- (FSSwitchState)stateForSwitchIdentifier:(NSString *)switchIdentifier
{
[self lazyLoadWithSwitchIdentifier:switchIdentifier];
return [[FSSwitchPanel sharedPanel] stateForSwitchIdentifier:switchIdentifier];
}
- (void)applyState:(FSSwitchState)newState forSwitchIdentifier:(NSString *)switchIdentifier
{
[self lazyLoadWithSwitchIdentifier:switchIdentifier];
[[FSSwitchPanel sharedPanel] setState:newState forSwitchIdentifier:switchIdentifier];
}
- (void)applyActionForSwitchIdentifier:(NSString *)switchIdentifier
{
[self lazyLoadWithSwitchIdentifier:switchIdentifier];
[[FSSwitchPanel sharedPanel] applyActionForSwitchIdentifier:switchIdentifier];
}
- (BOOL)hasAlternateActionForSwitchIdentifier:(NSString *)switchIdentifier
{
[self lazyLoadWithSwitchIdentifier:switchIdentifier];
return [[FSSwitchPanel sharedPanel] hasAlternateActionForSwitchIdentifier:switchIdentifier];
}
- (void)applyAlternateActionForSwitchIdentifier:(NSString *)switchIdentifier
{
[self lazyLoadWithSwitchIdentifier:switchIdentifier];
[[FSSwitchPanel sharedPanel] applyAlternateActionForSwitchIdentifier:switchIdentifier];
}
- (Class <FSSwitchSettingsViewController>)settingsViewControllerClassForSwitchIdentifier:(NSString *)switchIdentifier
{
NSString *className = [[self bundleForSwitchIdentifier:switchIdentifier] objectForInfoDictionaryKey:@"settings-view-controller-class"];
if (className) {
[self lazyLoadWithSwitchIdentifier:switchIdentifier];
return [[FSSwitchPanel sharedPanel] settingsViewControllerClassForSwitchIdentifier:switchIdentifier];
} else {
return nil;
}
}
@end