-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathFSSwitchDataSource.m
164 lines (144 loc) · 6.16 KB
/
FSSwitchDataSource.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
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
#import "FSSwitchDataSource.h"
#import "FSSwitchPanel.h"
#import "FSCapability.h"
#import "NSBundle+Flipswitch.h"
#import "FSSwitchPanel+Internal.h"
#import <dlfcn.h>
@implementation NSObject (FSSwitchDataSource)
- (NSBundle *)bundleForSwitchIdentifier:(NSString *)switchIdentifier
{
return [NSBundle bundleWithIdentifier:switchIdentifier] ?: [NSBundle bundleForClass:[self class]];
}
- (FSSwitchState)stateForSwitchIdentifier:(NSString *)switchIdentifier
{
return FSSwitchStateIndeterminate;
}
- (void)applyState:(FSSwitchState)newState forSwitchIdentifier:(NSString *)switchIdentifier
{
if (newState == FSSwitchStateIndeterminate || newState != [self stateForSwitchIdentifier:switchIdentifier]) {
[(id<FSSwitchDataSource>)self applyActionForSwitchIdentifier:switchIdentifier];
}
}
- (void)applyActionForSwitchIdentifier:(NSString *)switchIdentifier
{
switch ([self stateForSwitchIdentifier:switchIdentifier]) {
case FSSwitchStateOff:
[self applyState:FSSwitchStateOn forSwitchIdentifier:switchIdentifier];
break;
case FSSwitchStateOn:
[self applyState:FSSwitchStateOff forSwitchIdentifier:switchIdentifier];
break;
case FSSwitchStateIndeterminate:
break;
}
}
- (NSString *)titleForSwitchIdentifier:(NSString *)switchIdentifier
{
return [[self bundleForSwitchIdentifier:switchIdentifier] objectForInfoDictionaryKey:@"CFBundleDisplayName"] ?: switchIdentifier;
}
- (id)glyphImageDescriptorOfState:(FSSwitchState)switchState size:(CGFloat)size scale:(CGFloat)scale forSwitchIdentifier:(NSString *)switchIdentifier
{
NSBundle *bundle = [self bundleForSwitchIdentifier:switchIdentifier];
if (!bundle)
return nil;
NSString *stateName = [@"glyph-" stringByAppendingString:NSStringFromFSSwitchState(switchState)];
NSUInteger closestSize;
closestSize = [bundle imageSizeForFlipswitchImageName:stateName closestToSize:size inDirectory:nil];
if (closestSize != NSNotFound)
return [bundle imagePathForFlipswitchImageName:stateName imageSize:closestSize preferredScale:scale controlState:UIControlStateNormal inDirectory:nil];
closestSize = [bundle imageSizeForFlipswitchImageName:@"glyph" closestToSize:size inDirectory:nil];
if (closestSize != NSNotFound)
return [bundle imagePathForFlipswitchImageName:@"glyph" imageSize:closestSize preferredScale:scale controlState:UIControlStateNormal inDirectory:nil];
return nil;
}
- (id)glyphImageDescriptorOfState:(FSSwitchState)switchState variant:(NSString *)variant size:(CGFloat)size scale:(CGFloat)scale forSwitchIdentifier:(NSString *)switchIdentifier
{
if (variant) {
NSBundle *bundle = [self bundleForSwitchIdentifier:switchIdentifier];
if (!bundle)
return nil;
NSString *stateName = [NSString stringWithFormat:@"glyph-%@-%@", variant, NSStringFromFSSwitchState(switchState)];
NSUInteger closestSize;
closestSize = [bundle imageSizeForFlipswitchImageName:stateName closestToSize:size inDirectory:nil];
if (closestSize != NSNotFound)
return [bundle imagePathForFlipswitchImageName:stateName imageSize:closestSize preferredScale:scale controlState:UIControlStateNormal inDirectory:nil];
NSString *glyphVariant = [NSString stringWithFormat:@"glyph-%@", variant];
closestSize = [bundle imageSizeForFlipswitchImageName:glyphVariant closestToSize:size inDirectory:nil];
if (closestSize != NSNotFound)
return [bundle imagePathForFlipswitchImageName:glyphVariant imageSize:closestSize preferredScale:scale controlState:UIControlStateNormal inDirectory:nil];
}
return [self glyphImageDescriptorOfState:switchState size:size scale:scale forSwitchIdentifier:switchIdentifier];
}
- (UIColor *)primaryColorForSwitchIdentifier:(NSString *)switchIdentifier
{
NSString *colorString = [[self bundleForSwitchIdentifier:switchIdentifier] objectForInfoDictionaryKey:@"primary-color"];
if (colorString) {
return FSColorWithHexString(colorString);
}
return nil;
}
- (void)switchWasRegisteredForIdentifier:(NSString *)switchIdentifier
{
}
- (void)switchWasUnregisteredForIdentifier:(NSString *)switchIdentifier
{
}
- (BOOL)hasAlternateActionForSwitchIdentifier:(NSString *)switchIdentifier
{
if ([self methodForSelector:@selector(applyAlternateActionForSwitchIdentifier:)] != [NSObject instanceMethodForSelector:@selector(applyAlternateActionForSwitchIdentifier:)])
return YES;
if ([[self bundleForSwitchIdentifier:switchIdentifier] objectForInfoDictionaryKey:@"alternate-action-url"])
return YES;
return NO;
}
- (void)applyAlternateActionForSwitchIdentifier:(NSString *)switchIdentifier
{
id urlValue = (kCFCoreFoundationVersionNumber >= 800.0 ? [[self bundleForSwitchIdentifier:switchIdentifier] objectForInfoDictionaryKey:@"alternate-action-url-7"] : nil)
?: [[self bundleForSwitchIdentifier:switchIdentifier] objectForInfoDictionaryKey:@"alternate-action-url"];
if (urlValue) {
NSURL *url = [NSURL URLWithString:[urlValue description]];
if (url) {
[[FSSwitchPanel sharedPanel] openURLAsAlternateAction:url];
}
}
}
- (BOOL)switchWithIdentifierIsEnabled:(NSString *)switchIdentifier
{
return YES;
}
- (void)beginPrewarmingForSwitchIdentifier:(NSString *)switchIdentifier
{
}
- (void)cancelPrewarmingForSwitchIdentifier:(NSString *)switchIdentifier
{
}
- (NSString *)descriptionOfState:(FSSwitchState)state forSwitchIdentifier:(NSString *)switchIdentifier
{
switch (state) {
case FSSwitchStateOn:
return @"On";
case FSSwitchStateOff:
return @"Off";
default:
return nil;
}
}
- (Class <FSSwitchSettingsViewController>)settingsViewControllerClassForSwitchIdentifier:(NSString *)switchIdentifier
{
NSBundle *bundle = [self bundleForSwitchIdentifier:switchIdentifier];
NSString *binaryPath = [bundle objectForInfoDictionaryKey:@"settings-view-controller-binary"];
if (binaryPath) {
dlopen([binaryPath UTF8String], RTLD_LAZY);
}
NSString *className = [bundle objectForInfoDictionaryKey:@"settings-view-controller-class"];
return NSClassFromString(className);
}
- (BOOL)switchWithIdentifierIsSimpleAction:(NSString *)switchIdentifier
{
id result = [[self bundleForSwitchIdentifier:switchIdentifier] objectForInfoDictionaryKey:@"is-simple-action"];
if (result) {
return [result boolValue];
}
return [self methodForSelector:@selector(stateForSwitchIdentifier:)] == [NSObject instanceMethodForSelector:@selector(stateForSwitchIdentifier:)];
}
@end