FLBFlutterViewContainer.m 7.18 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 26
#import "FLBFlutterViewContainer.h"
#import "FLBFlutterApplication.h"
Jidong Chen's avatar
Jidong Chen committed
27
#import "BoostMessageChannel.h"
28 29
#import "FLBFlutterContainerManager.h"
#import "FlutterBoostPlugin_private.h"
Jidong Chen's avatar
init  
Jidong Chen committed
30

31
#define FLUTTER_APP [FlutterBoostPlugin sharedInstance].application
Jidong Chen's avatar
Jidong Chen committed
32 33
#define FLUTTER_VIEW FLUTTER_APP.flutterViewController.view
#define FLUTTER_VC FLUTTER_APP.flutterViewController
Jidong Chen's avatar
init  
Jidong Chen committed
34

余玠's avatar
余玠 committed
35
@interface FLBFlutterViewContainer  ()
Jidong Chen's avatar
init  
Jidong Chen committed
36 37
@property (nonatomic,strong,readwrite) NSDictionary *params;
@property (nonatomic,assign) long long identifier;
Leo's avatar
Leo committed
38 39
@property (nonatomic, copy) NSString *flbNibName;
@property (nonatomic, strong) NSBundle *flbNibBundle;
Jidong Chen's avatar
init  
Jidong Chen committed
40 41
@end

余玠's avatar
余玠 committed
42 43
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wincomplete-implementation"
44
@implementation FLBFlutterViewContainer
Jidong Chen's avatar
init  
Jidong Chen committed
45

Jidong Chen's avatar
Jidong Chen committed
46 47 48 49
- (instancetype)init
{
    [FLUTTER_APP.flutterProvider prepareEngineIfNeeded];
    if(self = [super initWithEngine:FLUTTER_APP.flutterProvider.engine
Leo's avatar
Leo committed
50 51
                            nibName:_flbNibName
                            bundle:_flbNibBundle]){
Jidong Chen's avatar
Jidong Chen committed
52 53 54 55 56
        [self _setup];
    }
    return self;
}

Leo's avatar
Leo committed
57 58
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-designated-initializers"
Jidong Chen's avatar
Jidong Chen committed
59 60 61 62 63 64 65
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder: aDecoder]) {
        NSAssert(NO, @"unsupported init method!");
        [self _setup];
    }
    return self;
}
Leo's avatar
Leo committed
66 67 68 69 70 71 72
#pragma pop

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    _flbNibName = nibNameOrNil;
    _flbNibBundle = nibBundleOrNil;
    return [self init];
}
Jidong Chen's avatar
Jidong Chen committed
73

Jidong Chen's avatar
init  
Jidong Chen committed
74 75 76 77 78
- (void)setName:(NSString *)name params:(NSDictionary *)params
{
    if(!_name && name){
        _name = name;
        _params = params;
Jidong Chen's avatar
Jidong Chen committed
79
        [BoostMessageChannel didInitPageContainer:^(NSNumber *r) {}
Jidong Chen's avatar
init  
Jidong Chen committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
                                               pageName:name
                                                 params:params
                                               uniqueId:[self uniqueIDString]];
    }
}

static NSUInteger kInstanceCounter = 0;

+ (NSUInteger)instanceCounter
{
    return kInstanceCounter;
}

+ (void)instanceCounterIncrease
{
    kInstanceCounter++;
    if(kInstanceCounter == 1){
Jidong Chen's avatar
Jidong Chen committed
97
        [FLUTTER_APP resume];
Jidong Chen's avatar
init  
Jidong Chen committed
98 99 100 101 102 103 104
    }
}

+ (void)instanceCounterDecrease
{
    kInstanceCounter--;
    if([self.class instanceCounter] == 0){
Jidong Chen's avatar
Jidong Chen committed
105
        [FLUTTER_APP pause];
Jidong Chen's avatar
init  
Jidong Chen committed
106 107 108 109 110 111 112 113
    }
}

- (NSString *)uniqueIDString
{
    return @(_identifier).stringValue;
}

114 115 116 117 118
- (void)_setup
{
    static long long sCounter = 0;
    _identifier = sCounter++;
    [self.class instanceCounterIncrease];
Jidong Chen's avatar
init  
Jidong Chen committed
119 120 121 122 123 124 125 126 127 128
}

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

- (void)notifyWillDealloc
{
Jidong Chen's avatar
Jidong Chen committed
129
    [BoostMessageChannel willDeallocPageContainer:^(NSNumber *r) {}
Jidong Chen's avatar
init  
Jidong Chen committed
130 131 132
                                               pageName:_name params:_params
                                               uniqueId:[self uniqueIDString]];

Jidong Chen's avatar
Jidong Chen committed
133
    [FLUTTER_APP removeViewController:self];
Jidong Chen's avatar
init  
Jidong Chen committed
134 135 136 137
    
    [self.class instanceCounterDecrease];
}

Jidong Chen's avatar
Jidong Chen committed
138 139 140 141
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
}
Jidong Chen's avatar
init  
Jidong Chen committed
142 143 144 145 146 147 148

#pragma mark - ScreenShots
- (BOOL)isFlutterViewAttatched
{
    return FLUTTER_VIEW.superview == self.view;
}

Jidong Chen's avatar
Jidong Chen committed
149
- (void)attatchFlutterEngine
Jidong Chen's avatar
init  
Jidong Chen committed
150
{
Jidong Chen's avatar
Jidong Chen committed
151 152
    [FLUTTER_APP.flutterProvider prepareEngineIfNeeded];
    [FLUTTER_APP.flutterProvider atacheToViewController:self];
Jidong Chen's avatar
init  
Jidong Chen committed
153 154
}

Jidong Chen's avatar
Jidong Chen committed
155
- (void)detatchFlutterEngine
Jidong Chen's avatar
init  
Jidong Chen committed
156
{
Jidong Chen's avatar
Jidong Chen committed
157
    [FLUTTER_APP.flutterProvider detach];
Jidong Chen's avatar
init  
Jidong Chen committed
158 159
}

160 161 162 163
- (void)setEnableForRunnersBatch:(BOOL)enable{
    //dummy function
    NSLog(@"[DEBUG]- I did nothing, I am innocent");
}
Jidong Chen's avatar
init  
Jidong Chen committed
164 165 166 167 168 169

#pragma mark - Life circle methods

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
Jidong Chen's avatar
Jidong Chen committed
170
    [FLUTTER_APP resume];
Jidong Chen's avatar
init  
Jidong Chen committed
171 172 173 174
}

- (void)viewWillAppear:(BOOL)animated
{
Jidong Chen's avatar
Jidong Chen committed
175
    if([FLUTTER_APP contains:self]){
176
        [self surfaceUpdated:NO];
Jidong Chen's avatar
Jidong Chen committed
177 178 179
        [self detatchFlutterEngine];
    }else{
        [self attatchFlutterEngine];
180
        [self surfaceUpdated:YES];
Jidong Chen's avatar
Jidong Chen committed
181
    }
Jidong Chen's avatar
Jidong Chen committed
182
  
Jidong Chen's avatar
Jidong Chen committed
183
    [FLUTTER_APP resume];
Jidong Chen's avatar
Jidong Chen committed
184
    
Jidong Chen's avatar
init  
Jidong Chen committed
185 186
    //For new page we should attach flutter view in view will appear
    //for better performance.
Jidong Chen's avatar
Jidong Chen committed
187
 
Jidong Chen's avatar
Jidong Chen committed
188
    [BoostMessageChannel willShowPageContainer:^(NSNumber *result) {}
Jidong Chen's avatar
init  
Jidong Chen committed
189 190 191 192
                                            pageName:_name
                                              params:_params
                                            uniqueId:self.uniqueIDString];
    //Save some first time page info.
193 194 195 196
    if(![FlutterBoostPlugin sharedInstance].fPagename){
        [FlutterBoostPlugin sharedInstance].fPagename = _name;
        [FlutterBoostPlugin sharedInstance].fPageId = self.uniqueIDString;
        [FlutterBoostPlugin sharedInstance].fParams = _params;
Jidong Chen's avatar
init  
Jidong Chen committed
197 198 199 200 201 202 203
    }
    
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
Jidong Chen's avatar
Jidong Chen committed
204
    [FLUTTER_APP addUniqueViewController:self];
Jidong Chen's avatar
init  
Jidong Chen committed
205
    
Jidong Chen's avatar
Jidong Chen committed
206 207
    //Ensure flutter view is attached.
    [self attatchFlutterEngine];
Jidong Chen's avatar
Jidong Chen committed
208 209
    [FLUTTER_APP resume];
 
Jidong Chen's avatar
Jidong Chen committed
210
    [BoostMessageChannel didShowPageContainer:^(NSNumber *result) {}
Jidong Chen's avatar
init  
Jidong Chen committed
211 212 213 214 215 216 217 218 219
                                           pageName:_name
                                             params:_params
                                           uniqueId:self.uniqueIDString];
    
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
Jidong Chen's avatar
Jidong Chen committed
220
    [BoostMessageChannel willDisappearPageContainer:^(NSNumber *result) {}
Jidong Chen's avatar
init  
Jidong Chen committed
221 222 223
                                                 pageName:_name
                                                   params:_params
                                                 uniqueId:self.uniqueIDString];
224
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
Jidong Chen's avatar
init  
Jidong Chen committed
225 226 227 228 229 230
    [super viewWillDisappear:animated];
}


- (void)viewDidDisappear:(BOOL)animated
{
Jidong Chen's avatar
Jidong Chen committed
231
    [FLUTTER_APP resume];
Jidong Chen's avatar
Jidong Chen committed
232
    [BoostMessageChannel didDisappearPageContainer:^(NSNumber *result) {}
Jidong Chen's avatar
init  
Jidong Chen committed
233 234 235
                                                pageName:_name
                                                  params:_params
                                                uniqueId:self.uniqueIDString];
236
    [super viewDidDisappear:animated];
余玠's avatar
余玠 committed
237
    [FLUTTER_APP resume];
238

Jidong Chen's avatar
init  
Jidong Chen committed
239 240
}

Jidong Chen's avatar
Jidong Chen committed
241 242 243 244 245 246 247 248 249
- (void)installSplashScreenViewIfNecessary {
    //Do nothing.
}

- (BOOL)loadDefaultSplashScreenView
{
    return NO;
}

Jidong Chen's avatar
init  
Jidong Chen committed
250 251

@end
余玠's avatar
余玠 committed
252
#pragma clang diagnostic pop