InterfaceController.m 4.7 KB

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