FLBFlutterEngine.m 3.63 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
#import "FLBFlutterEngine.h"
Jidong Chen's avatar
init  
Jidong Chen committed
26
#import <Flutter/Flutter.h>
27
#import "FLBFlutterViewContainer.h"
28
#import "BoostMessageChannel.h"
Jidong Chen's avatar
init  
Jidong Chen committed
29 30


31
@interface FLBFlutterEngine()
Jidong Chen's avatar
init  
Jidong Chen committed
32
@property (nonatomic,strong) FlutterEngine *engine;
33
@property (nonatomic,strong)  FLBFlutterViewContainer *dummy;
Jidong Chen's avatar
init  
Jidong Chen committed
34 35
@end

36
@implementation FLBFlutterEngine
Jidong Chen's avatar
Jidong Chen committed
37
    
38
- (instancetype)initWithPlatform:(id<FLBPlatform> _Nullable)platform engine:(FlutterEngine * _Nullable)engine
Jidong Chen's avatar
init  
Jidong Chen committed
39 40 41 42 43
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    
    if (self = [super init]) {
44 45 46 47 48
        if(!engine){
            _engine = [[FlutterEngine alloc] initWithName:@"io.flutter" project:nil];
        }else{
            _engine = engine;
        }
Jidong Chen's avatar
Jidong Chen committed
49 50 51 52 53 54 55
        if(platform &&
           [platform respondsToSelector: @selector(entryForDart)] &&
           platform.entryForDart){
            [_engine runWithEntrypoint:platform.entryForDart];
        }else{
            [_engine runWithEntrypoint:nil];
        }
56 57 58 59
//        _dummy = [[FLBFlutterViewContainer alloc] initWithEngine:_engine
//                                                          nibName:nil
//                                                           bundle:nil];
//        _dummy.name = kIgnoreMessageWithName;
Jidong Chen's avatar
init  
Jidong Chen committed
60 61 62 63 64 65
    }
    
    return self;
#pragma clang diagnostic pop
}

Jidong Chen's avatar
Jidong Chen committed
66 67
- (instancetype)init
{
68
    return [self initWithPlatform:nil engine:nil];
Jidong Chen's avatar
Jidong Chen committed
69 70
}

Jidong Chen's avatar
init  
Jidong Chen committed
71 72
- (void)pause
{
Jidong Chen's avatar
Jidong Chen committed
73 74
    [[_engine lifecycleChannel] sendMessage:@"AppLifecycleState.paused"];
    [self detach];
Jidong Chen's avatar
init  
Jidong Chen committed
75 76 77 78
}

- (void)resume
{
Jidong Chen's avatar
Jidong Chen committed
79
    [[_engine lifecycleChannel] sendMessage:@"AppLifecycleState.resumed"];
Jidong Chen's avatar
init  
Jidong Chen committed
80 81 82 83 84 85 86
}

- (void)inactive
{
    [[_engine lifecycleChannel] sendMessage:@"AppLifecycleState.inactive"];
}

Jidong Chen's avatar
Jidong Chen committed
87

88 89
- (void)didEnterBackground
{
90 91
    [BoostMessageChannel sendEvent:@"lifecycle"
                         arguments:@{@"type":@"background"}];
92 93 94 95
}

- (void)willEnterForeground
{
96 97
    [BoostMessageChannel sendEvent:@"lifecycle"
                         arguments:@{@"type":@"foreground"}];
98
}
Jidong Chen's avatar
Jidong Chen committed
99

Jidong Chen's avatar
Jidong Chen committed
100 101 102
- (void)atacheToViewController:(FlutterViewController *)vc
{
    if(_engine.viewController != vc){
余玠's avatar
余玠 committed
103
//        [(FLBFlutterViewContainer *)_engine.viewController surfaceUpdated:NO];
Jidong Chen's avatar
Jidong Chen committed
104 105 106 107 108 109 110 111 112 113 114 115
        _engine.viewController = vc;
    }
}

- (void)detach
{
    if(_engine.viewController != _dummy){
        _engine.viewController = _dummy;
    }
}

- (void)prepareEngineIfNeeded
116
{
余玠's avatar
余玠 committed
117 118
//    [(FLBFlutterViewContainer *)_engine.viewController surfaceUpdated:NO];
//    NSLog(@"[XDEBUG]---surface changed--reset-");
119
//    [self detach];
120
}
Jidong Chen's avatar
Jidong Chen committed
121

122 123 124
- (void)dealloc{
    [self.engine setViewController:nil];
}
Jidong Chen's avatar
init  
Jidong Chen committed
125 126
@end