-
Notifications
You must be signed in to change notification settings - Fork 2
/
SRStore.m
54 lines (47 loc) · 2.1 KB
/
SRStore.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
//
// SRStore.m
// Soundrocket
//
// Created by Sebastian Boldt on 02.06.15.
// Copyright (c) 2015 sebastianboldt. All rights reserved.
//
#import "SRStore.h"
@implementation SRStore
+(void)storeStrackToHistory:(Track*)track{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(){
NSMutableArray * history =[NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:@"history"]];
NSMutableIndexSet * setOfIndices = [[NSMutableIndexSet alloc]init];
for (NSData * historytrack in history) {
Track * decodedhistoryTrack = [NSKeyedUnarchiver unarchiveObjectWithData:historytrack];
if ([decodedhistoryTrack.id floatValue] == [track.id floatValue]) {
[setOfIndices addIndex:[history indexOfObject:historytrack]];
}
}
[history removeObjectsAtIndexes:setOfIndices];
[history insertObject:[NSKeyedArchiver archivedDataWithRootObject:track] atIndex:0];
[[NSUserDefaults standardUserDefaults] setObject:history forKey:@"history"];
[[NSUserDefaults standardUserDefaults] synchronize];
});
}
+(void)loadHistoryWithCompletion:(void(^)(NSMutableArray*tracksOfHistory))success{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(){
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray * downloadedTracks = [[NSMutableArray alloc]init];
if ([[defaults objectForKey:@"history"]isKindOfClass:[NSMutableArray class]]){
NSMutableArray * tracksFromDefaults = [[defaults objectForKey:@"history"]mutableCopy];
for (NSData * data in tracksFromDefaults) {
Track *track = [NSKeyedUnarchiver unarchiveObjectWithData:data];
[downloadedTracks addObject:track];
}
}
if (success) {
success(downloadedTracks);
}
});
}
+(void)clearHistory {
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:@"history"];
[defaults synchronize];
}
@end