VLCMediaDataSource.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*****************************************************************************
  2. * VLCMediaDataSource.swift
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2017 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <caro # videolan.org>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. import Foundation
  13. @objc enum VLCMediaCategory: Int {
  14. case unknown
  15. case audio
  16. case video
  17. }
  18. @objc enum VLCMediaSubcategory: Int {
  19. case unknown
  20. case movies
  21. case episodes
  22. case artists
  23. case albums
  24. case tracks
  25. case genres
  26. case videoPlaylists
  27. case audioPlaylists
  28. case allVideos
  29. }
  30. struct VLCMediaType {
  31. let category: VLCMediaCategory
  32. var subcategory: VLCMediaSubcategory
  33. }
  34. @objc class VLCMediaDataSource: NSObject {
  35. var foundVideos = [MLFile]()
  36. var foundAudio = [MLFile]()
  37. var movies = [MLFile]()
  38. var episodes = [MLFile]()
  39. var artists = [MLFile]()
  40. var albums = [MLFile]()
  41. var tracks = [MLFile]() // might be just foundAudio
  42. var genres = [MLFile]()
  43. var audioPlaylist = [MLFile]()
  44. var videoPlaylist = [MLFile]()
  45. var allVideos = [MLFile]() // might be just foundVideo
  46. override init() {
  47. super.init()
  48. getAllVideos()
  49. getAllAudio()
  50. }
  51. @objc func numberOfFiles(subcategory: VLCMediaSubcategory) -> Int {
  52. return array(for: subcategory).count
  53. }
  54. private func array(for subcategory: VLCMediaSubcategory ) -> [MLFile] {
  55. switch subcategory {
  56. case .unknown:
  57. preconditionFailure("No")
  58. case .movies:
  59. preconditionFailure("TODO")
  60. return movies
  61. case .episodes:
  62. preconditionFailure("TODO")
  63. return episodes
  64. case .artists:
  65. preconditionFailure("TODO")
  66. return artists
  67. case .albums:
  68. preconditionFailure("TODO")
  69. return albums
  70. case .tracks:
  71. return tracks
  72. case .genres:
  73. preconditionFailure("TODO")
  74. return genres
  75. case .audioPlaylists:
  76. preconditionFailure("TODO")
  77. return audioPlaylist
  78. case .videoPlaylists:
  79. preconditionFailure("TODO")
  80. return videoPlaylist
  81. case .allVideos:
  82. return allVideos
  83. }
  84. }
  85. @objc func object(at index: Int, subcategory: VLCMediaSubcategory) -> NSManagedObject {
  86. guard index >= 0 else {
  87. preconditionFailure("a negative value ? I don't think so!")
  88. }
  89. let categoryArray = array(for: subcategory)
  90. if index < categoryArray.count {
  91. return categoryArray[Int(index)]
  92. }
  93. preconditionFailure("index is taller than count")
  94. }
  95. func allObjects(for subcategory: VLCMediaSubcategory) -> [MLFile] {
  96. return array(for:subcategory)
  97. }
  98. internal func removeObject(at index: Int, subcategory: VLCMediaSubcategory) {
  99. guard index >= 0 else {
  100. preconditionFailure("a negative value ? I don't think so!")
  101. }
  102. var categoryArray = array(for: subcategory)
  103. if index < categoryArray.count {
  104. categoryArray.remove(at: index)
  105. }
  106. preconditionFailure("index is taller than count")
  107. }
  108. internal func insert(_ item: MLFile, at index: Int, subcategory: VLCMediaSubcategory) {
  109. guard index >= 0 else {
  110. preconditionFailure("a negative value ? I don't think so!")
  111. }
  112. var categoryArray = array(for: subcategory)
  113. if index < categoryArray.count {
  114. categoryArray.insert(item, at: index)
  115. }
  116. categoryArray.append(item)
  117. }
  118. private func getAllVideos() {
  119. let files = MLFile.allFiles() as! [MLFile]
  120. foundVideos = files.filter {
  121. ($0 as MLFile).isKind(ofType: kMLFileTypeMovie) ||
  122. ($0 as MLFile).isKind(ofType: kMLFileTypeTVShowEpisode) ||
  123. ($0 as MLFile).isKind(ofType: kMLFileTypeClip)
  124. }
  125. allVideosFromVideos()
  126. //TODO: generate video subcategories
  127. }
  128. private func getAllAudio() {
  129. let files = MLFile.allFiles() as! [MLFile]
  130. foundAudio = files.filter { $0.isSupportedAudioFile() }
  131. tracksFromAudio()
  132. //TODO: generate remaining subcategories
  133. }
  134. private func tracksFromAudio() {
  135. if tracks != foundAudio {
  136. tracks = foundAudio
  137. NotificationCenter.default.post(name: .VLCTracksDidChangeNotification, object: tracks)
  138. }
  139. }
  140. private func allVideosFromVideos() {
  141. if allVideos != foundVideos {
  142. allVideos = foundVideos
  143. NotificationCenter.default.post(name: .VLCAllVideosDidChangeNotification, object: allVideos)
  144. }
  145. }
  146. }
  147. //Todo: implement the remove
  148. // - (void)removeMediaObjectFromFolder:(NSManagedObject *)managedObject
  149. //{
  150. // NSAssert(([managedObject isKindOfClass:[MLFile class]] && ((MLFile *)managedObject).labels.count > 0), @"All media in a folder should be of type MLFile and it should be in a folder");
  151. //
  152. // if (![managedObject isKindOfClass:[MLFile class]]) return;
  153. //
  154. // MLFile *mediaFile = (MLFile *)managedObject;
  155. // [self rearrangeFolderTrackNumbersForRemovedItem:mediaFile];
  156. // mediaFile.labels = nil;
  157. // mediaFile.folderTrackNumber = nil;
  158. // }
  159. //
  160. // - (void)removeMediaObject:(NSManagedObject *)managedObject
  161. //{
  162. // if ([managedObject isKindOfClass:[MLAlbum class]]) {
  163. // MLAlbum *album = (MLAlbum *)managedObject;
  164. // NSSet *iterAlbumTrack = [NSSet setWithSet:album.tracks];
  165. //
  166. // for (MLAlbumTrack *track in iterAlbumTrack) {
  167. // NSSet *iterFiles = [NSSet setWithSet:track.files];
  168. //
  169. // for (MLFile *file in iterFiles)
  170. // [self _deleteMediaObject:file];
  171. // }
  172. // [[MLMediaLibrary sharedMediaLibrary] removeObject: album];
  173. // // delete all episodes from a show
  174. // } else if ([managedObject isKindOfClass:[MLShow class]]) {
  175. // MLShow *show = (MLShow *)managedObject;
  176. // NSSet *iterShowEpisodes = [NSSet setWithSet:show.episodes];
  177. //
  178. // for (MLShowEpisode *episode in iterShowEpisodes) {
  179. // NSSet *iterFiles = [NSSet setWithSet:episode.files];
  180. //
  181. // for (MLFile *file in iterFiles)
  182. // [self _deleteMediaObject:file];
  183. // }
  184. // [[MLMediaLibrary sharedMediaLibrary] removeObject: show];
  185. // // delete all files from an episode
  186. // } else if ([managedObject isKindOfClass:[MLShowEpisode class]]) {
  187. // MLShowEpisode *episode = (MLShowEpisode *)managedObject;
  188. // NSSet *iterFiles = [NSSet setWithSet:episode.files];
  189. //
  190. // for (MLFile *file in iterFiles)
  191. // [self _deleteMediaObject:file];
  192. // // delete all files from a track
  193. // [[MLMediaLibrary sharedMediaLibrary] removeObject: episode];
  194. // } else if ([managedObject isKindOfClass:[MLAlbumTrack class]]) {
  195. // MLAlbumTrack *track = (MLAlbumTrack *)managedObject;
  196. // NSSet *iterFiles = [NSSet setWithSet:track.files];
  197. //
  198. // for (MLFile *file in iterFiles)
  199. // [self _deleteMediaObject:file];
  200. // } else if ([managedObject isKindOfClass:[MLLabel class]]) {
  201. // MLLabel *folder = (MLLabel *)managedObject;
  202. // NSSet *iterFiles = [NSSet setWithSet:folder.files];
  203. // [folder removeFiles:folder.files];
  204. // for (MLFile *file in iterFiles)
  205. // [self _deleteMediaObject:file];
  206. // [[MLMediaLibrary sharedMediaLibrary] removeObject:folder];
  207. // }
  208. // else
  209. // [self _deleteMediaObject:(MLFile *)managedObject];
  210. // }
  211. //
  212. // - (void)_deleteMediaObject:(MLFile *)mediaObject
  213. //{
  214. // [self rearrangeFolderTrackNumbersForRemovedItem:mediaObject];
  215. //
  216. // /* stop playback if needed */
  217. // VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  218. // VLCMedia *media = [vpc currentlyPlayingMedia];
  219. // MLFile *currentlyPlayingFile = [MLFile fileForURL:media.url].firstObject;
  220. // if (currentlyPlayingFile && currentlyPlayingFile == mediaObject) {
  221. // [vpc stopPlayback];
  222. // }
  223. //
  224. // NSFileManager *fileManager = [NSFileManager defaultManager];
  225. // NSString *folderLocation = [[mediaObject.url path] stringByDeletingLastPathComponent];
  226. // NSArray *allfiles = [fileManager contentsOfDirectoryAtPath:folderLocation error:nil];
  227. // NSString *fileName = [mediaObject.path.lastPathComponent stringByDeletingPathExtension];
  228. // if (!fileName)
  229. // return;
  230. // NSIndexSet *indexSet = [allfiles indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
  231. // return ([obj rangeOfString:fileName].location != NSNotFound);
  232. // }];
  233. // NSUInteger count = indexSet.count;
  234. // NSString *additionalFilePath;
  235. // NSUInteger currentIndex = [indexSet firstIndex];
  236. // for (unsigned int x = 0; x < count; x++) {
  237. // additionalFilePath = allfiles[currentIndex];
  238. // if ([additionalFilePath isSupportedSubtitleFormat])
  239. // [fileManager removeItemAtPath:[folderLocation stringByAppendingPathComponent:additionalFilePath] error:nil];
  240. // currentIndex = [indexSet indexGreaterThanIndex:currentIndex];
  241. // }
  242. // [fileManager removeItemAtURL:mediaObject.url error:nil];
  243. // }
  244. //
  245. // - (void)rearrangeFolderTrackNumbersForRemovedItem:(MLFile *) mediaObject
  246. //{
  247. // MLLabel *label = [mediaObject.labels anyObject];
  248. // NSSet *allFiles = label.files;
  249. // for (MLFile *file in allFiles) {
  250. // if (file.folderTrackNumber > mediaObject.folderTrackNumber) {
  251. // int value = [file.folderTrackNumber intValue];
  252. // file.folderTrackNumber = [NSNumber numberWithInt:value - 1];
  253. // }
  254. // }
  255. //}
  256. //@end