summaryrefslogtreecommitdiff
path: root/VKPC/Playlist.m
blob: 0b51cd400ffde9871059a9b39499c4f3badc4d77 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
//  Playlist.m
//  VKPC
//
//  Created by Evgeny on 12/4/13.
//  Copyright (c) 2013-2014 Eugene Z. All rights reserved.
//

#import "Playlist.h"

@implementation Playlist 

- (id)init {
    if (self = [super init]) {
        _tracks = [[NSMutableArray alloc] init];
        _title = [[NSString alloc] init];
        _playlistID = 0;
        _lastPlaylistID = 0;
        _lastTitle = @"";
        _lastTracksCount = 0;
        
        _lastPlaying.index = -1;
        _lastPlaying.status = PlayingStatusNotPlaying;
        
        _playing.index = -1;
        _playing.status = PlayingStatusNotPlaying;
        
        _browser = @"";
        _delegate = nil;
    }
    
    return self;
}

- (void)setTracks:(NSArray *)tracks {
    _lastTracksCount = tracks.count;
    
    [_tracks removeAllObjects];
    [_tracks addObjectsFromArray:tracks];
}

- (void)setPlaylistID:(NSInteger)playlistID {
    _lastPlaylistID = playlistID;
    _playlistID = playlistID;
    
    if (_delegate != nil) {
        [_delegate playlistIDChanged:playlistID];
    }
}

- (void)setTitle:(NSString *)title {
    _lastTitle = [NSString stringWithString:_title];
    _title = [NSString stringWithString:title];
}

// TODO fix in receiver
//- (NSString *)title {
//    return [_title isEqualToString:@""] ? [[[NSBundle mainBundle] infoDictionary] objectForKey:kCFBundleDisplayName] : _title;
//}

//- (NSString *)lastTitle {
//    return [_lastTitle isEqualToString:@""] ? [[[NSBundle mainBundle] infoDictionary] objectForKey:kCFBundleDisplayName] : _lastTitle;
//}

//- (int)lastTracksCount {
//    return lastTracksCount;
//}

//- (int)playlistId {
//    return playlistId;
//}

//- (int)lastPlaylistId {
//    return lastPlaylistId;
//}

//- (PlayingTrackStatus)playing {
//    return playing;
//}

//- (PlayingTrackStatus)lastPlaying {
//    return lastPlaying;
//}

- (void)setPlayingIndex:(NSInteger)index {
    _lastPlaying.index = _playing.index;
    _playing.index = index;
}

- (void)setPlayingStatus:(PlayingStatus)status {
    _lastPlaying.status = _playing.status;
    _playing.status = status;
}

- (void)replaceWithDataFromPlaylist:(Playlist *)pl {
    self.tracks = pl.tracks;
    self.title = pl.title;
    self.playlistID = pl.playlistID;
    self.browser = pl.browser;
    
    [self setPlayingIndex:pl.playing.index];
    [self setPlayingStatus:pl.playing.status];
}

// TODO можно хранить таблицу _id => index, обновлять ее при каждом обновлении tracks
- (int)trackIndexById:(NSString *)_id {
    for (int i = 0; i < _tracks.count; i++) {
        if ([(NSString *)((NSDictionary *)_tracks[i])[@"id"] isEqualToString:_id]) return i;
    }
    return -1;
}

- (void)clear {
    self.title = @"";
    self.browser = @"";
//    [self setTitle:@""];
//    [self setBrowser:@""];
    
    _lastTracksCount = _tracks.count;
    [_tracks removeAllObjects];
    
    self.playlistID = 0;
//    [self setId:0];
    _playing.status = PlayingStatusNotPlaying;
    _playing.index = -1;
    
    _lastPlaying.status = PlayingStatusNotPlaying;
    _lastPlaying.index = -1;
}

- (BOOL)changed {
    return _tracks.count != 0 || _lastTracksCount != _tracks.count || !([_title isEqualToString:_lastTitle]) || _playlistID != _lastPlaylistID;
}

- (BOOL)empty {
    return _tracks.count == 0 && _playlistID <= 0;
}

@end