VLCRowController.m 5.2 KB

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