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