VLCPlaylistInterfaceController.m 10 KB

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