-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVideoManager.m
85 lines (68 loc) · 2.74 KB
/
VideoManager.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
#import "VideoManager.h"
@interface VideoManager ()
@property(nonatomic, strong) NSMutableArray<NSDictionary *> *blockedVideoArray;
@end
@implementation VideoManager
+ (instancetype)sharedInstance {
static VideoManager *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ instance = [[self alloc] init]; });
return instance;
}
- (instancetype)init {
self = [super init];
if (self) {
_blockedVideoArray = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"GonerinoBlockedVideos"] mutableCopy]
?: [NSMutableArray array];
}
return self;
}
- (NSArray<NSDictionary *> *)blockedVideos {
return [self.blockedVideoArray copy];
}
- (void)addBlockedVideo:(NSString *)videoId title:(NSString *)title channel:(NSString *)channel { // Fixed: Method name
if (!videoId.length)
return;
NSDictionary *videoInfo = @{@"id": videoId, @"title": title ?: @"", @"channel": channel ?: @""};
// Check if video is already blocked
NSInteger existingIndex =
[self.blockedVideoArray indexOfObjectPassingTest:^BOOL(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
return [obj[@"id"] isEqualToString:videoId];
}];
if (existingIndex == NSNotFound) {
[self.blockedVideoArray addObject:videoInfo];
[self saveBlockedVideos];
}
}
- (void)removeBlockedVideo:(NSString *)videoId {
NSIndexSet *indexes =
[self.blockedVideoArray indexesOfObjectsPassingTest:^BOOL(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
return [obj[@"id"] isEqualToString:videoId];
}];
if (indexes.count > 0) {
[self.blockedVideoArray removeObjectsAtIndexes:indexes];
[self saveBlockedVideos];
}
}
- (BOOL)isVideoBlocked:(NSString *)videoId {
if (!videoId)
return NO;
return [self.blockedVideoArray indexOfObjectPassingTest:^BOOL(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
return [obj[@"id"] isEqualToString:videoId];
}] != NSNotFound;
}
- (void)saveBlockedVideos {
[[NSUserDefaults standardUserDefaults] setObject:self.blockedVideoArray forKey:@"GonerinoBlockedVideos"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)setBlockedVideos:(NSArray<NSDictionary *> *)videos {
// Validate the array contents
NSArray *validVideos = [videos
filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSDictionary *dict, NSDictionary *bindings) {
return [dict isKindOfClass:[NSDictionary class]] && dict[@"id"] &&
[dict[@"id"] isKindOfClass:[NSString class]] && [dict[@"id"] length] > 0;
}]];
self.blockedVideoArray = [validVideos mutableCopy];
[self saveBlockedVideos];
}
@end