VLCPlaylistInterfaceController.m 11 KB

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