PlaylistModel.swift 2.13 KB
Newer Older
Soomin Lee's avatar
Soomin Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*****************************************************************************
 * PlaylistModel.swift
 *
 * Copyright © 2018 VLC authors and VideoLAN
 * Copyright © 2018 Videolabs
 *
 * Authors: Soomin Lee <bubu@mikan.io>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

class PlaylistModel: MLBaseModel {
    typealias MLType = VLCMLPlaylist

    var updateView: (() -> Void)?

    var files = [VLCMLPlaylist]()

19 20
    var cellType: BaseCollectionViewCell.Type { return MovieCollectionViewCell.self }

Soomin Lee's avatar
Soomin Lee committed
21 22 23 24 25 26 27 28 29 30 31
    var medialibrary: VLCMediaLibraryManager

    var indicatorName: String = NSLocalizedString("PLAYLISTS", comment: "")

    required init(medialibrary: VLCMediaLibraryManager) {
        self.medialibrary = medialibrary
        medialibrary.addObserver(self)
        files = medialibrary.getPlaylists()
    }

    func append(_ item: VLCMLPlaylist) {
32 33 34 35 36
        for file in files {
            if file.identifier() == item.identifier() {
                return
            }
        }
Soomin Lee's avatar
Soomin Lee committed
37 38
        files.append(item)
    }
39

40 41 42 43 44 45 46 47
    func delete(_ items: [VLCMLObject]) {
        for playlist in items where playlist is VLCMLPlaylist {
            if !(medialibrary.deletePlaylist(with: playlist.identifier())) {
                assertionFailure("PlaylistModel: Failed to delete playlist: \(playlist.identifier())")
            }
        }
    }

48 49 50 51 52
    // Creates a VLCMLPlaylist appending it and updates linked view
    func create(name: String) {
        append(medialibrary.createPlaylist(with: name))
        updateView?()
    }
Soomin Lee's avatar
Soomin Lee committed
53 54 55 56 57 58 59
}

extension PlaylistModel: MediaLibraryObserver {
    func medialibrary(_ medialibrary: VLCMediaLibraryManager, didAddPlaylists playlists: [VLCMLPlaylist]) {
        playlists.forEach({ append($0) })
        updateView?()
    }
60 61 62 63 64 65 66 67 68 69

    func medialibrary(_ medialibrary: VLCMediaLibraryManager, didDeletePlaylistsWithIds playlistsIds: [NSNumber]) {
        files = files.filter() {
            for id in playlistsIds where $0.identifier() == id.int64Value {
                return false
            }
            return true
        }
        updateView?()
    }
Soomin Lee's avatar
Soomin Lee committed
70
}