FLBFlutterApplication.m 7.96 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"
29
#import "FLBFlutterViewContainer.h"
Jidong Chen's avatar
init  
Jidong Chen committed
30

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


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

- (BOOL)isRunning
{
    return _isRunning;
}

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

54
- (void)startFlutterWithPlatform:(id<FLBPlatform>)platform
55
                      withEngine:(FlutterEngine* _Nullable)engine
56
                        withPluginRegisterred:(BOOL)registerPlugin
余玠's avatar
余玠 committed
57
                         onStart:(void (^)(FlutterEngine *engine))callback
Jidong Chen's avatar
init  
Jidong Chen committed
58 59 60 61
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        self.platform = platform;
62
        self.viewProvider = [[FLBFlutterEngine alloc] initWithPlatform:platform engine:engine];
Jidong Chen's avatar
init  
Jidong Chen committed
63
        self.isRunning = YES;
64 65 66 67 68 69 70 71 72 73
        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
74
        if(callback) callback(self.viewProvider.engine);
Jidong Chen's avatar
init  
Jidong Chen committed
75 76 77 78 79 80
    });
}

- (instancetype)init
{
    if (self = [super init]) {
81
        _manager = [FLBFlutterContainerManager new];
Jidong Chen's avatar
Jidong Chen committed
82 83
        _pageResultCallbacks = NSMutableDictionary.new;
        _callbackCache = NSMutableDictionary.new;
84 85 86 87 88 89 90 91 92 93
        
        [[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
94 95 96 97 98 99
    }
    return self;
}

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

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

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

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

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

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

Jidong Chen's avatar
Jidong Chen committed
127
- (void)removeViewController:(id<FLBFlutterContainer>)vc
Jidong Chen's avatar
init  
Jidong Chen committed
128 129 130 131
{
    return [_manager remove:vc];
}

132 133 134
- (NSInteger)pageCount{
    return [_manager pageCount];
}
Jidong Chen's avatar
init  
Jidong Chen committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

- (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
156
- (FlutterViewController *)flutterViewController
157
{
Jidong Chen's avatar
Jidong Chen committed
158
    return self.flutterProvider.engine.viewController;
159
}
Jidong Chen's avatar
init  
Jidong Chen committed
160

161 162 163 164 165 166 167
- (void)attachToPreviousContainer{
    if([self.viewProvider atacheToViewController:self.previousViewController]){
        [self.previousViewController.view setNeedsLayout];
        [(FLBFlutterViewContainer*)self.previousViewController surfaceUpdated:YES];
    }
}

Jidong Chen's avatar
Jidong Chen committed
168
- (void)close:(NSString *)uniqueId
余玠's avatar
余玠 committed
169
       result:(NSDictionary *)resultData
Jidong Chen's avatar
Jidong Chen committed
170
         exts:(NSDictionary *)exts
Jidong Chen's avatar
Jidong Chen committed
171 172 173
   completion:(void (^)(BOOL))completion
{
    [self.platform close:uniqueId
余玠's avatar
余玠 committed
174
                  result:resultData
Jidong Chen's avatar
Jidong Chen committed
175 176
                    exts:exts
              completion:completion];
Jidong Chen's avatar
Jidong Chen committed
177
    
Jidong Chen's avatar
Jidong Chen committed
178 179
    if(_pageResultCallbacks[uniqueId]){
        void (^cb)(NSDictionary *) = _pageResultCallbacks[uniqueId];
余玠's avatar
余玠 committed
180
        cb(resultData);
Jidong Chen's avatar
Jidong Chen committed
181 182
        [_pageResultCallbacks removeObjectForKey:uniqueId];
    }
Jidong Chen's avatar
Jidong Chen committed
183 184 185 186 187
}

- (void)open:(NSString *)url
   urlParams:(NSDictionary *)urlParams
        exts:(NSDictionary *)exts
余玠's avatar
余玠 committed
188
       onPageFinished:(void (^)(NSDictionary *))resultCallback
Jidong Chen's avatar
Jidong Chen committed
189 190
  completion:(void (^)(BOOL))completion
{
191
    NSString *cid = urlParams[kPageCallBackId];
Jidong Chen's avatar
Jidong Chen committed
192 193 194 195 196
   
    if(!cid){
        static int64_t sCallbackID = 1;
        cid = @(sCallbackID).stringValue;
        sCallbackID += 2;
197 198 199
        NSMutableDictionary *newParams = [[NSMutableDictionary alloc]initWithDictionary:urlParams];
        [newParams setObject:cid?cid:@"__default#0__" forKey:kPageCallBackId];
        urlParams = newParams;
Jidong Chen's avatar
Jidong Chen committed
200
    }
201
    _previousViewController = [self flutterViewController];
Jidong Chen's avatar
Jidong Chen committed
202
    _callbackCache[cid] = resultCallback;
余玠's avatar
余玠 committed
203 204 205 206 207 208 209 210 211 212 213
    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
214 215 216 217 218 219
}

- (void)didInitPageContainer:(NSString *)url
                      params:(NSDictionary *)urlParams
                    uniqueId:(NSString *)uniqueId
{
220
    NSString *cid = urlParams[kPageCallBackId];
Jidong Chen's avatar
Jidong Chen committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    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
236 237
}

238 239 240 241 242
- (void)onShownContainerChanged:(NSString *)uniqueId
                         params:(NSDictionary *)params{
    NSString *oldName = params[@"oldName"];
    NSString *newName = params[@"newName"];
    if (oldName!=nil && [newName isEqualToString:@"default"]) {
余玠's avatar
余玠 committed
243
        [self.flutterProvider detach];
244 245 246
    }
}

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