VLCPlaylistGridView.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 18 // twice 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. self.titleLabel.text = self.mediaObject.title;
  69. if (self.isEditing)
  70. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %i MB", [VLCTime timeWithNumber:[self.mediaObject duration]], (int)([self.mediaObject fileSizeInBytes] / 1e6)];
  71. else {
  72. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[self.mediaObject duration]]];
  73. if (self.mediaObject.videoTrack) {
  74. NSString *width = [[self.mediaObject videoTrack] valueForKey:@"width"];
  75. NSString *height = [[self.mediaObject videoTrack] valueForKey:@"height"];
  76. if (width.intValue > 0 && height.intValue > 0)
  77. self.subtitleLabel.text = [self.subtitleLabel.text stringByAppendingFormat:@" — %@x%@", width, height];
  78. }
  79. }
  80. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) {
  81. static NSMutableArray *_thumbnailCacheIndex;
  82. static NSMutableDictionary *_thumbnailCache;
  83. if (!_thumbnailCache)
  84. _thumbnailCache = [[NSMutableDictionary alloc] initWithCapacity:MAX_CACHE_SIZE];
  85. if (!_thumbnailCacheIndex)
  86. _thumbnailCacheIndex = [[NSMutableArray alloc] initWithCapacity:MAX_CACHE_SIZE];
  87. NSManagedObjectID *objID = self.mediaObject.objectID;
  88. UIImage *displayedImage;
  89. if ([_thumbnailCacheIndex containsObject:objID]) {
  90. [_thumbnailCacheIndex removeObject:objID];
  91. [_thumbnailCacheIndex insertObject:objID atIndex:0];
  92. displayedImage = [_thumbnailCache objectForKey:objID];
  93. } else {
  94. if (_thumbnailCacheIndex.count >= MAX_CACHE_SIZE) {
  95. [_thumbnailCache removeObjectForKey:[_thumbnailCacheIndex lastObject]];
  96. [_thumbnailCacheIndex removeLastObject];
  97. }
  98. displayedImage = self.mediaObject.computedThumbnail;
  99. if (displayedImage) {
  100. [_thumbnailCache setObject:self.mediaObject.computedThumbnail forKey:objID];
  101. [_thumbnailCacheIndex insertObject:objID atIndex:0];
  102. }
  103. }
  104. self.thumbnailView.image = displayedImage;
  105. }
  106. self.progressView.progress = self.mediaObject.lastPosition.floatValue;
  107. self.progressView.hidden = (self.progressView.progress < .1f || .95f) ? YES : NO;
  108. self.mediaIsUnreadView.hidden = !self.mediaObject.unread.intValue;
  109. [self setNeedsDisplay];
  110. }
  111. - (IBAction)removeMedia:(id)sender
  112. {
  113. /* ask user if s/he really wants to delete the media file */
  114. 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];
  115. [alert show];
  116. }
  117. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  118. {
  119. if (buttonIndex == 1) {
  120. NSUInteger cellIndex = [self.gridView indexForCell:self];
  121. [self.gridView.delegate gridView:self.gridView commitEditingStyle:UITableViewCellEditingStyleDelete forRowAtIndex:cellIndex];
  122. }
  123. }
  124. @end