iOS:DoricViewController add loadingView and errorView

This commit is contained in:
pengfei.zhou 2020-01-19 18:25:04 +08:00 committed by osborn
parent 7c83581c1e
commit 5a68f10cb5
4 changed files with 93 additions and 20 deletions

View File

@ -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) {

View File

@ -24,7 +24,7 @@
#import "DoricAsyncResult.h"
#import "DoricRegistry.h"
typedef NS_ENUM(NSInteger, QueueMode) {
typedef NS_ENUM(NSInteger, DoricQueueMode) {
JS = 0,
UI,
INDEPENDENT

View File

@ -22,10 +22,15 @@
#import "DoricNavBarDelegate.h"
#import "DoricPanel.h"
extern NSString *const DORIC_MASK_RETRY;
@interface DoricViewController : UIViewController <DoricNavigatorDelegate, DoricNavBarDelegate>
@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

View File

@ -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 <NSString *> *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 <NSString *> *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