feat:add toast bridge

This commit is contained in:
pengfei.zhou
2019-11-20 17:24:50 +08:00
parent 38823700a2
commit d495043dc9
9 changed files with 96 additions and 7 deletions

View File

@@ -21,15 +21,13 @@
//
#import "DoricModalPlugin.h"
#import "DoricRegistry.h"
#import "DoricUtil.h"
@implementation DoricModalPlugin
- (void)toast:(NSString *)message withPromise:(DoricPromise *)promise {
- (void)toast:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"toast:%@", message);
[promise resolve:@"Resolved"];
showToast(dic[@"msg"]);
});
}

View File

@@ -35,3 +35,8 @@ NSBundle *_Nonnull DoricBundle(void);
#ifndef DC_UNLOCK
#define DC_UNLOCK(lock) dispatch_semaphore_signal(lock);
#endif
void showToastInView(NSString *_Nonnull text, UIView *_Nonnull superView);
void showToast(NSString *_Nonnull text);

View File

@@ -22,6 +22,7 @@
#import "DoricUtil.h"
#import "DoricContext.h"
#import "UIView+Doric.h"
void DoricLog(NSString *_Nonnull format, ...) {
va_list args;
@@ -45,3 +46,32 @@ void DoricLog(NSString *_Nonnull format, ...) {
NSURL *url = [bundle URLForResource:@"Doric" withExtension:@"bundle"];
return [NSBundle bundleWithURL:url];
}
void showToastInView(NSString *text, UIView *superView) {
if (!superView) {
return;
}
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:20.f];
label.text = text;
label.textAlignment = NSTextAlignmentCenter;
label.layer.masksToBounds = YES;
label.backgroundColor = [UIColor grayColor];
label.textColor = [UIColor whiteColor];
[label sizeToFit];
label.width += 30;
label.height += 10;
label.layer.cornerRadius = label.height / 2;
label.bottom = superView.height - 20;
label.centerX = superView.width / 2;
[superView addSubview:label];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[label removeFromSuperview];
});
}
void showToast(NSString *text) {
showToastInView(text, [UIApplication sharedApplication].windows.lastObject);
}