InterfaceController.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. self.mediaObjects = [self mediaArray];
  47. NSUInteger newCount = self.mediaObjects.count;
  48. if (newCount < self.table.numberOfRows) {
  49. [self.table removeRowsAtIndexes:[[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(newCount, self.table.numberOfRows-newCount)]];
  50. }
  51. [self.table setNumberOfRows:self.mediaObjects.count withRowType:rowType];
  52. [self configureTable:self.table withObjects:self.mediaObjects];
  53. }
  54. - (void)configureTable:(WKInterfaceTable *)table withObjects:(NSArray *)objects {
  55. [objects enumerateObjectsUsingBlock:^(MLFile *obj, NSUInteger idx, BOOL *stop) {
  56. VLCRowController *row = [table rowControllerAtIndex:idx];
  57. row.titleLabel.text = obj.title;
  58. row.durationLabel.text = [VLCTime timeWithNumber:obj.duration].stringValue,
  59. [row.group setBackgroundImage:obj.computedThumbnail];
  60. }];
  61. }
  62. - (NSMutableArray *)mediaArray {
  63. NSMutableArray *objects = [NSMutableArray array];
  64. // /* add all albums */
  65. // NSArray *rawAlbums = [MLAlbum allAlbums];
  66. // for (MLAlbum *album in rawAlbums) {
  67. // if (album.name.length > 0 && album.tracks.count > 1)
  68. // [objects addObject:album];
  69. // }
  70. //
  71. // /* add all shows */
  72. // NSArray *rawShows = [MLShow allShows];
  73. // for (MLShow *show in rawShows) {
  74. // if (show.name.length > 0 && show.episodes.count > 1)
  75. // [objects addObject:show];
  76. // }
  77. //
  78. // /* add all folders*/
  79. // NSArray *allFolders = [MLLabel allLabels];
  80. // for (MLLabel *folder in allFolders)
  81. // [objects addObject:folder];
  82. /* add all remaining files */
  83. NSArray *allFiles = [MLFile allFiles];
  84. for (MLFile *file in allFiles) {
  85. if (file.labels.count > 0) continue;
  86. if (!file.isShowEpisode && !file.isAlbumTrack)
  87. [objects addObject:file];
  88. else if (file.isShowEpisode) {
  89. if (file.showEpisode.show.episodes.count < 2)
  90. [objects addObject:file];
  91. } else if (file.isAlbumTrack) {
  92. if (file.albumTrack.album.tracks.count < 2)
  93. [objects addObject:file];
  94. }
  95. }
  96. return objects;
  97. }
  98. @end