FlutterBoostPlugin.m 9.02 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
                                                        result:resultData
                                                          exts:exts
                                                    completion:^(BOOL r){
                                                        result(@(r));
                                                    }];
    }else if([@"onShownContainerChanged" isEqualToString:call.method]){
70 71 72 73 74 75 76
        NSDictionary *args = [FLBCollectionHelper deepCopyNSDictionary:call.arguments
        filter:^bool(id  _Nonnull value) {
            return ![value isKindOfClass:NSNull.class];
        }];
        
        NSString *newName = args[@"newName"];
        NSString *uid = args[@"uniqueId"];
Jidong Chen's avatar
Jidong Chen committed
77
        if(newName){
余玠's avatar
余玠 committed
78
            [[FlutterBoostPlugin sharedInstance].application onShownContainerChanged:uid params:args];
Jidong Chen's avatar
Jidong Chen committed
79 80
            [NSNotificationCenter.defaultCenter postNotificationName:@"flutter_boost_container_showed"
                                                              object:newName];
Jidong Chen's avatar
Jidong Chen committed
81
        }
Jidong Chen's avatar
Jidong Chen committed
82
    }else if([@"openPage" isEqualToString:call.method]){
83 84 85 86
        NSDictionary *args = [FLBCollectionHelper deepCopyNSDictionary:call.arguments
                                                                filter:^bool(id  _Nonnull value) {
                                                                    return ![value isKindOfClass:NSNull.class];
                                                                }];
Jidong Chen's avatar
Jidong Chen committed
87 88 89
        NSString *url = args[@"url"];
        NSDictionary *urlParams = args[@"urlParams"];
        NSDictionary *exts = args[@"exts"];
Jidong Chen's avatar
Jidong Chen committed
90 91 92
        NSNull2Nil(url);
        NSNull2Nil(urlParams);
        NSNull2Nil(exts);
93
        [[FlutterBoostPlugin sharedInstance].application open:url
Jidong Chen's avatar
Jidong Chen committed
94 95
                                                    urlParams:urlParams
                                                         exts:exts
余玠's avatar
余玠 committed
96
                                                        onPageFinished:result
Jidong Chen's avatar
Jidong Chen committed
97 98 99
                                                   completion:^(BOOL r) {}];
    }else if([@"pageOnStart" isEqualToString:call.method]){
        NSMutableDictionary *pageInfo = [NSMutableDictionary new];
100 101 102
        pageInfo[@"name"] =[FlutterBoostPlugin sharedInstance].fPagename;
        pageInfo[@"params"] = [FlutterBoostPlugin sharedInstance].fParams;
        pageInfo[@"uniqueId"] = [FlutterBoostPlugin sharedInstance].fPageId;
Jidong Chen's avatar
Jidong Chen committed
103 104 105
        if(result) result(pageInfo);
    }else{
        result(FlutterMethodNotImplemented);
Jidong Chen's avatar
Jidong Chen committed
106 107 108
    }
}

Jidong Chen's avatar
Jidong Chen committed
109

Jidong Chen's avatar
init  
Jidong Chen committed
110 111 112 113 114 115 116 117 118 119 120
+ (instancetype)sharedInstance
{
    static id _instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [self.class new];
    });
    
    return _instance;
}

121 122 123 124 125
+ (NSInteger)pageCount{
    id<FLBFlutterApplicationInterface> app = [[FlutterBoostPlugin sharedInstance] application];
    return [app pageCount];
}

126
- (void)startFlutterWithPlatform:(id<FLBPlatform>)platform
余玠's avatar
余玠 committed
127
                         onStart:(void (^)(FlutterEngine *engine))callback;
128
{
129 130 131 132
    [self startFlutterWithPlatform:platform
                            engine:nil
             pluginRegisterred:YES
                           onStart:callback];
133 134 135 136 137
}

- (void)startFlutterWithPlatform:(id<FLBPlatform>)platform
                         engine:(FlutterEngine* _Nullable)engine
                         onStart:(void (^)(FlutterEngine *engine))callback;
Jidong Chen's avatar
init  
Jidong Chen committed
138
{
139 140 141 142 143 144 145 146 147 148
    [self startFlutterWithPlatform:platform
                                 engine:engine
                                  pluginRegisterred:YES
                                   onStart:callback];
}

- (void)startFlutterWithPlatform:(id<FLBPlatform>)platform
                          engine:(FlutterEngine *)engine
           pluginRegisterred:(BOOL)registerPlugin
                         onStart:(void (^)(FlutterEngine * _Nonnull))callback{
Jidong Chen's avatar
Jidong Chen committed
149
    static dispatch_once_t onceToken;
150
    __weak __typeof__(self) weakSelf = self;
Jidong Chen's avatar
Jidong Chen committed
151
    dispatch_once(&onceToken, ^{
152
        __strong __typeof__(weakSelf) self = weakSelf;
153 154
        FLBFactory *factory = FLBFactory.new;
        self.application = [factory createApplication:platform];
155
        [self.application startFlutterWithPlatform:platform
156
                                     withEngine:engine
157
                                      withPluginRegisterred:registerPlugin
158
                                       onStart:callback];
Jidong Chen's avatar
Jidong Chen committed
159
    });
Jidong Chen's avatar
init  
Jidong Chen committed
160 161 162 163
}

- (BOOL)isRunning
{
Jidong Chen's avatar
Jidong Chen committed
164
    return [self.application isRunning];
Jidong Chen's avatar
init  
Jidong Chen committed
165 166
}

Jidong Chen's avatar
Jidong Chen committed
167

Jidong Chen's avatar
init  
Jidong Chen committed
168 169
- (FlutterViewController *)currentViewController
{
Jidong Chen's avatar
Jidong Chen committed
170
    return [self.application flutterViewController];
Jidong Chen's avatar
init  
Jidong Chen committed
171 172 173
}


Jidong Chen's avatar
Jidong Chen committed
174 175 176 177
#pragma mark - broadcast event to/from flutter
- (void)sendEvent:(NSString *)eventName
        arguments:(NSDictionary *)arguments
{
Jidong Chen's avatar
Jidong Chen committed
178 179
    [BoostMessageChannel sendEvent:eventName
                         arguments:arguments];
Jidong Chen's avatar
Jidong Chen committed
180 181 182 183 184
}

- (FLBVoidCallback)addEventListener:(FLBEventListener)listner
                            forName:(NSString *)name
{
Jidong Chen's avatar
Jidong Chen committed
185 186
   return [BoostMessageChannel addEventListener:listner
                                        forName:name];
Jidong Chen's avatar
Jidong Chen committed
187 188
}

189
#pragma mark - open/close Page
余玠's avatar
余玠 committed
190
+ (void)open:(NSString *)url urlParams:(NSDictionary *)urlParams exts:(NSDictionary *)exts onPageFinished:(void (^)(NSDictionary *))resultCallback completion:(void (^)(BOOL))completion{
191
    id<FLBFlutterApplicationInterface> app = [[FlutterBoostPlugin sharedInstance] application];
余玠's avatar
余玠 committed
192
    [app open:url urlParams:urlParams exts:exts onPageFinished:resultCallback completion:completion];
193 194
}

余玠's avatar
余玠 committed
195 196 197 198 199 200 201
+ (void)present:(NSString *)url urlParams:(NSDictionary *)urlParams exts:(NSDictionary *)exts onPageFinished:(void (^)(NSDictionary *))resultCallback completion:(void (^)(BOOL))completion{
    id<FLBFlutterApplicationInterface> app = [[FlutterBoostPlugin sharedInstance] application];
    NSMutableDictionary *myParams = [[NSMutableDictionary alloc]initWithDictionary:urlParams];
    [myParams setObject:@(YES) forKey:@"present"];
    [app open:url urlParams:myParams exts:exts onPageFinished:resultCallback completion:completion];
}

余玠's avatar
余玠 committed
202
+ (void)close:(NSString *)uniqueId result:(NSDictionary *)resultData exts:(NSDictionary *)exts completion:(void (^)(BOOL))completion{
203
    id<FLBFlutterApplicationInterface> app = [[FlutterBoostPlugin sharedInstance] application];
余玠's avatar
余玠 committed
204
    [app close:uniqueId result:resultData exts:exts completion:completion];
205
}
206

207
- (void)destroyPluginContext{
208 209 210
    self.methodChannel = nil;
    self.application = nil;
}
Jidong Chen's avatar
init  
Jidong Chen committed
211
@end