VLCRowController.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <MediaLibraryKit/MediaLibraryKit.h>
  14. #import "WKInterfaceObject+VLCProgress.h"
  15. #import "VLCThumbnailsCache.h"
  16. @interface VLCRowController()
  17. @property (nonatomic, weak, readwrite) id mediaLibraryObject;
  18. @property (nonatomic, readonly) CGRect thumbnailSize;
  19. @property (nonatomic, readonly) CGFloat rowWidth;
  20. @property (nonatomic) UIImage *rawBackgroundImage;
  21. @end
  22. @implementation VLCRowController
  23. - (instancetype)init
  24. {
  25. self = [super init];
  26. if (self) {
  27. [self calculateThumbnailSizeAndRowWidth];
  28. _playbackProgress = -1;
  29. }
  30. return self;
  31. }
  32. - (void)calculateThumbnailSizeAndRowWidth
  33. {
  34. WKInterfaceDevice *currentDevice = WKInterfaceDevice.currentDevice;
  35. CGRect screenRect = currentDevice.screenBounds;
  36. CGFloat screenScale = currentDevice.screenScale;
  37. _thumbnailSize = CGRectMake(0,
  38. 0,
  39. screenRect.size.width * screenScale,
  40. 120. * screenScale
  41. );
  42. _rowWidth = screenRect.size.width * screenScale;
  43. }
  44. - (void)configureWithMediaLibraryObject:(id)storageObject
  45. {
  46. NSString *title = nil;
  47. float playbackProgress = 0.0;
  48. if ([storageObject isKindOfClass:[MLShow class]]) {
  49. title = ((MLAlbum *)storageObject).name;
  50. } else if ([storageObject isKindOfClass:[MLShowEpisode class]]) {
  51. title = ((MLShowEpisode *)storageObject).name;
  52. } else if ([storageObject isKindOfClass:[MLLabel class]]) {
  53. title = ((MLLabel *)storageObject).name;
  54. } else if ([storageObject isKindOfClass:[MLAlbum class]]) {
  55. title = ((MLAlbum *)storageObject).name;
  56. } else if ([storageObject isKindOfClass:[MLAlbumTrack class]]) {
  57. title = ((MLAlbumTrack *)storageObject).title;
  58. } else if ([storageObject isKindOfClass:[MLFile class]]){
  59. MLFile *file = (MLFile *)storageObject;
  60. title = [file title];
  61. playbackProgress = file.lastPosition.floatValue;
  62. }
  63. self.mediaTitle = title;
  64. self.playbackProgress = playbackProgress;
  65. /* FIXME: add placeholder image once designed */
  66. if (storageObject != self.mediaLibraryObject) {
  67. self.group.backgroundImage = [UIImage imageNamed:@"tableview-gradient"];
  68. }
  69. NSArray *array = @[self.group, storageObject];
  70. [self performSelectorInBackground:@selector(backgroundThumbnailSetter:) withObject:array];
  71. self.mediaLibraryObject = storageObject;
  72. }
  73. - (void)backgroundThumbnailSetter:(NSArray *)array
  74. {
  75. UIImage *backgroundImage = [VLCThumbnailsCache thumbnailForManagedObject:array[1] toFitRect:_thumbnailSize shouldReplaceCache:YES];
  76. // don't redo image processing if no necessary
  77. if ([self.rawBackgroundImage isEqual:backgroundImage]) {
  78. return;
  79. }
  80. self.rawBackgroundImage = backgroundImage;
  81. UIImage *gradient = [UIImage imageNamed:@"tableview-gradient"];
  82. CGSize newSize = backgroundImage ? backgroundImage.size : CGSizeMake(_rowWidth, 120.);
  83. UIGraphicsBeginImageContext(newSize);
  84. if (backgroundImage)
  85. [backgroundImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  86. else {
  87. [[UIColor darkGrayColor] set];
  88. UIRectFill(CGRectMake(.0, .0, newSize.width, newSize.height));
  89. }
  90. [gradient drawInRect:CGRectMake(.0, newSize.height / 2., newSize.width, newSize.height / 2.) blendMode:kCGBlendModeNormal alpha:1.];
  91. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  92. UIGraphicsEndImageContext();
  93. [array.firstObject performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:newImage waitUntilDone:NO];
  94. }
  95. - (void)setMediaTitle:(NSString *)mediaTitle {
  96. if (![_mediaTitle isEqualToString:mediaTitle]) {
  97. _mediaTitle = [mediaTitle copy];
  98. self.titleLabel.text = mediaTitle;
  99. self.accessibilityValue = mediaTitle;
  100. self.titleLabel.hidden = mediaTitle.length == 0;
  101. }
  102. }
  103. - (void)setPlaybackProgress:(CGFloat)playbackProgress {
  104. if (_playbackProgress != playbackProgress) {
  105. _playbackProgress = playbackProgress;
  106. [self.progressObject vlc_setProgress:playbackProgress hideForNoProgress:YES];
  107. }
  108. }
  109. @end