AudioModel.swift 2.17 KB
Newer Older
Soomin Lee's avatar
Soomin Lee committed
1
2
3
4
5
6
7
8
9
10
11
/*****************************************************************************
 * AudioModel.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.
 *****************************************************************************/

12
class AudioModel: MLBaseModel {
Soomin Lee's avatar
Soomin Lee committed
13
14
    typealias MLType = VLCMLMedia

15
    var updateView: (() -> Void)?
Soomin Lee's avatar
Soomin Lee committed
16

17
    var files = [VLCMLMedia]()
Soomin Lee's avatar
Soomin Lee committed
18

19
20
    var medialibrary: VLCMediaLibraryManager

21
    var indicatorName: String = NSLocalizedString("SONGS", comment: "")
Soomin Lee's avatar
Soomin Lee committed
22
23

    required init(medialibrary: VLCMediaLibraryManager) {
24
        self.medialibrary = medialibrary
Soomin Lee's avatar
Soomin Lee committed
25
26
27
28
29
        medialibrary.addObserver(self)
        files = medialibrary.media(ofType: .audio)
    }

    func append(_ item: VLCMLMedia) {
Soomin Lee's avatar
Soomin Lee committed
30
31
        if !files.contains { $0 == item } {
            files.append(item)
32
        }
Soomin Lee's avatar
Soomin Lee committed
33
    }
Soomin Lee's avatar
Soomin Lee committed
34
35
36
37
38
39
40
41
42
43
44
45

    func delete(_ items: [VLCMLObject]) {
        do {
            for case let media as VLCMLMedia in items {
                try FileManager.default.removeItem(atPath: media.mainFile().mrl.path)
            }
            medialibrary.reload()
        }
        catch let error as NSError {
            assertionFailure("VideoModel: Delete failed: \(error.localizedDescription)")
        }
    }
Soomin Lee's avatar
Soomin Lee committed
46
}
47

Soomin Lee's avatar
Soomin Lee committed
48
49
50
51
52
53
54
55
56
57
// MARK: - Sort

extension AudioModel {
    func sort(by criteria: VLCMLSortingCriteria) {
        // FIXME: Currently if sorted by name, the files are sorted by filename but displaying title
        files = medialibrary.media(ofType: .audio, sortingCriteria: criteria, desc: false)
        updateView?()
    }
}

Soomin Lee's avatar
Soomin Lee committed
58
// MARK: - MediaLibraryObserver
Soomin Lee's avatar
Soomin Lee committed
59
60

extension AudioModel: MediaLibraryObserver {
61
62
    func medialibrary(_ medialibrary: VLCMediaLibraryManager, didAddAudios audios: [VLCMLMedia]) {
        audios.forEach({ append($0) })
63
        updateView?()
Soomin Lee's avatar
Soomin Lee committed
64
    }
65
66
67
68
69
70
71
72
73
74

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