VLCRowController.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. @end
  21. @implementation VLCRowController
  22. - (instancetype)init
  23. {
  24. self = [super init];
  25. if (self) {
  26. [self calculateThumbnailSizeAndRowWidth];
  27. }
  28. return self;
  29. }
  30. - (void)calculateThumbnailSizeAndRowWidth
  31. {
  32. WKInterfaceDevice *currentDevice = WKInterfaceDevice.currentDevice;
  33. CGRect screenRect = currentDevice.screenBounds;
  34. CGFloat screenScale = currentDevice.screenScale;
  35. _thumbnailSize = CGRectMake(0,
  36. 0,
  37. screenRect.size.width * screenScale,
  38. 120. * screenScale
  39. );
  40. _rowWidth = screenRect.size.width * screenScale;
  41. }
  42. - (void)configureWithMediaLibraryObject:(id)storageObject
  43. {
  44. float playbackProgress = 0.0;
  45. if ([storageObject isKindOfClass:[MLShow class]]) {
  46. self.titleLabel.text = ((MLAlbum *)storageObject).name;
  47. } else if ([storageObject isKindOfClass:[MLShowEpisode class]]) {
  48. self.titleLabel.text = ((MLShowEpisode *)storageObject).name;
  49. } else if ([storageObject isKindOfClass:[MLLabel class]]) {
  50. self.titleLabel.text = ((MLLabel *)storageObject).name;
  51. } else if ([storageObject isKindOfClass:[MLAlbum class]]) {
  52. self.titleLabel.text = ((MLAlbum *)storageObject).name;
  53. } else if ([storageObject isKindOfClass:[MLAlbumTrack class]]) {
  54. self.titleLabel.text = ((MLAlbumTrack *)storageObject).title;
  55. } else if ([storageObject isKindOfClass:[MLFile class]]){
  56. MLFile *file = (MLFile *)storageObject;
  57. self.titleLabel.text = [file title];
  58. playbackProgress = file.lastPosition.floatValue;
  59. }
  60. [self.progressObject vlc_setProgress:playbackProgress hideForNoProgress:YES];
  61. /* FIXME: add placeholder image once designed */
  62. if (storageObject != self.mediaLibraryObject) {
  63. self.group.backgroundImage = [UIImage imageNamed:@"tableview-gradient"];
  64. }
  65. NSArray *array = @[self.group, storageObject];
  66. [self performSelectorInBackground:@selector(backgroundThumbnailSetter:) withObject:array];
  67. self.mediaLibraryObject = storageObject;
  68. }
  69. - (void)backgroundThumbnailSetter:(NSArray *)array
  70. {
  71. UIImage *backgroundImage = [VLCThumbnailsCache thumbnailForManagedObject:array[1] toFitRect:_thumbnailSize shouldReplaceCache:YES];
  72. UIImage *gradient = [UIImage imageNamed:@"tableview-gradient"];
  73. CGSize newSize = backgroundImage ? backgroundImage.size : CGSizeMake(_rowWidth, 120.);
  74. UIGraphicsBeginImageContext(newSize);
  75. if (backgroundImage)
  76. [backgroundImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  77. else {
  78. [[UIColor darkGrayColor] set];
  79. UIRectFill(CGRectMake(.0, .0, newSize.width, newSize.height));
  80. }
  81. [gradient drawInRect:CGRectMake(.0, newSize.height / 2., newSize.width, newSize.height / 2.) blendMode:kCGBlendModeNormal alpha:1.];
  82. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  83. UIGraphicsEndImageContext();
  84. [array.firstObject performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:newImage waitUntilDone:NO];
  85. }
  86. @end