FLB2FlutterApplication.m 6.24 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.
 */

Jidong Chen's avatar
Jidong Chen committed
25
#import "FLB2FlutterApplication.h"
26 27
#import "FlutterBoost2.h"
#import "FLB2FlutterContainerManager.h"
Jidong Chen's avatar
Jidong Chen committed
28
#import "FLB2FlutterEngine.h"
Jidong Chen's avatar
init  
Jidong Chen committed
29

Jidong Chen's avatar
Jidong Chen committed
30
@interface FLB2FlutterApplication()
31
@property (nonatomic,strong) FLB2FlutterContainerManager *manager;
Jidong Chen's avatar
Jidong Chen committed
32
@property (nonatomic,strong) id<FLB2FlutterProvider> 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


Jidong Chen's avatar
Jidong Chen committed
39
@implementation FLB2FlutterApplication
Jidong Chen's avatar
init  
Jidong Chen committed
40

Jidong Chen's avatar
Jidong Chen committed
41
+ (FLB2FlutterApplication *)sharedApplication
Jidong Chen's avatar
init  
Jidong Chen committed
42
{
Jidong Chen's avatar
Jidong Chen committed
43
    static FLB2FlutterApplication *instance = nil;
Jidong Chen's avatar
init  
Jidong Chen committed
44 45 46 47 48 49 50 51 52 53 54 55
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [self new];
    });
    return instance;
}

- (BOOL)isRunning
{
    return _isRunning;
}

Jidong Chen's avatar
Jidong Chen committed
56 57 58 59 60
- (id)flutterProvider
{
    return _viewProvider;
}

Jidong Chen's avatar
Jidong Chen committed
61
- (void)startFlutterWithPlatform:(id<FLB2Platform>)platform
Jidong Chen's avatar
Jidong Chen committed
62
                         onStart:(void (^)(id<FlutterBinaryMessenger,FlutterTextureRegistry,FlutterPluginRegistry> _Nonnull))callback
Jidong Chen's avatar
init  
Jidong Chen committed
63 64 65 66
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        self.platform = platform;
Jidong Chen's avatar
Jidong Chen committed
67
        self.viewProvider = [[FLB2FlutterEngine alloc] initWithPlatform:platform];
Jidong Chen's avatar
init  
Jidong Chen committed
68
        self.isRunning = YES;
Jidong Chen's avatar
Jidong Chen committed
69
        if(callback) callback(self.viewProvider.engine);
Jidong Chen's avatar
init  
Jidong Chen committed
70 71 72 73 74 75
    });
}

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

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

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

104 105 106 107 108 109 110
- (void)applicationDidEnterBackground:(UIApplication *)application {
    [self.viewProvider didEnterBackground];
}

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

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

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

Jidong Chen's avatar
Jidong Chen committed
122
- (void)removeViewController:(id<FLBFlutterContainer>)vc
Jidong Chen's avatar
init  
Jidong Chen committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
{
    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
148
- (FlutterViewController *)flutterViewController
149
{
Jidong Chen's avatar
Jidong Chen committed
150
    return self.flutterProvider.engine.viewController;
151
}
Jidong Chen's avatar
init  
Jidong Chen committed
152

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

- (void)open:(NSString *)url
   urlParams:(NSDictionary *)urlParams
        exts:(NSDictionary *)exts
       reult:(void (^)(NSDictionary *))resultCallback
Jidong Chen's avatar
Jidong Chen committed
174 175 176 177 178 179 180 181 182
  completion:(void (^)(BOOL))completion
{
    NSString *cid = urlParams[@"__calback_id__"];
   
    if(!cid){
        static int64_t sCallbackID = 1;
        cid = @(sCallbackID).stringValue;
        sCallbackID += 2;
    }
Jidong Chen's avatar
Jidong Chen committed
183
    
Jidong Chen's avatar
Jidong Chen committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    _callbackCache[cid] = resultCallback;
    
    [self.platform open:url
              urlParams:urlParams
                   exts:exts
             completion:completion];
}

- (void)didInitPageContainer:(NSString *)url
                      params:(NSDictionary *)urlParams
                    uniqueId:(NSString *)uniqueId
{
    NSString *cid = urlParams[@"__calback_id__"];
    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
212 213
}

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