<commit_before>//
//  ReactViewController.swift
//  NativeNavigation
//
//  Created by <NAME> on 5/2/16.
//  Copyright © 2016 Airbnb. All rights reserved.
//

import React
import UIKit

// MARK: Public

@objc public enum ReactFlowResultCode: Int {
  // These must match the values in React Native and Android codebases.
  case cancelled = 0
  case ok = -1

  public static func isOk(_ value: Int?) -> Bool {
    if let value = value {
      return ReactFlowResultCode(rawValue: value) == .ok
    } else {
      return false
    }
  }
}

@objc public protocol ReactViewControllerDelegate: class {
  func didDismiss(_ viewController: ReactViewController, withPayload payload: [String: AnyObject])
}


// MARK: Private

let kNativeNavigationInstanceId = "nativeNavigationInstanceId"
let kNativeNavigationBarHeight = "nativeNavigationInitialBarHeight"
let kViewControllerId = "viewControllerId"
private var index = 0
let EMPTY_MAP = [String: AnyObject]()

func generateId(_ moduleName: String) -> String {
  index += 1
  return "\(moduleName)_\(index)"
}

func propsWithMetadata(
  _ props: [String: AnyObject],
  _ nativeNavigationInstanceId: String,
  _ barHeight: CGFloat
) -> [String: AnyObject] {
  // TODO(lmr): make this non mutative?
  var newProps = props
  newProps[kNativeNavigationInstanceId] = nativeNavigationInstanceId as AnyObject?
  newProps[kNativeNavigationBarHeight] = barHeight as AnyObject?
  return newProps
}

public struct WeakViewHolder {
  weak var view: UIView?
}

// MARK: Class Definition

open class ReactViewController: UIViewController {

  let nativeNavigationInstanceId: String
  var sharedElementsById: [String: WeakViewHolder] = [:]
  var sharedElementGroupsById: [String: WeakViewHolder] = [:]
  var isPendingNavigationTransition: Bool = false
  var isCurrentlyTransitioning: Bool = false
  var onTransitionCompleted: (() -> Void)?
  var onNavigationBarTypeUpdated: (() -> Void)?
  var reactViewHasBeenRendered: Bool = false
  var transition: ReactSharedElementTransition?
  var eagerNavigationController: UINavigationController?
  open var reactFlowId: String?
  open var showTabBar: Bool = false // TODO(lmr): showTabBar? is this needed?
  open weak var delegate: ReactViewControllerDelegate?
  var dismissResultCode: ReactFlowResultCode?
  var dismissPayload: [String: AnyObject]?
  fileprivate let moduleName: String
  fileprivate var props: [String: AnyObject]
  fileprivate let coordinator: ReactNavigationCoordinator = ReactNavigationCoordinator.sharedInstance
  fileprivate var initialConfig: [String: AnyObject]
  fileprivate var prevConfig: [String: AnyObject]
  fileprivate var renderedConfig: [String: AnyObject]
  fileprivate var reactView: UIView!
  fileprivate var statusBarAnimation: UIStatusBarAnimation = .fade
  fileprivate var statusBarHidden: Bool = false
  fileprivate var statusBarStyle: UIStatusBarStyle = UIStatusBarStyle.default
  fileprivate var statusBarIsDirty: Bool = false
  fileprivate var leadingButtonVisible: Bool = true
  private var barHeight: CGFloat

  // MARK: Lifecycle

  public convenience init(moduleName: String) {
    self.init(moduleName: moduleName, props: [:])
  }

  public init(moduleName: String, props: [String: AnyObject]) {
    self.nativeNavigationInstanceId = generateId(moduleName)
    self.moduleName = moduleName

    self.barHeight = -1;
    self.props = EMPTY_MAP

    self.initialConfig = EMPTY_MAP
    self.prevConfig = EMPTY_MAP
    self.renderedConfig = EMPTY_MAP

    super.init(nibName: nil, bundle: nil)

    if let initialConfig = coordinator.getScreenProperties(moduleName) {
      self.initialConfig = initialConfig
      self.renderedConfig = initialConfig
    }

    self.barHeight = coordinator.navigation.getBarHeight(
      viewController: self,
      navigationController: navigationController,
      config: renderedConfig
    )
    self.props = propsWithMetadata(props, nativeNavigationInstanceId, barHeight)<commit_msg>Add ObjC support for the ReactViewController initializer<commit_after>//
//  ReactViewController.swift
//  NativeNavigation
//
//  Created by <NAME> on 5/2/16.
//  Copyright © 2016 Airbnb. All rights reserved.
//

import React
import UIKit

// MARK: Public

@objc public enum ReactFlowResultCode: Int {
  // These must match the values in React Native and Android codebases.
  case cancelled = 0
  case ok = -1

  public static func isOk(_ value: Int?) -> Bool {
    if let value = value {
      return ReactFlowResultCode(rawValue: value) == .ok
    } else {
      return false
    }
  }
}

@objc public protocol ReactViewControllerDelegate: class {
  func didDismiss(_ viewController: ReactViewController, withPayload payload: [String: AnyObject])
}


// MARK: Private

let kNativeNavigationInstanceId = "nativeNavigationInstanceId"
let kNativeNavigationBarHeight = "nativeNavigationInitialBarHeight"
let kViewControllerId = "viewControllerId"
private var index = 0
let EMPTY_MAP = [String: AnyObject]()

func generateId(_ moduleName: String) -> String {
  index += 1
  return "\(moduleName)_\(index)"
}

func propsWithMetadata(
  _ props: [String: AnyObject],
  _ nativeNavigationInstanceId: String,
  _ barHeight: CGFloat
) -> [String: AnyObject] {
  // TODO(lmr): make this non mutative?
  var newProps = props
  newProps[kNativeNavigationInstanceId] = nativeNavigationInstanceId as AnyObject?
  newProps[kNativeNavigationBarHeight] = barHeight as AnyObject?
  return newProps
}

public struct WeakViewHolder {
  weak var view: UIView?
}

// MARK: Class Definition

open class ReactViewController: UIViewController {

  let nativeNavigationInstanceId: String
  var sharedElementsById: [String: WeakViewHolder] = [:]
  var sharedElementGroupsById: [String: WeakViewHolder] = [:]
  var isPendingNavigationTransition: Bool = false
  var isCurrentlyTransitioning: Bool = false
  var onTransitionCompleted: (() -> Void)?
  var onNavigationBarTypeUpdated: (() -> Void)?
  var reactViewHasBeenRendered: Bool = false
  var transition: ReactSharedElementTransition?
  var eagerNavigationController: UINavigationController?
  open var reactFlowId: String?
  open var showTabBar: Bool = false // TODO(lmr): showTabBar? is this needed?
  open weak var delegate: ReactViewControllerDelegate?
  var dismissResultCode: ReactFlowResultCode?
  var dismissPayload: [String: AnyObject]?
  fileprivate let moduleName: String
  fileprivate var props: [String: AnyObject]
  fileprivate let coordinator: ReactNavigationCoordinator = ReactNavigationCoordinator.sharedInstance
  fileprivate var initialConfig: [String: AnyObject]
  fileprivate var prevConfig: [String: AnyObject]
  fileprivate var renderedConfig: [String: AnyObject]
  fileprivate var reactView: UIView!
  fileprivate var statusBarAnimation: UIStatusBarAnimation = .fade
  fileprivate var statusBarHidden: Bool = false
  fileprivate var statusBarStyle: UIStatusBarStyle = UIStatusBarStyle.default
  fileprivate var statusBarIsDirty: Bool = false
  fileprivate var leadingButtonVisible: Bool = true
  private var barHeight: CGFloat

  // MARK: Lifecycle

  public convenience init(moduleName: String) {
    self.init(moduleName: moduleName, props: [:])
  }

  @objc public init(moduleName: String, props: [String: AnyObject]) {
    self.nativeNavigationInstanceId = generateId(moduleName)
    self.moduleName = moduleName

    self.barHeight = -1;
    self.props = EMPTY_MAP

    self.initialConfig = EMPTY_MAP
    self.prevConfig = EMPTY_MAP
    self.renderedConfig = EMPTY_MAP

    super.init(nibName: nil, bundle: nil)

    if let initialConfig = coordinator.getScreenProperties(moduleName) {
      self.initialConfig = initialConfig
      self.renderedConfig = initialConfig
    }

    self.barHeight = coordinator.navigation.getBarHeight(
      viewController: self,
      navigationController: navigationController,
      config: renderedConfig
    )
    self.props = propsWithMetadata(props, nativeNavigationInstanceId, barHeight)
{'fraction_non_alphanumeric': 0.08147025388404698, 'fraction_numerical': 0.003284072249589491, 'mean_word_length': 4.58392101551481, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2272721', 'n_tokens_mistral': 2295, 'n_tokens_neox': 2184, 'n_words': 648}