InterfaceController.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_MUSIC", nil);
  28. [[VLCNotificationRelay sharedRelay] addRelayRemoteName:VLCDBUpdateNotificationRemote toLocalName:VLCDBUpdateNotification];
  29. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateData) name:VLCDBUpdateNotification object:nil];
  30. [self updateData];
  31. [self addMenuItemWithItemIcon:WKMenuItemIconMore title:@"currently playing" action:@selector(showNowPlaying:)];
  32. }
  33. - (void)willActivate {
  34. // This method is called when watch view controller is about to be visible to user
  35. [super willActivate];
  36. NSLog(@"%s",__PRETTY_FUNCTION__);
  37. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateData) name:VLCDBUpdateNotification object:nil];
  38. }
  39. - (void)didDeactivate {
  40. // This method is called when watch view controller is no longer visible
  41. [super didDeactivate];
  42. NSLog(@"%s",__PRETTY_FUNCTION__);
  43. [[NSNotificationCenter defaultCenter] removeObserver:self name:VLCDBUpdateNotification object:nil];
  44. }
  45. - (void)showNowPlaying:(id)sender {
  46. [self presentControllerWithName:@"nowPlaying" context:nil];
  47. }
  48. #pragma mark - data handling
  49. - (void)updateData {
  50. NSArray *oldObjects = self.mediaObjects;
  51. NSSet *oldSet = [[NSSet alloc] initWithArray:oldObjects];
  52. NSMutableArray *newObjects = [self mediaArray];
  53. NSMutableSet *newSet = [[NSMutableSet alloc] initWithArray:newObjects];
  54. WKInterfaceTable *table = self.table;
  55. [oldObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  56. if (![newSet containsObject:obj]) {
  57. [table removeRowsAtIndexes:[NSIndexSet indexSetWithIndex:idx]];
  58. }
  59. }];
  60. [newObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  61. if (![oldSet containsObject:obj]) {
  62. [table insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:idx] withRowType:rowType];
  63. }
  64. [self configureTableCellAtIndex:idx withObject:obj];
  65. }];
  66. self.mediaObjects = newObjects;
  67. }
  68. - (void)configureTableCellAtIndex:(NSUInteger)index withObject:(MLFile *)object {
  69. VLCRowController *row = [self.table rowControllerAtIndex:index];
  70. row.titleLabel.text = object.title;
  71. row.durationLabel.text = [VLCTime timeWithNumber:object.duration].stringValue,
  72. [row.group setBackgroundImage:object.computedThumbnail];
  73. }
  74. - (NSMutableArray *)mediaArray {
  75. NSMutableArray *objects = [NSMutableArray array];
  76. // /* add all albums */
  77. // NSArray *rawAlbums = [MLAlbum allAlbums];
  78. // for (MLAlbum *album in rawAlbums) {
  79. // if (album.name.length > 0 && album.tracks.count > 1)
  80. // [objects addObject:album];
  81. // }
  82. //
  83. // /* add all shows */
  84. // NSArray *rawShows = [MLShow allShows];
  85. // for (MLShow *show in rawShows) {
  86. // if (show.name.length > 0 && show.episodes.count > 1)
  87. // [objects addObject:show];
  88. // }
  89. //
  90. // /* add all folders*/
  91. // NSArray *allFolders = [MLLabel allLabels];
  92. // for (MLLabel *folder in allFolders)
  93. // [objects addObject:folder];
  94. /* add all remaining files */
  95. NSArray *allFiles = [MLFile allFiles];
  96. for (MLFile *file in allFiles) {
  97. if (file.labels.count > 0) continue;
  98. if (!file.isShowEpisode && !file.isAlbumTrack)
  99. [objects addObject:file];
  100. else if (file.isShowEpisode) {
  101. if (file.showEpisode.show.episodes.count < 2)
  102. [objects addObject:file];
  103. } else if (file.isAlbumTrack) {
  104. if (file.albumTrack.album.tracks.count < 2)
  105. [objects addObject:file];
  106. }
  107. }
  108. return objects;
  109. }
  110. @end