VLCPlaylistInterfaceController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. 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) BOOL needsUpdate;
  38. @property (nonatomic) id groupObject;
  39. @end
  40. @implementation VLCPlaylistInterfaceController
  41. - (void)awakeWithContext:(id)context {
  42. [super awakeWithContext:context];
  43. NSLog(@"%s",__PRETTY_FUNCTION__);
  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. NSLog(@"%s",__PRETTY_FUNCTION__);
  88. if (self.needsUpdate) {
  89. [self updateData];
  90. }
  91. }
  92. - (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex {
  93. id object = self.tableController.displayedObjects[rowIndex];
  94. if ([object isKindOfClass:[MLAlbum class]] || [object isKindOfClass:[MLLabel class]] || [object isKindOfClass:[MLShow class]]) {
  95. [self pushControllerWithName:@"tableViewController" context:object];
  96. [self updateUserActivity:@"org.videolan.vlc-ios.librarymode" userInfo:@{@"state" : @(self.libraryMode), @"folder":((NSManagedObject *)object).objectID.URIRepresentation} webpageURL:nil];
  97. } else {
  98. [self pushControllerWithName:@"detailInfo" context:object];
  99. }
  100. }
  101. - (IBAction)nextPagePressed {
  102. [self.tableController nextPageButtonPressed];
  103. }
  104. - (IBAction)previousPagePressed {
  105. [self.tableController previousPageButtonPressed];
  106. }
  107. - (void)setupMenuButtons {
  108. [self addMenuItemWithImageNamed:@"AllFiles" title: NSLocalizedString(@"LIBRARY_ALL_FILES", nil) action:@selector(switchToAllFiles)];
  109. [self addMenuItemWithImageNamed:@"MusicAlbums" title: NSLocalizedString(@"LIBRARY_MUSIC", nil) action:@selector(switchToMusic)];
  110. [self addMenuItemWithImageNamed:@"TVShows" title: NSLocalizedString(@"LIBRARY_SERIES", nil) action:@selector(switchToSeries)];
  111. [self addNowPlayingMenu];
  112. }
  113. - (void)switchToAllFiles{
  114. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
  115. self.libraryMode = VLCLibraryModeAllFiles;
  116. [self updateData];
  117. }
  118. - (void)switchToMusic{
  119. self.title = NSLocalizedString(@"LIBRARY_MUSIC", nil);
  120. self.libraryMode = VLCLibraryModeAllAlbums;
  121. [self updateData];
  122. }
  123. - (void)switchToSeries{
  124. self.title = NSLocalizedString(@"LIBRARY_SERIES", nil);
  125. self.libraryMode = VLCLibraryModeAllSeries;
  126. [self updateData];
  127. }
  128. #pragma mark - data handling
  129. - (void)updateData {
  130. // if not activated/visible we defer the update til activation
  131. if (self.activated) {
  132. [self performSelectorInBackground:@selector(backgroundUpdateData) withObject:nil];
  133. } else {
  134. self.needsUpdate = YES;
  135. }
  136. }
  137. - (void)backgroundUpdateData
  138. {
  139. self.tableController.objects = [self mediaArray];
  140. self.needsUpdate = NO;
  141. }
  142. - (void)configureTableRowController:(id)rowController withObject:(id)storageObject {
  143. VLCRowController *row = rowController;
  144. float playbackProgress = 0.0;
  145. if ([storageObject isKindOfClass:[MLShow class]]) {
  146. row.titleLabel.text = ((MLAlbum *)storageObject).name;
  147. } else if ([storageObject isKindOfClass:[MLShowEpisode class]]) {
  148. row.titleLabel.text = ((MLShowEpisode *)storageObject).name;
  149. } else if ([storageObject isKindOfClass:[MLLabel class]]) {
  150. row.titleLabel.text = ((MLLabel *)storageObject).name;
  151. } else if ([storageObject isKindOfClass:[MLAlbum class]]) {
  152. row.titleLabel.text = ((MLAlbum *)storageObject).name;
  153. } else if ([storageObject isKindOfClass:[MLAlbumTrack class]]) {
  154. row.titleLabel.text = ((MLAlbumTrack *)storageObject).title;
  155. } else if ([storageObject isKindOfClass:[MLFile class]]){
  156. MLFile *file = (MLFile *)storageObject;
  157. row.titleLabel.text = [file title];
  158. playbackProgress = file.lastPosition.floatValue;
  159. }
  160. BOOL noProgress = (playbackProgress == 0.0 || playbackProgress == 1.0);
  161. row.progressSeparator.hidden = noProgress;
  162. row.progressSeparator.width = floor(playbackProgress * CGRectGetWidth([WKInterfaceDevice currentDevice].screenBounds));
  163. /* FIXME: add placeholder image once designed
  164. if (backgroundImage == nil)
  165. backgroundImage = nil;
  166. */
  167. NSArray *array = @[row.group, storageObject];
  168. [self performSelectorInBackground:@selector(backgroundThumbnailSetter:) withObject:array];
  169. }
  170. - (void)backgroundThumbnailSetter:(NSArray *)array
  171. {
  172. UIImage *backgroundImage = [VLCThumbnailsCache thumbnailForManagedObject:array[1] toFitRect:_thumbnailSize shouldReplaceCache:YES];
  173. UIImage *gradient = [UIImage imageNamed:@"tableview-gradient"];
  174. CGSize newSize = backgroundImage ? backgroundImage.size : CGSizeMake(_rowWidth, 120.);
  175. UIGraphicsBeginImageContext(newSize);
  176. if (backgroundImage)
  177. [backgroundImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  178. else {
  179. [[UIColor darkGrayColor] set];
  180. UIRectFill(CGRectMake(.0, .0, newSize.width, newSize.height));
  181. }
  182. [gradient drawInRect:CGRectMake(.0, newSize.height / 2., newSize.width, newSize.height / 2.) blendMode:kCGBlendModeNormal alpha:1.];
  183. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  184. UIGraphicsEndImageContext();
  185. [array[0] setBackgroundImage:newImage];
  186. }
  187. //TODO: this code could use refactoring to be more readable
  188. - (NSMutableArray *)mediaArray {
  189. if (_libraryMode == VLCLibraryModeInGroup) {
  190. id groupObject = self.groupObject;
  191. if([groupObject isKindOfClass:[MLLabel class]]) {
  192. return [NSMutableArray arrayWithArray:[(MLLabel *)groupObject sortedFolderItems]];
  193. } else if ([groupObject isKindOfClass:[MLAlbum class]]) {
  194. return [NSMutableArray arrayWithArray:[(MLAlbum *)groupObject sortedTracks]];
  195. } else if ([groupObject isKindOfClass:[MLShow class]]){
  196. return [NSMutableArray arrayWithArray:[(MLShow *)groupObject sortedEpisodes]];
  197. } else {
  198. NSAssert(NO, @"this shouldn't have happened check the grouObjects type");
  199. }
  200. }
  201. NSMutableArray *objects = [NSMutableArray array];
  202. /* add all albums */
  203. if (_libraryMode != VLCLibraryModeAllSeries) {
  204. NSArray *rawAlbums = [MLAlbum allAlbums];
  205. for (MLAlbum *album in rawAlbums) {
  206. if (album.name.length > 0 && album.tracks.count > 1)
  207. [objects addObject:album];
  208. }
  209. }
  210. if (_libraryMode == VLCLibraryModeAllAlbums) {
  211. return objects;
  212. }
  213. /* add all shows */
  214. NSArray *rawShows = [MLShow allShows];
  215. for (MLShow *show in rawShows) {
  216. if (show.name.length > 0 && show.episodes.count > 1)
  217. [objects addObject:show];
  218. }
  219. if (_libraryMode == VLCLibraryModeAllSeries) {
  220. return objects;
  221. }
  222. /* add all folders*/
  223. NSArray *allFolders = [MLLabel allLabels];
  224. for (MLLabel *folder in allFolders)
  225. [objects addObject:folder];
  226. /* add all remaining files */
  227. NSArray *allFiles = [MLFile allFiles];
  228. for (MLFile *file in allFiles) {
  229. if (file.labels.count > 0) continue;
  230. if (!file.isShowEpisode && !file.isAlbumTrack)
  231. [objects addObject:file];
  232. else if (file.isShowEpisode) {
  233. if (file.showEpisode.show.episodes.count < 2)
  234. [objects addObject:file];
  235. /* older MediaLibraryKit versions don't send a show name in a popular
  236. * corner case. hence, we need to work-around here and force a reload
  237. * afterwards as this could lead to the 'all my shows are gone'
  238. * syndrome (see #10435, #10464, #10432 et al) */
  239. if (file.showEpisode.show.name.length == 0) {
  240. file.showEpisode.show.name = NSLocalizedString(@"UNTITLED_SHOW", nil);
  241. }
  242. } else if (file.isAlbumTrack) {
  243. if (file.albumTrack.album.tracks.count < 2)
  244. [objects addObject:file];
  245. }
  246. }
  247. return objects;
  248. }
  249. - (void)setLibraryMode:(VLCLibraryMode)libraryMode
  250. {
  251. //should also handle diving into a folder
  252. [self updateUserActivity:@"org.videolan.vlc-ios.librarymode" userInfo:@{@"state" : @(libraryMode)} webpageURL:nil];
  253. _libraryMode = libraryMode;
  254. }
  255. @end