VLCPlaylistInterfaceController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. } 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 performSelectorInBackground:@selector(backgroundUpdateData) withObject:nil];
  132. } else {
  133. self.needsUpdate = YES;
  134. }
  135. }
  136. - (void)backgroundUpdateData
  137. {
  138. self.tableController.objects = [self mediaArray];
  139. self.needsUpdate = NO;
  140. }
  141. - (void)configureTableRowController:(id)rowController withObject:(id)storageObject {
  142. VLCRowController *row = rowController;
  143. float playbackProgress = 0.0;
  144. if ([storageObject isKindOfClass:[MLShow class]]) {
  145. row.titleLabel.text = ((MLAlbum *)storageObject).name;
  146. } else if ([storageObject isKindOfClass:[MLShowEpisode class]]) {
  147. row.titleLabel.text = ((MLShowEpisode *)storageObject).name;
  148. } else if ([storageObject isKindOfClass:[MLLabel class]]) {
  149. row.titleLabel.text = ((MLLabel *)storageObject).name;
  150. } else if ([storageObject isKindOfClass:[MLAlbum class]]) {
  151. row.titleLabel.text = ((MLAlbum *)storageObject).name;
  152. } else if ([storageObject isKindOfClass:[MLAlbumTrack class]]) {
  153. row.titleLabel.text = ((MLAlbumTrack *)storageObject).title;
  154. } else if ([storageObject isKindOfClass:[MLFile class]]){
  155. MLFile *file = (MLFile *)storageObject;
  156. row.titleLabel.text = [file title];
  157. playbackProgress = file.lastPosition.floatValue;
  158. }
  159. BOOL noProgress = (playbackProgress == 0.0 || playbackProgress == 1.0);
  160. row.progressSeparator.hidden = noProgress;
  161. row.progressSeparator.width = floor(playbackProgress * CGRectGetWidth([WKInterfaceDevice currentDevice].screenBounds));
  162. /* FIXME: add placeholder image once designed
  163. if (backgroundImage == nil)
  164. backgroundImage = nil;
  165. */
  166. NSArray *array = @[row.group, storageObject];
  167. [self performSelectorInBackground:@selector(backgroundThumbnailSetter:) withObject:array];
  168. }
  169. - (void)backgroundThumbnailSetter:(NSArray *)array
  170. {
  171. UIImage *backgroundImage = [VLCThumbnailsCache thumbnailForManagedObject:array[1] toFitRect:_thumbnailSize shouldReplaceCache:YES];
  172. UIImage *gradient = [UIImage imageNamed:@"tableview-gradient"];
  173. CGSize newSize = backgroundImage ? backgroundImage.size : CGSizeMake(_rowWidth, 120.);
  174. UIGraphicsBeginImageContext(newSize);
  175. if (backgroundImage)
  176. [backgroundImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  177. else {
  178. [[UIColor darkGrayColor] set];
  179. UIRectFill(CGRectMake(.0, .0, newSize.width, newSize.height));
  180. }
  181. [gradient drawInRect:CGRectMake(.0, newSize.height / 2., newSize.width, newSize.height / 2.) blendMode:kCGBlendModeNormal alpha:1.];
  182. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  183. UIGraphicsEndImageContext();
  184. [array[0] setBackgroundImage:newImage];
  185. }
  186. //TODO: this code could use refactoring to be more readable
  187. - (NSMutableArray *)mediaArray {
  188. if (_libraryMode == VLCLibraryModeInGroup) {
  189. id groupObject = self.groupObject;
  190. if([groupObject isKindOfClass:[MLLabel class]]) {
  191. return [NSMutableArray arrayWithArray:[(MLLabel *)groupObject sortedFolderItems]];
  192. } else if ([groupObject isKindOfClass:[MLAlbum class]]) {
  193. return [NSMutableArray arrayWithArray:[(MLAlbum *)groupObject sortedTracks]];
  194. } else if ([groupObject isKindOfClass:[MLShow class]]){
  195. return [NSMutableArray arrayWithArray:[(MLShow *)groupObject sortedEpisodes]];
  196. } else {
  197. NSAssert(NO, @"this shouldn't have happened check the grouObjects type");
  198. }
  199. }
  200. NSMutableArray *objects = [NSMutableArray array];
  201. /* add all albums */
  202. if (_libraryMode != VLCLibraryModeAllSeries) {
  203. NSArray *rawAlbums = [MLAlbum allAlbums];
  204. for (MLAlbum *album in rawAlbums) {
  205. if (album.name.length > 0 && album.tracks.count > 1)
  206. [objects addObject:album];
  207. }
  208. }
  209. if (_libraryMode == VLCLibraryModeAllAlbums) {
  210. return objects;
  211. }
  212. /* add all shows */
  213. NSArray *rawShows = [MLShow allShows];
  214. for (MLShow *show in rawShows) {
  215. if (show.name.length > 0 && show.episodes.count > 1)
  216. [objects addObject:show];
  217. }
  218. if (_libraryMode == VLCLibraryModeAllSeries) {
  219. return objects;
  220. }
  221. /* add all folders*/
  222. NSArray *allFolders = [MLLabel allLabels];
  223. for (MLLabel *folder in allFolders)
  224. [objects addObject:folder];
  225. /* add all remaining files */
  226. NSArray *allFiles = [MLFile allFiles];
  227. for (MLFile *file in allFiles) {
  228. if (file.labels.count > 0) continue;
  229. if (!file.isShowEpisode && !file.isAlbumTrack)
  230. [objects addObject:file];
  231. else if (file.isShowEpisode) {
  232. if (file.showEpisode.show.episodes.count < 2)
  233. [objects addObject:file];
  234. /* older MediaLibraryKit versions don't send a show name in a popular
  235. * corner case. hence, we need to work-around here and force a reload
  236. * afterwards as this could lead to the 'all my shows are gone'
  237. * syndrome (see #10435, #10464, #10432 et al) */
  238. if (file.showEpisode.show.name.length == 0) {
  239. file.showEpisode.show.name = NSLocalizedString(@"UNTITLED_SHOW", nil);
  240. }
  241. } else if (file.isAlbumTrack) {
  242. if (file.albumTrack.album.tracks.count < 2)
  243. [objects addObject:file];
  244. }
  245. }
  246. return objects;
  247. }
  248. - (void)setLibraryMode:(VLCLibraryMode)libraryMode
  249. {
  250. //should also handle diving into a folder
  251. [self updateUserActivity:@"org.videolan.vlc-ios.librarymode" userInfo:@{@"state" : @(self.libraryMode)} webpageURL:nil];
  252. _libraryMode = libraryMode;
  253. }
  254. @end