VLCPlaylistInterfaceController.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #if TARGET_IPHONE_SIMULATOR
  39. // if something went wrong with the entitlements in the Simulator
  40. if (!groupURL) {
  41. NSArray *pathComponents = [[[NSBundle mainBundle] bundlePath] pathComponents];
  42. pathComponents = [pathComponents subarrayWithRange:NSMakeRange(0, pathComponents.count-4)];
  43. NSString *groupPath = [[NSString pathWithComponents:pathComponents] stringByAppendingPathComponent:@"Shared/AppGroup/fake-group.org.videolan.vlc-ios"];
  44. groupURL = [NSURL fileURLWithPath:groupPath];
  45. }
  46. #endif
  47. MLMediaLibrary *mediaLibrary = [MLMediaLibrary sharedMediaLibrary];
  48. mediaLibrary.libraryBasePath = groupURL.path;
  49. mediaLibrary.additionalPersitentStoreOptions = @{NSReadOnlyPersistentStoreOption : @YES};
  50. if (context == nil) {
  51. self.libraryMode = VLCLibraryModeAllFiles;
  52. [self setupMenuButtons];
  53. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
  54. self.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", nil);
  55. } else {
  56. self.groupObject = context;
  57. self.title = [self.groupObject name];
  58. self.libraryMode = VLCLibraryModeNone;
  59. }
  60. [self addNowPlayingMenu];
  61. [[VLCNotificationRelay sharedRelay] addRelayRemoteName:VLCDBUpdateNotificationRemote toLocalName:VLCDBUpdateNotification];
  62. /* setup table view controller */
  63. VLCWatchTableController *tableController = [[VLCWatchTableController alloc] init];
  64. tableController.table = self.table;
  65. tableController.previousPageButton = self.previousButton;
  66. tableController.nextPageButton = self.nextButton;
  67. tableController.emptyLibraryInterfaceObjects = self.emptyLibraryGroup;
  68. tableController.pageSize = 20;
  69. tableController.rowType = rowType;
  70. tableController.configureRowControllerWithObjectBlock = ^(id controller, id object) {
  71. if ([controller respondsToSelector:@selector(configureWithMediaLibraryObject:)]) {
  72. [controller configureWithMediaLibraryObject:object];
  73. }
  74. };
  75. self.tableController = tableController;
  76. [self updateData];
  77. }
  78. - (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex {
  79. id object = self.tableController.displayedObjects[rowIndex];
  80. if ([object isKindOfClass:[MLAlbum class]] || [object isKindOfClass:[MLLabel class]] || [object isKindOfClass:[MLShow class]]) {
  81. [self pushControllerWithName:@"tableViewController" context:object];
  82. [self updateUserActivity:@"org.videolan.vlc-ios.librarymode" userInfo:@{@"state" : @(self.libraryMode), @"folder":((NSManagedObject *)object).objectID.URIRepresentation} webpageURL:nil];
  83. } else {
  84. [self pushControllerWithName:@"detailInfo" context:object];
  85. }
  86. }
  87. - (IBAction)nextPagePressed {
  88. [self.tableController nextPageButtonPressed];
  89. }
  90. - (IBAction)previousPagePressed {
  91. [self.tableController previousPageButtonPressed];
  92. }
  93. - (void)setupMenuButtons {
  94. [self addMenuItemWithImageNamed:@"AllFiles" title: NSLocalizedString(@"LIBRARY_ALL_FILES", nil) action:@selector(switchToAllFiles)];
  95. [self addMenuItemWithImageNamed:@"MusicAlbums" title: NSLocalizedString(@"LIBRARY_MUSIC", nil) action:@selector(switchToMusic)];
  96. [self addMenuItemWithImageNamed:@"TVShows" title: NSLocalizedString(@"LIBRARY_SERIES", nil) action:@selector(switchToSeries)];
  97. }
  98. - (void)switchToAllFiles{
  99. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
  100. self.libraryMode = VLCLibraryModeAllFiles;
  101. [self updateData];
  102. }
  103. - (void)switchToMusic{
  104. self.title = NSLocalizedString(@"LIBRARY_MUSIC", nil);
  105. self.libraryMode = VLCLibraryModeAllAlbums;
  106. [self updateData];
  107. }
  108. - (void)switchToSeries{
  109. self.title = NSLocalizedString(@"LIBRARY_SERIES", nil);
  110. self.libraryMode = VLCLibraryModeAllSeries;
  111. [self updateData];
  112. }
  113. #pragma mark - data handling
  114. - (void)updateData {
  115. [super updateData];
  116. NSManagedObjectContext *moc = [(NSManagedObject *)self.tableController.objects.firstObject managedObjectContext];
  117. [moc vlc_refreshAllObjectsMerge:NO];
  118. self.tableController.objects = [self mediaArray];
  119. }
  120. - (void)setLibraryMode:(VLCLibraryMode)libraryMode
  121. {
  122. //should also handle diving into a folder
  123. [self updateUserActivity:@"org.videolan.vlc-ios.librarymode" userInfo:@{@"state" : @(libraryMode)} webpageURL:nil];
  124. _libraryMode = libraryMode;
  125. }
  126. - (NSArray *)mediaArray
  127. {
  128. id groupObject = self.groupObject;
  129. if (groupObject) {
  130. return [[MLMediaLibrary sharedMediaLibrary] playlistArrayForGroupObject:groupObject];
  131. } else {
  132. return [[MLMediaLibrary sharedMediaLibrary] playlistArrayForLibraryMode:self.libraryMode];
  133. }
  134. }
  135. @end