InterfaceController.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*****************************************************************************
  2. * InterfaceController.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 "InterfaceController.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. } VLCLibraryMode;
  29. @interface InterfaceController()
  30. @property (nonatomic, strong) VLCWatchTableController *tableController;
  31. @property (nonatomic) VLCLibraryMode libraryMode;
  32. @end
  33. @implementation InterfaceController
  34. - (void)awakeWithContext:(id)context {
  35. [super awakeWithContext:context];
  36. NSLog(@"%s",__PRETTY_FUNCTION__);
  37. NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.org.videolan.vlc-ios"];
  38. [self setupMenuButtons];
  39. self.libraryMode = VLCLibraryModeAllFiles;
  40. MLMediaLibrary *mediaLibrary = [MLMediaLibrary sharedMediaLibrary];
  41. mediaLibrary.libraryBasePath = groupURL.path;
  42. mediaLibrary.additionalPersitentStoreOptions = @{NSReadOnlyPersistentStoreOption : @YES};
  43. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
  44. [[VLCNotificationRelay sharedRelay] addRelayRemoteName:VLCDBUpdateNotificationRemote toLocalName:VLCDBUpdateNotification];
  45. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateData) name:VLCDBUpdateNotification object:nil];
  46. VLCWatchTableController *tableController = [[VLCWatchTableController alloc] init];
  47. tableController.table = self.table;
  48. tableController.previousPageButton = self.previousButton;
  49. tableController.nextPageButton = self.nextButton;
  50. tableController.pageSize = 5;
  51. tableController.rowType = rowType;
  52. __weak typeof(self) weakSelf = self;
  53. tableController.configureRowControllerWithObjectBlock = ^(id controller, id object) {
  54. [weakSelf configureTableRowController:controller withObject:object];
  55. };
  56. self.tableController = tableController;
  57. [self updateData];
  58. }
  59. - (void)willActivate {
  60. // This method is called when watch view controller is about to be visible to user
  61. [super willActivate];
  62. NSLog(@"%s",__PRETTY_FUNCTION__);
  63. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateData) name:VLCDBUpdateNotification object:nil];
  64. }
  65. - (void)didDeactivate {
  66. // This method is called when watch view controller is no longer visible
  67. [super didDeactivate];
  68. NSLog(@"%s",__PRETTY_FUNCTION__);
  69. [[NSNotificationCenter defaultCenter] removeObserver:self name:VLCDBUpdateNotification object:nil];
  70. }
  71. - (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex {
  72. id object = self.tableController.displayedObjects[rowIndex];
  73. if ([object isKindOfClass:[MLFile class]]) {
  74. [self pushControllerWithName:@"detailInfo" context:object];
  75. }
  76. }
  77. - (IBAction)nextPagePressed {
  78. [self.tableController nextPageButtonPressed];
  79. }
  80. - (IBAction)previousPagePressed {
  81. [self.tableController previousPageButtonPressed];
  82. }
  83. - (void)setupMenuButtons {
  84. [self addMenuItemWithImageNamed:@"AllFiles" title: NSLocalizedString(@"LIBRARY_ALL_FILES", nil) action:@selector(switchToAllFiles)];
  85. [self addMenuItemWithImageNamed:@"MusicAlbums" title: NSLocalizedString(@"LIBRARY_MUSIC", nil) action:@selector(switchToMusic)];
  86. [self addMenuItemWithImageNamed:@"TVShowsIcon" title: NSLocalizedString(@"LIBRARY_SERIES", nil) action:@selector(switchToSeries)];
  87. [self addMenuItemWithItemIcon:WKMenuItemIconMore title: NSLocalizedString(@"NOW_PLAYING", nil) action:@selector(showNowPlaying:)];
  88. }
  89. - (void)switchToAllFiles{
  90. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
  91. self.libraryMode = VLCLibraryModeAllFiles;
  92. [self updateData];
  93. }
  94. - (void)switchToMusic{
  95. self.title = NSLocalizedString(@"LIBRARY_MUSIC", nil);
  96. self.libraryMode = VLCLibraryModeAllAlbums;
  97. [self updateData];
  98. }
  99. - (void)switchToSeries{
  100. self.title = NSLocalizedString(@"LIBRARY_SERIES", nil);
  101. self.libraryMode = VLCLibraryModeAllSeries;
  102. [self updateData];
  103. }
  104. - (void)showNowPlaying:(id)sender {
  105. [self presentControllerWithName:@"nowPlaying" context:nil];
  106. }
  107. #pragma mark - data handling
  108. - (void)updateData {
  109. self.tableController.objects = [self mediaArray];
  110. }
  111. - (void)configureTableRowController:(id)rowController withObject:(id)storageObject {
  112. VLCRowController *row = rowController;
  113. UIImage *backgroundImage;
  114. if ([storageObject isKindOfClass:[MLShow class]]) {
  115. backgroundImage = [VLCThumbnailsCache thumbnailForShow:storageObject];
  116. row.titleLabel.text = ((MLAlbum *)storageObject).name;
  117. } else if ([storageObject isKindOfClass:[MLShowEpisode class]]) {
  118. MLFile *anyFileFromEpisode = [(MLShowEpisode *)storageObject files].anyObject;
  119. backgroundImage = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromEpisode];
  120. row.titleLabel.text = ((MLShowEpisode *)storageObject).name;
  121. } else if ([storageObject isKindOfClass:[MLLabel class]]) {
  122. backgroundImage = [VLCThumbnailsCache thumbnailForLabel:storageObject];
  123. row.titleLabel.text = ((MLLabel *)storageObject).name;
  124. } else if ([storageObject isKindOfClass:[MLAlbum class]]) {
  125. MLFile *anyFileFromAnyTrack = [[(MLAlbum *)storageObject tracks].anyObject files].anyObject;
  126. backgroundImage = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromAnyTrack];
  127. row.titleLabel.text = ((MLAlbum *)storageObject).name;
  128. } else if ([storageObject isKindOfClass:[MLAlbumTrack class]]) {
  129. MLFile *anyFileFromTrack = [(MLAlbumTrack *)storageObject files].anyObject;
  130. backgroundImage = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromTrack];
  131. row.titleLabel.text = ((MLAlbumTrack *)storageObject).title;
  132. } else {
  133. row.titleLabel.text = [(MLFile *)storageObject title];
  134. row.durationLabel.text = [VLCTime timeWithNumber:[(MLFile *)storageObject duration]].stringValue;
  135. backgroundImage = [VLCThumbnailsCache thumbnailForMediaFile:(MLFile *)storageObject];
  136. }
  137. /* FIXME: add placeholder image once designed
  138. if (backgroundImage == nil)
  139. backgroundImage = nil;
  140. */
  141. [row.group setBackgroundImage:[self generateBackgroundiImageWithGradient:backgroundImage]];
  142. }
  143. - (UIImage *)generateBackgroundiImageWithGradient:(UIImage *)backgroundImage {
  144. UIImage *gradient = [UIImage imageNamed:@"gradient-cell-ios7"];
  145. //TODO: make this dynamical width
  146. CGSize newSize = CGSizeMake(130, 60);
  147. UIGraphicsBeginImageContext(newSize);
  148. // Use existing opacity as is
  149. [backgroundImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  150. [gradient drawInRect:CGRectMake(0,40,newSize.width,20) blendMode:kCGBlendModeNormal alpha:1.0];
  151. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  152. UIGraphicsEndImageContext();
  153. return newImage;
  154. }
  155. //TODO: this code could use refactoring to be more readable
  156. - (NSMutableArray *)mediaArray {
  157. NSMutableArray *objects = [NSMutableArray array];
  158. /* add all albums */
  159. if (_libraryMode != VLCLibraryModeAllSeries) {
  160. NSArray *rawAlbums = [MLAlbum allAlbums];
  161. for (MLAlbum *album in rawAlbums) {
  162. if (album.name.length > 0 && album.tracks.count > 1)
  163. [objects addObject:album];
  164. }
  165. }
  166. if (_libraryMode == VLCLibraryModeAllAlbums) {
  167. return objects;
  168. }
  169. /* add all shows */
  170. NSArray *rawShows = [MLShow allShows];
  171. for (MLShow *show in rawShows) {
  172. if (show.name.length > 0 && show.episodes.count > 1)
  173. [objects addObject:show];
  174. }
  175. if (_libraryMode == VLCLibraryModeAllSeries) {
  176. return objects;
  177. }
  178. /* add all folders*/
  179. NSArray *allFolders = [MLLabel allLabels];
  180. for (MLLabel *folder in allFolders)
  181. [objects addObject:folder];
  182. /* add all remaining files */
  183. NSArray *allFiles = [MLFile allFiles];
  184. for (MLFile *file in allFiles) {
  185. if (file.labels.count > 0) continue;
  186. if (!file.isShowEpisode && !file.isAlbumTrack)
  187. [objects addObject:file];
  188. else if (file.isShowEpisode) {
  189. if (file.showEpisode.show.episodes.count < 2)
  190. [objects addObject:file];
  191. /* older MediaLibraryKit versions don't send a show name in a popular
  192. * corner case. hence, we need to work-around here and force a reload
  193. * afterwards as this could lead to the 'all my shows are gone'
  194. * syndrome (see #10435, #10464, #10432 et al) */
  195. if (file.showEpisode.show.name.length == 0) {
  196. file.showEpisode.show.name = NSLocalizedString(@"UNTITLED_SHOW", nil);
  197. }
  198. } else if (file.isAlbumTrack) {
  199. if (file.albumTrack.album.tracks.count < 2)
  200. [objects addObject:file];
  201. }
  202. }
  203. return objects;
  204. }
  205. @end