Browse Source

library view: add support for runtime meta data changes (close #8658)

aka thumbnails showup after being generated asynchronously
Felix Paul Kühne 12 years ago
parent
commit
02ab754778

+ 36 - 7
AspenProject/VLCPlaylistGridView.m

@@ -36,19 +36,48 @@
 
 + (CGSize)preferredSize
 {
-    return CGSizeMake(384, 240);
+    return CGSizeMake(384, 243);
+}
+
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
+{
+    [self _updatedDisplayedInformation];
 }
 
 - (void)setMediaObject:(MLFile *)mediaObject
 {
-    [mediaObject willDisplay];
+    if (_mediaObject != mediaObject) {
+        [_mediaObject removeObserver:self forKeyPath:@"computedThumbnail"];
+        [_mediaObject removeObserver:self forKeyPath:@"lastPosition"];
+        [_mediaObject removeObserver:self forKeyPath:@"duration"];
+        [_mediaObject removeObserver:self forKeyPath:@"fileSizeInBytes"];
+        [_mediaObject removeObserver:self forKeyPath:@"title"];
+        [_mediaObject removeObserver:self forKeyPath:@"thumbnailTimeouted"];
+        [_mediaObject didHide];
+
+        _mediaObject = mediaObject;
 
-    self.titleLabel.text = mediaObject.title;
-    self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %.2f MB", [VLCTime timeWithNumber:[mediaObject duration]], [mediaObject fileSizeInBytes] / 2e6];
-    self.thumbnailView.image = mediaObject.computedThumbnail;
-    self.progressView.progress = mediaObject.lastPosition.floatValue;
+        [_mediaObject addObserver:self forKeyPath:@"computedThumbnail" options:0 context:nil];
+        [_mediaObject addObserver:self forKeyPath:@"lastPosition" options:0 context:nil];
+        [_mediaObject addObserver:self forKeyPath:@"duration" options:0 context:nil];
+        [_mediaObject addObserver:self forKeyPath:@"fileSizeInBytes" options:0 context:nil];
+        [_mediaObject addObserver:self forKeyPath:@"title" options:0 context:nil];
+        [_mediaObject addObserver:self forKeyPath:@"thumbnailTimeouted" options:0 context:nil];
+        [_mediaObject willDisplay];
+    }
+
+    [self _updatedDisplayedInformation];
+}
+
+- (void)_updatedDisplayedInformation
+{
+    self.titleLabel.text = self.mediaObject.title;
+    self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %.2f MB", [VLCTime timeWithNumber:[self.mediaObject duration]], [self.mediaObject fileSizeInBytes] / 2e6];
+    self.thumbnailView.image = self.mediaObject.computedThumbnail;
+    self.progressView.progress = self.mediaObject.lastPosition.floatValue;
 
-    _mediaObject = mediaObject;
+    if (self.progressView.progress < 0.1f)
+        self.progressView.hidden = YES;
 
     [self setNeedsDisplay];
 }

+ 2 - 0
AspenProject/VLCPlaylistTableViewCell.h

@@ -16,6 +16,8 @@
 @property (nonatomic, strong) IBOutlet UIImageView *thumbnailView;
 @property (nonatomic, strong) IBOutlet VLCLinearProgressIndicator*progressIndicator;
 
+@property (nonatomic, retain) MLFile *mediaObject;
+
 + (VLCPlaylistTableViewCell *)cellWithReuseIdentifier:(NSString *)ident;
 + (CGFloat)heightOfCell;
 

+ 38 - 0
AspenProject/VLCPlaylistTableViewCell.m

@@ -27,6 +27,44 @@
     return cell;
 }
 
+- (void)setMediaObject:(MLFile *)mediaObject
+{
+    if (_mediaObject != mediaObject) {
+        [_mediaObject removeObserver:self forKeyPath:@"computedThumbnail"];
+        [_mediaObject removeObserver:self forKeyPath:@"lastPosition"];
+        [_mediaObject removeObserver:self forKeyPath:@"duration"];
+        [_mediaObject removeObserver:self forKeyPath:@"fileSizeInBytes"];
+        [_mediaObject removeObserver:self forKeyPath:@"title"];
+        [_mediaObject removeObserver:self forKeyPath:@"thumbnailTimeouted"];
+        [_mediaObject didHide];
+
+        _mediaObject = mediaObject;
+
+        [_mediaObject addObserver:self forKeyPath:@"computedThumbnail" options:0 context:nil];
+        [_mediaObject addObserver:self forKeyPath:@"lastPosition" options:0 context:nil];
+        [_mediaObject addObserver:self forKeyPath:@"duration" options:0 context:nil];
+        [_mediaObject addObserver:self forKeyPath:@"fileSizeInBytes" options:0 context:nil];
+        [_mediaObject addObserver:self forKeyPath:@"title" options:0 context:nil];
+        [_mediaObject addObserver:self forKeyPath:@"thumbnailTimeouted" options:0 context:nil];
+        [_mediaObject willDisplay];
+    }
+
+    [self _updatedDisplayedInformation];
+}
+
+- (void)_updatedDisplayedInformation
+{
+    self.titleLabel.text = self.mediaObject.title;
+    self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %.2f MB", [VLCTime timeWithNumber:[self.mediaObject duration]], [self.mediaObject fileSizeInBytes] / 2e6];
+    self.thumbnailView.image = self.mediaObject.computedThumbnail;
+    self.progressIndicator.progress = self.mediaObject.lastPosition.floatValue;
+
+    if (self.progressIndicator.progress < 0.1f)
+        self.progressIndicator.hidden = YES;
+
+    [self setNeedsDisplay];
+}
+
 + (CGFloat)heightOfCell
 {
     return 80.;

+ 2 - 7
AspenProject/VLCPlaylistViewController.m

@@ -111,13 +111,8 @@
     if (cell == nil)
         cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
 
-    MLFile *object = _foundMedia[indexPath.row];
-    cell.titleLabel.text = object.title;
-    cell.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %.2f MB", [VLCTime timeWithNumber:[object duration]], [object fileSizeInBytes] / 2e6];
-    cell.thumbnailView.image = object.computedThumbnail;
-    cell.progressIndicator.progress = object.lastPosition.floatValue;
-    if (cell.progressIndicator.progress < 0.1f)
-        cell.progressIndicator.hidden = YES;
+    cell.mediaObject = _foundMedia[indexPath.row];
+
     return cell;
 }