VLCPlaylistInterfaceController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "VLCThumbnailsCache.h"
  21. #import "WKInterfaceObject+VLCProgress.h"
  22. #import "NSManagedObjectContext+refreshAll.h"
  23. static NSString *const rowType = @"mediaRow";
  24. static NSString *const VLCDBUpdateNotification = @"VLCUpdateDataBase";
  25. static NSString *const VLCDBUpdateNotificationRemote = @"org.videolan.ios-app.dbupdate";
  26. typedef enum {
  27. VLCLibraryModeAllFiles = 0,
  28. VLCLibraryModeAllAlbums = 1,
  29. VLCLibraryModeAllSeries = 2,
  30. VLCLibraryModeInGroup = 3
  31. } VLCLibraryMode;
  32. @interface VLCPlaylistInterfaceController()
  33. {
  34. CGRect _thumbnailSize;
  35. CGFloat _rowWidth;
  36. }
  37. @property (nonatomic, strong) VLCWatchTableController *tableController;
  38. @property (nonatomic) VLCLibraryMode libraryMode;
  39. @property (nonatomic) id groupObject;
  40. @end
  41. @implementation VLCPlaylistInterfaceController
  42. - (void)awakeWithContext:(id)context {
  43. [super awakeWithContext:context];
  44. WKInterfaceDevice *currentDevice = WKInterfaceDevice.currentDevice;
  45. CGRect screenRect = currentDevice.screenBounds;
  46. CGFloat screenScale = currentDevice.screenScale;
  47. currentDevice = nil;
  48. _thumbnailSize = (CGRect){CGPointZero, CGSizeMake(screenRect.size.width * screenScale, 120. * screenScale)};
  49. _rowWidth = screenRect.size.width * screenScale;
  50. NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.org.videolan.vlc-ios"];
  51. MLMediaLibrary *mediaLibrary = [MLMediaLibrary sharedMediaLibrary];
  52. mediaLibrary.libraryBasePath = groupURL.path;
  53. mediaLibrary.additionalPersitentStoreOptions = @{NSReadOnlyPersistentStoreOption : @YES};
  54. if (context == nil) {
  55. self.libraryMode = VLCLibraryModeAllFiles;
  56. [self setupMenuButtons];
  57. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
  58. self.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", nil);
  59. } else {
  60. self.groupObject = context;
  61. self.title = [self.groupObject name];
  62. self.libraryMode = VLCLibraryModeInGroup;
  63. }
  64. [[VLCNotificationRelay sharedRelay] addRelayRemoteName:VLCDBUpdateNotificationRemote toLocalName:VLCDBUpdateNotification];
  65. /* setup table view controller */
  66. VLCWatchTableController *tableController = [[VLCWatchTableController alloc] init];
  67. tableController.table = self.table;
  68. tableController.previousPageButton = self.previousButton;
  69. tableController.nextPageButton = self.nextButton;
  70. tableController.emptyLibraryInterfaceObjects = self.emptyLibraryGroup;
  71. tableController.pageSize = 5;
  72. tableController.rowType = rowType;
  73. __weak typeof(self) weakSelf = self;
  74. tableController.configureRowControllerWithObjectBlock = ^(id controller, id object) {
  75. [weakSelf configureTableRowController:controller withObject:object];
  76. };
  77. self.tableController = tableController;
  78. [self updateData];
  79. }
  80. - (void)willActivate {
  81. // This method is called when watch view controller is about to be visible to user
  82. [super willActivate];
  83. }
  84. - (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex {
  85. id object = self.tableController.displayedObjects[rowIndex];
  86. if ([object isKindOfClass:[MLAlbum class]] || [object isKindOfClass:[MLLabel class]] || [object isKindOfClass:[MLShow class]]) {
  87. [self pushControllerWithName:@"tableViewController" context:object];
  88. [self updateUserActivity:@"org.videolan.vlc-ios.librarymode" userInfo:@{@"state" : @(self.libraryMode), @"folder":((NSManagedObject *)object).objectID.URIRepresentation} webpageURL:nil];
  89. } else {
  90. [self pushControllerWithName:@"detailInfo" context:object];
  91. }
  92. }
  93. - (IBAction)nextPagePressed {
  94. [self.tableController nextPageButtonPressed];
  95. }
  96. - (IBAction)previousPagePressed {
  97. [self.tableController previousPageButtonPressed];
  98. }
  99. - (void)setupMenuButtons {
  100. [self addMenuItemWithImageNamed:@"AllFiles" title: NSLocalizedString(@"LIBRARY_ALL_FILES", nil) action:@selector(switchToAllFiles)];
  101. [self addMenuItemWithImageNamed:@"MusicAlbums" title: NSLocalizedString(@"LIBRARY_MUSIC", nil) action:@selector(switchToMusic)];
  102. [self addMenuItemWithImageNamed:@"TVShows" title: NSLocalizedString(@"LIBRARY_SERIES", nil) action:@selector(switchToSeries)];
  103. [self addNowPlayingMenu];
  104. }
  105. - (void)switchToAllFiles{
  106. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
  107. self.libraryMode = VLCLibraryModeAllFiles;
  108. [self updateData];
  109. }
  110. - (void)switchToMusic{
  111. self.title = NSLocalizedString(@"LIBRARY_MUSIC", nil);
  112. self.libraryMode = VLCLibraryModeAllAlbums;
  113. [self updateData];
  114. }
  115. - (void)switchToSeries{
  116. self.title = NSLocalizedString(@"LIBRARY_SERIES", nil);
  117. self.libraryMode = VLCLibraryModeAllSeries;
  118. [self updateData];
  119. }
  120. #pragma mark - data handling
  121. - (void)updateData {
  122. [super updateData];
  123. NSManagedObjectContext *moc = [(NSManagedObject *)self.tableController.objects.firstObject managedObjectContext];
  124. [moc vlc_refreshAllObjectsMerge:NO];
  125. self.tableController.objects = [self mediaArray];
  126. }
  127. - (void)configureTableRowController:(id)rowController withObject:(id)storageObject {
  128. VLCRowController *row = rowController;
  129. float playbackProgress = 0.0;
  130. if ([storageObject isKindOfClass:[MLShow class]]) {
  131. row.titleLabel.text = ((MLAlbum *)storageObject).name;
  132. } else if ([storageObject isKindOfClass:[MLShowEpisode class]]) {
  133. row.titleLabel.text = ((MLShowEpisode *)storageObject).name;
  134. } else if ([storageObject isKindOfClass:[MLLabel class]]) {
  135. row.titleLabel.text = ((MLLabel *)storageObject).name;
  136. } else if ([storageObject isKindOfClass:[MLAlbum class]]) {
  137. row.titleLabel.text = ((MLAlbum *)storageObject).name;
  138. } else if ([storageObject isKindOfClass:[MLAlbumTrack class]]) {
  139. row.titleLabel.text = ((MLAlbumTrack *)storageObject).title;
  140. } else if ([storageObject isKindOfClass:[MLFile class]]){
  141. MLFile *file = (MLFile *)storageObject;
  142. row.titleLabel.text = [file title];
  143. playbackProgress = file.lastPosition.floatValue;
  144. }
  145. [row.progressObject vlc_setProgress:playbackProgress hideForNoProgress:YES];
  146. /* FIXME: add placeholder image once designed */
  147. row.group.backgroundImage = [UIImage imageNamed:@"tableview-gradient"];
  148. NSArray *array = @[row.group, storageObject];
  149. [self performSelectorInBackground:@selector(backgroundThumbnailSetter:) withObject:array];
  150. }
  151. - (void)backgroundThumbnailSetter:(NSArray *)array
  152. {
  153. UIImage *backgroundImage = [VLCThumbnailsCache thumbnailForManagedObject:array[1] toFitRect:_thumbnailSize shouldReplaceCache:YES];
  154. UIImage *gradient = [UIImage imageNamed:@"tableview-gradient"];
  155. CGSize newSize = backgroundImage ? backgroundImage.size : CGSizeMake(_rowWidth, 120.);
  156. UIGraphicsBeginImageContext(newSize);
  157. if (backgroundImage)
  158. [backgroundImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  159. else {
  160. [[UIColor darkGrayColor] set];
  161. UIRectFill(CGRectMake(.0, .0, newSize.width, newSize.height));
  162. }
  163. [gradient drawInRect:CGRectMake(.0, newSize.height / 2., newSize.width, newSize.height / 2.) blendMode:kCGBlendModeNormal alpha:1.];
  164. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  165. UIGraphicsEndImageContext();
  166. [array.firstObject performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:newImage waitUntilDone:NO];
  167. }
  168. //TODO: this code could use refactoring to be more readable
  169. - (NSMutableArray *)mediaArray {
  170. if (_libraryMode == VLCLibraryModeInGroup) {
  171. id groupObject = self.groupObject;
  172. if([groupObject isKindOfClass:[MLLabel class]]) {
  173. return [NSMutableArray arrayWithArray:[(MLLabel *)groupObject sortedFolderItems]];
  174. } else if ([groupObject isKindOfClass:[MLAlbum class]]) {
  175. return [NSMutableArray arrayWithArray:[(MLAlbum *)groupObject sortedTracks]];
  176. } else if ([groupObject isKindOfClass:[MLShow class]]){
  177. return [NSMutableArray arrayWithArray:[(MLShow *)groupObject sortedEpisodes]];
  178. } else {
  179. NSAssert(NO, @"this shouldn't have happened check the grouObjects type");
  180. }
  181. }
  182. NSMutableArray *objects = [NSMutableArray array];
  183. /* add all albums */
  184. if (_libraryMode != VLCLibraryModeAllSeries) {
  185. NSArray *rawAlbums = [MLAlbum allAlbums];
  186. for (MLAlbum *album in rawAlbums) {
  187. if (album.name.length > 0 && album.tracks.count > 1)
  188. [objects addObject:album];
  189. }
  190. }
  191. if (_libraryMode == VLCLibraryModeAllAlbums) {
  192. return objects;
  193. }
  194. /* add all shows */
  195. NSArray *rawShows = [MLShow allShows];
  196. for (MLShow *show in rawShows) {
  197. if (show.name.length > 0 && show.episodes.count > 1)
  198. [objects addObject:show];
  199. }
  200. if (_libraryMode == VLCLibraryModeAllSeries) {
  201. return objects;
  202. }
  203. /* add all folders*/
  204. NSArray *allFolders = [MLLabel allLabels];
  205. for (MLLabel *folder in allFolders)
  206. [objects addObject:folder];
  207. /* add all remaining files */
  208. NSArray *allFiles = [MLFile allFiles];
  209. for (MLFile *file in allFiles) {
  210. if (file.labels.count > 0) continue;
  211. if (!file.isShowEpisode && !file.isAlbumTrack)
  212. [objects addObject:file];
  213. else if (file.isShowEpisode) {
  214. if (file.showEpisode.show.episodes.count < 2)
  215. [objects addObject:file];
  216. /* older MediaLibraryKit versions don't send a show name in a popular
  217. * corner case. hence, we need to work-around here and force a reload
  218. * afterwards as this could lead to the 'all my shows are gone'
  219. * syndrome (see #10435, #10464, #10432 et al) */
  220. if (file.showEpisode.show.name.length == 0) {
  221. file.showEpisode.show.name = NSLocalizedString(@"UNTITLED_SHOW", nil);
  222. }
  223. } else if (file.isAlbumTrack) {
  224. if (file.albumTrack.album.tracks.count < 2)
  225. [objects addObject:file];
  226. }
  227. }
  228. return objects;
  229. }
  230. - (void)setLibraryMode:(VLCLibraryMode)libraryMode
  231. {
  232. //should also handle diving into a folder
  233. [self updateUserActivity:@"org.videolan.vlc-ios.librarymode" userInfo:@{@"state" : @(libraryMode)} webpageURL:nil];
  234. _libraryMode = libraryMode;
  235. }
  236. @end