InterfaceController.m 8.5 KB

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