ソースを参照

Allow registration of content type viewers (#241)

Geor Kasapidi 7 年 前
コミット
ab9515caaf
共有4 個のファイルを変更した35 個の追加0 個の削除を含む
  1. 11 0
      Classes/FLEXManager.h
  2. 2 0
      Classes/Manager/FLEXManager+Private.h
  3. 11 0
      Classes/Manager/FLEXManager.m
  4. 11 0
      Classes/Network/FLEXNetworkTransactionDetailTableViewController.m

+ 11 - 0
Classes/FLEXManager.h

@@ -9,6 +9,8 @@
 #import <Foundation/Foundation.h>
 #import <Foundation/Foundation.h>
 #import <UIKit/UIKit.h>
 #import <UIKit/UIKit.h>
 
 
+typedef UIViewController *(^FLEXCustomContentViewerFuture)(NSData *data);
+
 @interface FLEXManager : NSObject
 @interface FLEXManager : NSObject
 
 
 + (instancetype)sharedManager;
 + (instancetype)sharedManager;
@@ -77,4 +79,13 @@
 - (void)registerGlobalEntryWithName:(NSString *)entryName
 - (void)registerGlobalEntryWithName:(NSString *)entryName
           viewControllerFutureBlock:(UIViewController * (^)(void))viewControllerFutureBlock;
           viewControllerFutureBlock:(UIViewController * (^)(void))viewControllerFutureBlock;
 
 
+/// Sets custom viewer for specific content type.
+/// @param contentType Mime type like application/json
+/// @param viewControllerFutureBlock Viewer (view controller) creation block
+/// @note This method must be called from the main thread.
+/// The viewControllerFutureBlock will be invoked from the main thread and may not return nil.
+/// @note The passed block will be copied and retain for the duration of the application, you may want to use __weak references.
+- (void)setCustomViewerForContentType:(NSString *)contentType
+            viewControllerFutureBlock:(FLEXCustomContentViewerFuture)viewControllerFutureBlock;
+
 @end
 @end

+ 2 - 0
Classes/Manager/FLEXManager+Private.h

@@ -15,4 +15,6 @@
 /// An array of FLEXGlobalsTableViewControllerEntry objects that have been registered by the user.
 /// An array of FLEXGlobalsTableViewControllerEntry objects that have been registered by the user.
 @property (nonatomic, readonly, strong) NSArray<FLEXGlobalsTableViewControllerEntry *> *userGlobalEntries;
 @property (nonatomic, readonly, strong) NSArray<FLEXGlobalsTableViewControllerEntry *> *userGlobalEntries;
 
 
+@property (nonatomic, readonly, strong) NSDictionary<NSString *, FLEXCustomContentViewerFuture> *customContentTypeViewers;
+
 @end
 @end

+ 11 - 0
Classes/Manager/FLEXManager.m

@@ -25,6 +25,7 @@
 @property (nonatomic, strong) FLEXExplorerViewController *explorerViewController;
 @property (nonatomic, strong) FLEXExplorerViewController *explorerViewController;
 
 
 @property (nonatomic, readonly, strong) NSMutableArray<FLEXGlobalsTableViewControllerEntry *> *userGlobalEntries;
 @property (nonatomic, readonly, strong) NSMutableArray<FLEXGlobalsTableViewControllerEntry *> *userGlobalEntries;
+@property (nonatomic, readonly, strong) NSMutableDictionary<NSString *, FLEXCustomContentViewerFuture> *customContentTypeViewers;
 
 
 @end
 @end
 
 
@@ -45,6 +46,7 @@
     self = [super init];
     self = [super init];
     if (self) {
     if (self) {
         _userGlobalEntries = [NSMutableArray array];
         _userGlobalEntries = [NSMutableArray array];
+        _customContentTypeViewers = [NSMutableDictionary dictionary];
     }
     }
     return self;
     return self;
 }
 }
@@ -288,6 +290,15 @@
     [self.userGlobalEntries addObject:entry];
     [self.userGlobalEntries addObject:entry];
 }
 }
 
 
+- (void)setCustomViewerForContentType:(NSString *)contentType viewControllerFutureBlock:(FLEXCustomContentViewerFuture)viewControllerFutureBlock
+{
+    NSParameterAssert(contentType.length);
+    NSParameterAssert(viewControllerFutureBlock);
+    NSAssert([NSThread isMainThread], @"This method must be called from the main thread.");
+
+    self.customContentTypeViewers[contentType.lowercaseString] = viewControllerFutureBlock;
+}
+
 - (void)tryScrollDown
 - (void)tryScrollDown
 {
 {
     UIScrollView *firstScrollView = [self firstScrollView];
     UIScrollView *firstScrollView = [self firstScrollView];

+ 11 - 0
Classes/Network/FLEXNetworkTransactionDetailTableViewController.m

@@ -14,6 +14,7 @@
 #import "FLEXImagePreviewViewController.h"
 #import "FLEXImagePreviewViewController.h"
 #import "FLEXMultilineTableViewCell.h"
 #import "FLEXMultilineTableViewCell.h"
 #import "FLEXUtility.h"
 #import "FLEXUtility.h"
+#import "FLEXManager+Private.h"
 
 
 typedef UIViewController *(^FLEXNetworkDetailRowSelectionFuture)(void);
 typedef UIViewController *(^FLEXNetworkDetailRowSelectionFuture)(void);
 
 
@@ -425,6 +426,16 @@ typedef UIViewController *(^FLEXNetworkDetailRowSelectionFuture)(void);
 
 
 + (UIViewController *)detailViewControllerForMIMEType:(NSString *)mimeType data:(NSData *)data
 + (UIViewController *)detailViewControllerForMIMEType:(NSString *)mimeType data:(NSData *)data
 {
 {
+    FLEXCustomContentViewerFuture makeCustomViewer = [FLEXManager sharedManager].customContentTypeViewers[mimeType.lowercaseString];
+
+    if (makeCustomViewer) {
+        UIViewController *viewer = makeCustomViewer(data);
+
+        if (viewer) {
+            return viewer;
+        }
+    }
+
     // FIXME (RKO): Don't rely on UTF8 string encoding
     // FIXME (RKO): Don't rely on UTF8 string encoding
     UIViewController *detailViewController = nil;
     UIViewController *detailViewController = nil;
     if ([FLEXUtility isValidJSONData:data]) {
     if ([FLEXUtility isValidJSONData:data]) {