FlutterBoostPlugin.m 5.92 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 25
/*
 * 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.
 */

#import "FlutterBoostPlugin.h"
Jidong Chen's avatar
Jidong Chen committed
26
#import "FlutterBoostPlugin_private.h"
Jidong Chen's avatar
Jidong Chen committed
27 28
#import "FLBFactory.h"
#import "FLB2Factory.h"
Jidong Chen's avatar
Jidong Chen committed
29 30
#import "BoostMessageChannel.h"
#import "FlutterBoostPlugin_private.h"
Jidong Chen's avatar
Jidong Chen committed
31 32 33 34

@interface FlutterBoostPlugin()
@end

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

Jidong Chen's avatar
Jidong Chen committed
37 38
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
    FlutterMethodChannel* channel = [FlutterMethodChannel
Jidong Chen's avatar
Jidong Chen committed
39
                                     methodChannelWithName:@"flutter_boost_method"
Jidong Chen's avatar
Jidong Chen committed
40 41 42 43 44 45 46 47 48
                                     binaryMessenger:[registrar messenger]];
    FlutterBoostPlugin* instance = [self.class sharedInstance];
    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
49
    } else if([@"__event__" isEqual: call.method]){
Jidong Chen's avatar
Jidong Chen committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        [BoostMessageChannel handleMethodCall:call result:result];
    }else if([@"closePage" isEqualToString:call.method]){
        NSDictionary *args = call.arguments;
        NSDictionary *exts = args[@"exts"];
        NSString *uid = args[@"uniqueId"];
        NSDictionary *resultData = args[@"result"];
        [[FlutterBoostPlugin sharedInstance].application close:uid
                                                        result:resultData
                                                          exts:exts
                                                    completion:^(BOOL r){
                                                        result(@(r));
                                                    }];
    }else if([@"onFlutterPageResult" isEqualToString:call.method]){
        //Do nothing
    }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
69
        }
Jidong Chen's avatar
Jidong Chen committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    }else if([@"openPage" isEqualToString:call.method]){
        NSDictionary *args = call.arguments;
        NSString *url = args[@"url"];
        NSDictionary *urlParams = args[@"urlParams"];
        NSDictionary *exts = args[@"exts"];
        [[FlutterBoostPlugin sharedInstance].application open:url
                                                    urlParams:urlParams
                                                         exts:exts
                                                        reult:result
                                                   completion:^(BOOL r) {}];
    }else if([@"pageOnStart" isEqualToString:call.method]){
        NSMutableDictionary *pageInfo = [NSMutableDictionary new];
        pageInfo[@"name"] =[FlutterBoostPlugin sharedInstance].fPagename;
        pageInfo[@"params"] = [FlutterBoostPlugin sharedInstance].fParams;
        pageInfo[@"uniqueId"] = [FlutterBoostPlugin sharedInstance].fPageId;
        if(result) result(pageInfo);
    }else{
        result(FlutterMethodNotImplemented);
Jidong Chen's avatar
Jidong Chen committed
88 89 90
    }
}

Jidong Chen's avatar
Jidong Chen committed
91

Jidong Chen's avatar
Jidong Chen committed
92

Jidong Chen's avatar
init  
Jidong Chen committed
93 94 95 96 97 98 99 100 101 102 103 104
+ (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
105 106 107 108 109 110 111 112 113 114 115
- (id<FLBFlutterApplicationInterface>)application
{
    return _application;
}


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

Jidong Chen's avatar
Jidong Chen committed
116 117 118 119
- (void)startFlutterWithPlatform:(id<FLB2Platform>)platform
                         onStart:(void (^)(id<FlutterBinaryMessenger,
                                             FlutterTextureRegistry,
                                           FlutterPluginRegistry> engine))callback;
Jidong Chen's avatar
init  
Jidong Chen committed
120
{
Jidong Chen's avatar
Jidong Chen committed
121 122 123
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
Jidong Chen's avatar
Jidong Chen committed
124
        if([platform respondsToSelector:@selector(useBoost2)] && platform.useBoost2){
Jidong Chen's avatar
Jidong Chen committed
125 126 127 128 129 130 131 132
            _factory = FLB2Factory.new;
        }else{
            _factory = FLBFactory.new;
        }
        
        _application = [_factory createApplication:platform];
        [_application startFlutterWithPlatform:platform onStart:callback];
    });
Jidong Chen's avatar
init  
Jidong Chen committed
133 134 135 136
}

- (BOOL)isRunning
{
Jidong Chen's avatar
Jidong Chen committed
137
    return [self.application isRunning];
Jidong Chen's avatar
init  
Jidong Chen committed
138 139
}

Jidong Chen's avatar
Jidong Chen committed
140

Jidong Chen's avatar
init  
Jidong Chen committed
141 142
- (FlutterViewController *)currentViewController
{
Jidong Chen's avatar
Jidong Chen committed
143
    return [self.application flutterViewController];
Jidong Chen's avatar
init  
Jidong Chen committed
144 145 146
}


Jidong Chen's avatar
Jidong Chen committed
147 148 149 150
#pragma mark - broadcast event to/from flutter
- (void)sendEvent:(NSString *)eventName
        arguments:(NSDictionary *)arguments
{
Jidong Chen's avatar
Jidong Chen committed
151 152
    [BoostMessageChannel sendEvent:eventName
                         arguments:arguments];
Jidong Chen's avatar
Jidong Chen committed
153 154 155 156 157
}

- (FLBVoidCallback)addEventListener:(FLBEventListener)listner
                            forName:(NSString *)name
{
Jidong Chen's avatar
Jidong Chen committed
158 159
   return [BoostMessageChannel addEventListener:listner
                                        forName:name];
Jidong Chen's avatar
Jidong Chen committed
160 161
}

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