FLBFlutterEngine.m 3.42 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 33 34
@property (nonatomic,strong) FlutterEngine *engine;
@end

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

Jidong Chen's avatar
Jidong Chen committed
61 62
- (instancetype)init
{
63
    return [self initWithPlatform:nil engine:nil];
Jidong Chen's avatar
Jidong Chen committed
64 65
}

Jidong Chen's avatar
init  
Jidong Chen committed
66 67
- (void)pause
{
Jidong Chen's avatar
Jidong Chen committed
68 69
    [[_engine lifecycleChannel] sendMessage:@"AppLifecycleState.paused"];
    [self detach];
Jidong Chen's avatar
init  
Jidong Chen committed
70 71 72 73
}

- (void)resume
{
余玠's avatar
余玠 committed
74 75 76
    if([UIApplication sharedApplication].applicationState == UIApplicationStateActive){
        [[_engine lifecycleChannel] sendMessage:@"AppLifecycleState.resumed"];
    }
Jidong Chen's avatar
init  
Jidong Chen committed
77 78 79 80 81 82 83
}

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

Jidong Chen's avatar
Jidong Chen committed
84

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

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

97
- (BOOL)atacheToViewController:(FlutterViewController *)vc
Jidong Chen's avatar
Jidong Chen committed
98 99 100
{
    if(_engine.viewController != vc){
        _engine.viewController = vc;
101
        return YES;
Jidong Chen's avatar
Jidong Chen committed
102
    }
103
    return NO;
Jidong Chen's avatar
Jidong Chen committed
104 105 106 107
}

- (void)detach
{
108
    if(_engine.viewController != nil){
余玠's avatar
余玠 committed
109
        [(FLBFlutterViewContainer *)_engine.viewController surfaceUpdated:NO];
110
        _engine.viewController = nil;
Jidong Chen's avatar
Jidong Chen committed
111 112 113 114
    }
}

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

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