VLCPlaylistInterfaceController.m 5.7 KB

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