VLCPlaylistTableViewCell.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // VLCPlaylistTableViewCell.m
  3. // AspenProject
  4. //
  5. // Created by Felix Paul Kühne on 01.04.13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. // Refer to the COPYING file of the official project for license.
  9. //
  10. #import "VLCPlaylistTableViewCell.h"
  11. #define MAX_CACHE_SIZE 21 // three times the number of items shown on iPhone 5
  12. @implementation VLCPlaylistTableViewCell
  13. + (VLCPlaylistTableViewCell *)cellWithReuseIdentifier:(NSString *)ident
  14. {
  15. NSArray *nibContentArray = [[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistTableViewCell" owner:nil options:nil];
  16. NSAssert([nibContentArray count] == 1, @"meh");
  17. NSAssert([[nibContentArray lastObject] isKindOfClass:[VLCPlaylistTableViewCell class]], @"meh meh");
  18. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[nibContentArray lastObject];
  19. return cell;
  20. }
  21. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  22. {
  23. [self _updatedDisplayedInformationForKeyPath:keyPath];
  24. }
  25. - (void)setMediaObject:(MLFile *)mediaObject
  26. {
  27. if (_mediaObject != mediaObject) {
  28. [_mediaObject removeObserver:self forKeyPath:@"computedThumbnail"];
  29. [_mediaObject removeObserver:self forKeyPath:@"lastPosition"];
  30. [_mediaObject removeObserver:self forKeyPath:@"duration"];
  31. [_mediaObject removeObserver:self forKeyPath:@"fileSizeInBytes"];
  32. [_mediaObject removeObserver:self forKeyPath:@"title"];
  33. [_mediaObject removeObserver:self forKeyPath:@"thumbnailTimeouted"];
  34. [_mediaObject removeObserver:self forKeyPath:@"unread"];
  35. [_mediaObject didHide];
  36. _mediaObject = mediaObject;
  37. [_mediaObject addObserver:self forKeyPath:@"computedThumbnail" options:0 context:nil];
  38. [_mediaObject addObserver:self forKeyPath:@"lastPosition" options:0 context:nil];
  39. [_mediaObject addObserver:self forKeyPath:@"duration" options:0 context:nil];
  40. [_mediaObject addObserver:self forKeyPath:@"fileSizeInBytes" options:0 context:nil];
  41. [_mediaObject addObserver:self forKeyPath:@"title" options:0 context:nil];
  42. [_mediaObject addObserver:self forKeyPath:@"thumbnailTimeouted" options:0 context:nil];
  43. [_mediaObject addObserver:self forKeyPath:@"unread" options:0 context:nil];
  44. [_mediaObject willDisplay];
  45. }
  46. [self _updatedDisplayedInformationForKeyPath:NULL];
  47. }
  48. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  49. {
  50. [super setEditing:editing animated:animated];
  51. [self _updatedDisplayedInformationForKeyPath:@"editing"];
  52. }
  53. - (void)_updatedDisplayedInformationForKeyPath:(NSString *)keyPath
  54. {
  55. MLFile *mediaObject = self.mediaObject;
  56. self.titleLabel.text = mediaObject.title;
  57. if (self.isEditing)
  58. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %i MB", [VLCTime timeWithNumber:[mediaObject duration]], (int)([mediaObject fileSizeInBytes] / 1e6)];
  59. else {
  60. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[mediaObject duration]]];
  61. if (mediaObject.videoTrack) {
  62. NSString *width = [[mediaObject videoTrack] valueForKey:@"width"];
  63. NSString *height = [[mediaObject videoTrack] valueForKey:@"height"];
  64. if (width.intValue > 0 && height.intValue > 0)
  65. self.subtitleLabel.text = [self.subtitleLabel.text stringByAppendingFormat:@" — %@x%@", width, height];
  66. }
  67. }
  68. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) {
  69. static NSMutableArray *_thumbnailCacheIndex;
  70. static NSMutableDictionary *_thumbnailCache;
  71. if (!_thumbnailCache)
  72. _thumbnailCache = [[NSMutableDictionary alloc] initWithCapacity:MAX_CACHE_SIZE];
  73. if (!_thumbnailCacheIndex)
  74. _thumbnailCacheIndex = [[NSMutableArray alloc] initWithCapacity:MAX_CACHE_SIZE];
  75. NSManagedObjectID *objID = mediaObject.objectID;
  76. UIImage *displayedImage;
  77. if ([_thumbnailCacheIndex containsObject:objID]) {
  78. [_thumbnailCacheIndex removeObject:objID];
  79. [_thumbnailCacheIndex insertObject:objID atIndex:0];
  80. displayedImage = [_thumbnailCache objectForKey:objID];
  81. } else {
  82. if (_thumbnailCacheIndex.count >= MAX_CACHE_SIZE) {
  83. [_thumbnailCache removeObjectForKey:[_thumbnailCacheIndex lastObject]];
  84. [_thumbnailCacheIndex removeLastObject];
  85. }
  86. displayedImage = mediaObject.computedThumbnail;
  87. if (displayedImage) {
  88. [_thumbnailCache setObject:displayedImage forKey:objID];
  89. [_thumbnailCacheIndex insertObject:objID atIndex:0];
  90. }
  91. }
  92. self.thumbnailView.image = displayedImage;
  93. }
  94. CGFloat position = mediaObject.lastPosition.floatValue;
  95. self.progressIndicator.progress = position;
  96. self.progressIndicator.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
  97. self.mediaIsUnreadView.hidden = !mediaObject.unread.intValue;
  98. [self setNeedsDisplay];
  99. }
  100. + (CGFloat)heightOfCell
  101. {
  102. return 80.;
  103. }
  104. @end