NativeViewController.m 5.06 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
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "NativeViewController.h"

@interface NativeViewController ()

@end

@implementation NativeViewController {
  int _counter;
  UILabel* _incrementLabel;
}

- (instancetype)initWithDelegate:(id<NativeViewControllerDelegate>)delegate {
  self = [super initWithNibName:nil bundle:nil];
  if (self) {
    self.delegate = delegate;
  }
  return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
  return [self initWithDelegate:nil];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
   return [self initWithDelegate:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"Native iOS View";
    self.view.backgroundColor = UIColor.lightGrayColor;
37 38 39 40 41
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
                                                initWithTitle:@"Back"
                                                        style:UIBarButtonItemStylePlain
                                                       target:nil
                                                       action:nil];
Dan Field's avatar
Dan Field committed
42 43 44 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

    _incrementLabel = [self addIncrementLabel];
    UIStackView* footer = [self addFooter];

    _incrementLabel.translatesAutoresizingMaskIntoConstraints = false;
    footer.translatesAutoresizingMaskIntoConstraints = false;
    UILayoutGuide* marginsGuide = self.view.layoutMarginsGuide;
    NSMutableArray* array = [[NSMutableArray alloc] init];
    [array addObject:[_incrementLabel.centerXAnchor
                         constraintEqualToAnchor:self.view.centerXAnchor]];
    [array addObject:[_incrementLabel.centerYAnchor
                         constraintEqualToAnchor:self.view.centerYAnchor]];
    [array addObject:[footer.centerXAnchor
                         constraintEqualToAnchor:self.view.centerXAnchor]];
    [array addObject:[footer.widthAnchor
                         constraintEqualToAnchor:marginsGuide.widthAnchor]];
    [array addObject:[footer.bottomAnchor
                         constraintEqualToAnchor:marginsGuide.bottomAnchor]];

    [NSLayoutConstraint activateConstraints:array];
    [self updateIncrementLabel];
}

/// Adds a label to the view that will contain the counter text.
///
/// - Returns: The new label.
-(UILabel*) addIncrementLabel {
  UILabel* incrementLabel = [[UILabel alloc] init];
  incrementLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
  incrementLabel.textColor = UIColor.blackColor;
  incrementLabel.accessibilityIdentifier = @"counter_on_iOS";
  [self.view addSubview:incrementLabel];
  return incrementLabel;
}

/// Adds a horizontal stack to the view, anchored to the bottom.
///
/// - Returns: The new stack.
-(UIStackView*) addFooter {
  UILabel* mainLabel = [self createMainLabel];
  UIButton* incrementButton = [self createIncrementButton];
  UIStackView* stackView = [[UIStackView alloc] initWithFrame:self.view.frame];
  [stackView addArrangedSubview:mainLabel];
  [stackView addArrangedSubview:incrementButton];
  stackView.axis = UILayoutConstraintAxisHorizontal;
  stackView.alignment = UIStackViewAlignmentBottom;
  [self.view addSubview:stackView];
  return stackView;
}

/// Creates a label identifying this view.  Does not add it to the view.
///
/// - Returns: The new label.
-(UILabel*) createMainLabel {
  UILabel* mainLabel = [[UILabel alloc] init];
  mainLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleTitle1];
  mainLabel.textColor = UIColor.blackColor;
  mainLabel.text = @"Native";
  return mainLabel;
}

/// Creates a button that will increment a counter.  Does not add it to the view.
///
/// - Returns: The new button.
-(UIButton*) createIncrementButton {
  UIButton *incrementButton = [UIButton buttonWithType:UIButtonTypeSystem];
  incrementButton.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleTitle2];
  [incrementButton setTitle:@"Add" forState:UIControlStateNormal];
  incrementButton.accessibilityLabel = @"Increment via iOS";
  incrementButton.layer.cornerRadius = 15.0;
  [incrementButton addTarget:self
                      action:@selector(handleIncrement:)
            forControlEvents:UIControlEventTouchUpInside];
  return incrementButton;
}

// MARK: - Actions

/// Action triggered from tapping on the increment button.  Triggers a corresponding event in the
/// delegate if one is available, otherwise increments our internal counter.
-(void) handleIncrement:(UIButton*)sender {
  if (self.delegate) {
    [self.delegate didTapIncrementButton];
  } else {
    [self didReceiveIncrement];
  }
}

/// Updates the increment label text to match the increment counter.
-(void) updateIncrementLabel {
  _incrementLabel.text = [NSString
      stringWithFormat:@"%@ tapped %d %@.",
                       self.delegate == nil ? @"Button" : @"Flutter button",
                       _counter, _counter == 1 ? @"time" : @"times"];
}

/// Increments our internal counter and updates the view.
-(void) didReceiveIncrement {
  _counter += 1;
  [self updateIncrementLabel];
}

@end