VLCPlaylistInterfaceController.m 5.6 KB

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