-
Notifications
You must be signed in to change notification settings - Fork 417
/
DeletedMessagesManager.m
68 lines (55 loc) · 1.94 KB
/
DeletedMessagesManager.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
#import "DeletedMessagesManager.h"
@implementation DeletedMessagesManager
NSString *const plistFileName = @"deleted_messages.plist";
+ (instancetype)sharedManager {
static DeletedMessagesManager *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (NSString *)plistPath {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
return [documentsDirectory stringByAppendingPathComponent:plistFileName];
}
- (NSMutableDictionary *)loadPlistData {
NSString *path = [self plistPath];
NSMutableDictionary *data = [NSMutableDictionary dictionaryWithContentsOfFile:path];
if (!data) {
data = [NSMutableDictionary dictionary];
}
return data;
}
- (void)savePlistData:(NSDictionary *)data {
NSString *path = [self plistPath];
[data writeToFile:path atomically:YES];
}
- (void)saveDeletedMessageWithID:(NSString *)messageID {
if (messageID.length == 0) {
NSLog(@"Message ID cannot be empty.");
return;
}
NSMutableDictionary *plistData = [self loadPlistData];
NSString *currentDate = [[NSDate date] description];
plistData[messageID] = currentDate;
[self savePlistData:plistData];
NSLog(@"Saved message ID: %@ with date: %@", messageID, currentDate);
}
- (BOOL)messageExistsWithID:(NSString *)messageID {
if (messageID.length == 0) {
NSLog(@"Message ID cannot be empty.");
return NO;
}
NSMutableDictionary *plistData = [self loadPlistData];
return plistData[messageID] != nil;
}
- (NSString *)dateForDeletedMessageWithID:(NSString *)messageID {
if (messageID.length == 0) {
NSLog(@"Message ID cannot be empty.");
return nil;
}
NSMutableDictionary *plistData = [self loadPlistData];
return plistData[messageID];
}
@end