InterfaceController.m 9.9 KB

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