VLCPlaylistInterfaceController.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*****************************************************************************
  2. * VLCPlaylistInterfaceController.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 "VLCPlaylistInterfaceController.h"
  15. #import "VLCRowController.h"
  16. #import "VLCNotificationRelay.h"
  17. #import "VLCWatchTableController.h"
  18. #import "NSManagedObjectContext+refreshAll.h"
  19. #import "MLMediaLibrary+playlist.h"
  20. static NSString *const rowType = @"mediaRow";
  21. static NSString *const VLCDBUpdateNotification = @"VLCUpdateDataBase";
  22. static NSString *const VLCDBUpdateNotificationRemote = @"org.videolan.ios-app.dbupdate";
  23. @interface VLCPlaylistInterfaceController()
  24. {
  25. CGRect _thumbnailSize;
  26. CGFloat _rowWidth;
  27. }
  28. @property (nonatomic, strong) VLCWatchTableController *tableController;
  29. @property (nonatomic) VLCLibraryMode libraryMode;
  30. @property (nonatomic) id groupObject;
  31. @end
  32. @implementation VLCPlaylistInterfaceController
  33. - (void)awakeWithContext:(id)context {
  34. [super awakeWithContext:context];
  35. MLMediaLibrary *mediaLibrary = [MLMediaLibrary sharedMediaLibrary];
  36. mediaLibrary.additionalPersitentStoreOptions = @{NSReadOnlyPersistentStoreOption : @YES};
  37. if (context == nil) {
  38. self.libraryMode = VLCLibraryModeAllFiles;
  39. [self setupMenuButtons];
  40. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
  41. self.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", nil);
  42. } else {
  43. self.groupObject = context;
  44. self.title = [self.groupObject name];
  45. self.libraryMode = VLCLibraryModeFolder;
  46. }
  47. [self addNowPlayingMenu];
  48. [[VLCNotificationRelay sharedRelay] addRelayRemoteName:VLCDBUpdateNotificationRemote toLocalName:VLCDBUpdateNotification];
  49. /* setup table view controller */
  50. VLCWatchTableController *tableController = [[VLCWatchTableController alloc] init];
  51. tableController.table = self.table;
  52. tableController.previousPageButton = self.previousButton;
  53. tableController.nextPageButton = self.nextButton;
  54. tableController.emptyLibraryInterfaceObjects = self.emptyLibraryGroup;
  55. tableController.pageSize = 20;
  56. tableController.rowType = rowType;
  57. tableController.configureRowControllerWithObjectBlock = ^(id controller, id object) {
  58. if ([controller respondsToSelector:@selector(configureWithMediaLibraryObject:)]) {
  59. [controller configureWithMediaLibraryObject:object];
  60. }
  61. };
  62. self.tableController = tableController;
  63. [self updateData];
  64. }
  65. - (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex {
  66. id object = self.tableController.displayedObjects[rowIndex];
  67. if ([object isKindOfClass:[MLAlbum class]] || [object isKindOfClass:[MLLabel class]] || [object isKindOfClass:[MLShow class]]) {
  68. [self pushControllerWithName:@"tableViewController" context:object];
  69. NSString *folderRepresentation = [((NSManagedObject *)object).objectID.URIRepresentation absoluteString];
  70. NSDictionary *userDict = @{@"state" : @(self.libraryMode),
  71. @"folder" : folderRepresentation};
  72. [self invalidateUserActivity];
  73. [self updateUserActivity:kVLCUserActivityLibrarySelection
  74. userInfo:userDict
  75. webpageURL:nil];
  76. } else {
  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:@"TVShows" title: NSLocalizedString(@"LIBRARY_SERIES", nil) action:@selector(switchToSeries)];
  90. }
  91. - (void)switchToAllFiles{
  92. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
  93. self.libraryMode = VLCLibraryModeAllFiles;
  94. [self updateData];
  95. }
  96. - (void)switchToMusic{
  97. self.title = NSLocalizedString(@"LIBRARY_MUSIC", nil);
  98. self.libraryMode = VLCLibraryModeAllAlbums;
  99. [self updateData];
  100. }
  101. - (void)switchToSeries{
  102. self.title = NSLocalizedString(@"LIBRARY_SERIES", nil);
  103. self.libraryMode = VLCLibraryModeAllSeries;
  104. [self updateData];
  105. }
  106. #pragma mark - data handling
  107. - (void)updateData {
  108. [super updateData];
  109. NSManagedObjectContext *moc = [(NSManagedObject *)self.tableController.objects.firstObject managedObjectContext];
  110. [moc vlc_refreshAllObjectsMerge:NO];
  111. self.tableController.objects = [self mediaArray];
  112. }
  113. - (void)setLibraryMode:(VLCLibraryMode)libraryMode
  114. {
  115. //should also handle diving into a folder
  116. [self invalidateUserActivity];
  117. [self updateUserActivity:kVLCUserActivityLibraryMode userInfo:@{@"state" : @(libraryMode)} webpageURL:nil];
  118. _libraryMode = libraryMode;
  119. }
  120. - (NSArray *)mediaArray
  121. {
  122. id groupObject = self.groupObject;
  123. if (groupObject) {
  124. return [[MLMediaLibrary sharedMediaLibrary] playlistArrayForGroupObject:groupObject];
  125. } else {
  126. return [[MLMediaLibrary sharedMediaLibrary] playlistArrayForLibraryMode:self.libraryMode];
  127. }
  128. }
  129. @end