From c666c7b81c8150f7deebdec7f13277e4cc031b38 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sat, 23 Nov 2019 19:44:46 +0800 Subject: [PATCH] feat:The same VC use diffrent childVC --- iOS/Example/Example/ViewController.m | 12 +++- iOS/Pod/Classes/Doric.h | 5 +- iOS/Pod/Classes/DoricPanel.h | 1 + iOS/Pod/Classes/DoricPanel.m | 7 +- iOS/Pod/Classes/DoricViewController.h | 25 +++++++ iOS/Pod/Classes/DoricViewController.m | 71 +++++++++++++++++++ .../Classes}/Loader/DoricHttpJSLoader.h | 0 .../Classes}/Loader/DoricHttpJSLoader.m | 0 .../Classes}/Loader/DoricJSLoaderManager.h | 0 .../Classes}/Loader/DoricJSLoaderManager.m | 0 .../Classes}/Loader/DoricLoaderProtocol.h | 0 .../Classes}/Loader/DoricMainBundleJSLoader.h | 0 .../Classes}/Loader/DoricMainBundleJSLoader.m | 0 .../Navigator/DoricNavigatorProtocol.h | 0 iOS/Pod/Classes/Util/DoricAsyncResult.h | 12 +--- iOS/Pod/Classes/Util/DoricAsyncResult.m | 10 +-- 16 files changed, 124 insertions(+), 19 deletions(-) create mode 100644 iOS/Pod/Classes/DoricViewController.h create mode 100644 iOS/Pod/Classes/DoricViewController.m rename iOS/{ => Pod/Classes}/Loader/DoricHttpJSLoader.h (100%) rename iOS/{ => Pod/Classes}/Loader/DoricHttpJSLoader.m (100%) rename iOS/{ => Pod/Classes}/Loader/DoricJSLoaderManager.h (100%) rename iOS/{ => Pod/Classes}/Loader/DoricJSLoaderManager.m (100%) rename iOS/{ => Pod/Classes}/Loader/DoricLoaderProtocol.h (100%) rename iOS/{ => Pod/Classes}/Loader/DoricMainBundleJSLoader.h (100%) rename iOS/{ => Pod/Classes}/Loader/DoricMainBundleJSLoader.m (100%) rename iOS/{ => Pod/Classes}/Navigator/DoricNavigatorProtocol.h (100%) diff --git a/iOS/Example/Example/ViewController.m b/iOS/Example/Example/ViewController.m index 8acce53f..6110d39c 100644 --- a/iOS/Example/Example/ViewController.m +++ b/iOS/Example/Example/ViewController.m @@ -58,8 +58,16 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath [self.navigationController pushViewController:[QRScanViewController new] animated:NO]; return; } - DemoVC *demoVC = [[DemoVC alloc] initWithPath:self.demoFilePaths[(NSUInteger) indexPath.row]]; - [self.navigationController pushViewController:demoVC animated:NO]; + NSString *file = self.demoFilePaths[(NSUInteger) indexPath.row]; + if ([file containsString:@"NavigatorDemo"]) { + DoricViewController *doricViewController = [[DoricViewController alloc] + initWithScheme:[NSString stringWithFormat:@"assets://demo/%@", file] + alias:self.demoFilePaths[(NSUInteger) indexPath.row]]; + [self.navigationController pushViewController:doricViewController animated:NO]; + } else { + DemoVC *demoVC = [[DemoVC alloc] initWithPath:file]; + [self.navigationController pushViewController:demoVC animated:NO]; + } } @end diff --git a/iOS/Pod/Classes/Doric.h b/iOS/Pod/Classes/Doric.h index 705cc394..9df6d257 100644 --- a/iOS/Pod/Classes/Doric.h +++ b/iOS/Pod/Classes/Doric.h @@ -20,4 +20,7 @@ #import "DoricRootNode.h" #import "UIView+Doric.h" #import "DoricUtil.h" -#import "DoricPanel.h" \ No newline at end of file +#import "DoricPanel.h" +#import "DoricJSLoaderManager.h" +#import "DoricNavigatorProtocol.h" +#import "DoricViewController.h" \ No newline at end of file diff --git a/iOS/Pod/Classes/DoricPanel.h b/iOS/Pod/Classes/DoricPanel.h index a262ddce..66ad7657 100644 --- a/iOS/Pod/Classes/DoricPanel.h +++ b/iOS/Pod/Classes/DoricPanel.h @@ -20,6 +20,7 @@ #import #import "DoricContext.h" +#import "DoricNavigatorProtocol.h" @interface DoricPanel : UIViewController @property(nonatomic, strong) DoricContext *doricContext; diff --git a/iOS/Pod/Classes/DoricPanel.m b/iOS/Pod/Classes/DoricPanel.m index d647f1bb..b4cc5af1 100644 --- a/iOS/Pod/Classes/DoricPanel.m +++ b/iOS/Pod/Classes/DoricPanel.m @@ -20,11 +20,13 @@ #import "DoricPanel.h" #import "Doric.h" - @implementation DoricPanel - (void)config:(NSString *)script alias:(NSString *)alias { self.doricContext = [[[DoricContext alloc] initWithScript:script source:alias] also:^(DoricContext *it) { + if ([self.parentViewController conformsToProtocol:@protocol(DoricNavigatorProtocol)]) { + it.navigator = (id ) self.parentViewController; + } [it.rootNode setupRootView:[[DoricStackView new] also:^(DoricStackView *it) { it.width = self.view.width; it.height = self.view.height; @@ -43,4 +45,5 @@ - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [self.doricContext onHidden]; } -@end \ No newline at end of file + +@end diff --git a/iOS/Pod/Classes/DoricViewController.h b/iOS/Pod/Classes/DoricViewController.h new file mode 100644 index 00000000..b39c3e4a --- /dev/null +++ b/iOS/Pod/Classes/DoricViewController.h @@ -0,0 +1,25 @@ +/* + * Copyright [2019] [Doric.Pub] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// +// Created by pengfei.zhou on 2019/11/23. +// + +#import +#import "DoricNavigatorProtocol.h" + +@interface DoricViewController : UIViewController +- (instancetype)initWithScheme:(NSString *)scheme alias:(NSString *)alias; +@end \ No newline at end of file diff --git a/iOS/Pod/Classes/DoricViewController.m b/iOS/Pod/Classes/DoricViewController.m new file mode 100644 index 00000000..49d1068a --- /dev/null +++ b/iOS/Pod/Classes/DoricViewController.m @@ -0,0 +1,71 @@ +/* + * Copyright [2019] [Doric.Pub] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// +// Created by pengfei.zhou on 2019/11/23. +// + +#import "DoricViewController.h" +#import "DoricAsyncResult.h" +#import "DoricJSLoaderManager.h" +#import "DoricPanel.h" +#import "UIView+Doric.h" +#import "DoricExtensions.h" + +@implementation DoricViewController +- (instancetype)initWithScheme:(NSString *)scheme alias:(NSString *)alias { + if (self = [super init]) { + [self push:scheme alias:alias]; + self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" + style:UIBarButtonItemStylePlain + target:self + action:@selector(pop)]; + } + return self; +} + +- (void)push:(NSString *)scheme alias:(NSString *)alias { + 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 - 88; + it.top = 88; + }]; + [self.view addSubview:panel.view]; + [self addChildViewController:panel]; + [panel config:result alias:alias]; + }); + }; +} + +- (void)pop { + dispatch_async(dispatch_get_main_queue(), ^{ + if (self.childViewControllers.count > 1) { + [self.childViewControllers.lastObject also:^(UIViewController *it) { + [it removeFromParentViewController]; + [it.view removeFromSuperview]; + }]; + } else { + [self.navigationController popViewControllerAnimated:NO]; + } + + }); +} + +@end diff --git a/iOS/Loader/DoricHttpJSLoader.h b/iOS/Pod/Classes/Loader/DoricHttpJSLoader.h similarity index 100% rename from iOS/Loader/DoricHttpJSLoader.h rename to iOS/Pod/Classes/Loader/DoricHttpJSLoader.h diff --git a/iOS/Loader/DoricHttpJSLoader.m b/iOS/Pod/Classes/Loader/DoricHttpJSLoader.m similarity index 100% rename from iOS/Loader/DoricHttpJSLoader.m rename to iOS/Pod/Classes/Loader/DoricHttpJSLoader.m diff --git a/iOS/Loader/DoricJSLoaderManager.h b/iOS/Pod/Classes/Loader/DoricJSLoaderManager.h similarity index 100% rename from iOS/Loader/DoricJSLoaderManager.h rename to iOS/Pod/Classes/Loader/DoricJSLoaderManager.h diff --git a/iOS/Loader/DoricJSLoaderManager.m b/iOS/Pod/Classes/Loader/DoricJSLoaderManager.m similarity index 100% rename from iOS/Loader/DoricJSLoaderManager.m rename to iOS/Pod/Classes/Loader/DoricJSLoaderManager.m diff --git a/iOS/Loader/DoricLoaderProtocol.h b/iOS/Pod/Classes/Loader/DoricLoaderProtocol.h similarity index 100% rename from iOS/Loader/DoricLoaderProtocol.h rename to iOS/Pod/Classes/Loader/DoricLoaderProtocol.h diff --git a/iOS/Loader/DoricMainBundleJSLoader.h b/iOS/Pod/Classes/Loader/DoricMainBundleJSLoader.h similarity index 100% rename from iOS/Loader/DoricMainBundleJSLoader.h rename to iOS/Pod/Classes/Loader/DoricMainBundleJSLoader.h diff --git a/iOS/Loader/DoricMainBundleJSLoader.m b/iOS/Pod/Classes/Loader/DoricMainBundleJSLoader.m similarity index 100% rename from iOS/Loader/DoricMainBundleJSLoader.m rename to iOS/Pod/Classes/Loader/DoricMainBundleJSLoader.m diff --git a/iOS/Navigator/DoricNavigatorProtocol.h b/iOS/Pod/Classes/Navigator/DoricNavigatorProtocol.h similarity index 100% rename from iOS/Navigator/DoricNavigatorProtocol.h rename to iOS/Pod/Classes/Navigator/DoricNavigatorProtocol.h diff --git a/iOS/Pod/Classes/Util/DoricAsyncResult.h b/iOS/Pod/Classes/Util/DoricAsyncResult.h index 9eb813ff..6a5b6a7d 100644 --- a/iOS/Pod/Classes/Util/DoricAsyncResult.h +++ b/iOS/Pod/Classes/Util/DoricAsyncResult.h @@ -26,15 +26,9 @@ NS_ASSUME_NONNULL_BEGIN @interface DoricAsyncResult : NSObject -typedef void(^DoricResultCallback)(R); - -typedef void(^DoricExceptionCallback)(NSException *); - -typedef void(^DoricFinishCallback)(void); - -@property(nonatomic, strong) DoricResultCallback resultCallback; -@property(nonatomic, strong) DoricExceptionCallback exceptionCallback; -@property(nonatomic, strong) DoricFinishCallback finishCallback; +@property(nonatomic, strong) void (^resultCallback)(R result); +@property(nonatomic, strong) void (^exceptionCallback)(NSException *e); +@property(nonatomic, strong) void (^finishCallback)(void); - (void)setupResult:(R)result; diff --git a/iOS/Pod/Classes/Util/DoricAsyncResult.m b/iOS/Pod/Classes/Util/DoricAsyncResult.m index 786f2eff..e70b8805 100644 --- a/iOS/Pod/Classes/Util/DoricAsyncResult.m +++ b/iOS/Pod/Classes/Util/DoricAsyncResult.m @@ -56,24 +56,24 @@ - (id)getResult { return self.result; } -- (void)setResultCallback:(DoricResultCallback)callback { +- (void)setResultCallback:(void (^)(id))callback { _resultCallback = callback; if (self.result && ![self.result isKindOfClass:[NSException class]]) { callback(self.result); } } -- (void)setExceptionCallback:(DoricExceptionCallback)exceptionCallback { +- (void)setExceptionCallback:(void (^)(NSException *))exceptionCallback { _exceptionCallback = exceptionCallback; if ([self.result isKindOfClass:[NSException class]]) { exceptionCallback(self.result); } } -- (void)setFinishCallback:(DoricFinishCallback)callback { - _finishCallback = callback; +- (void)setFinishCallback:(void (^)(void))finishCallback { + _finishCallback = finishCallback; if (self.result) { - callback(); + finishCallback(); } }