VLCDetailInterfaceController.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*****************************************************************************
  2. * VLCDetailInterfaceController.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. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCDetailInterfaceController.h"
  13. #import <MediaLibraryKit/MediaLibraryKit.h>
  14. #import <MobileVLCKit/MobileVLCKit.h>
  15. #import "VLCThumbnailsCache.h"
  16. #import "WKInterfaceObject+VLCProgress.h"
  17. @interface VLCDetailInterfaceController ()
  18. @property (nonatomic, weak) NSManagedObject *managedObject;
  19. @end
  20. @implementation VLCDetailInterfaceController
  21. - (instancetype)init
  22. {
  23. self = [super init];
  24. if (self) {
  25. [self setTitle:NSLocalizedString(@"DETAIL", nil)];
  26. }
  27. return self;
  28. }
  29. - (void)awakeWithContext:(id)context {
  30. [super awakeWithContext:context];
  31. [self setTitle:NSLocalizedString(@"DETAIL", nil)];
  32. [self addNowPlayingMenu];
  33. [self configureWithFile:context];
  34. }
  35. - (void)updateData {
  36. [super updateData];
  37. NSManagedObject *managedObject = self.managedObject;
  38. [managedObject.managedObjectContext refreshObject:managedObject mergeChanges:NO];
  39. [self configureWithFile:managedObject];
  40. }
  41. - (void)configureWithFile:(NSManagedObject *)managedObject {
  42. self.managedObject = managedObject;
  43. NSString *title = nil;
  44. NSString *durationString = nil;
  45. float playbackProgress = 0.0;
  46. if ([managedObject isKindOfClass:[MLShowEpisode class]]) {
  47. title = ((MLShowEpisode *)managedObject).name;
  48. } else if ([managedObject isKindOfClass:[MLFile class]]) {
  49. MLFile *file = (MLFile *)managedObject;
  50. durationString = [VLCTime timeWithNumber:file.duration].stringValue;
  51. playbackProgress = file.lastPosition.floatValue;
  52. title = ((MLFile *)file).title;
  53. } else if ([managedObject isKindOfClass:[MLAlbumTrack class]]) {
  54. title = ((MLAlbumTrack *)managedObject).title;
  55. } else {
  56. NSAssert(NO, @"check what filetype we try to show here and add it above");
  57. }
  58. BOOL playEnabled = managedObject != nil;
  59. self.playNowButton.enabled = playEnabled;
  60. self.mediaTitle = title;
  61. self.mediaDuration = durationString;
  62. self.playbackProgress = playbackProgress;
  63. /* do not block the main thread */
  64. [self performSelectorInBackground:@selector(loadThumbnailForManagedObject:) withObject:managedObject];
  65. }
  66. - (void)loadThumbnailForManagedObject:(NSManagedObject *)managedObject
  67. {
  68. UIImage *thumbnail = [VLCThumbnailsCache thumbnailForManagedObject:managedObject];
  69. if (thumbnail) {
  70. [self.group performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:thumbnail waitUntilDone:NO];
  71. }
  72. }
  73. - (IBAction)playNow {
  74. NSDictionary *dict = @{@"name":@"playFile",
  75. @"userInfo":@{
  76. @"URIRepresentation": self.managedObject.objectID.URIRepresentation.absoluteString,
  77. }
  78. };
  79. [self updateUserActivity:@"org.videolan.vlc-ios.playing" userInfo:@{@"playingmedia":self.managedObject.objectID.URIRepresentation} webpageURL:nil];
  80. [WKInterfaceController openParentApplication:dict reply:^(NSDictionary *replyInfo, NSError *error) {
  81. [self showNowPlaying:nil];
  82. }];
  83. }
  84. - (void)setMediaTitle:(NSString *)mediaTitle {
  85. if (![_mediaTitle isEqualToString:mediaTitle]) {
  86. _mediaTitle = [mediaTitle copy];
  87. self.titleLabel.text = mediaTitle;
  88. self.titleLabel.hidden = mediaTitle.length == 0;
  89. }
  90. }
  91. - (void)setMediaDuration:(NSString *)mediaDuration {
  92. if (![_mediaDuration isEqualToString:mediaDuration]) {
  93. _mediaDuration = [mediaDuration copy];
  94. self.durationLabel.text = mediaDuration;
  95. self.durationLabel.hidden = mediaDuration.length == 0;
  96. }
  97. }
  98. - (void)setPlaybackProgress:(CGFloat)playbackProgress {
  99. if (_playbackProgress != playbackProgress) {
  100. _playbackProgress = playbackProgress;
  101. [self.progressObject vlc_setProgress:playbackProgress hideForNoProgress:YES];
  102. }
  103. }
  104. @end