-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTXTStyleManager.m
89 lines (65 loc) · 2.85 KB
/
TXTStyleManager.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
#import "TXTStyleManager.h"
#import "TXTConstants.h"
#import "NSString+Stylize.h"
@interface NSDistributedNotificationCenter : NSNotificationCenter
@end
@implementation TXTStyleManager
+ (instancetype)sharedManager {
static TXTStyleManager *sharedManager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedManager = [[self alloc] init];
});
return sharedManager;
}
- (instancetype)init {
self = [super init];
if (self) {
NSArray *styles;
if ([[NSFileManager defaultManager] fileExistsAtPath:kUserStylesPath]) {
styles = [[NSArray alloc] initWithContentsOfFile:kUserStylesPath];
} else {
styles = [[NSArray alloc] initWithContentsOfFile:kEnabledStylesPath];
}
NSDictionary *preferences = [[NSDictionary alloc] initWithContentsOfFile:kEnabledStylesPath];
if (preferences) {
_enabledStyles = [styles filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id style, NSDictionary *bindings) {
if ([preferences objectForKey:style[@"name"]] == nil) {
return YES;
}
return [[preferences objectForKey:style[@"name"]] boolValue];
}]];
} else {
_enabledStyles = styles;
}
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(loadActiveStyle:) name:@"TextyleStyleDidChange" object:nil];
}
return self;
}
- (void)loadActiveStyle:(NSNotification *)notification {
self.activeStyle = [self styleWithName:notification.userInfo[@"ActiveStyle"]];
CFStringRef const identifier = CFBundleGetIdentifier(CFBundleGetMainBundle());
BOOL const isSpringBoard = CFEqual(identifier, CFSTR("com.apple.springboard"));
if (isSpringBoard) {
NSMutableDictionary *prefs = [NSMutableDictionary dictionary];
[prefs addEntriesFromDictionary:[NSDictionary dictionaryWithContentsOfFile:kPrefsPath]];
[prefs setObject:self.activeStyle[@"name"] forKey:@"ActiveStyle"];
[prefs writeToFile:kPrefsPath atomically:YES];
}
}
- (void)selectStyle:(NSString *)label {
NSUInteger index = [_enabledStyles indexOfObjectPassingTest:^BOOL (NSDictionary *dict, NSUInteger idx, BOOL *stop) {
return [[dict objectForKey:@"label"] isEqualToString:label];
}];
self.activeStyle = [_enabledStyles objectAtIndex:index];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"TextyleStyleDidChange" object:nil userInfo:@{@"ActiveStyle" : self.activeStyle[@"name"]}];
}
- (NSDictionary *)styleWithName:(NSString *)name {
for (NSDictionary* object in self.enabledStyles) {
if ([[object objectForKey:@"name"] isEqualToString:name]) {
return object;
}
}
return [_enabledStyles firstObject];
}
@end