VLCRowController.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. NSString *objectType = nil;
  49. if ([storageObject isKindOfClass:[MLShow class]]) {
  50. objectType = NSLocalizedString(@"OBJECT_TYPE_SHOW", nil);
  51. title = ((MLAlbum *)storageObject).name;
  52. } else if ([storageObject isKindOfClass:[MLShowEpisode class]]) {
  53. objectType = NSLocalizedString(@"OBJECT_TYPE_SHOW_EPISODE", nil);
  54. title = ((MLShowEpisode *)storageObject).name;
  55. } else if ([storageObject isKindOfClass:[MLLabel class]]) {
  56. objectType = NSLocalizedString(@"OBJECT_TYPE_LABEL", nil);
  57. title = ((MLLabel *)storageObject).name;
  58. } else if ([storageObject isKindOfClass:[MLAlbum class]]) {
  59. objectType = NSLocalizedString(@"OBJECT_TYPE_ALBUM", nil);
  60. title = ((MLAlbum *)storageObject).name;
  61. } else if ([storageObject isKindOfClass:[MLAlbumTrack class]]) {
  62. objectType = NSLocalizedString(@"OBJECT_TYPE_ALBUM_TRACK", nil);
  63. title = ((MLAlbumTrack *)storageObject).title;
  64. } else if ([storageObject isKindOfClass:[MLFile class]]){
  65. MLFile *file = (MLFile *)storageObject;
  66. title = [file title];
  67. playbackProgress = file.lastPosition.floatValue;
  68. if (file.isSupportedAudioFile) {
  69. objectType = NSLocalizedString(@"OBJECT_TYPE_FILE_AUDIO", nil);
  70. } else {
  71. objectType = NSLocalizedString(@"OBJECT_TYPE_FILE", nil);
  72. }
  73. }
  74. self.titleLabel.accessibilityValue = objectType;
  75. self.mediaTitle = title;
  76. self.playbackProgress = playbackProgress;
  77. /* FIXME: add placeholder image once designed */
  78. if (storageObject != self.mediaLibraryObject) {
  79. self.group.backgroundImage = [UIImage imageNamed:@"tableview-gradient"];
  80. }
  81. NSArray *array = @[self.group, storageObject];
  82. [self performSelectorInBackground:@selector(backgroundThumbnailSetter:) withObject:array];
  83. self.mediaLibraryObject = storageObject;
  84. }
  85. - (void)backgroundThumbnailSetter:(NSArray *)array
  86. {
  87. UIImage *backgroundImage = [VLCThumbnailsCache thumbnailForManagedObject:array[1] toFitRect:_thumbnailSize shouldReplaceCache:YES];
  88. // don't redo image processing if no necessary
  89. if ([self.rawBackgroundImage isEqual:backgroundImage]) {
  90. return;
  91. }
  92. self.rawBackgroundImage = backgroundImage;
  93. UIImage *gradient = [UIImage imageNamed:@"tableview-gradient"];
  94. CGSize newSize = backgroundImage ? backgroundImage.size : CGSizeMake(_rowWidth, 120.);
  95. UIGraphicsBeginImageContext(newSize);
  96. if (backgroundImage)
  97. [backgroundImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  98. else {
  99. [[UIColor darkGrayColor] set];
  100. UIRectFill(CGRectMake(.0, .0, newSize.width, newSize.height));
  101. }
  102. [gradient drawInRect:CGRectMake(.0, newSize.height / 2., newSize.width, newSize.height / 2.) blendMode:kCGBlendModeNormal alpha:1.];
  103. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  104. UIGraphicsEndImageContext();
  105. [array.firstObject performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:newImage waitUntilDone:NO];
  106. }
  107. - (void)setMediaTitle:(NSString *)mediaTitle {
  108. if (![_mediaTitle isEqualToString:mediaTitle]) {
  109. _mediaTitle = [mediaTitle copy];
  110. self.titleLabel.text = mediaTitle;
  111. self.accessibilityLabel = mediaTitle;
  112. self.titleLabel.hidden = mediaTitle.length == 0;
  113. }
  114. }
  115. - (void)setPlaybackProgress:(CGFloat)playbackProgress {
  116. if (_playbackProgress != playbackProgress) {
  117. _playbackProgress = playbackProgress;
  118. [self.progressObject vlc_setProgress:playbackProgress hideForNoProgress:YES];
  119. }
  120. }
  121. @end