MainViewController.m 5.79 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Dan Field's avatar
Dan Field committed
2 3 4 5 6 7 8 9 10 11 12 13 14
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "MainViewController.h"

#import "AppDelegate.h"
#import "DualFlutterViewController.h"
#import "FullScreenViewController.h"
#import "HybridViewController.h"
#import "NativeViewController.h"

@interface MainViewController ()

15 16
@property (weak, nonatomic) UIButton* flutterViewWarmButton;

Dan Field's avatar
Dan Field committed
17 18 19 20 21 22 23 24 25 26 27
@end


@implementation MainViewController {
  UIStackView *_stackView;
}

- (void)viewDidLoad {
  [super viewDidLoad];

  [self.view setFrame:self.view.window.bounds];
28
  self.title = @"Flutter iOS Demos Home";
Dan Field's avatar
Dan Field committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  self.view.backgroundColor = UIColor.whiteColor;

  _stackView = [[UIStackView alloc] initWithFrame:self.view.frame];
  _stackView.axis = UILayoutConstraintAxisVertical;
  _stackView.distribution = UIStackViewDistributionEqualSpacing;
  _stackView.alignment = UIStackViewAlignmentCenter;
  _stackView.autoresizingMask =
      UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  _stackView.layoutMargins = UIEdgeInsetsMake(0, 0, 50, 0);
  _stackView.layoutMarginsRelativeArrangement = YES;
  [self.view addSubview:_stackView];

  [self addButton:@"Native iOS View" action:@selector(showNative)];
  [self addButton:@"Full Screen (Cold)" action:@selector(showFullScreenCold)];
  [self addButton:@"Full Screen (Warm)" action:@selector(showFullScreenWarm)];
44
  self.flutterViewWarmButton = [self addButton:@"Flutter View (Warm)" action:@selector(showFlutterViewWarm)];
Dan Field's avatar
Dan Field committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  [self addButton:@"Hybrid View (Warm)" action:@selector(showHybridView)];
  [self addButton:@"Dual Flutter View (Cold)" action:@selector(showDualView)];
}

- (FlutterEngine *)engine {
  return [(AppDelegate *)[[UIApplication sharedApplication] delegate] engine];
}

- (FlutterBasicMessageChannel*)reloadMessageChannel {
  return [(AppDelegate *)[[UIApplication sharedApplication] delegate] reloadMessageChannel];
}

- (void)showDualView {
  DualFlutterViewController *dualViewController =
      [[DualFlutterViewController alloc] init];
  [self.navigationController pushViewController:dualViewController
                                       animated:YES];
}

- (void)showHybridView {
  HybridViewController *hybridViewController =
      [[HybridViewController alloc] init];
  [self.navigationController pushViewController:hybridViewController
                                       animated:YES];
}
- (void)showNative {
  NativeViewController *nativeViewController =
      [[NativeViewController alloc] init];
  [self.navigationController pushViewController:nativeViewController
                                       animated:YES];
}

- (void)showFullScreenCold {
  FullScreenViewController *flutterViewController =
      [[FullScreenViewController alloc] init];
  [flutterViewController setInitialRoute:@"full"];
  [[self reloadMessageChannel] sendMessage:@"full"];
  [self.navigationController
      pushViewController:flutterViewController
                animated:NO]; // Animating this is janky because of
                              // transitions with header on the native side.
                              // It's especially bad with a cold engine.
}

- (void)showFullScreenWarm {
  [[self engine].navigationChannel invokeMethod:@"setInitialRoute"
                                      arguments:@"full"];
  [[self reloadMessageChannel] sendMessage:@"full"];

  FullScreenViewController *flutterViewController =
      [[FullScreenViewController alloc] initWithEngine:[self engine]
                                               nibName:nil
                                                bundle:nil];
  [self.navigationController
      pushViewController:flutterViewController
                animated:NO]; // Animating this is problematic.
}

- (void)showFlutterViewWarm {
104 105 106 107 108 109 110 111
  self.flutterViewWarmButton.backgroundColor = UIColor.redColor;
  FlutterEngine *engine = [self engine];
  FlutterBasicMessageChannel* messageChannel = [self reloadMessageChannel];
  NSAssert(engine != nil, @"Engine is not nil.");
  NSAssert(engine.navigationChannel != nil, @"Engine.navigationChannel is not nil.");
  NSAssert(messageChannel != nil, @"messageChannel is not nil.");

  [engine.navigationChannel invokeMethod:@"setInitialRoute"
Dan Field's avatar
Dan Field committed
112
                                      arguments:@"/"];
113
  [messageChannel sendMessage:@"/"];
Dan Field's avatar
Dan Field committed
114 115 116 117 118 119


  FlutterViewController *flutterViewController =
      [[FlutterViewController alloc] initWithEngine:[self engine]
                                            nibName:nil
                                             bundle:nil];
120 121
  flutterViewController.view.accessibilityLabel = @"flutter view";
  NSAssert(self.navigationController != nil, @"self.navigationController is not nil.");
Dan Field's avatar
Dan Field committed
122
  [self.navigationController pushViewController:flutterViewController
123 124 125 126 127 128 129 130 131 132 133 134
                                       animated:NO];

  if (self.navigationController.topViewController != flutterViewController) {
    // For debugging:
    // Some unknown issue happened caused `flutterViewController` not being pushed.
    // We try to push an basic UIViewController to see if it would work.
    UIViewController *viewController = [[UIViewController alloc] init];
    viewController.view.backgroundColor = UIColor.blueColor;
    [self.navigationController pushViewController:viewController
                                         animated:NO];
    NSAssert(self.navigationController.topViewController == viewController, @"self.navigationController.topViewController should be the basic view controller");
  }
Dan Field's avatar
Dan Field committed
135 136
}

137
- (UIButton *)addButton:(NSString *)title action:(SEL)action {
Dan Field's avatar
Dan Field committed
138 139 140 141 142 143
  UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
  [button setTitle:title forState:UIControlStateNormal];
  [button addTarget:self
                action:action
      forControlEvents:UIControlEventTouchUpInside];
  [_stackView addArrangedSubview:button];
144
  return button;
Dan Field's avatar
Dan Field committed
145 146 147
}

@end