VLCMediaSubcategory.swift 7.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*****************************************************************************
 * VLCMediaSubcategory.swift
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2017 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Carola Nitz <caro # videolan.org>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/
import Foundation

enum VLCDataUnit {
15
    case file(VLCMLMedia)
16 17 18 19 20
    case episode(MLShowEpisode)
    case album(MLAlbum)
    case label(MLLabel)
}

Carola Nitz's avatar
Carola Nitz committed
21
class VLCMediaSubcategoryModel<T>: NSObject {
22 23
    var files: [T]
    var indicatorInfoName: String
Carola Nitz's avatar
Carola Nitz committed
24
    var changeNotificationName: Notification.Name
25 26 27 28 29 30 31 32 33 34 35 36 37 38
    var includesFunc: (VLCDataUnit) -> Bool
    var appendFunc: (VLCDataUnit) -> Void

    var indicatorInfo: IndicatorInfo {
        return IndicatorInfo(title: indicatorInfoName)
    }

    init(files: [T],
         indicatorInfoName: String,
         notificationName: Notification.Name,
         includesFunc: @escaping (VLCDataUnit) -> Bool,
         appendFunc: @escaping (VLCDataUnit) -> Void) {
        self.files = files
        self.indicatorInfoName = indicatorInfoName
Carola Nitz's avatar
Carola Nitz committed
39
        self.changeNotificationName = notificationName
40 41 42 43 44 45
        self.includesFunc = includesFunc
        self.appendFunc = appendFunc
    }
}

struct VLCMediaSubcategories {
46
    static var movies = VLCMediaSubcategory<VLCMLMedia>(
47
        files: {
48 49 50 51 52 53 54 55
//            (MLFile.allFiles() as! [MLFile]).filter {
//            ($0 as MLFile).isKind(ofType: kMLFileTypeMovie) ||
//                ($0 as MLFile).isKind(ofType: kMLFileTypeTVShowEpisode) ||
//                ($0 as MLFile).isKind(ofType: kMLFileTypeClip)
//            }s

        //medialibrary.media(ofType: .video)
            VLCMediaLibraryManager.media(ofType: .video)
56 57
        }(),
        indicatorInfoName: NSLocalizedString("MOVIES", comment: ""),
58
        notificationName: .VLCVideosDidChangeNotification,
59 60
        includesFunc: { (dataUnit: VLCDataUnit) in
            if case .file(let f) = dataUnit {
61
//                return f.isMovie()
62 63 64 65 66 67 68
            }
            return false
        },
        appendFunc: { (dataUnit: VLCDataUnit) in

        })

Carola Nitz's avatar
Carola Nitz committed
69
    static var episodes = VLCMediaSubcategoryModel<MLShowEpisode>(
70 71 72 73 74 75 76 77 78 79 80 81 82
        files: MLShowEpisode.allEpisodes() as! [MLShowEpisode],
        indicatorInfoName: NSLocalizedString("EPISODES", comment: ""),
        notificationName: .VLCEpisodesDidChangeNotification,
        includesFunc: { (dataUnit: VLCDataUnit) in
            if case .episode(let f) = dataUnit {
                return true
            }
            return false
        },
        appendFunc: { (dataUnit: VLCDataUnit) in

        })

Carola Nitz's avatar
Carola Nitz committed
83
    static var artists = VLCMediaSubcategoryModel<String>(
84 85 86 87 88 89 90 91
        files: {
            let tracksWithArtist = (MLAlbumTrack.allTracks() as! [MLAlbumTrack]).filter { $0.artist != nil && $0.artist != "" }
            return tracksWithArtist.map { $0.artist } as! [String]
        }(),
        indicatorInfoName: NSLocalizedString("ARTISTS", comment: ""),
        notificationName: .VLCArtistsDidChangeNotification,
        includesFunc: { (dataUnit: VLCDataUnit) in
            if case .file(let f) = dataUnit {
92 93
//                return f.artist != nil
                return true
94 95 96 97 98 99 100
            }
            return false
        },
        appendFunc: { (dataUnit: VLCDataUnit) in

        })

Carola Nitz's avatar
Carola Nitz committed
101
    static var albums = VLCMediaSubcategoryModel<MLAlbum>(
102 103 104 105 106 107 108 109 110 111 112 113 114
        files: MLAlbum.allAlbums() as! [MLAlbum],
        indicatorInfoName: NSLocalizedString("ALBUMS", comment: ""),
        notificationName: .VLCAlbumsDidChangeNotification,
        includesFunc: { (dataUnit: VLCDataUnit) in
            if case .album(let f) = dataUnit {
                return true
            }
            return false
        },
        appendFunc: { (dataUnit: VLCDataUnit) in

        })

Carola Nitz's avatar
Carola Nitz committed
115
    static var tracks = VLCMediaSubcategoryModel<MLFile>(
116 117 118 119 120
        files: (MLFile.allFiles() as! [MLFile]).filter { $0.isSupportedAudioFile()},
        indicatorInfoName: NSLocalizedString("SONGS", comment: ""),
        notificationName: .VLCTracksDidChangeNotification,
        includesFunc: { (dataUnit: VLCDataUnit) in
            if case .file(let f) = dataUnit {
121 122
//                return f.isSupportedAudioFile()
                return true
123 124 125 126 127 128 129
            }
            return false
        },
        appendFunc: { (dataUnit: VLCDataUnit) in

        })

Carola Nitz's avatar
Carola Nitz committed
130
    static var genres = VLCMediaSubcategoryModel<String>(
131 132 133 134 135 136 137 138 139
        files: {
            let albumtracks = MLAlbumTrack.allTracks() as! [MLAlbumTrack]
            let tracksWithArtist = albumtracks.filter { $0.genre != nil && $0.genre != "" }
            return tracksWithArtist.map { $0.genre }
        }(),
        indicatorInfoName: NSLocalizedString("GENRES", comment: ""),
        notificationName: .VLCGenresDidChangeNotification ,
        includesFunc: { (dataUnit: VLCDataUnit) in
            if case .file(let f) = dataUnit {
140 141
//                return f.genre != nil
                return true
142 143 144 145 146 147 148
            }
            return false
        },
        appendFunc: { (dataUnit: VLCDataUnit) in

        })

Carola Nitz's avatar
Carola Nitz committed
149
    static var audioPlaylists = VLCMediaSubcategoryModel<MLLabel>(
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
        files: {
            let labels = MLLabel.allLabels() as! [MLLabel]
            let audioPlaylist = labels.filter {
                let audioFiles = $0.files.filter {
                    if let file = $0 as? MLFile {
                        return file.isSupportedAudioFile()
                    }
                    return false
                }
                return !audioFiles.isEmpty
            }
            return audioPlaylist
        }(),
        indicatorInfoName: NSLocalizedString("AUDIO_PLAYLISTS", comment: ""),
        notificationName: .VLCAudioPlaylistsDidChangeNotification ,
        includesFunc: { (dataUnit: VLCDataUnit) in
            if case .label(let l) = dataUnit {
                let audioFiles = l.files.filter {
                    if let file = $0 as? MLFile {
                        return file.isSupportedAudioFile()
                    } else {
                        return false
                    }
                }
                return !audioFiles.isEmpty
            }
            return false
        },
        appendFunc: { (dataUnit: VLCDataUnit) in

        })

Carola Nitz's avatar
Carola Nitz committed
182
    static var videoPlaylists = VLCMediaSubcategoryModel<MLLabel>(
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
        files: {
            let labels = MLLabel.allLabels() as! [MLLabel]
            let audioPlaylist = labels.filter {
                let audioFiles = $0.files.filter {
                    if let file = $0 as? MLFile {
                        return file.isShowEpisode() || file.isMovie() || file.isClip()
                    }
                    return false
                }
                return !audioFiles.isEmpty
            }
            return audioPlaylist
        }(),
        indicatorInfoName: NSLocalizedString("VIDEO_PLAYLISTS", comment: ""),
        notificationName: .VLCVideoPlaylistsDidChangeNotification ,
        includesFunc: { (dataUnit: VLCDataUnit) in
            if case .label(let l) = dataUnit {
                let videoFiles = l.files.filter {
                    if let file = $0 as? MLFile {
                        return file.isShowEpisode() || file.isMovie() || file.isClip()
                    } else {
                        return false
                    }
                }
                return !videoFiles.isEmpty
            }
            return false
    },
        appendFunc: { (dataUnit: VLCDataUnit) in

    })
}