HybridViewController.m 2.69 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 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <Flutter/Flutter.h>

#import "AppDelegate.h"
#import "HybridViewController.h"

@interface HybridViewController ()

@end

static NSString *_kChannel = @"increment";
static NSString *_kPing = @"ping";

@implementation HybridViewController {
  FlutterBasicMessageChannel *_messageChannel;
}

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

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

- (void)viewDidLoad {
  [super viewDidLoad];
  self.title = @"Hybrid Flutter/Native";
  UIStackView *stackView = [[UIStackView alloc] initWithFrame:self.view.frame];
  stackView.axis = UILayoutConstraintAxisVertical;
  stackView.distribution = UIStackViewDistributionFillEqually;
  stackView.layoutMargins = UIEdgeInsetsMake(0, 0, 50, 0);
  stackView.layoutMarginsRelativeArrangement = YES;
  [self.view addSubview:stackView];
39 40 41 42 43
  self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
                                              initWithTitle:@"Back"
                                                      style:UIBarButtonItemStylePlain
                                                     target:nil
                                                     action:nil];
Dan Field's avatar
Dan Field committed
44 45 46 47 48 49 50

  NativeViewController *nativeViewController =
      [[NativeViewController alloc] initWithDelegate:self];
  [self addChildViewController:nativeViewController];
  [stackView addArrangedSubview:nativeViewController.view];
  [nativeViewController didMoveToParentViewController:self];

51
  _flutterViewController =
Dan Field's avatar
Dan Field committed
52 53 54 55 56 57 58
      [[FlutterViewController alloc] initWithEngine:[self engine]
                                            nibName:nil
                                             bundle:nil];
  [[self reloadMessageChannel] sendMessage:@"hybrid"];

  _messageChannel = [[FlutterBasicMessageChannel alloc]
         initWithName:_kChannel
59
      binaryMessenger:_flutterViewController.binaryMessenger
Dan Field's avatar
Dan Field committed
60
                codec:[FlutterStringCodec sharedInstance]];
61 62 63
  [self addChildViewController:_flutterViewController];
  [stackView addArrangedSubview:_flutterViewController.view];
  [_flutterViewController didMoveToParentViewController:self];
Dan Field's avatar
Dan Field committed
64 65 66 67 68 69 70 71 72 73 74 75 76

  __weak NativeViewController *weakNativeViewController = nativeViewController;
  [_messageChannel setMessageHandler:^(id message, FlutterReply reply) {
    [weakNativeViewController didReceiveIncrement];
    reply(@"");
  }];
}

- (void)didTapIncrementButton {
  [_messageChannel sendMessage:_kPing];
}

@end