VLCRowController.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*****************************************************************************
  2. * VLCRowController.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 "VLCRowController.h"
  13. #import "WKInterfaceObject+VLCProgress.h"
  14. #import "VLCThumbnailsCache.h"
  15. #import "VLCWatchMessage.h"
  16. #import <WatchConnectivity/WatchConnectivity.h>
  17. @interface VLCRowController()
  18. @property (nonatomic, weak, readwrite) NSManagedObject *mediaLibraryObject;
  19. @property (nonatomic, readonly) CGRect thumbnailSize;
  20. @property (nonatomic, readonly) CGFloat rowWidth;
  21. @property (nonatomic, readonly) CGFloat scale;
  22. @property (nonatomic) UIImage *rawBackgroundImage;
  23. @end
  24. @implementation VLCRowController
  25. - (instancetype)init
  26. {
  27. self = [super init];
  28. if (self) {
  29. [self calculateThumbnailSizeAndRowWidth];
  30. _playbackProgress = -1;
  31. }
  32. return self;
  33. }
  34. - (void)calculateThumbnailSizeAndRowWidth
  35. {
  36. WKInterfaceDevice *currentDevice = WKInterfaceDevice.currentDevice;
  37. CGRect screenRect = currentDevice.screenBounds;
  38. CGFloat screenScale = currentDevice.screenScale;
  39. _thumbnailSize = CGRectMake(0,
  40. 0,
  41. screenRect.size.width,
  42. 120.
  43. );
  44. _rowWidth = screenRect.size.width;
  45. _scale = screenScale;
  46. }
  47. - (void)configureWithMediaLibraryObject:(NSManagedObject *)storageObject
  48. {
  49. NSString *title = nil;
  50. float playbackProgress = 0.0;
  51. NSString *objectType = nil;
  52. if ([storageObject isKindOfClass:[MLShow class]]) {
  53. objectType = NSLocalizedString(@"OBJECT_TYPE_SHOW", nil);
  54. title = ((MLAlbum *)storageObject).name;
  55. } else if ([storageObject isKindOfClass:[MLShowEpisode class]]) {
  56. objectType = NSLocalizedString(@"OBJECT_TYPE_SHOW_EPISODE", nil);
  57. title = ((MLShowEpisode *)storageObject).name;
  58. } else if ([storageObject isKindOfClass:[MLLabel class]]) {
  59. objectType = NSLocalizedString(@"OBJECT_TYPE_LABEL", nil);
  60. title = ((MLLabel *)storageObject).name;
  61. } else if ([storageObject isKindOfClass:[MLAlbum class]]) {
  62. objectType = NSLocalizedString(@"OBJECT_TYPE_ALBUM", nil);
  63. title = ((MLAlbum *)storageObject).name;
  64. } else if ([storageObject isKindOfClass:[MLAlbumTrack class]]) {
  65. objectType = NSLocalizedString(@"OBJECT_TYPE_ALBUM_TRACK", nil);
  66. title = ((MLAlbumTrack *)storageObject).title;
  67. } else if ([storageObject isKindOfClass:[MLFile class]]){
  68. MLFile *file = (MLFile *)storageObject;
  69. title = [file title];
  70. playbackProgress = file.lastPosition.floatValue;
  71. if (file.isSupportedAudioFile) {
  72. objectType = NSLocalizedString(@"OBJECT_TYPE_FILE_AUDIO", nil);
  73. } else {
  74. objectType = NSLocalizedString(@"OBJECT_TYPE_FILE", nil);
  75. }
  76. }
  77. self.titleLabel.accessibilityValue = objectType;
  78. self.mediaTitle = title;
  79. self.playbackProgress = playbackProgress;
  80. /* FIXME: add placeholder image once designed */
  81. if (![storageObject.objectID.URIRepresentation isEqual: self.mediaLibraryObject.objectID.URIRepresentation]) {
  82. self.group.backgroundImage = [UIImage imageNamed:@"tableview-gradient"];
  83. }
  84. NSArray *array = @[self.group, storageObject];
  85. [self performSelectorInBackground:@selector(backgroundThumbnailSetter:) withObject:array];
  86. self.mediaLibraryObject = storageObject;
  87. }
  88. - (void)requestBackgroundImageForObjectID:(NSManagedObjectID *)objectID {
  89. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameRequestThumbnail
  90. payload:@{VLCWatchMessageKeyURIRepresentation : objectID.URIRepresentation.absoluteString}];
  91. [[WCSession defaultSession] sendMessage:dict replyHandler:nil errorHandler:nil];
  92. }
  93. - (void)backgroundThumbnailSetter:(NSArray *)array
  94. {
  95. WKInterfaceGroup *interfaceGroup = array.firstObject;
  96. NSManagedObject *managedObject = array[1];
  97. UIImage *backgroundImage = [VLCThumbnailsCache thumbnailForManagedObject:managedObject
  98. refreshCache:NO
  99. toFitRect:_thumbnailSize
  100. scale:_scale
  101. shouldReplaceCache:YES];
  102. if (!backgroundImage) {
  103. [self requestBackgroundImageForObjectID:managedObject.objectID];
  104. return;
  105. }
  106. // don't redo image processing if no necessary
  107. if ([self.rawBackgroundImage isEqual:backgroundImage]) {
  108. return;
  109. }
  110. self.rawBackgroundImage = backgroundImage;
  111. UIImage *gradient = [UIImage imageNamed:@"tableview-gradient"];
  112. CGSize newSize = backgroundImage ? backgroundImage.size : CGSizeMake(_rowWidth, 120.);
  113. UIGraphicsBeginImageContextWithOptions(newSize, YES, _scale);
  114. if (backgroundImage)
  115. [backgroundImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  116. else {
  117. [[UIColor darkGrayColor] set];
  118. UIRectFill(CGRectMake(0., 0., newSize.width, newSize.height));
  119. }
  120. [gradient drawInRect:CGRectMake(0., 0., newSize.width, newSize.height / 2.) blendMode:kCGBlendModeNormal alpha:1.];
  121. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  122. UIGraphicsEndImageContext();
  123. [interfaceGroup performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:newImage waitUntilDone:NO];
  124. }
  125. - (void)setMediaTitle:(NSString *)mediaTitle {
  126. if (![_mediaTitle isEqualToString:mediaTitle]) {
  127. _mediaTitle = [mediaTitle copy];
  128. self.titleLabel.text = mediaTitle;
  129. // self.accessibilityLabel = mediaTitle; // TODO: toco
  130. self.titleLabel.hidden = mediaTitle.length == 0;
  131. }
  132. }
  133. - (void)setPlaybackProgress:(CGFloat)playbackProgress {
  134. if (_playbackProgress != playbackProgress) {
  135. _playbackProgress = playbackProgress;
  136. [self.progressObject vlc_setProgress:playbackProgress hideForNoProgress:YES];
  137. }
  138. }
  139. @end