FLBFlutterEngine.m 4.13 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
    
余玠's avatar
余玠 committed
38
- (instancetype)initWithPlatform:(id<FLBPlatform> _Nullable)platform
Jidong Chen's avatar
init  
Jidong Chen committed
39 40 41 42 43 44
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    
    if (self = [super init]) {
        _engine = [[FlutterEngine alloc] initWithName:@"io.flutter" project:nil];
Jidong Chen's avatar
Jidong Chen committed
45 46 47 48 49 50 51
        if(platform &&
           [platform respondsToSelector: @selector(entryForDart)] &&
           platform.entryForDart){
            [_engine runWithEntrypoint:platform.entryForDart];
        }else{
            [_engine runWithEntrypoint:nil];
        }
52
        _dummy = [[FLBFlutterViewContainer alloc] initWithEngine:_engine
Jidong Chen's avatar
Jidong Chen committed
53 54
                                                          nibName:nil
                                                           bundle:nil];
55 56
        _dummy.name = kIgnoreMessageWithName;
        
Jidong Chen's avatar
init  
Jidong Chen committed
57 58 59 60
        Class clazz = NSClassFromString(@"GeneratedPluginRegistrant");
        if (clazz) {
            if ([clazz respondsToSelector:NSSelectorFromString(@"registerWithRegistry:")]) {
                [clazz performSelector:NSSelectorFromString(@"registerWithRegistry:")
Jidong Chen's avatar
Jidong Chen committed
61
                            withObject:_engine];
Jidong Chen's avatar
init  
Jidong Chen committed
62 63 64 65 66 67 68 69
            }
        }
    }
    
    return self;
#pragma clang diagnostic pop
}

Jidong Chen's avatar
Jidong Chen committed
70 71 72 73 74
- (instancetype)init
{
    return [self initWithPlatform:nil];
}

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

- (void)resume
{
Jidong Chen's avatar
Jidong Chen committed
83
    [[_engine lifecycleChannel] sendMessage:@"AppLifecycleState.resumed"];
Jidong Chen's avatar
init  
Jidong Chen committed
84 85 86 87 88 89 90
}

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

Jidong Chen's avatar
Jidong Chen committed
91

92 93 94 95 96 97 98 99 100 101 102
- (void)didEnterBackground
{
    [BoostMessageChannel sendEvent:@"background"
                         arguments:nil];
}

- (void)willEnterForeground
{
    [BoostMessageChannel sendEvent:@"foreground"
                         arguments:nil];
}
Jidong Chen's avatar
Jidong Chen committed
103

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

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

- (void)prepareEngineIfNeeded
120
{
121 122 123
    [(FLBFlutterViewContainer *)_engine.viewController surfaceUpdated:NO];
    NSLog(@"[XDEBUG]---surface changed--reset-");
//    [self detach];
124
}
Jidong Chen's avatar
Jidong Chen committed
125

126 127 128 129 130 131 132 133 134 135
- (void)dealloc{
    [self.engine setViewController:nil];
    [self.engine destroyContext];
    __weak __typeof__(_engine) weak = _engine;
    NSLog(@"Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)weak));
////    CFRelease((__bridge CFTypeRef)_engine);
//    _engine = nil;
//    NSLog(@"after Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)weak));
//    NSLog(@"[DEBUG]---dealloc--");
}
Jidong Chen's avatar
init  
Jidong Chen committed
136 137
@end