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 {
|
2021-02-01 15:31:24 +08:00
|
|
|
__weak typeof(self) _self = self;
|
|
|
|
[self.doricContext dispatchToMainQueue:^{
|
|
|
|
__strong typeof(_self) self = _self;
|
2020-09-04 15:04:55 +08:00
|
|
|
UIView *superView = self.doricContext.vc.view;
|
2019-11-28 19:57:36 +08:00
|
|
|
if (!self.fullScreenView) {
|
2020-04-03 16:36:43 +08:00
|
|
|
self.fullScreenView = [[UIView new] also:^(UIView *it) {
|
2019-11-28 19:57:36 +08:00
|
|
|
it.width = superView.width;
|
|
|
|
it.height = superView.height;
|
|
|
|
it.top = it.left = 0;
|
2020-04-08 16:16:57 +08:00
|
|
|
it.doricLayout.layoutType = DoricStack;
|
2019-11-28 21:04:27 +08:00
|
|
|
[superView addSubview:it];
|
2019-11-28 19:57:36 +08:00
|
|
|
}];
|
2020-09-04 15:04:55 +08:00
|
|
|
} else {
|
|
|
|
self.fullScreenView.doricLayout.width = superView.width;
|
|
|
|
self.fullScreenView.doricLayout.height = superView.height;
|
2019-11-28 19:57:36 +08:00
|
|
|
}
|
|
|
|
[superView bringSubviewToFront:self.fullScreenView];
|
2019-11-28 21:04:27 +08:00
|
|
|
self.fullScreenView.hidden = NO;
|
2021-02-01 18:10:11 +08:00
|
|
|
NSString *viewId = [params optString:@"id"];
|
|
|
|
NSString *type = [params optString:@"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];
|
|
|
|
[self.fullScreenView addSubview:it.view];
|
2020-04-08 16:16:57 +08:00
|
|
|
|
2020-01-09 19:00:53 +08:00
|
|
|
NSMutableDictionary <NSString *, DoricViewNode *> *map = self.doricContext.headNodes[TYPE];
|
|
|
|
if (map != nil) {
|
2020-04-08 16:16:57 +08:00
|
|
|
self.doricContext.headNodes[TYPE][viewId] = it;
|
2020-01-09 19:00:53 +08:00
|
|
|
} else {
|
|
|
|
map = [[NSMutableDictionary alloc] init];
|
|
|
|
map[viewId] = it;
|
|
|
|
self.doricContext.headNodes[TYPE] = map;
|
|
|
|
}
|
2019-11-29 09:35:20 +08:00
|
|
|
}];
|
|
|
|
}
|
2021-02-01 18:10:11 +08:00
|
|
|
[viewNode blend:[params optObject:@"props"]];
|
2020-04-08 16:16:57 +08:00
|
|
|
[self.fullScreenView.doricLayout apply];
|
2019-11-28 19:57:36 +08:00
|
|
|
[promise resolve:nil];
|
2021-02-01 15:31:24 +08:00
|
|
|
}];
|
2019-11-28 19:57:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dismiss:(NSDictionary *)params withPromise:(DoricPromise *)promise {
|
2021-02-01 18:10:11 +08:00
|
|
|
NSString *viewId = [params optString:@"id"];
|
2021-02-01 15:31:24 +08:00
|
|
|
__weak typeof(self) _self = self;
|
|
|
|
[self.doricContext dispatchToMainQueue:^{
|
|
|
|
__strong typeof(_self) self = _self;
|
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];
|
2021-02-01 15:31:24 +08:00
|
|
|
}];
|
2019-11-28 19:57:36 +08:00
|
|
|
}
|
|
|
|
|
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
|