2019-11-28 19:57:36 +08:00
|
|
|
//
|
|
|
|
// Created by pengfei.zhou on 2019/11/28.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "DoricPopoverPlugin.h"
|
|
|
|
#import "DoricRootNode.h"
|
|
|
|
#import "Doric.h"
|
|
|
|
|
|
|
|
@interface DoricPopoverPlugin ()
|
|
|
|
@property(nonatomic, strong) UIView *fullScreenView;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DoricPopoverPlugin
|
2020-01-09 19:00:53 +08:00
|
|
|
static NSString *TYPE = @"popover";
|
|
|
|
|
2019-11-28 19:57:36 +08:00
|
|
|
- (void)show:(NSDictionary *)params withPromise:(DoricPromise *)promise {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
UIView *superView = [UIApplication sharedApplication].windows.lastObject;
|
|
|
|
if (!self.fullScreenView) {
|
|
|
|
self.fullScreenView = [[DoricStackView new] also:^(UIView *it) {
|
|
|
|
it.width = superView.width;
|
|
|
|
it.height = superView.height;
|
|
|
|
it.top = it.left = 0;
|
2019-11-28 21:04:27 +08:00
|
|
|
[superView addSubview:it];
|
2019-11-28 19:57:36 +08:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
[superView bringSubviewToFront:self.fullScreenView];
|
2019-11-28 21:04:27 +08:00
|
|
|
self.fullScreenView.hidden = NO;
|
|
|
|
NSString *viewId = params[@"id"];
|
|
|
|
NSString *type = params[@"type"];
|
2019-11-29 09:35:20 +08:00
|
|
|
DoricViewNode *viewNode = [self.doricContext targetViewNode:viewId];
|
|
|
|
if (!viewNode) {
|
|
|
|
viewNode = [[DoricViewNode create:self.doricContext withType:type] also:^(DoricViewNode *it) {
|
|
|
|
it.viewId = viewId;
|
|
|
|
[it initWithSuperNode:nil];
|
|
|
|
it.view.layoutConfig = [DoricLayoutConfig new];
|
|
|
|
[self.fullScreenView addSubview:it.view];
|
2020-01-09 19:00:53 +08:00
|
|
|
|
|
|
|
NSMutableDictionary <NSString *, DoricViewNode *> *map = self.doricContext.headNodes[TYPE];
|
|
|
|
if (map != nil) {
|
|
|
|
self.doricContext.headNodes[TYPE][viewId] = it;
|
|
|
|
} else {
|
|
|
|
map = [[NSMutableDictionary alloc] init];
|
|
|
|
map[viewId] = it;
|
|
|
|
self.doricContext.headNodes[TYPE] = map;
|
|
|
|
}
|
2019-11-29 09:35:20 +08:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
[viewNode blend:params[@"props"]];
|
2019-11-28 19:57:36 +08:00
|
|
|
[promise resolve:nil];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dismiss:(NSDictionary *)params withPromise:(DoricPromise *)promise {
|
2019-11-29 09:35:20 +08:00
|
|
|
NSString *viewId = params[@"id"];
|
2019-11-28 19:57:36 +08:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2019-11-29 09:35:20 +08:00
|
|
|
if (viewId) {
|
|
|
|
DoricViewNode *viewNode = [self.doricContext targetViewNode:viewId];
|
|
|
|
[self dismissViewNode:viewNode];
|
|
|
|
} else {
|
|
|
|
[self dismissPopover];
|
|
|
|
}
|
2019-11-28 19:57:36 +08:00
|
|
|
[promise resolve:nil];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-29 09:35:20 +08:00
|
|
|
- (void)dismissViewNode:(DoricViewNode *)node {
|
2020-01-09 19:00:53 +08:00
|
|
|
[self.doricContext.headNodes[TYPE] removeObjectForKey:node.viewId];
|
2019-11-29 09:35:20 +08:00
|
|
|
[node.view removeFromSuperview];
|
2020-01-09 19:00:53 +08:00
|
|
|
if (self.doricContext.headNodes[TYPE].count == 0) {
|
2019-11-29 09:35:20 +08:00
|
|
|
self.fullScreenView.hidden = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 19:57:36 +08:00
|
|
|
- (void)dismissPopover {
|
2020-01-09 19:00:53 +08:00
|
|
|
for (DoricViewNode *node in self.doricContext.headNodes[TYPE].allValues) {
|
2019-11-29 09:35:20 +08:00
|
|
|
[self dismissViewNode:node];
|
|
|
|
}
|
2019-11-28 19:57:36 +08:00
|
|
|
}
|
2019-11-28 21:04:27 +08:00
|
|
|
@end
|