VLCPlaylistTableViewCell.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 14 // twice 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. self.titleLabel.text = self.mediaObject.title;
  56. if (self.isEditing)
  57. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %i MB", [VLCTime timeWithNumber:[self.mediaObject duration]], (int)([self.mediaObject fileSizeInBytes] / 1e6)];
  58. else {
  59. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[self.mediaObject duration]]];
  60. if (self.mediaObject.videoTrack) {
  61. NSString *width = [[self.mediaObject videoTrack] valueForKey:@"width"];
  62. NSString *height = [[self.mediaObject videoTrack] valueForKey:@"height"];
  63. if (width.intValue > 0 && height.intValue > 0)
  64. self.subtitleLabel.text = [self.subtitleLabel.text stringByAppendingFormat:@" — %@x%@", width, height];
  65. }
  66. }
  67. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) {
  68. static NSMutableArray *_thumbnailCacheIndex;
  69. static NSMutableDictionary *_thumbnailCache;
  70. if (!_thumbnailCache)
  71. _thumbnailCache = [[NSMutableDictionary alloc] initWithCapacity:MAX_CACHE_SIZE];
  72. if (!_thumbnailCacheIndex)
  73. _thumbnailCacheIndex = [[NSMutableArray alloc] initWithCapacity:MAX_CACHE_SIZE];
  74. NSManagedObjectID *objID = self.mediaObject.objectID;
  75. UIImage *displayedImage;
  76. if ([_thumbnailCacheIndex containsObject:objID]) {
  77. [_thumbnailCacheIndex removeObject:objID];
  78. [_thumbnailCacheIndex insertObject:objID atIndex:0];
  79. displayedImage = [_thumbnailCache objectForKey:objID];
  80. } else {
  81. if (_thumbnailCacheIndex.count >= MAX_CACHE_SIZE) {
  82. [_thumbnailCache removeObjectForKey:[_thumbnailCacheIndex lastObject]];
  83. [_thumbnailCacheIndex removeLastObject];
  84. }
  85. displayedImage = self.mediaObject.computedThumbnail;
  86. if (displayedImage) {
  87. [_thumbnailCache setObject:self.mediaObject.computedThumbnail forKey:objID];
  88. [_thumbnailCacheIndex insertObject:objID atIndex:0];
  89. }
  90. }
  91. self.thumbnailView.image = displayedImage;
  92. }
  93. self.progressIndicator.progress = self.mediaObject.lastPosition.floatValue;
  94. self.progressIndicator.hidden = ((self.progressIndicator.progress < .1f) || (self.progressIndicator.progress > .95f)) ? YES : NO;
  95. self.mediaIsUnreadView.hidden = !self.mediaObject.unread.intValue;
  96. [self setNeedsDisplay];
  97. }
  98. + (CGFloat)heightOfCell
  99. {
  100. return 80.;
  101. }
  102. @end