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

Jidong Chen's avatar
Jidong Chen committed
25
#import "FLB2FlutterApplication.h"
Jidong Chen's avatar
init  
Jidong Chen committed
26
#import "FlutterBoost.h"
Jidong Chen's avatar
Jidong Chen committed
27
#import "FLBFlutterContainerManager.h"
Jidong Chen's avatar
init  
Jidong Chen committed
28

Jidong Chen's avatar
Jidong Chen committed
29 30 31
@interface FLB2FlutterApplication()
@property (nonatomic,strong) FLBFlutterContainerManager *manager;
@property (nonatomic,strong) id<FLB2FlutterProvider> viewProvider;
Jidong Chen's avatar
init  
Jidong Chen committed
32 33 34 35 36

@property (nonatomic,assign) BOOL isRunning;
@end


Jidong Chen's avatar
Jidong Chen committed
37
@implementation FLB2FlutterApplication
Jidong Chen's avatar
init  
Jidong Chen committed
38

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

- (BOOL)isRunning
{
    return _isRunning;
}

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

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

- (instancetype)init
{
    if (self = [super init]) {
Jidong Chen's avatar
Jidong Chen committed
74
        _manager = [FLBFlutterContainerManager new];
Jidong Chen's avatar
init  
Jidong Chen committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

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


Jidong Chen's avatar
Jidong Chen committed
90
- (BOOL)contains:(id<FLBFlutterContainer>)vc
Jidong Chen's avatar
init  
Jidong Chen committed
91 92 93 94
{
    return [_manager contains:vc];
}

Jidong Chen's avatar
Jidong Chen committed
95
- (void)addUniqueViewController:(id<FLBFlutterContainer>)vc
Jidong Chen's avatar
init  
Jidong Chen committed
96 97 98 99
{
    return [_manager addUnique:vc];
}

Jidong Chen's avatar
Jidong Chen committed
100
- (void)removeViewController:(id<FLBFlutterContainer>)vc
Jidong Chen's avatar
init  
Jidong Chen committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
{
    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
126
- (FlutterViewController *)flutterViewController
127
{
Jidong Chen's avatar
Jidong Chen committed
128
    return self.flutterProvider.engine.viewController;
129
}
Jidong Chen's avatar
init  
Jidong Chen committed
130 131

@end