-
Notifications
You must be signed in to change notification settings - Fork 1
/
WordManager.m
62 lines (51 loc) · 1.56 KB
/
WordManager.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
#import "WordManager.h"
@interface WordManager ()
@property(nonatomic, strong) NSMutableSet<NSString *> *blockedWordSet;
@end
@implementation WordManager
+ (instancetype)sharedInstance {
static WordManager *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ instance = [[self alloc] init]; });
return instance;
}
- (instancetype)init {
self = [super init];
if (self) {
_blockedWordSet = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"GonerinoBlockedWords"] mutableCopy]
?: [NSMutableSet set];
}
return self;
}
- (NSArray<NSString *> *)blockedWords {
return [self.blockedWordSet allObjects];
}
- (void)addBlockedWord:(NSString *)word {
if (word.length > 0) {
[self.blockedWordSet addObject:word];
[self saveBlockedWords];
}
}
- (void)removeBlockedWord:(NSString *)word {
if (word) {
[self.blockedWordSet removeObject:word];
[self saveBlockedWords];
}
}
- (BOOL)isWordBlocked:(NSString *)text {
for (NSString *word in self.blockedWordSet) {
if ([text.lowercaseString containsString:word.lowercaseString]) {
return YES;
}
}
return NO;
}
- (void)saveBlockedWords {
[[NSUserDefaults standardUserDefaults] setObject:[self.blockedWordSet allObjects] forKey:@"GonerinoBlockedWords"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)setBlockedWords:(NSArray<NSString *> *)words {
self.blockedWordSet = [NSMutableSet setWithArray:words];
[self saveBlockedWords];
}
@end