InterfaceController.m 9.4 KB

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