FlutterBoostPlugin.m 8.09 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
                                                    urlParams:urlParams
                                                         exts:exts
余玠's avatar
余玠 committed
89
                                                        onPageFinished:result
Jidong Chen's avatar
Jidong Chen committed
90 91 92
                                                   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
+ (instancetype)sharedInstance
{
    static id _instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [self.class new];
    });
    
    return _instance;
}

114 115 116 117 118
+ (NSInteger)pageCount{
    id<FLBFlutterApplicationInterface> app = [[FlutterBoostPlugin sharedInstance] application];
    return [app pageCount];
}

119
- (void)startFlutterWithPlatform:(id<FLBPlatform>)platform
余玠's avatar
余玠 committed
120
                         onStart:(void (^)(FlutterEngine *engine))callback;
121 122 123 124 125 126 127
{
    [self startFlutterWithPlatform:platform engine:nil onStart:callback];
}

- (void)startFlutterWithPlatform:(id<FLBPlatform>)platform
                         engine:(FlutterEngine* _Nullable)engine
                         onStart:(void (^)(FlutterEngine *engine))callback;
Jidong Chen's avatar
init  
Jidong Chen committed
128
{
Jidong Chen's avatar
Jidong Chen committed
129
    static dispatch_once_t onceToken;
130
    __weak __typeof__(self) weakSelf = self;
Jidong Chen's avatar
Jidong Chen committed
131
    dispatch_once(&onceToken, ^{
132
        __strong __typeof__(weakSelf) self = weakSelf;
133 134
        FLBFactory *factory = FLBFactory.new;
        self.application = [factory createApplication:platform];
135
        [self.application startFlutterWithPlatform:platform
136
                                     withEngine:engine
137
                                       onStart:callback];
Jidong Chen's avatar
Jidong Chen committed
138
    });
Jidong Chen's avatar
init  
Jidong Chen committed
139 140 141 142
}

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

Jidong Chen's avatar
Jidong Chen committed
146

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


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

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

168
#pragma mark - open/close Page
余玠's avatar
余玠 committed
169
+ (void)open:(NSString *)url urlParams:(NSDictionary *)urlParams exts:(NSDictionary *)exts onPageFinished:(void (^)(NSDictionary *))resultCallback completion:(void (^)(BOOL))completion{
170
    id<FLBFlutterApplicationInterface> app = [[FlutterBoostPlugin sharedInstance] application];
余玠's avatar
余玠 committed
171
    [app open:url urlParams:urlParams exts:exts onPageFinished:resultCallback completion:completion];
172 173
}

余玠's avatar
余玠 committed
174 175 176 177 178 179 180
+ (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
181
+ (void)close:(NSString *)uniqueId result:(NSDictionary *)resultData exts:(NSDictionary *)exts completion:(void (^)(BOOL))completion{
182
    id<FLBFlutterApplicationInterface> app = [[FlutterBoostPlugin sharedInstance] application];
余玠's avatar
余玠 committed
183
    [app close:uniqueId result:resultData exts:exts completion:completion];
184
}
185

186
- (void)destroyPluginContext{
187 188 189
    self.methodChannel = nil;
    self.application = nil;
}
Jidong Chen's avatar
init  
Jidong Chen committed
190
@end