VLCPlaylistInterfaceController.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*****************************************************************************
  2. * VLCPlaylistInterfaceController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  9. * Carola Nitz <caro # videolan.org>
  10. * Felix Paul Kühne <fkuehne # videolan.org>
  11. *
  12. * Refer to the COPYING file of the official project for license.
  13. *****************************************************************************/
  14. #import "VLCPlaylistInterfaceController.h"
  15. #import <MediaLibraryKit/MediaLibraryKit.h>
  16. #import "VLCRowController.h"
  17. #import <MobileVLCKit/VLCTime.h>
  18. #import "VLCNotificationRelay.h"
  19. #import "VLCWatchTableController.h"
  20. #import "NSManagedObjectContext+refreshAll.h"
  21. static NSString *const rowType = @"mediaRow";
  22. static NSString *const VLCDBUpdateNotification = @"VLCUpdateDataBase";
  23. static NSString *const VLCDBUpdateNotificationRemote = @"org.videolan.ios-app.dbupdate";
  24. typedef enum {
  25. VLCLibraryModeAllFiles = 0,
  26. VLCLibraryModeAllAlbums = 1,
  27. VLCLibraryModeAllSeries = 2,
  28. VLCLibraryModeInGroup = 3
  29. } VLCLibraryMode;
  30. @interface VLCPlaylistInterfaceController()
  31. {
  32. CGRect _thumbnailSize;
  33. CGFloat _rowWidth;
  34. }
  35. @property (nonatomic, strong) VLCWatchTableController *tableController;
  36. @property (nonatomic) VLCLibraryMode libraryMode;
  37. @property (nonatomic) id groupObject;
  38. @end
  39. @implementation VLCPlaylistInterfaceController
  40. - (void)awakeWithContext:(id)context {
  41. [super awakeWithContext:context];
  42. NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.org.videolan.vlc-ios"];
  43. MLMediaLibrary *mediaLibrary = [MLMediaLibrary sharedMediaLibrary];
  44. mediaLibrary.libraryBasePath = groupURL.path;
  45. mediaLibrary.additionalPersitentStoreOptions = @{NSReadOnlyPersistentStoreOption : @YES};
  46. if (context == nil) {
  47. self.libraryMode = VLCLibraryModeAllFiles;
  48. [self setupMenuButtons];
  49. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
  50. self.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", nil);
  51. } else {
  52. self.groupObject = context;
  53. self.title = [self.groupObject name];
  54. self.libraryMode = VLCLibraryModeInGroup;
  55. }
  56. [self addNowPlayingMenu];
  57. [[VLCNotificationRelay sharedRelay] addRelayRemoteName:VLCDBUpdateNotificationRemote toLocalName:VLCDBUpdateNotification];
  58. /* setup table view controller */
  59. VLCWatchTableController *tableController = [[VLCWatchTableController alloc] init];
  60. tableController.table = self.table;
  61. tableController.previousPageButton = self.previousButton;
  62. tableController.nextPageButton = self.nextButton;
  63. tableController.emptyLibraryInterfaceObjects = self.emptyLibraryGroup;
  64. tableController.pageSize = 5;
  65. tableController.rowType = rowType;
  66. tableController.configureRowControllerWithObjectBlock = ^(id controller, id object) {
  67. if ([controller respondsToSelector:@selector(configureWithMediaLibraryObject:)]) {
  68. [controller configureWithMediaLibraryObject:object];
  69. }
  70. };
  71. self.tableController = tableController;
  72. [self updateData];
  73. }
  74. - (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex {
  75. id object = self.tableController.displayedObjects[rowIndex];
  76. if ([object isKindOfClass:[MLAlbum class]] || [object isKindOfClass:[MLLabel class]] || [object isKindOfClass:[MLShow class]]) {
  77. [self pushControllerWithName:@"tableViewController" context:object];
  78. [self updateUserActivity:@"org.videolan.vlc-ios.librarymode" userInfo:@{@"state" : @(self.libraryMode), @"folder":((NSManagedObject *)object).objectID.URIRepresentation} webpageURL:nil];
  79. } else {
  80. [self pushControllerWithName:@"detailInfo" context:object];
  81. }
  82. }
  83. - (IBAction)nextPagePressed {
  84. [self.tableController nextPageButtonPressed];
  85. }
  86. - (IBAction)previousPagePressed {
  87. [self.tableController previousPageButtonPressed];
  88. }
  89. - (void)setupMenuButtons {
  90. [self addMenuItemWithImageNamed:@"AllFiles" title: NSLocalizedString(@"LIBRARY_ALL_FILES", nil) action:@selector(switchToAllFiles)];
  91. [self addMenuItemWithImageNamed:@"MusicAlbums" title: NSLocalizedString(@"LIBRARY_MUSIC", nil) action:@selector(switchToMusic)];
  92. [self addMenuItemWithImageNamed:@"TVShows" title: NSLocalizedString(@"LIBRARY_SERIES", nil) action:@selector(switchToSeries)];
  93. }
  94. - (void)switchToAllFiles{
  95. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
  96. self.libraryMode = VLCLibraryModeAllFiles;
  97. [self updateData];
  98. }
  99. - (void)switchToMusic{
  100. self.title = NSLocalizedString(@"LIBRARY_MUSIC", nil);
  101. self.libraryMode = VLCLibraryModeAllAlbums;
  102. [self updateData];
  103. }
  104. - (void)switchToSeries{
  105. self.title = NSLocalizedString(@"LIBRARY_SERIES", nil);
  106. self.libraryMode = VLCLibraryModeAllSeries;
  107. [self updateData];
  108. }
  109. #pragma mark - data handling
  110. - (void)updateData {
  111. [super updateData];
  112. NSManagedObjectContext *moc = [(NSManagedObject *)self.tableController.objects.firstObject managedObjectContext];
  113. [moc vlc_refreshAllObjectsMerge:NO];
  114. self.tableController.objects = [self mediaArray];
  115. }
  116. //TODO: this code could use refactoring to be more readable
  117. - (NSMutableArray *)mediaArray {
  118. if (_libraryMode == VLCLibraryModeInGroup) {
  119. id groupObject = self.groupObject;
  120. if([groupObject isKindOfClass:[MLLabel class]]) {
  121. return [NSMutableArray arrayWithArray:[(MLLabel *)groupObject sortedFolderItems]];
  122. } else if ([groupObject isKindOfClass:[MLAlbum class]]) {
  123. return [NSMutableArray arrayWithArray:[(MLAlbum *)groupObject sortedTracks]];
  124. } else if ([groupObject isKindOfClass:[MLShow class]]){
  125. return [NSMutableArray arrayWithArray:[(MLShow *)groupObject sortedEpisodes]];
  126. } else {
  127. NSAssert(NO, @"this shouldn't have happened check the grouObjects type");
  128. }
  129. }
  130. NSMutableArray *objects = [NSMutableArray array];
  131. /* add all albums */
  132. if (_libraryMode != VLCLibraryModeAllSeries) {
  133. NSArray *rawAlbums = [MLAlbum allAlbums];
  134. for (MLAlbum *album in rawAlbums) {
  135. if (album.name.length > 0 && album.tracks.count > 1)
  136. [objects addObject:album];
  137. }
  138. }
  139. if (_libraryMode == VLCLibraryModeAllAlbums) {
  140. return objects;
  141. }
  142. /* add all shows */
  143. NSArray *rawShows = [MLShow allShows];
  144. for (MLShow *show in rawShows) {
  145. if (show.name.length > 0 && show.episodes.count > 1)
  146. [objects addObject:show];
  147. }
  148. if (_libraryMode == VLCLibraryModeAllSeries) {
  149. return objects;
  150. }
  151. /* add all folders*/
  152. NSArray *allFolders = [MLLabel allLabels];
  153. for (MLLabel *folder in allFolders)
  154. [objects addObject:folder];
  155. /* add all remaining files */
  156. NSArray *allFiles = [MLFile allFiles];
  157. for (MLFile *file in allFiles) {
  158. if (file.labels.count > 0) continue;
  159. if (!file.isShowEpisode && !file.isAlbumTrack)
  160. [objects addObject:file];
  161. else if (file.isShowEpisode) {
  162. if (file.showEpisode.show.episodes.count < 2)
  163. [objects addObject:file];
  164. /* older MediaLibraryKit versions don't send a show name in a popular
  165. * corner case. hence, we need to work-around here and force a reload
  166. * afterwards as this could lead to the 'all my shows are gone'
  167. * syndrome (see #10435, #10464, #10432 et al) */
  168. if (file.showEpisode.show.name.length == 0) {
  169. file.showEpisode.show.name = NSLocalizedString(@"UNTITLED_SHOW", nil);
  170. }
  171. } else if (file.isAlbumTrack) {
  172. if (file.albumTrack.album.tracks.count < 2)
  173. [objects addObject:file];
  174. }
  175. }
  176. return objects;
  177. }
  178. - (void)setLibraryMode:(VLCLibraryMode)libraryMode
  179. {
  180. //should also handle diving into a folder
  181. [self updateUserActivity:@"org.videolan.vlc-ios.librarymode" userInfo:@{@"state" : @(libraryMode)} webpageURL:nil];
  182. _libraryMode = libraryMode;
  183. }
  184. @end