feat:The same VC use diffrent childVC

This commit is contained in:
pengfei.zhou
2019-11-23 19:44:46 +08:00
parent 76bceb084e
commit c666c7b81c
16 changed files with 124 additions and 19 deletions

View File

@@ -20,4 +20,7 @@
#import "DoricRootNode.h"
#import "UIView+Doric.h"
#import "DoricUtil.h"
#import "DoricPanel.h"
#import "DoricPanel.h"
#import "DoricJSLoaderManager.h"
#import "DoricNavigatorProtocol.h"
#import "DoricViewController.h"

View File

@@ -20,6 +20,7 @@
#import <Foundation/Foundation.h>
#import "DoricContext.h"
#import "DoricNavigatorProtocol.h"
@interface DoricPanel : UIViewController
@property(nonatomic, strong) DoricContext *doricContext;

View File

@@ -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 <DoricNavigatorProtocol>) 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
@end

View File

@@ -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 <Foundation/Foundation.h>
#import "DoricNavigatorProtocol.h"
@interface DoricViewController : UIViewController <DoricNavigatorProtocol>
- (instancetype)initWithScheme:(NSString *)scheme alias:(NSString *)alias;
@end

View File

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

View File

@@ -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 <Foundation/Foundation.h>
#import "DoricLoaderProtocol.h"
@interface DoricHttpJSLoader : NSObject <DoricLoaderProtocol>
@end

View File

@@ -0,0 +1,45 @@
/*
* 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 "DoricHttpJSLoader.h"
@implementation DoricHttpJSLoader
- (BOOL)filter:(NSString *)scheme {
return [scheme hasPrefix:@"http"];
}
- (DoricAsyncResult <NSString *> *)request:(NSString *)scheme {
DoricAsyncResult *ret = [DoricAsyncResult new];
NSURL *URL = [NSURL URLWithString:scheme];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[[[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]
dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[ret setupResult:dataStr];
} else {
[ret setupError:[[NSException alloc] initWithName:@"DoricJSLoaderManager Exception" reason:error.description userInfo:nil]];
}
}] resume];
return ret;
}
@end

View File

@@ -0,0 +1,33 @@
/*
* 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.
*/
//
// DoricJSLoaderManager.h
// Doric
//
// Created by pengfei.zhou on 2019/11/23.
//
#import <Foundation/Foundation.h>
#import "DoricLoaderProtocol.h"
#import "DoricAsyncResult.h"
@interface DoricJSLoaderManager : NSObject
+ (instancetype)instance;
- (void)addJSLoader:(id <DoricLoaderProtocol>)loader;
- (DoricAsyncResult <NSString *> *)request:(NSString *)scheme;
@end

View File

@@ -0,0 +1,68 @@
/*
* 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.
*/
//
// DoricJSLoaderManager.m
// Doric
//
// Created by pengfei.zhou on 2019/11/23.
//
#import "DoricJSLoaderManager.h"
#import "DoricMainBundleJSLoader.h"
#import "DoricHttpJSLoader.h"
#import "Doric.h"
@interface DoricJSLoaderManager ()
@property(nonatomic, copy) NSSet <id <DoricLoaderProtocol>> *loaders;
@end
@implementation DoricJSLoaderManager
+ (instancetype)instance {
static DoricJSLoaderManager *_instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [DoricJSLoaderManager new];
});
return _instance;
}
- (instancetype)init {
if (self = [super init]) {
_loaders = [[NSSet alloc] initWithArray:@[
[DoricMainBundleJSLoader new],
[DoricHttpJSLoader new],
]];
}
return self;
}
- (void)addJSLoader:(id <DoricLoaderProtocol>)loader {
self.loaders = [[self.loaders mutableCopy] also:^(NSMutableSet *it) {
[it addObject:loader];
}];
}
- (DoricAsyncResult <NSString *> *)request:(NSString *)scheme {
__block DoricAsyncResult *ret;
[self.loaders enumerateObjectsUsingBlock:^(id <DoricLoaderProtocol> obj, BOOL *stop) {
if ([obj filter:scheme]) {
ret = [obj request:scheme];
*stop = YES;
}
}];
return ret;
}
@end

View File

@@ -0,0 +1,27 @@
/*
* 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 <Foundation/Foundation.h>
#import "DoricAsyncResult.h"
@protocol DoricLoaderProtocol <NSObject>
- (BOOL)filter:(NSString *)scheme;
- (DoricAsyncResult <NSString *> *)request:(NSString *)scheme;
@end

View File

@@ -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 <Foundation/Foundation.h>
#import "DoricLoaderProtocol.h"
@interface DoricMainBundleJSLoader : NSObject <DoricLoaderProtocol>
@end

View File

@@ -0,0 +1,42 @@
/*
* 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 "DoricMainBundleJSLoader.h"
@implementation DoricMainBundleJSLoader
- (BOOL)filter:(NSString *)scheme {
return [scheme hasPrefix:@"assets"];
}
- (DoricAsyncResult <NSString *> *)request:(NSString *)scheme {
DoricAsyncResult <NSString *> *ret = [DoricAsyncResult new];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *fullPath = [path stringByAppendingPathComponent:[scheme substringFromIndex:@"assets://".length]];
NSError *error;
NSString *jsContent = [NSString stringWithContentsOfFile:fullPath encoding:NSUTF8StringEncoding error:&error];
if (error) {
[ret setupError:[NSException new]];
} else {
[ret setupResult:jsContent];
}
return ret;
}
@end

View File

@@ -0,0 +1,11 @@
//
// Created by pengfei.zhou on 2019/11/23.
//
#import <Foundation/Foundation.h>
@protocol DoricNavigatorProtocol <NSObject>
- (void)push:(NSString *)scheme alias:(NSString *)alias;
- (void)pop;
@end

View File

@@ -26,15 +26,9 @@ NS_ASSUME_NONNULL_BEGIN
@interface DoricAsyncResult <R> : 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;

View File

@@ -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();
}
}