MainViewController.m 1.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.Copyright © 2017 The Chromium Authors. All rights reserved.


#import <Foundation/Foundation.h>

#import "MainViewController.h"
#import "NativeViewController.h"

@interface MainViewController ()

13 14
@property (nonatomic) NativeViewController* nativeViewController;
@property (nonatomic) FlutterViewController* flutterViewController;
15
@property (nonatomic) FlutterBasicMessageChannel* messageChannel;
16 17 18
@end

static NSString* const emptyString = @"";
19
static NSString* const ping = @"ping";
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
static NSString* const channel = @"increment";

@implementation MainViewController

- (NSString*) messageName {
  return channel;
}

- (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender {

  if ([segue.identifier isEqualToString: @"NativeViewControllerSegue"]) {
    self.nativeViewController = segue.destinationViewController;
    self.nativeViewController.delegate = self;
  }

  if ([segue.identifier isEqualToString:@"FlutterViewControllerSegue"]) {
36 37
    self.flutterViewController = segue.destinationViewController;

38 39 40
    self.messageChannel = [FlutterBasicMessageChannel messageChannelWithName:channel
                                                             binaryMessenger:self.flutterViewController
                                                                       codec:[FlutterStringCodec sharedInstance]];
41 42

    MainViewController*  __weak weakSelf = self;
43
    [self.messageChannel setMessageHandler:^(id message, FlutterReply reply) {
44
      [weakSelf.nativeViewController didReceiveIncrement];
45
      reply(emptyString);
46
    }];
47 48 49 50
  }
}

- (void)didTapIncrementButton {
51
  [self.messageChannel sendMessage:ping];
52 53 54
}

@end