VLCPlaylistInterfaceController.m 11 KB

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