FlutterBoostPlugin.m 6.47 KB
Newer Older
Jidong Chen's avatar
init  
Jidong Chen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2019 Alibaba Group
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

25 26 27
#import "FlutterBoostPlugin.h"
#import "FlutterBoostPlugin_private.h"
#import "FLBFactory.h"
Jidong Chen's avatar
Jidong Chen committed
28
#import "BoostMessageChannel.h"
29
#import "FLBCollectionHelper.h"
Jidong Chen's avatar
Jidong Chen committed
30

Jidong Chen's avatar
Jidong Chen committed
31 32
#define NSNull2Nil(_x_) if([_x_ isKindOfClass: NSNull.class]) _x_ = nil;

33
@interface FlutterBoostPlugin()
Jidong Chen's avatar
Jidong Chen committed
34 35
@end

36
@implementation FlutterBoostPlugin
Jidong Chen's avatar
init  
Jidong Chen committed
37

Jidong Chen's avatar
Jidong Chen committed
38 39
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
    FlutterMethodChannel* channel = [FlutterMethodChannel
Jidong Chen's avatar
Jidong Chen committed
40
                                     methodChannelWithName:@"flutter_boost"
Jidong Chen's avatar
Jidong Chen committed
41
                                     binaryMessenger:[registrar messenger]];
42
    FlutterBoostPlugin* instance = [self.class sharedInstance];
Jidong Chen's avatar
Jidong Chen committed
43 44 45 46 47 48 49
    instance.methodChannel = channel;
    [registrar addMethodCallDelegate:instance channel:channel];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    if ([@"getPlatformVersion" isEqualToString:call.method]) {
        result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
Jidong Chen's avatar
Jidong Chen committed
50
    } else if([@"__event__" isEqual: call.method]){
Jidong Chen's avatar
Jidong Chen committed
51 52
        [BoostMessageChannel handleMethodCall:call result:result];
    }else if([@"closePage" isEqualToString:call.method]){
53 54 55 56
        NSDictionary *args = [FLBCollectionHelper deepCopyNSDictionary:call.arguments
                                                                filter:^bool(id  _Nonnull value) {
                                                    return ![value isKindOfClass:NSNull.class];
        }];
Jidong Chen's avatar
Jidong Chen committed
57 58 59
        NSDictionary *exts = args[@"exts"];
        NSString *uid = args[@"uniqueId"];
        NSDictionary *resultData = args[@"result"];
Jidong Chen's avatar
Jidong Chen committed
60 61 62
        NSNull2Nil(exts);
        NSNull2Nil(resultData);
        NSNull2Nil(uid);
63
        [[FlutterBoostPlugin sharedInstance].application close:uid
Jidong Chen's avatar
Jidong Chen committed
64 65 66 67 68 69 70 71 72 73
                                                        result:resultData
                                                          exts:exts
                                                    completion:^(BOOL r){
                                                        result(@(r));
                                                    }];
    }else if([@"onShownContainerChanged" isEqualToString:call.method]){
        NSString *newName = call.arguments[@"newName"];
        if(newName){
            [NSNotificationCenter.defaultCenter postNotificationName:@"flutter_boost_container_showed"
                                                              object:newName];
Jidong Chen's avatar
Jidong Chen committed
74
        }
Jidong Chen's avatar
Jidong Chen committed
75
    }else if([@"openPage" isEqualToString:call.method]){
76 77 78 79
        NSDictionary *args = [FLBCollectionHelper deepCopyNSDictionary:call.arguments
                                                                filter:^bool(id  _Nonnull value) {
                                                                    return ![value isKindOfClass:NSNull.class];
                                                                }];
Jidong Chen's avatar
Jidong Chen committed
80 81 82
        NSString *url = args[@"url"];
        NSDictionary *urlParams = args[@"urlParams"];
        NSDictionary *exts = args[@"exts"];
Jidong Chen's avatar
Jidong Chen committed
83 84 85
        NSNull2Nil(url);
        NSNull2Nil(urlParams);
        NSNull2Nil(exts);
86
        [[FlutterBoostPlugin sharedInstance].application open:url
Jidong Chen's avatar
Jidong Chen committed
87 88 89 90 91 92
                                                    urlParams:urlParams
                                                         exts:exts
                                                        reult:result
                                                   completion:^(BOOL r) {}];
    }else if([@"pageOnStart" isEqualToString:call.method]){
        NSMutableDictionary *pageInfo = [NSMutableDictionary new];
93 94 95
        pageInfo[@"name"] =[FlutterBoostPlugin sharedInstance].fPagename;
        pageInfo[@"params"] = [FlutterBoostPlugin sharedInstance].fParams;
        pageInfo[@"uniqueId"] = [FlutterBoostPlugin sharedInstance].fPageId;
Jidong Chen's avatar
Jidong Chen committed
96 97 98
        if(result) result(pageInfo);
    }else{
        result(FlutterMethodNotImplemented);
Jidong Chen's avatar
Jidong Chen committed
99 100 101
    }
}

Jidong Chen's avatar
Jidong Chen committed
102

Jidong Chen's avatar
init  
Jidong Chen committed
103 104 105 106 107 108 109 110 111 112 113 114
+ (instancetype)sharedInstance
{
    static id _instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [self.class new];
    });
    
    return _instance;
}


Jidong Chen's avatar
Jidong Chen committed
115 116 117 118 119 120 121 122 123 124 125
- (id<FLBFlutterApplicationInterface>)application
{
    return _application;
}


- (id<FLBAbstractFactory>)factory
{
    return _factory;
}

126
- (void)startFlutterWithPlatform:(id<FLBPlatform>)platform
Jidong Chen's avatar
Jidong Chen committed
127 128 129
                         onStart:(void (^)(id<FlutterBinaryMessenger,
                                             FlutterTextureRegistry,
                                           FlutterPluginRegistry> engine))callback;
Jidong Chen's avatar
init  
Jidong Chen committed
130
{
Jidong Chen's avatar
Jidong Chen committed
131 132
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
133
        self->_factory = FLBFactory.new;
Jidong Chen's avatar
Jidong Chen committed
134 135
        self->_application = [self->_factory createApplication:platform];
        [self->_application startFlutterWithPlatform:platform
136
                                       onStart:callback];
Jidong Chen's avatar
Jidong Chen committed
137
    });
Jidong Chen's avatar
init  
Jidong Chen committed
138 139 140 141
}

- (BOOL)isRunning
{
Jidong Chen's avatar
Jidong Chen committed
142
    return [self.application isRunning];
Jidong Chen's avatar
init  
Jidong Chen committed
143 144
}

Jidong Chen's avatar
Jidong Chen committed
145

Jidong Chen's avatar
init  
Jidong Chen committed
146 147
- (FlutterViewController *)currentViewController
{
Jidong Chen's avatar
Jidong Chen committed
148
    return [self.application flutterViewController];
Jidong Chen's avatar
init  
Jidong Chen committed
149 150 151
}


Jidong Chen's avatar
Jidong Chen committed
152 153 154 155
#pragma mark - broadcast event to/from flutter
- (void)sendEvent:(NSString *)eventName
        arguments:(NSDictionary *)arguments
{
Jidong Chen's avatar
Jidong Chen committed
156 157
    [BoostMessageChannel sendEvent:eventName
                         arguments:arguments];
Jidong Chen's avatar
Jidong Chen committed
158 159 160 161 162
}

- (FLBVoidCallback)addEventListener:(FLBEventListener)listner
                            forName:(NSString *)name
{
Jidong Chen's avatar
Jidong Chen committed
163 164
   return [BoostMessageChannel addEventListener:listner
                                        forName:name];
Jidong Chen's avatar
Jidong Chen committed
165 166
}

Jidong Chen's avatar
init  
Jidong Chen committed
167
@end