-
Notifications
You must be signed in to change notification settings - Fork 42
/
FSLazySwitch.m
91 lines (77 loc) · 3.13 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
83
84
85
86
87
88
89
90
91
#import "FSLazySwitch.h"
#import "FSSwitchPanel.h"
#import "FSSwitchMainPanel.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
{
if ([bundle objectForInfoDictionaryKey:@"CFBundleExecutable"]) {
NSError *error = nil;
if (![bundle loadAndReturnError:&error]) {
NSLog(@"Flipswitch: Lazy switch with identifier '%@' was unregistered because the bundle failed to load with error: %@", switchIdentifier, error);
[[FSSwitchPanel sharedPanel] unregisterSwitchIdentifier:switchIdentifier];
return;
}
}
Class switchClass = [bundle principalClass] ?: NSClassFromString([bundle objectForInfoDictionaryKey:@"NSPrincipalClass"]);
[[self retain] autorelease];
id<FSSwitchDataSource> switchImplementation = [switchClass respondsToSelector:@selector(initWithBundle:)] ? [[switchClass alloc] initWithBundle:bundle] : [[switchClass alloc] init];
if (switchImplementation) {
[[FSSwitchPanel sharedPanel] registerDataSource:switchImplementation forSwitchIdentifier:switchIdentifier];
[switchImplementation release];
} else if ([_switchImplementations objectForKey:switchIdentifier] == self) {
[[FSSwitchPanel sharedPanel] unregisterSwitchIdentifier:switchIdentifier];
NSLog(@"Flipswitch: Lazy switch with identifier '%@' was unregistered because it failed to load!", switchIdentifier);
}
}
- (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