Skip to content

Commit

Permalink
Add isDisplayingFlutterUI to FlutterViewController (#10816)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnfield authored Aug 14, 2019
1 parent d75af60 commit 90656d8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ FLUTTER_EXPORT
*/
- (id<FlutterPluginRegistry>)pluginRegistry;

/**
* True if at least one frame has rendered and the ViewController has appeared.
*
* This property is reset to false when the ViewController disappears. It is
* guaranteed to only alternate between true and false for observers.
*/
@property(nonatomic, readonly, getter=isDisplayingFlutterUI) BOOL displayingFlutterUI;

/**
* Specifies the view to use as a splash screen. Flutter's rendering is asynchronous, so the first
* frame rendered by the Flutter application might not immediately appear when theFlutter view is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
// change. Unfortunately unless you have Werror turned on, incompatible pointers as arguments are
// just a warning.
@interface FlutterViewController () <FlutterBinaryMessenger>
@property(nonatomic, readwrite, getter=isDisplayingFlutterUI) BOOL displayingFlutterUI;
@end

@implementation FlutterViewController {
Expand All @@ -49,6 +50,8 @@ @implementation FlutterViewController {
NSMutableSet<NSNumber*>* _ongoingTouches;
}

@synthesize displayingFlutterUI = _displayingFlutterUI;

#pragma mark - Manage and override all designated initializers

- (instancetype)initWithEngine:(FlutterEngine*)engine
Expand Down Expand Up @@ -263,7 +266,25 @@ - (void)installSplashScreenViewIfNecessary {
[self.view addSubview:splashScreenView];
}

+ (BOOL)automaticallyNotifiesObserversOfDisplayingFlutterUI {
return NO;
}

- (void)setDisplayingFlutterUI:(BOOL)displayingFlutterUI {
if (_displayingFlutterUI != displayingFlutterUI) {
if (displayingFlutterUI == YES) {
if (!self.isViewLoaded || !self.view.window) {
return;
}
}
[self willChangeValueForKey:@"displayingFlutterUI"];
_displayingFlutterUI = displayingFlutterUI;
[self didChangeValueForKey:@"displayingFlutterUI"];
}
}

- (void)callViewRenderedCallback {
self.displayingFlutterUI = YES;
if (_flutterViewRenderedCallback != nil) {
_flutterViewRenderedCallback.get()();
_flutterViewRenderedCallback.reset();
Expand Down Expand Up @@ -395,6 +416,7 @@ - (void)surfaceUpdated:(BOOL)appeared {
[_engine.get() platformViewsController] -> SetFlutterViewController(self);
[_engine.get() platformView] -> NotifyCreated();
} else {
self.displayingFlutterUI = NO;
[_engine.get() platformView] -> NotifyDestroyed();
[_engine.get() platformViewsController] -> SetFlutterView(nullptr);
[_engine.get() platformViewsController] -> SetFlutterViewController(nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,31 @@ - (void)tearDown {
}

- (void)testFirstFrameCallback {
XCTestExpectation* firstFrameRendered = [self expectationWithDescription:@"firstFrameRendered"];

FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
[engine runWithEntrypoint:nil];
self.flutterViewController = [[FlutterViewController alloc] initWithEngine:engine
nibName:nil
bundle:nil];
__block BOOL shouldKeepRunning = YES;

XCTAssertFalse(self.flutterViewController.isDisplayingFlutterUI);

XCTestExpectation* displayingFlutterUIExpectation =
[self keyValueObservingExpectationForObject:self.flutterViewController
keyPath:@"displayingFlutterUI"
expectedValue:@YES];
displayingFlutterUIExpectation.assertForOverFulfill = YES;

[self.flutterViewController setFlutterViewDidRenderCallback:^{
shouldKeepRunning = NO;
[firstFrameRendered fulfill];
}];

AppDelegate* appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate;
UIViewController* rootVC = appDelegate.window.rootViewController;
[rootVC presentViewController:self.flutterViewController animated:NO completion:nil];
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
int countDownMs = 2000;
while (shouldKeepRunning && countDownMs > 0) {
[runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
countDownMs -= 100;
}
XCTAssertGreaterThan(countDownMs, 0);

[self waitForExpectationsWithTimeout:30.0 handler:nil];
}

@end

0 comments on commit 90656d8

Please sign in to comment.