From 5a68f10cb53a098327e9fcb2bed8ed95e73c04a3 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sun, 19 Jan 2020 18:25:04 +0800 Subject: [PATCH] iOS:DoricViewController add loadingView and errorView --- .../java/pub/doric/DoricPanelFragment.java | 2 +- doric-iOS/Pod/Classes/DoricDriver.h | 2 +- doric-iOS/Pod/Classes/DoricViewController.h | 5 + doric-iOS/Pod/Classes/DoricViewController.m | 104 +++++++++++++++--- 4 files changed, 93 insertions(+), 20 deletions(-) diff --git a/doric-android/doric/src/main/java/pub/doric/DoricPanelFragment.java b/doric-android/doric/src/main/java/pub/doric/DoricPanelFragment.java index 74494c2a..676eb6b4 100644 --- a/doric-android/doric/src/main/java/pub/doric/DoricPanelFragment.java +++ b/doric-android/doric/src/main/java/pub/doric/DoricPanelFragment.java @@ -157,7 +157,7 @@ public class DoricPanelFragment extends Fragment implements IDoricNavigator { }); } - public void loadJSBundle() { + private void loadJSBundle() { Bundle argument = getArguments(); if (argument == null) { if (getActivity() != null && getActivity().getIntent() != null) { diff --git a/doric-iOS/Pod/Classes/DoricDriver.h b/doric-iOS/Pod/Classes/DoricDriver.h index 4cebc069..3355001e 100644 --- a/doric-iOS/Pod/Classes/DoricDriver.h +++ b/doric-iOS/Pod/Classes/DoricDriver.h @@ -24,7 +24,7 @@ #import "DoricAsyncResult.h" #import "DoricRegistry.h" -typedef NS_ENUM(NSInteger, QueueMode) { +typedef NS_ENUM(NSInteger, DoricQueueMode) { JS = 0, UI, INDEPENDENT diff --git a/doric-iOS/Pod/Classes/DoricViewController.h b/doric-iOS/Pod/Classes/DoricViewController.h index 249625eb..f7fa75ba 100644 --- a/doric-iOS/Pod/Classes/DoricViewController.h +++ b/doric-iOS/Pod/Classes/DoricViewController.h @@ -22,10 +22,15 @@ #import "DoricNavBarDelegate.h" #import "DoricPanel.h" +extern NSString *const DORIC_MASK_RETRY; + + @interface DoricViewController : UIViewController @property(nonatomic, strong) DoricPanel *doricPanel; @property(nonatomic) BOOL statusBarHidden; @property(nonatomic) int statusBarMode; +@property(nonatomic, strong) UIView *loadingView; +@property(nonatomic, strong) UIView *errorView; - (instancetype)initWithScheme:(NSString *)scheme alias:(NSString *)alias extra:(NSString *)extra; @end diff --git a/doric-iOS/Pod/Classes/DoricViewController.m b/doric-iOS/Pod/Classes/DoricViewController.m index db1266f1..d97549bf 100644 --- a/doric-iOS/Pod/Classes/DoricViewController.m +++ b/doric-iOS/Pod/Classes/DoricViewController.m @@ -18,42 +18,71 @@ // #import "DoricViewController.h" -#import "DoricAsyncResult.h" #import "DoricJSLoaderManager.h" #import "UIView+Doric.h" #import "DoricExtensions.h" #import "DoricUtil.h" +NSString *const DORIC_MASK_RETRY = @"doric_mask_retry"; + @interface DoricViewController () @property(nonatomic) BOOL navBarHidden; @property(nonatomic, strong) UIImage *navBarImage; +@property(nonatomic, strong) UIView *maskView; +@property(nonatomic, copy) NSString *scheme; +@property(nonatomic, copy) NSString *alias; +@property(nonatomic, copy) NSString *extra; @end @implementation DoricViewController - (instancetype)initWithScheme:(NSString *)scheme alias:(NSString *)alias extra:(NSString *)extra { if (self = [super init]) { self.edgesForExtendedLayout = UIRectEdgeNone; - DoricAsyncResult *result = [DoricJSLoaderManager.instance request:scheme]; - result.resultCallback = ^(NSString *result) { - dispatch_async(dispatch_get_main_queue(), ^{ - DoricPanel *panel = [DoricPanel new]; - [panel.view also:^(UIView *it) { - it.backgroundColor = [UIColor whiteColor]; - it.width = self.view.width; - it.height = self.view.height; - }]; - [self.view addSubview:panel.view]; - [self addChildViewController:panel]; - [panel config:result alias:alias extra:extra]; - panel.doricContext.navigator = self; - panel.doricContext.navBar = self; - self.doricPanel = panel; - }); - }; + _scheme = scheme; + _alias = alias; + _extra = extra; + _doricPanel = [DoricPanel new]; } return self; } +- (void)viewDidLoad { + [super viewDidLoad]; + self.doricPanel = [[DoricPanel new] also:^(DoricPanel *it) { + [it.view also:^(UIView *it) { + it.backgroundColor = [UIColor whiteColor]; + it.width = self.view.width; + it.height = self.view.height; + }]; + [self.view addSubview:it.view]; + [self addChildViewController:it]; + }]; + self.maskView = [[UIView new] also:^(UIView *it) { + it.backgroundColor = [UIColor whiteColor]; + it.width = self.view.width; + it.height = self.view.height; + [self.view addSubview:it]; + if (self.loadingView) { + [it addSubview:self.loadingView]; + } + if (self.errorView) { + [it addSubview:self.errorView]; + } + UIView *retryView = [it viewWithTagString:DORIC_MASK_RETRY]; + if (retryView) { + UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] + initWithTarget:self + action:@selector(retry:)]; + [retryView addGestureRecognizer:recognizer]; + } + }]; + [self loadJSBundle]; +} + +- (void)retry:(UIView *)view { + [self loadJSBundle]; +} + - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navBarHidden = self.navigationController.navigationBarHidden; @@ -127,4 +156,43 @@ - (BOOL)prefersStatusBarHidden { return self.statusBarHidden; } +- (void)showLoading { + dispatch_async(dispatch_get_main_queue(), ^{ + self.maskView.hidden = NO; + self.loadingView.hidden = NO; + self.errorView.hidden = YES; + }); +} + +- (void)showError { + dispatch_async(dispatch_get_main_queue(), ^{ + self.maskView.hidden = NO; + self.loadingView.hidden = YES; + self.errorView.hidden = NO; + }); +} + +- (void)hideMask { + dispatch_async(dispatch_get_main_queue(), ^{ + self.maskView.hidden = YES; + }); +} + +- (void)loadJSBundle { + [self showLoading]; + DoricAsyncResult *result = [DoricJSLoaderManager.instance request:self.scheme]; + result.resultCallback = ^(NSString *result) { + dispatch_async(dispatch_get_main_queue(), ^{ + [self hideMask]; + [self.doricPanel config:result alias:self.alias extra:self.extra]; + self.doricPanel.doricContext.navigator = self; + self.doricPanel.doricContext.navBar = self; + }); + }; + result.exceptionCallback = ^(NSException *e) { + dispatch_async(dispatch_get_main_queue(), ^{ + [self showError]; + }); + }; +} @end