Browse Source

move row configuration to RowController class to reduce unnecessary updates (e.g. of images)

Tobias Conradi 10 years ago
parent
commit
4b885c616a

+ 3 - 66
VLC for iOS WatchKit Extension/VLCPlaylistInterfaceController.m

@@ -19,8 +19,6 @@
 
 
 #import "VLCNotificationRelay.h"
 #import "VLCNotificationRelay.h"
 #import "VLCWatchTableController.h"
 #import "VLCWatchTableController.h"
-#import "VLCThumbnailsCache.h"
-#import "WKInterfaceObject+VLCProgress.h"
 #import "NSManagedObjectContext+refreshAll.h"
 #import "NSManagedObjectContext+refreshAll.h"
 
 
 static NSString *const rowType = @"mediaRow";
 static NSString *const rowType = @"mediaRow";
@@ -51,13 +49,6 @@ typedef enum {
 - (void)awakeWithContext:(id)context {
 - (void)awakeWithContext:(id)context {
     [super awakeWithContext:context];
     [super awakeWithContext:context];
 
 
-    WKInterfaceDevice *currentDevice = WKInterfaceDevice.currentDevice;
-    CGRect screenRect = currentDevice.screenBounds;
-    CGFloat screenScale = currentDevice.screenScale;
-    currentDevice = nil;
-    _thumbnailSize = (CGRect){CGPointZero, CGSizeMake(screenRect.size.width * screenScale, 120. * screenScale)};
-    _rowWidth = screenRect.size.width * screenScale;
-
     NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.org.videolan.vlc-ios"];
     NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.org.videolan.vlc-ios"];
     MLMediaLibrary *mediaLibrary = [MLMediaLibrary sharedMediaLibrary];
     MLMediaLibrary *mediaLibrary = [MLMediaLibrary sharedMediaLibrary];
     mediaLibrary.libraryBasePath = groupURL.path;
     mediaLibrary.libraryBasePath = groupURL.path;
@@ -85,9 +76,10 @@ typedef enum {
     tableController.pageSize = 5;
     tableController.pageSize = 5;
     tableController.rowType = rowType;
     tableController.rowType = rowType;
 
 
-    __weak typeof(self) weakSelf = self;
     tableController.configureRowControllerWithObjectBlock = ^(id controller, id object) {
     tableController.configureRowControllerWithObjectBlock = ^(id controller, id object) {
-        [weakSelf configureTableRowController:controller withObject:object];
+        if ([controller respondsToSelector:@selector(configureWithMediaLibraryObject:)]) {
+            [controller configureWithMediaLibraryObject:object];
+        }
     };
     };
     self.tableController = tableController;
     self.tableController = tableController;
 
 
@@ -153,61 +145,6 @@ typedef enum {
     self.tableController.objects = [self mediaArray];
     self.tableController.objects = [self mediaArray];
 }
 }
 
 
-- (void)configureTableRowController:(id)rowController withObject:(id)storageObject {
-
-    VLCRowController *row = rowController;
-
-    float playbackProgress = 0.0;
-    if ([storageObject isKindOfClass:[MLShow class]]) {
-        row.titleLabel.text = ((MLAlbum *)storageObject).name;
-    } else if ([storageObject isKindOfClass:[MLShowEpisode class]]) {
-        row.titleLabel.text = ((MLShowEpisode *)storageObject).name;
-    } else if ([storageObject isKindOfClass:[MLLabel class]]) {
-        row.titleLabel.text = ((MLLabel *)storageObject).name;
-    } else if ([storageObject isKindOfClass:[MLAlbum class]]) {
-        row.titleLabel.text = ((MLAlbum *)storageObject).name;
-    } else if ([storageObject isKindOfClass:[MLAlbumTrack class]]) {
-        row.titleLabel.text = ((MLAlbumTrack *)storageObject).title;
-    } else if ([storageObject isKindOfClass:[MLFile class]]){
-        MLFile *file = (MLFile *)storageObject;
-        row.titleLabel.text = [file title];
-        playbackProgress = file.lastPosition.floatValue;
-    }
-
-    [row.progressObject vlc_setProgress:playbackProgress hideForNoProgress:YES];
-
-    /* FIXME: add placeholder image once designed */
-
-    row.group.backgroundImage = [UIImage imageNamed:@"tableview-gradient"];
-
-    NSArray *array = @[row.group, storageObject];
-    [self performSelectorInBackground:@selector(backgroundThumbnailSetter:) withObject:array];
-}
-
-- (void)backgroundThumbnailSetter:(NSArray *)array
-{
-    UIImage *backgroundImage = [VLCThumbnailsCache thumbnailForManagedObject:array[1] toFitRect:_thumbnailSize shouldReplaceCache:YES];
-    UIImage *gradient = [UIImage imageNamed:@"tableview-gradient"];
-
-    CGSize newSize = backgroundImage ? backgroundImage.size : CGSizeMake(_rowWidth, 120.);
-    UIGraphicsBeginImageContext(newSize);
-
-    if (backgroundImage)
-        [backgroundImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
-    else {
-        [[UIColor darkGrayColor] set];
-        UIRectFill(CGRectMake(.0, .0, newSize.width, newSize.height));
-    }
-
-    [gradient drawInRect:CGRectMake(.0, newSize.height / 2., newSize.width, newSize.height / 2.) blendMode:kCGBlendModeNormal alpha:1.];
-
-    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
-
-    UIGraphicsEndImageContext();
-
-    [array.firstObject performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:newImage waitUntilDone:NO];
-}
-
 //TODO: this code could use refactoring to be more readable
 //TODO: this code could use refactoring to be more readable
 - (NSMutableArray *)mediaArray {
 - (NSMutableArray *)mediaArray {
 
 

+ 4 - 0
VLC for iOS WatchKit Extension/VLCRowController.h

@@ -16,4 +16,8 @@
 @property (nonatomic, weak) IBOutlet WKInterfaceGroup *group;
 @property (nonatomic, weak) IBOutlet WKInterfaceGroup *group;
 @property (nonatomic, weak) IBOutlet WKInterfaceObject *progressObject;
 @property (nonatomic, weak) IBOutlet WKInterfaceObject *progressObject;
 
 
+@property (nonatomic, weak, readonly) id mediaLibraryObject;
+
+- (void) configureWithMediaLibraryObject:(id)object;
+
 @end
 @end

+ 88 - 0
VLC for iOS WatchKit Extension/VLCRowController.m

@@ -11,7 +11,95 @@
  *****************************************************************************/
  *****************************************************************************/
 
 
 #import "VLCRowController.h"
 #import "VLCRowController.h"
+#import <MediaLibraryKit/MediaLibraryKit.h>
+#import "WKInterfaceObject+VLCProgress.h"
+#import "VLCThumbnailsCache.h"
+
+@interface VLCRowController()
+@property (nonatomic, weak, readwrite) id mediaLibraryObject;
+@property (nonatomic, readonly) CGRect thumbnailSize;
+@property (nonatomic, readonly) CGFloat rowWidth;
+
+@end
 
 
 @implementation VLCRowController
 @implementation VLCRowController
 
 
+- (instancetype)init
+{
+    self = [super init];
+    if (self) {
+        [self calculateThumbnailSizeAndRowWidth];
+    }
+    return self;
+}
+
+- (void)calculateThumbnailSizeAndRowWidth
+{
+    WKInterfaceDevice *currentDevice = WKInterfaceDevice.currentDevice;
+    CGRect screenRect = currentDevice.screenBounds;
+    CGFloat screenScale = currentDevice.screenScale;
+    _thumbnailSize =  CGRectMake(0,
+                                       0,
+                                       screenRect.size.width * screenScale,
+                                       120. * screenScale
+                                       );
+    _rowWidth = screenRect.size.width * screenScale;
+}
+
+- (void)configureWithMediaLibraryObject:(id)storageObject
+{
+    float playbackProgress = 0.0;
+    if ([storageObject isKindOfClass:[MLShow class]]) {
+        self.titleLabel.text = ((MLAlbum *)storageObject).name;
+    } else if ([storageObject isKindOfClass:[MLShowEpisode class]]) {
+        self.titleLabel.text = ((MLShowEpisode *)storageObject).name;
+    } else if ([storageObject isKindOfClass:[MLLabel class]]) {
+        self.titleLabel.text = ((MLLabel *)storageObject).name;
+    } else if ([storageObject isKindOfClass:[MLAlbum class]]) {
+        self.titleLabel.text = ((MLAlbum *)storageObject).name;
+    } else if ([storageObject isKindOfClass:[MLAlbumTrack class]]) {
+        self.titleLabel.text = ((MLAlbumTrack *)storageObject).title;
+    } else if ([storageObject isKindOfClass:[MLFile class]]){
+        MLFile *file = (MLFile *)storageObject;
+        self.titleLabel.text = [file title];
+        playbackProgress = file.lastPosition.floatValue;
+    }
+
+    [self.progressObject vlc_setProgress:playbackProgress hideForNoProgress:YES];
+
+    /* FIXME: add placeholder image once designed */
+
+    if (storageObject != self.mediaLibraryObject) {
+        self.group.backgroundImage = [UIImage imageNamed:@"tableview-gradient"];
+    }
+
+    NSArray *array = @[self.group, storageObject];
+    [self performSelectorInBackground:@selector(backgroundThumbnailSetter:) withObject:array];
+
+    self.mediaLibraryObject = storageObject;
+}
+
+- (void)backgroundThumbnailSetter:(NSArray *)array
+{
+    UIImage *backgroundImage = [VLCThumbnailsCache thumbnailForManagedObject:array[1] toFitRect:_thumbnailSize shouldReplaceCache:YES];
+    UIImage *gradient = [UIImage imageNamed:@"tableview-gradient"];
+
+    CGSize newSize = backgroundImage ? backgroundImage.size : CGSizeMake(_rowWidth, 120.);
+    UIGraphicsBeginImageContext(newSize);
+
+    if (backgroundImage)
+        [backgroundImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
+    else {
+        [[UIColor darkGrayColor] set];
+        UIRectFill(CGRectMake(.0, .0, newSize.width, newSize.height));
+    }
+
+    [gradient drawInRect:CGRectMake(.0, newSize.height / 2., newSize.width, newSize.height / 2.) blendMode:kCGBlendModeNormal alpha:1.];
+
+    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
+
+    UIGraphicsEndImageContext();
+
+    [array.firstObject performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:newImage waitUntilDone:NO];
+}
 @end
 @end