This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-iOS/Pod/Classes/Plugin/DoricPopoverPlugin.m
2020-01-10 11:23:28 +08:00

81 lines
2.7 KiB
Objective-C

//
// 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
static NSString *TYPE = @"popover";
- (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;
[superView addSubview:it];
}];
}
[superView bringSubviewToFront:self.fullScreenView];
self.fullScreenView.hidden = NO;
NSString *viewId = params[@"id"];
NSString *type = params[@"type"];
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];
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;
}
}];
}
[viewNode blend:params[@"props"]];
[promise resolve:nil];
});
}
- (void)dismiss:(NSDictionary *)params withPromise:(DoricPromise *)promise {
NSString *viewId = params[@"id"];
dispatch_async(dispatch_get_main_queue(), ^{
if (viewId) {
DoricViewNode *viewNode = [self.doricContext targetViewNode:viewId];
[self dismissViewNode:viewNode];
} else {
[self dismissPopover];
}
[promise resolve:nil];
});
}
- (void)dismissViewNode:(DoricViewNode *)node {
[self.doricContext.headNodes[TYPE] removeObjectForKey:node.viewId];
[node.view removeFromSuperview];
if (self.doricContext.headNodes[TYPE].count == 0) {
self.fullScreenView.hidden = YES;
}
}
- (void)dismissPopover {
for (DoricViewNode *node in self.doricContext.headNodes[TYPE].allValues) {
[self dismissViewNode:node];
}
}
@end