aboutsummaryrefslogtreecommitdiff
path: root/VKPC/PlaylistTableController.m
blob: 71a2cff276e5cf54e6d27d013058038f342257f3 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//
//  PlaylistTableController.m
//  VKPC
//
//  Created by Eugene on 12/1/13.
//  Copyright (c) 2013-2014 Eugene Z. All rights reserved.
//

#import "PopoverController.h"
#import "PlaylistTableController.h"
#import "PlaylistTableCellView.h"
#import "PlaylistTableView.h"
#import "Queue.h"
#import "QueueControllerProtocol.h"
#import "Playlist.h"
#import "Controller.h"
#import "Playlist.h"

static NSString * const kTitleKey = @"title";
static NSString * const kArtistKey = @"artist";
static NSString * const kPlayImageKey = @"playImage";
static NSString * const kDurationKey = @"duration";
static NSString * const kIdKey = @"id";

static Playlist *prePlaylist = nil;
//static const int kRowHeight = 51;

@implementation PlaylistTableController {
    BOOL haveTrackForNextPlaylist;
    PlaylistTableView *playlistTableView;
    NSArrayController *playlistArrayController;
    NSView *placeholderView;
    Queue *setTracksQueue;
    NSMutableDictionary *trackForNextPlaylist;
}

- (id)init {
    if (self = [super init]) {
        _inited = NO;
        haveTrackForNextPlaylist = NO;
        
        trackForNextPlaylist = [[NSMutableDictionary alloc] init];
        
        setTracksQueue = [[Queue alloc] init];
        [setTracksQueue setHandler:self];
        
        _playlist = [[Playlist alloc] init];
        
        playlistTableView = [PopoverController shared].playlistTableView;
        playlistArrayController = [PopoverController shared].playlistArrayController;
        placeholderView = [PopoverController shared].customView;
        //    popoverController = [PopoverController shared];
        
        // set some variables
        //    playlistTableView = _playlistTableView;
        //    playlistArrayController = controller;
        //    placeholderView = view;
        //    popoverController = _popoverController;
        
        // init some objects ..
        NSScrollView *scrollView = [[PopoverController shared] scrollView];
        [[scrollView contentView] setPostsBoundsChangedNotifications: YES];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(viewDidScroll:) name:NSViewBoundsDidChangeNotification object:nil];
        
        playlistTableView.controller = self;
        //    [playlistTableView setController:self];
        [playlistTableView setDataSource:self];
        [playlistTableView setDelegate:self];
        [playlistTableView numberOfRows];
        
        [playlistArrayController addObject:[_playlist tracks]];
        
        if (prePlaylist != nil) {
            [setTracksQueue addTask:prePlaylist];
        } else {
            [self playlistUpdated];
        }
        _inited = YES;

    }
    return self;
}


//- (void)initController:(PlaylistTableView *)_playlistTableView withArrayController:(NSArrayController *)controller placeholderView:(NSView *)view withPopoverController:(PopoverController *)_popoverController {
//    }

//- (BOOL)inited {
//    return inited;
//}

//- (void)dealloc {
//    [super dealloc];
//}

/** Playlist related methods **/

//- (Playlist *)playlist {
//    return playlist;
//}

- (void)setPlaylistDataWithTracks:(NSArray *)tracks title:(NSString *)title id:(NSInteger)_id activeId:(NSString *)activeId activePlaying:(BOOL)activePlaying browser:(NSString *)browser {
    for (int i = 0; i < [tracks count]; i++) {
        [[tracks objectAtIndex:i] setObject:VKPCGetImagesDictionary()[VKPCImageEmpty] forKey:kPlayImageKey];
    }
    Playlist *pl = [[Playlist alloc] init];
    pl.title = title;
    pl.tracks = [NSMutableArray arrayWithArray:tracks];
    pl.playlistID = _id;
    pl.browser = browser;
    
    if (![activeId isEqualToString:@""]) {
        [pl setPlayingIndex:[pl trackIndexById:activeId]];
        [pl setPlayingStatus:(activePlaying ? PlayingStatusPlaying : PlayingStatusPaused)];
    } else {
        [pl setPlayingIndex:-1];
        [pl setPlayingStatus:PlayingStatusNotPlaying];
    }
    
    [setTracksQueue addTask:pl];
}

+ (void)preSetPlaylistDataWithTracks:(NSArray *)tracks title:(NSString *)title id:(NSInteger)_id activeId:(NSString *)activeId activePlaying:(BOOL)activePlaying browser:(NSString *)browser {
    if (prePlaylist == nil) {
        prePlaylist = [[Playlist alloc] init];
    }
    prePlaylist.tracks = [NSMutableArray arrayWithArray:tracks];
    prePlaylist.title = title;
    prePlaylist.browser = browser;
    prePlaylist.playlistID = _id;
    
    if (![activeId isEqualToString:@""]) {
        int index = [prePlaylist trackIndexById:activeId];
        if (index != [prePlaylist playing].index) [self showNotification:index];
        [prePlaylist setPlayingIndex:index];
        [prePlaylist setPlayingStatus:(activePlaying ? PlayingStatusPlaying : PlayingStatusPaused)];
    }  else {
        [prePlaylist setPlayingIndex:-1];
        [prePlaylist setPlayingStatus:PlayingStatusNotPlaying];
    }
}

- (void)clearPlaylist {
    if (![_playlist empty]) {
        NSLog(@"clearPlaylist(): is not empty");
        if (_inited) {
            Playlist *pl = [[Playlist alloc] init];
            [pl clear];
            
            [setTracksQueue addTask:pl];
        } else {
            [_playlist clear];
        }
    }
}

- (void)onQueueTask:(id)task forQueue:(Queue *)queue {
    if (setTracksQueue == queue) {
        [_playlist replaceWithDataFromPlaylist:task];
        
        if (haveTrackForNextPlaylist) {
            NSInteger toPlaylistId = [(NSNumber *)[trackForNextPlaylist objectForKey:@"playlistId"] intValue];
            if (toPlaylistId == _playlist.playlistID) {
                [_playlist setPlayingIndex:[_playlist trackIndexById:[trackForNextPlaylist objectForKey:@"id"]]];
                [_playlist setPlayingStatus:( [(NSString *)[trackForNextPlaylist objectForKey:@"status"] isEqualToString:@"play"] ? PlayingStatusPlaying : PlayingStatusPaused )];
            }
            [trackForNextPlaylist removeAllObjects];
            haveTrackForNextPlaylist = false;
        }
        
        [self playlistUpdated];
    }
}

- (void)playlistUpdated {
    if ([_playlist lastPlaying].index != -1 && [_playlist lastPlaying].index < [self numberOfRowsInTable]) {
        [[self getCellViewForIndex:_playlist.lastPlaying.index] setPlayingStatus:PlayingStatusNotPlaying];
    }
    
    // Update title
    [[PopoverController shared] updateTitle:_playlist.title];
    
    // Update tracks
    [playlistTableView beginUpdates];
    [playlistTableView performSelectorOnMainThread:@selector(reloadData)
                                        withObject:nil
                                     waitUntilDone:YES];
    
    NSLog(@"in playlistUpdated: dispatch_async() now");
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"<reloadData done>");
        [playlistTableView endUpdates];
        [placeholderView setHidden:(_playlist.tracks.count > 0)]; // TODO maybe just send message to popoverController?
        
        if ([_playlist playing].index != -1) {
            [self setPlayingRow:_playlist.playing.index withStatus:[_playlist playing].status];
        } else if ([_playlist lastPlaying].index != -1) {
            [self unselectRow:_playlist.lastPlaying.index];
        }
        
        [[PopoverController shared] resizeWithContentHeight:[playlistTableView getContentSize]];
        [setTracksQueue taskDone];
    });
}

- (void)setPlayingTrackById:(NSString *)_id withStatus:(PlayingStatus)status forPlaylist:(NSInteger)playlistId {
    if (playlistId != _playlist.playlistID) {
        [trackForNextPlaylist setValue:_id forKey:@"id"];
        [trackForNextPlaylist setValue:(status == PlayingStatusPlaying ? @"play" : @"pause") forKey:@"status"];
        [trackForNextPlaylist setValue:[NSNumber numberWithLong:playlistId] forKey:@"playlistId"];
        haveTrackForNextPlaylist = YES;
        return;
    }
    
    int index = [_playlist trackIndexById:_id];
    if (index != -1) {
        if ([_playlist playing].index != index) [self showNotification:index];
        
        if (_inited) {
            if (index <= [playlistTableView numberOfRows]) {
                //if ([playlist playing].index != index) [self showNotification:index];
                [self setPlayingRow:index withStatus:status];
            }
        } else {
            [_playlist setPlayingIndex:index];
            [_playlist setPlayingStatus:status];
        }
    }
}

+ (void)showNotification:(NSInteger)trackIndex {
    if (trackIndex < prePlaylist.tracks.count && [[NSUserDefaults standardUserDefaults] boolForKey:VKPCPreferencesShowNotifications] == YES) {
        NSDictionary *track = [[prePlaylist tracks] objectAtIndex:trackIndex];
        ShowNotification([track objectForKey:@"artist"], [track objectForKey:@"title"]);
    }
}

- (void)showNotification:(NSInteger)trackIndex {
    if (trackIndex < _playlist.tracks.count && [[NSUserDefaults standardUserDefaults] boolForKey:VKPCPreferencesShowNotifications] == YES) {
        NSDictionary *track = [[_playlist tracks] objectAtIndex:trackIndex];
        ShowNotification([track objectForKey:@"artist"], [track objectForKey:@"title"]);
    }
}

/** UI **/

- (void)viewDidScroll:(NSNotification *)notification {
    NSScrollView *view = [[PopoverController shared] scrollView];
    
    // Fix for retina
    if ([view contentView].bounds.origin.y != (int)[view contentView].bounds.origin.y) {
        NSPoint point = NSMakePoint([view contentView].bounds.origin.x, (int)([view contentView].bounds.origin.y + 0.5));
        [[view documentView] scrollPoint:point];
    }
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    return _playlist.tracks.count;
}

- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
            row:(NSInteger)row {
    if (row < [[_playlist tracks] count])
        return [[_playlist tracks] objectAtIndex:row];
    return nil;
}

- (NSView *)tableView:(NSTableView *)tableView
   viewForTableColumn:(NSTableColumn *)tableColumn
                  row:(NSInteger)row {
    PlaylistTableCellView *view = [playlistTableView makeViewWithIdentifier:@"VKPCCell" owner:self];
    if (view == nil) {
        NSLog(@"VIEW IS NIL");
    }
    PlayingStatus newPlayingStatus = _playlist.playing.index == row ? _playlist.playing.status : PlayingStatusNotPlaying;
        [view setPlayingStatus:newPlayingStatus];
    return view;
}

// User clicked on row
- (void)selectedRowAtIndex:(NSInteger)index {
    NSDictionary *track = _playlist.tracks[index];
    
    if (_playlist.playing.index != index) {
        [self setPlayingRow:index withStatus:PlayingStatusPlaying];
        
        [_playlist setPlayingIndex:index];
        [_playlist setPlayingStatus:PlayingStatusPlaying];
    } else if (_playlist.playing.index == index) {
        if (_playlist.playing.status == PlayingStatusPlaying) {
            [self setPlayingRow:index withStatus:PlayingStatusPaused];
            [_playlist setPlayingStatus:PlayingStatusPaused];
        } else {
            [self setPlayingRow:index withStatus:PlayingStatusPlaying];
            [_playlist setPlayingStatus:PlayingStatusPlaying];
        }
    }
    
    // TODO call script
//    [Script executeForAll:@"common" withCommand:@"operateTrack" withData:[track objectForKey:kIdKey]];
    [Controller operateTrack:track[kIdKey]];
}

- (void)setPlayingRow:(NSInteger)index withStatus:(PlayingStatus)status {
    if (index >= [self numberOfRowsInTable]) {
        return;
    }
    
    PlaylistTableCellView *cellView = [self getCellViewForIndex:index];
    
    if (_playlist.lastPlaying.index != index) {
        if (_playlist.lastPlaying.index >= 0 && _playlist.lastPlaying.index < [self numberOfRowsInTable]) {
            [[self getCellViewForIndex:_playlist.lastPlaying.index] setPlayingStatus:PlayingStatusNotPlaying];
        }
    }
    
    if (_playlist.playing.index != index) {
        if (_playlist.playing.index != -1 && _playlist.playing.index < [self numberOfRowsInTable]) {
            [[self getCellViewForIndex:_playlist.playing.index] setPlayingStatus:PlayingStatusNotPlaying];
        }
        [_playlist setPlayingIndex:index];
        [_playlist setPlayingStatus:status];
        
        [cellView setPlayingStatus:status];
    } else if (_playlist.playing.index == index) {
        [_playlist setPlayingStatus:status];
        [cellView setPlayingStatus:status];
    }
    
    [playlistTableView scrollRowToVisible:index];
}

- (void)unselectRow:(NSInteger)index {
    if (index >= [self numberOfRowsInTable]) {
        return;
    }
    
    PlaylistTableCellView *cellView = [self getCellViewForIndex:index];
    [cellView setPlayingStatus:PlayingStatusNotPlaying];
}

- (PlaylistTableCellView *)getCellViewForIndex:(NSInteger)index {
    return [playlistTableView viewAtColumn:0 row:index makeIfNecessary:YES];
}

- (int)numberOfRowsInTable {
    return (int)[playlistTableView numberOfRows];
}

@end