InterfaceController.m 5.1 KB

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