VLCPlaylistGridView.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // VLCGridViewCell.m
  3. // AspenProject
  4. //
  5. // Created by Felix Paul Kühne on 11.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 "VLCPlaylistGridView.h"
  11. #import "VLCAppDelegate.h"
  12. #import "AQGridView.h"
  13. #define MAX_CACHE_SIZE 27 // three times the number of items shown on iPad
  14. @interface VLCPlaylistGridView (Hack)
  15. @property (nonatomic, retain) NSString *reuseIdentifier;
  16. @end
  17. @implementation VLCPlaylistGridView
  18. + (CGSize)preferredSize
  19. {
  20. return CGSizeMake(288, 220);
  21. }
  22. - (void)awakeFromNib {
  23. [super awakeFromNib];
  24. _contentView = self;
  25. self.backgroundColor = [UIColor clearColor];
  26. self.reuseIdentifier = @"AQPlaylistCell";
  27. }
  28. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  29. {
  30. [super setEditing:editing animated:animated];
  31. self.removeMediaButton.hidden = !editing;
  32. [self _updatedDisplayedInformationForKeyPath:@"editing"];
  33. }
  34. - (void)prepareForReuse
  35. {
  36. [super prepareForReuse];
  37. self.removeMediaButton.hidden = YES;
  38. }
  39. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  40. {
  41. [self _updatedDisplayedInformationForKeyPath:keyPath];
  42. }
  43. - (void)setMediaObject:(MLFile *)mediaObject
  44. {
  45. if (_mediaObject != mediaObject) {
  46. [_mediaObject removeObserver:self forKeyPath:@"computedThumbnail"];
  47. [_mediaObject removeObserver:self forKeyPath:@"lastPosition"];
  48. [_mediaObject removeObserver:self forKeyPath:@"duration"];
  49. [_mediaObject removeObserver:self forKeyPath:@"fileSizeInBytes"];
  50. [_mediaObject removeObserver:self forKeyPath:@"title"];
  51. [_mediaObject removeObserver:self forKeyPath:@"thumbnailTimeouted"];
  52. [_mediaObject removeObserver:self forKeyPath:@"unread"];
  53. [_mediaObject didHide];
  54. _mediaObject = mediaObject;
  55. [_mediaObject addObserver:self forKeyPath:@"computedThumbnail" options:0 context:nil];
  56. [_mediaObject addObserver:self forKeyPath:@"lastPosition" options:0 context:nil];
  57. [_mediaObject addObserver:self forKeyPath:@"duration" options:0 context:nil];
  58. [_mediaObject addObserver:self forKeyPath:@"fileSizeInBytes" options:0 context:nil];
  59. [_mediaObject addObserver:self forKeyPath:@"title" options:0 context:nil];
  60. [_mediaObject addObserver:self forKeyPath:@"thumbnailTimeouted" options:0 context:nil];
  61. [_mediaObject addObserver:self forKeyPath:@"unread" options:0 context:nil];
  62. [_mediaObject willDisplay];
  63. }
  64. [self _updatedDisplayedInformationForKeyPath:NULL];
  65. }
  66. - (void)_updatedDisplayedInformationForKeyPath:(NSString *)keyPath
  67. {
  68. MLFile *mediaObject = self.mediaObject;
  69. self.titleLabel.text = mediaObject.title;
  70. if (self.isEditing)
  71. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %i MB", [VLCTime timeWithNumber:[mediaObject duration]], (int)([mediaObject fileSizeInBytes] / 1e6)];
  72. else {
  73. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[mediaObject duration]]];
  74. if (mediaObject.videoTrack) {
  75. NSString *width = [[mediaObject videoTrack] valueForKey:@"width"];
  76. NSString *height = [[mediaObject videoTrack] valueForKey:@"height"];
  77. if (width.intValue > 0 && height.intValue > 0)
  78. self.subtitleLabel.text = [self.subtitleLabel.text stringByAppendingFormat:@" — %@x%@", width, height];
  79. }
  80. }
  81. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) {
  82. static NSMutableArray *_thumbnailCacheIndex;
  83. static NSMutableDictionary *_thumbnailCache;
  84. if (!_thumbnailCache)
  85. _thumbnailCache = [[NSMutableDictionary alloc] initWithCapacity:MAX_CACHE_SIZE];
  86. if (!_thumbnailCacheIndex)
  87. _thumbnailCacheIndex = [[NSMutableArray alloc] initWithCapacity:MAX_CACHE_SIZE];
  88. NSManagedObjectID *objID = mediaObject.objectID;
  89. UIImage *displayedImage;
  90. if ([_thumbnailCacheIndex containsObject:objID]) {
  91. [_thumbnailCacheIndex removeObject:objID];
  92. [_thumbnailCacheIndex insertObject:objID atIndex:0];
  93. displayedImage = [_thumbnailCache objectForKey:objID];
  94. } else {
  95. if (_thumbnailCacheIndex.count >= MAX_CACHE_SIZE) {
  96. [_thumbnailCache removeObjectForKey:[_thumbnailCacheIndex lastObject]];
  97. [_thumbnailCacheIndex removeLastObject];
  98. }
  99. displayedImage = mediaObject.computedThumbnail;
  100. if (displayedImage) {
  101. [_thumbnailCache setObject:displayedImage forKey:objID];
  102. [_thumbnailCacheIndex insertObject:objID atIndex:0];
  103. }
  104. }
  105. self.thumbnailView.image = displayedImage;
  106. }
  107. CGFloat position = mediaObject.lastPosition.floatValue;
  108. self.progressView.progress = position;
  109. self.progressView.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
  110. self.mediaIsUnreadView.hidden = !mediaObject.unread.intValue;
  111. [self setNeedsDisplay];
  112. }
  113. - (IBAction)removeMedia:(id)sender
  114. {
  115. /* ask user if s/he really wants to delete the media file */
  116. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DELETE_FILE", @"") message:[NSString stringWithFormat:NSLocalizedString(@"DELETE_FILE_LONG", @""), self.mediaObject.title] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_DELETE", @""), nil];
  117. [alert show];
  118. }
  119. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  120. {
  121. if (buttonIndex == 1) {
  122. NSUInteger cellIndex = [self.gridView indexForCell:self];
  123. [self.gridView.delegate gridView:self.gridView commitEditingStyle:UITableViewCellEditingStyleDelete forRowAtIndex:cellIndex];
  124. }
  125. }
  126. @end