InterfaceController.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "InterfaceController.h"
  14. #import <MediaLibraryKit/MediaLibraryKit.h>
  15. #import "VLCRowController.h"
  16. #import <MobileVLCKit/VLCTime.h>
  17. #import "VLCNotificationRelay.h"
  18. #import "VLCWatchTableController.h"
  19. static NSString *const rowType = @"mediaRow";
  20. static NSString *const VLCDBUpdateNotification = @"VLCUpdateDataBase";
  21. static NSString *const VLCDBUpdateNotificationRemote = @"org.videolan.ios-app.dbupdate";
  22. @interface InterfaceController()
  23. @property (nonatomic, strong) VLCWatchTableController *tableController;
  24. @end
  25. @implementation InterfaceController
  26. - (void)awakeWithContext:(id)context {
  27. [super awakeWithContext:context];
  28. NSLog(@"%s",__PRETTY_FUNCTION__);
  29. NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.org.videolan.vlc-ios"];
  30. MLMediaLibrary *mediaLibrary = [MLMediaLibrary sharedMediaLibrary];
  31. mediaLibrary.libraryBasePath = groupURL.path;
  32. mediaLibrary.additionalPersitentStoreOptions = @{NSReadOnlyPersistentStoreOption : @YES};
  33. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
  34. [[VLCNotificationRelay sharedRelay] addRelayRemoteName:VLCDBUpdateNotificationRemote toLocalName:VLCDBUpdateNotification];
  35. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateData) name:VLCDBUpdateNotification object:nil];
  36. VLCWatchTableController *tableController = [VLCWatchTableController new];
  37. tableController.table = self.table;
  38. tableController.previousPageButton = self.previousButton;
  39. tableController.nextPageButton = self.nextButton;
  40. tableController.pageSize = 5;
  41. tableController.rowTypeForObjectBlock = ^(id object) {
  42. return rowType;
  43. };
  44. tableController.configureRowControllerWithObjectBlock = ^(id controller, id object) {
  45. [self configureTableRowController:context withObject:object];
  46. };
  47. [self updateData];
  48. }
  49. - (void)willActivate {
  50. // This method is called when watch view controller is about to be visible to user
  51. [super willActivate];
  52. NSLog(@"%s",__PRETTY_FUNCTION__);
  53. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateData) name:VLCDBUpdateNotification object:nil];
  54. }
  55. - (void)didDeactivate {
  56. // This method is called when watch view controller is no longer visible
  57. [super didDeactivate];
  58. NSLog(@"%s",__PRETTY_FUNCTION__);
  59. [[NSNotificationCenter defaultCenter] removeObserver:self name:VLCDBUpdateNotification object:nil];
  60. }
  61. - (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex {
  62. id object = self.tableController.displayedObjects[rowIndex];
  63. if ([object isKindOfClass:[MLFile class]]) {
  64. [self pushControllerWithName:@"detailInfo" context:object];
  65. }
  66. }
  67. - (IBAction)nextPagePressed {
  68. [self.tableController nextPageButtonPressed];
  69. }
  70. - (IBAction)previousPagePressed {
  71. [self.tableController previousPageButtonPressed];
  72. }
  73. #pragma mark - data handling
  74. - (void)updateData {
  75. self.tableController.objects = [self mediaArray];
  76. }
  77. - (void)configureTableRowController:(id)rowController withObject:(MLFile *)object {
  78. VLCRowController *row = rowController;
  79. row.titleLabel.text = object.title;
  80. row.durationLabel.text = [VLCTime timeWithNumber:object.duration].stringValue;
  81. [row.group setBackgroundImage:object.computedThumbnail];
  82. }
  83. - (NSMutableArray *)mediaArray {
  84. NSMutableArray *objects = [NSMutableArray array];
  85. // /* add all albums */
  86. // NSArray *rawAlbums = [MLAlbum allAlbums];
  87. // for (MLAlbum *album in rawAlbums) {
  88. // if (album.name.length > 0 && album.tracks.count > 1)
  89. // [objects addObject:album];
  90. // }
  91. //
  92. // /* add all shows */
  93. // NSArray *rawShows = [MLShow allShows];
  94. // for (MLShow *show in rawShows) {
  95. // if (show.name.length > 0 && show.episodes.count > 1)
  96. // [objects addObject:show];
  97. // }
  98. //
  99. // /* add all folders*/
  100. // NSArray *allFolders = [MLLabel allLabels];
  101. // for (MLLabel *folder in allFolders)
  102. // [objects addObject:folder];
  103. /* add all remaining files */
  104. NSArray *allFiles = [MLFile allFiles];
  105. for (MLFile *file in allFiles) {
  106. if (file.labels.count > 0) continue;
  107. if (!file.isShowEpisode && !file.isAlbumTrack)
  108. [objects addObject:file];
  109. else if (file.isShowEpisode) {
  110. if (file.showEpisode.show.episodes.count < 2)
  111. [objects addObject:file];
  112. } else if (file.isAlbumTrack) {
  113. if (file.albumTrack.album.tracks.count < 2)
  114. [objects addObject:file];
  115. }
  116. }
  117. return objects;
  118. }
  119. @end