FLBFlutterApplication.m 7.17 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 28
#import "FLBFlutterApplication.h"
#import "FlutterBoost.h"
#import "FLBFlutterContainerManager.h"
#import "FLBFlutterEngine.h"
Jidong Chen's avatar
init  
Jidong Chen committed
29

30 31 32
@interface FLBFlutterApplication()
@property (nonatomic,strong) FLBFlutterContainerManager *manager;
@property (nonatomic,strong) id<FLBFlutterProvider> viewProvider;
Jidong Chen's avatar
init  
Jidong Chen committed
33
@property (nonatomic,assign) BOOL isRunning;
Jidong Chen's avatar
Jidong Chen committed
34 35
@property (nonatomic,strong) NSMutableDictionary *pageResultCallbacks;
@property (nonatomic,strong) NSMutableDictionary *callbackCache;
Jidong Chen's avatar
init  
Jidong Chen committed
36 37 38
@end


39
@implementation FLBFlutterApplication
40
@synthesize platform;
Jidong Chen's avatar
init  
Jidong Chen committed
41 42 43 44 45 46

- (BOOL)isRunning
{
    return _isRunning;
}

Jidong Chen's avatar
Jidong Chen committed
47 48 49 50 51
- (id)flutterProvider
{
    return _viewProvider;
}

52
- (void)startFlutterWithPlatform:(id<FLBPlatform>)platform
53
                      withEngine:(FlutterEngine* _Nullable)engine
54
                        withPluginRegisterred:(BOOL)registerPlugin
余玠's avatar
余玠 committed
55
                         onStart:(void (^)(FlutterEngine *engine))callback
Jidong Chen's avatar
init  
Jidong Chen committed
56 57 58 59
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        self.platform = platform;
60
        self.viewProvider = [[FLBFlutterEngine alloc] initWithPlatform:platform engine:engine];
Jidong Chen's avatar
init  
Jidong Chen committed
61
        self.isRunning = YES;
62 63 64 65 66 67 68 69 70 71
        if(registerPlugin){
            Class clazz = NSClassFromString(@"GeneratedPluginRegistrant");
            FlutterEngine *myengine = [self.viewProvider engine];
            if (clazz && myengine) {
                if ([clazz respondsToSelector:NSSelectorFromString(@"registerWithRegistry:")]) {
                    [clazz performSelector:NSSelectorFromString(@"registerWithRegistry:")
                                withObject:myengine];
                }
            }
        }
Jidong Chen's avatar
Jidong Chen committed
72
        if(callback) callback(self.viewProvider.engine);
Jidong Chen's avatar
init  
Jidong Chen committed
73 74 75 76 77 78
    });
}

- (instancetype)init
{
    if (self = [super init]) {
79
        _manager = [FLBFlutterContainerManager new];
Jidong Chen's avatar
Jidong Chen committed
80 81
        _pageResultCallbacks = NSMutableDictionary.new;
        _callbackCache = NSMutableDictionary.new;
82 83 84 85 86 87 88 89 90 91
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(applicationWillEnterForeground:)
                                                     name:UIApplicationWillEnterForegroundNotification
                                                   object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(applicationDidEnterBackground:)
                                                     name:UIApplicationDidEnterBackgroundNotification
                                                   object:nil];
Jidong Chen's avatar
init  
Jidong Chen committed
92 93 94 95 96 97
    }
    return self;
}

- (void)dealloc
{
98 99
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
Jidong Chen's avatar
init  
Jidong Chen committed
100 101 102 103 104 105 106
}

- (UIView *)flutterView
{
    return [self flutterViewController].view;
}

107 108 109 110 111 112 113
- (void)applicationDidEnterBackground:(UIApplication *)application {
    [self.viewProvider didEnterBackground];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [self.viewProvider willEnterForeground];
}
Jidong Chen's avatar
init  
Jidong Chen committed
114

Jidong Chen's avatar
Jidong Chen committed
115
- (BOOL)contains:(id<FLBFlutterContainer>)vc
Jidong Chen's avatar
init  
Jidong Chen committed
116 117 118 119
{
    return [_manager contains:vc];
}

Jidong Chen's avatar
Jidong Chen committed
120
- (void)addUniqueViewController:(id<FLBFlutterContainer>)vc
Jidong Chen's avatar
init  
Jidong Chen committed
121 122 123 124
{
    return [_manager addUnique:vc];
}

Jidong Chen's avatar
Jidong Chen committed
125
- (void)removeViewController:(id<FLBFlutterContainer>)vc
Jidong Chen's avatar
init  
Jidong Chen committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
{
    return [_manager remove:vc];
}


- (BOOL)isTop:(NSString *)pageId
{
    return [_manager.peak isEqual:pageId];
}

- (void)pause
{
    [self.viewProvider pause];
}

- (void)resume
{
    [self.viewProvider resume];
}

- (void)inactive
{
    [self.viewProvider inactive];
}

Jidong Chen's avatar
Jidong Chen committed
151
- (FlutterViewController *)flutterViewController
152
{
Jidong Chen's avatar
Jidong Chen committed
153
    return self.flutterProvider.engine.viewController;
154
}
Jidong Chen's avatar
init  
Jidong Chen committed
155

Jidong Chen's avatar
Jidong Chen committed
156
- (void)close:(NSString *)uniqueId
余玠's avatar
余玠 committed
157
       result:(NSDictionary *)resultData
Jidong Chen's avatar
Jidong Chen committed
158
         exts:(NSDictionary *)exts
Jidong Chen's avatar
Jidong Chen committed
159 160 161
   completion:(void (^)(BOOL))completion
{
    [self.platform close:uniqueId
余玠's avatar
余玠 committed
162
                  result:resultData
Jidong Chen's avatar
Jidong Chen committed
163 164
                    exts:exts
              completion:completion];
Jidong Chen's avatar
Jidong Chen committed
165
    
Jidong Chen's avatar
Jidong Chen committed
166 167
    if(_pageResultCallbacks[uniqueId]){
        void (^cb)(NSDictionary *) = _pageResultCallbacks[uniqueId];
余玠's avatar
余玠 committed
168
        cb(resultData);
Jidong Chen's avatar
Jidong Chen committed
169 170
        [_pageResultCallbacks removeObjectForKey:uniqueId];
    }
Jidong Chen's avatar
Jidong Chen committed
171 172 173 174 175
}

- (void)open:(NSString *)url
   urlParams:(NSDictionary *)urlParams
        exts:(NSDictionary *)exts
余玠's avatar
余玠 committed
176
       onPageFinished:(void (^)(NSDictionary *))resultCallback
Jidong Chen's avatar
Jidong Chen committed
177 178
  completion:(void (^)(BOOL))completion
{
179
    NSString *cid = urlParams[kPageCallBackId];
Jidong Chen's avatar
Jidong Chen committed
180 181 182 183 184
   
    if(!cid){
        static int64_t sCallbackID = 1;
        cid = @(sCallbackID).stringValue;
        sCallbackID += 2;
185 186 187
        NSMutableDictionary *newParams = [[NSMutableDictionary alloc]initWithDictionary:urlParams];
        [newParams setObject:cid?cid:@"__default#0__" forKey:kPageCallBackId];
        urlParams = newParams;
Jidong Chen's avatar
Jidong Chen committed
188
    }
Jidong Chen's avatar
Jidong Chen committed
189
    
Jidong Chen's avatar
Jidong Chen committed
190
    _callbackCache[cid] = resultCallback;
余玠's avatar
余玠 committed
191 192 193 194 195 196 197 198 199 200 201
    if([urlParams[@"present"]respondsToSelector:@selector(boolValue)] && [urlParams[@"present"] boolValue] && [self.platform respondsToSelector:@selector(present:urlParams:exts:completion:)]){
        [self.platform present:url
                  urlParams:urlParams
                       exts:exts
                 completion:completion];
    }else{
        [self.platform open:url
                  urlParams:urlParams
                       exts:exts
                 completion:completion];
    }
Jidong Chen's avatar
Jidong Chen committed
202 203 204 205 206 207
}

- (void)didInitPageContainer:(NSString *)url
                      params:(NSDictionary *)urlParams
                    uniqueId:(NSString *)uniqueId
{
208
    NSString *cid = urlParams[kPageCallBackId];
Jidong Chen's avatar
Jidong Chen committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    if(cid && _callbackCache[cid]){
        _pageResultCallbacks[uniqueId] = _callbackCache[cid];
        [_callbackCache removeObjectForKey:cid];
    }
}

- (void)willDeallocPageContainer:(NSString *)url
                          params:(NSDictionary *)params
                        uniqueId:(NSString *)uniqueId
{
    if(_pageResultCallbacks[uniqueId]){
        void (^cb)(NSDictionary *) = _pageResultCallbacks[uniqueId];
        cb(@{});
        [_pageResultCallbacks removeObjectForKey:uniqueId];
    }
Jidong Chen's avatar
Jidong Chen committed
224 225
}

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