InterfaceController.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. //TODO: make this dynamical width
  155. CGSize newSize = CGSizeMake(130, 60);
  156. UIGraphicsBeginImageContext(newSize);
  157. // Use existing opacity as is
  158. [backgroundImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  159. [gradient drawInRect:CGRectMake(0,40,newSize.width,20) blendMode:kCGBlendModeNormal alpha:1.0];
  160. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  161. UIGraphicsEndImageContext();
  162. return newImage;
  163. }
  164. //TODO: this code could use refactoring to be more readable
  165. - (NSMutableArray *)mediaArray {
  166. if (_libraryMode == VLCLibraryModeInGroup) {
  167. id groupObject = self.groupObject;
  168. if([groupObject isKindOfClass:[MLLabel class]]) {
  169. return [NSMutableArray arrayWithArray:[(MLLabel *)groupObject sortedFolderItems]];
  170. } else if ([groupObject isKindOfClass:[MLAlbum class]]) {
  171. return [NSMutableArray arrayWithArray:[(MLAlbum *)groupObject sortedTracks]];
  172. } else if ([groupObject isKindOfClass:[MLShow class]]){
  173. return [NSMutableArray arrayWithArray:[(MLShow *)groupObject sortedEpisodes]];
  174. } else {
  175. NSAssert(NO, @"this shouldn't have happened check the grouObjects type");
  176. }
  177. }
  178. NSMutableArray *objects = [NSMutableArray array];
  179. /* add all albums */
  180. if (_libraryMode != VLCLibraryModeAllSeries) {
  181. NSArray *rawAlbums = [MLAlbum allAlbums];
  182. for (MLAlbum *album in rawAlbums) {
  183. if (album.name.length > 0 && album.tracks.count > 1)
  184. [objects addObject:album];
  185. }
  186. }
  187. if (_libraryMode == VLCLibraryModeAllAlbums) {
  188. return objects;
  189. }
  190. /* add all shows */
  191. NSArray *rawShows = [MLShow allShows];
  192. for (MLShow *show in rawShows) {
  193. if (show.name.length > 0 && show.episodes.count > 1)
  194. [objects addObject:show];
  195. }
  196. if (_libraryMode == VLCLibraryModeAllSeries) {
  197. return objects;
  198. }
  199. /* add all folders*/
  200. NSArray *allFolders = [MLLabel allLabels];
  201. for (MLLabel *folder in allFolders)
  202. [objects addObject:folder];
  203. /* add all remaining files */
  204. NSArray *allFiles = [MLFile allFiles];
  205. for (MLFile *file in allFiles) {
  206. if (file.labels.count > 0) continue;
  207. if (!file.isShowEpisode && !file.isAlbumTrack)
  208. [objects addObject:file];
  209. else if (file.isShowEpisode) {
  210. if (file.showEpisode.show.episodes.count < 2)
  211. [objects addObject:file];
  212. /* older MediaLibraryKit versions don't send a show name in a popular
  213. * corner case. hence, we need to work-around here and force a reload
  214. * afterwards as this could lead to the 'all my shows are gone'
  215. * syndrome (see #10435, #10464, #10432 et al) */
  216. if (file.showEpisode.show.name.length == 0) {
  217. file.showEpisode.show.name = NSLocalizedString(@"UNTITLED_SHOW", nil);
  218. }
  219. } else if (file.isAlbumTrack) {
  220. if (file.albumTrack.album.tracks.count < 2)
  221. [objects addObject:file];
  222. }
  223. }
  224. return objects;
  225. }
  226. @end