InterfaceController.m 4.1 KB

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