# ViewController

    class ViewController : UIViewController, TextInputViewControllerProtocol, OptionsViewControllerProtocol, ReaderDiscoveredCallback

    extension ViewController : UIPickerViewDelegate, UIPickerViewDataSource

    extension ViewController: ToolbarUIPickerViewDelegateProtocol

    extension ViewController: ABCCardTerminalMonitorProtocol

Main view controller; controls the main screen Also acts as delegate for the TextInputViewController and OptionsViewControllerProtocol

- `UIState`

  Enumerator holding the possible UI States

  See more

  #### Declaration

  Swift

      private enum UIState

- `abcTerminalFactory`

  Main instance of the AB Circle Terminal Factory.

  #### Declaration

  Swift

      private let abcTerminalFactory: ABCircleTerminalFactory

- `abcCardTerminalMonitor`

  Create an instance for Card Terminal monitor

  #### Declaration

  Swift

      private let abcCardTerminalMonitor: ABCCardTerminalMonitor

- `terminals`

  Global array of Card Terminals

  #### Declaration

  Swift

      private var terminals: [CardTerminal]

- `currentTerminal`

  Current selected Card Terminal

  #### Declaration

  Swift

      private weak var currentTerminal: CardTerminal?

- `card`

  Current active Card

  #### Declaration

  Swift

      private var card: Card?

- `monitorStarted`

  Flag indicating if the CardTerminal monitor has started or not

  #### Declaration

  Swift

      private var monitorStarted: Bool

- `connected`

  Flag indicating if connected to a Card or not

  #### Declaration

  Swift

      private var connected: Bool

- `options`

  Options storage

  #### Declaration

  Swift

      private let options: Options

- `timer`

  Timer (used for automatically updating the Card Terminal list)

  #### Declaration

  Swift

      private var timer: Timer?

- `terminalPicker`

  Custom Card Terminal UIPickerView with Toolbar

  #### Declaration

  Swift

      private let terminalPicker: ToolbarUIPickerView

- `customKeyButton`

  Control outlet for custom key UIButton

  #### Declaration

  Swift

      @IBOutlet
      weak var customKeyButton: UIButton!

- `labelTerminal`

  Control outlet for terminal UILabel

  #### Declaration

  Swift

      @IBOutlet
      weak var labelTerminal: UILabel!

- `selectedTerminal`

  Control outlet for terminal NoCaretUITextField

  #### Declaration

  Swift

      @IBOutlet
      weak var selectedTerminal: NoCaretUITextField!

- `buttonDiscover`

  Control outlet for Discover UIButton

  #### Declaration

  Swift

      @IBOutlet
      weak var buttonDiscover: UIButton!

- `buttonRefresh`

  Control outlet for Refresh UIButton

  #### Declaration

  Swift

      @IBOutlet
      weak var buttonRefresh: UIButton!

- `monitorSwitch`

  Control outlet for Monitor UISwitch

  #### Declaration

  Swift

      @IBOutlet
      weak var monitorSwitch: UISwitch!

- `labelProtocol`

  Control outlet for protocol UILabel

  #### Declaration

  Swift

      @IBOutlet
      weak var labelProtocol: UILabel!

- `protocolT0`

  Control outlet for protocol T=0 UISwitch

  #### Declaration

  Swift

      @IBOutlet
      weak var protocolT0: UISwitch!

- `labelT0`

  Control outlet for protocol T=0 label

  #### Declaration

  Swift

      @IBOutlet
      weak var labelT0: UILabel!

- `protocolT1`

  Control outlet for protocol T=1 UISwitch

  #### Declaration

  Swift

      @IBOutlet
      weak var protocolT1: UISwitch!

- `labelT1`

  Control outlet for protocol T=1 label

  #### Declaration

  Swift

      @IBOutlet
      weak var labelT1: UILabel!

- `labelMode`

  Control outlet for Mode UILabel

  #### Declaration

  Swift

      @IBOutlet
      weak var labelMode: UILabel!

- `modeSelector`

  Control outlet for mode selection UISegmentedControl

  #### Declaration

  Swift

      @IBOutlet
      weak var modeSelector: UISegmentedControl!

- `buttonConnect`

  Control outlet for connect UIButton

  #### Declaration

  Swift

      @IBOutlet
      weak var buttonConnect: UIButton!

- `viewLog`

  Control outlet for log UIView

  #### Declaration

  Swift

      @IBOutlet
      weak var viewLog: UIView!

- `scrollViewLog`

  Control outlet for log UIScrollView

  #### Declaration

  Swift

      @IBOutlet
      weak var scrollViewLog: UIScrollView!

- `textViewLog`

  Control outlet for log NoCaretUITextView

  #### Declaration

  Swift

      @IBOutlet
      weak var textViewLog: NoCaretUITextView!

- `apduLabel`

  Control outlet for apdu label UILabel

  #### Declaration

  Swift

      @IBOutlet
      weak var apduLabel: UILabel!

- `apduTextField`

  Control outlet for apdu NoCaretUITextField

  #### Declaration

  Swift

      @IBOutlet
      weak var apduTextField: NoCaretUITextField!

- `apduSendButton`

  Control outlet for audo send UIButton

  #### Declaration

  Swift

      @IBOutlet
      weak var apduSendButton: UIButton!

- `waitForChangeButton`

  Control outlet for wait for change UIButton

  #### Declaration

  Swift

      @IBOutlet
      weak var waitForChangeButton: UIButton!

- `escapeCommandLabel`

  Control outlet for Mode escape command UILabel

  #### Declaration

  Swift

      @IBOutlet
      weak var escapeCommandLabel: UILabel!

- `escapeCommandTextField`

  Control outlet for escape command NoCaretUITextField

  #### Declaration

  Swift

      @IBOutlet
      weak var escapeCommandTextField: NoCaretUITextField!

- `escapeCommandSendButton`

  Control outlet for escape command send UIButton

  #### Declaration

  Swift

      @IBOutlet
      weak var escapeCommandSendButton: UIButton!

- `viewDidLoad()`

  Called after the controller’s view is loaded into memory. Initialises the local variables, the controls and starts the terminal list updater

  #### Declaration

  Swift

      override func viewDidLoad()

- `closePopup(textViewContent:isApdu:)`

  Closes the APDU / Escape Command popup and retrieves its content

  #### Declaration

  Swift

      func closePopup(textViewContent: String, isApdu: Bool)

  #### Parameters

  `textViewContent` | Textview content from the text view on the popup
  `isApdu` | Flag indicating APDU popup or Escape Command popup

- `onShowOptionsView(_:)`

  Action handler for opening the Options view

  #### Declaration

  Swift

      @IBAction
      func onShowOptionsView(_ sender: UIButton)

  #### Parameters

  `sender` | sender (UIButton)

- `prepare(for:sender:)`

  Overrride function called when the Custom Key view is preparing (just before opening). Used here to set the initial values of the controls on the Custom Key view.

  #### Declaration

  Swift

      override func prepare(for segue: UIStoryboardSegue, sender: Any?)

  #### Parameters

  `segue` | Segue being handled
  `sender` | sender

- `onCloseOptionsWindow(options:)`

  Call back function handling the closing of the Options view. Used for either clearing or setting of the Custom Encryption Key used by the AB Circle Terminal Factory. Note: the custom key entered is always saved in the “customKey” class variable, so it can be shown and activated at any time (usage is determined by the “useDefaultKey” flag). Also used for setting the scanBluetooth name and RSSI filter values, CardMonitor values and disabling or enabling of the ACK protocol for terminals that support it (terminals with fw 109.00 or higher).

  #### Declaration

  Swift

      func onCloseOptionsWindow(options: Options)

  #### Parameters

  `options` | Options as set in the Options view.

- `showApduInputView()`

  Callback handler for showing the APDU input view

  #### Declaration

  Swift

      @objc
      private func showApduInputView()

- `showEscapeCommandInputView()`

  Callback handler for showing the Escape Command input view

  #### Declaration

  Swift

      @objc
      private func showEscapeCommandInputView()

- `onModeChanged(_:)`

  Callback handler for handling connection mode change. Enables or disables the protocol fields.

  #### Declaration

  Swift

      @IBAction
      func onModeChanged(_ sender: UISegmentedControl)

  #### Parameters

  `sender` | sender

- `onDiscoverReaders(_:)`

  Callback handler for handling the Discover button

  #### Declaration

  Swift

      @IBAction
      func onDiscoverReaders(_ sender: UIButton)

  #### Parameters

  `sender` | sender

- `onReaderDiscovered(reader:)`

  Callback handler, called when a reader is discovered (part of the ReaderDiscoveredCallback protocol)

  #### Declaration

  Swift

      func onReaderDiscovered(reader: ABCSmartCardIO.DiscoveredReader)

  #### Parameters

  `reader` | Discovered reader

- `onDisconnectTerminal(_:)`

  Callback handler for handling the Disconnect button Disconnect the current selected terminal.

  #### Declaration

  Swift

      @IBAction
      func onDisconnectTerminal(_ sender: UIButton)

  #### Parameters

  `sender` | sender

- `onGetBatteryLevel(_:)`

  Callback handler for handling the Battery button Retrieves and shows the battery level of the current selected terminal.

  #### Declaration

  Swift

      @IBAction
      func onGetBatteryLevel(_ sender: UIButton)

  #### Parameters

  `sender` | sender

- `onGetFirmwareVersion(_:)`

  Callback handler for handling the Version button Retrieves and shows the firmware version of the current selected terminal.

  #### Declaration

  Swift

      @IBAction
      func onGetFirmwareVersion(_ sender: UIButton)

  #### Parameters

  `sender` | sender

- `onRefresh(_:)`

  Callback handler for handling the Refresh button. Calls the ABC Terminal Factory scanBluetooth() function for scanning and finding AB Circle bluetooth readers.

  #### Declaration

  Swift

      @IBAction
      func onRefresh(_ sender: UIButton)

  #### Parameters

  `sender` | sender

- `onMonitorChanged(_:)`

  Enables or disables the CardTerminal monitor

  #### Declaration

  Swift

      @IBAction
      func onMonitorChanged(_ sender: UISwitch)

  #### Parameters

  `sender` | UISwitch

- `startMonitor()`

  Starts the CardTerminal monitor

  #### Declaration

  Swift

      private func startMonitor()

- `stopMonitor()`

  Stops the CardTerminal monitor

  #### Declaration

  Swift

      private func stopMonitor()

- `startReaderListUpdater()`

  Starts the card terminal list updater, using a timer. Checks for changes in the card terminal list every second and updates the list if needed.

  #### Declaration

  Swift

      private func startReaderListUpdater()

- `listReaders()`

  Callback handler for updating the list of Card Terminals. Gets the latest list from the AB Circle Terminal Factory by calling “terminals.list()” and checks if there are any changes. If there are changes, rebuilds the terminal picker and resets the terminal TextInput box if needed.

  #### Declaration

  Swift

      @objc
      private func listReaders()

- `onProtocolT0ValueChanged(_:)`

  Callback function handling the Protocol T=0 switch. Enables or disables the Connect button.

  #### Declaration

  Swift

      @IBAction
      func onProtocolT0ValueChanged(_ sender: UISwitch)

  #### Parameters

  `sender` | sender

- `onProtocolT1ValueChanged(_:)`

  Callback function handling the Protocol T=1 switch. Enables or disables the Connect button.

  #### Declaration

  Swift

      @IBAction
      func onProtocolT1ValueChanged(_ sender: UISwitch)

  #### Parameters

  `sender` | sender

- `onConnect(_:)`

  Callback function handling the Connect button. If connected, tries to connect; else disconnects.

  #### Declaration

  Swift

      @IBAction
      func onConnect(_ sender: Any)

  #### Parameters

  `sender` | sender

- `onSendAPDU(_:)`

  Callback handler handling the “Send APDU” button. Sends an APDU command to a connected card (if any)

  #### Declaration

  Swift

      @IBAction
      func onSendAPDU(_ sender: UIButton)

  #### Parameters

  `sender` | sender

- `onSendEscapeCommand(_:)`

  Callback handler handling the “Send Escape Command” button. Sends an Escape Command to a connected card (if any)

  #### Declaration

  Swift

      @IBAction
      func onSendEscapeCommand(_ sender: UIButton)

  #### Parameters

  `sender` | sender

- `onWaitForChangeClicked(_:)`

  Callback function for handling the “Wait for Change” button. Calls the AB Circle Terminal Factory “waitForChange” function to wait for a card change action on the current selected card terminal. If a change occured, will show the change detected by calling “terminals().list()” function twice with state filters for card insertion and card removal.

  #### Declaration

  Swift

      @IBAction
      func onWaitForChangeClicked(_ sender: UIButton)

  #### Parameters

  `sender` | sender

- `onClear(_:)`

  Clears the log

  #### Declaration

  Swift

      @IBAction
      func onClear(_ sender: UIButton)

  #### Parameters

  `sender` | sender

- `initialiseTextView(textView:)`

  Initialises a text view control with empty text and a default border.

  #### Declaration

  Swift

      private func initialiseTextView(textView: UITextView)

  #### Parameters

  `textView` | Textview control to initialise

- `selectCurrentTerminal(newTerminal:)`

  (Re-)Selects the current card terminal

  #### Declaration

  Swift

      private func selectCurrentTerminal(newTerminal: CardTerminal)

  #### Parameters

  `newTerminal` | New terminal selected on the UI

- `terminalsChanged(terminalList1:terminalList2:)`

  Compares 2 CardTerminals array lists and determines if there are changes

  #### Declaration

  Swift

      private func terminalsChanged(terminalList1: [CardTerminal], terminalList2: [CardTerminal]) -> Bool

  #### Parameters

  `terminalList1` | CardTerminal list 1
  `terminalList2` | CardTerminal list 2

  #### Return Value

  True if changes were found, else false

- `sendAPDUThroughBasicChannel(apdu:)`

  Sends an APDU through a card’s basic channel and receives the response.

  #### Declaration

  Swift

      private func sendAPDUThroughBasicChannel(apdu: String)

  #### Parameters

  `apdu` | APDU in hexadecimal string form

- `getBasicChannel()`

  Gets the basic channel of a currently connected card

  #### Declaration

  Swift

      private func getBasicChannel() -> CardChannel?

  #### Return Value

  A CardChannel object on success, else nil

- `getCommandAPDU(apduString:)`

  Creates a CommandAPDU from the entered APDU hexadecimal string

  #### Declaration

  Swift

      private func getCommandAPDU(apduString: String) -> CommandAPDU?

  #### Parameters

  `apduString` | APDU in hexadecimal string form

  #### Return Value

  A CommandAPDU object on success, else nil

- `sendEscapeCommand(escapeCommand:)`

  Sends an Escape command to a connected card

  #### Declaration

  Swift

      private func sendEscapeCommand(escapeCommand: String)

  #### Parameters

  `escapeCommand` | Escape command in hexadecimal string form

- `connect()`

  Connects to a card in the selected terminal

  #### Declaration

  Swift

      private func connect()

- `disconnect()`

  Disconnects from a card (if connected)

  #### Declaration

  Swift

      private func disconnect()

- `selectedModeIsDirect()`

  Indicates if Direct Mode is selected

  #### Declaration

  Swift

      private func selectedModeIsDirect() -> Bool

  #### Return Value

  True if Direct Mode is selected, else false

- `enableUIFields(state:)`

  Enables groups of UI fields depending on the entered state

  #### Declaration

  Swift

      private func enableUIFields(state: UIState)

  #### Parameters

  `state` | UI state (see enum UIState in this class for possible values)

- `enableMainFields(enabled:)`

  Enables or disables the main UI fields

  #### Declaration

  Swift

      private func enableMainFields(enabled: Bool)

  #### Parameters

  `enabled` | True to set the fields as enabled, false to set the fields as disabled

- `enableCommandFields(enabled:)`

  Enables or disables the command related UI fields

  #### Declaration

  Swift

      private func enableCommandFields(enabled: Bool)

  #### Parameters

  `enabled` | True to set the fields as enabled, false to set the fields as disabled

- `enableConnectionFields(enabled:excludeConnect:)`

  Enables or disables the connection related UI fields

  #### Declaration

  Swift

      private func enableConnectionFields(enabled: Bool, excludeConnect: Bool = false)

  #### Parameters

  `enabled` | True to set the fields as enabled, false to set the fields as disabled

- `enableProtocolFields(enabled:)`

  Enables or disables the protocol selection related UI fields

  #### Declaration

  Swift

      private func enableProtocolFields(enabled: Bool)

  #### Parameters

  `enabled` | True to set the fields as enabled, false to set the fields as disabled

- `outputTerminalsToLog()`

  Output the terminal list to the log ui field

  #### Declaration

  Swift

      private func outputTerminalsToLog()

- `addLogLine(format:arguments:)`

  Add a formatted line to the log ui field

  #### Declaration

  Swift

      private func addLogLine(format: String, arguments: CVarArg...)

  #### Parameters

  `format` | Line format to use
  `arguments` | Arguments for the format

- `addLogLine(line:)`

  Adds a line to the log ui filed

  #### Declaration

  Swift

      private func addLogLine(line: String)

  #### Parameters

  `line` | Line to add

- `scrollLogToEnd()`

  Scrolls the log ui field contents to the end

  #### Declaration

  Swift

      private func scrollLogToEnd()

- `numberOfComponents(in:)`

  Gets the number of components for the picker view. Here always 1, since we only use 1 component.

  #### Declaration

  Swift

      func numberOfComponents(in pickerView: UIPickerView) -> Int

  #### Parameters

  `pickerView` | UIPickerView to handle

  #### Return Value

  Int holding the number of components

- `pickerView(_:numberOfRowsInComponent:)`

  Returns the number of rows for a component. Here returns the number of rows in the main CardTerminal list (as that is the data source for the UIPickerView)

  #### Declaration

  Swift

      func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int

  #### Parameters

  `pickerView` | An object representing the picker view requesting the data.
  `component` | A zero-indexed number identifying a component of pickerView. Components are numbered left-to-right.

  #### Return Value

  Int holding the number of rows for the specified component

- `pickerView(_:titleForRow:forComponent:)`

  Called by the picker view when it needs the title to use for a given row in a given component. Here returns the name of the terminal from the terminal list associated with the row as datasource

  #### Declaration

  Swift

      func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?

  #### Parameters

  `pickerView` | An object representing the picker view requesting the data.
  `row` | A zero-indexed number identifying a row of component. Rows are numbered top-to-bottom.
  `component` | A zero-indexed number identifying a component of pickerView. Components are numbered left-to-right.

  #### Return Value

  The string to use as the title of the indicated component row.

- `didTapDone()`

  User tapped ‘Done’ button. Here selects the current terminal and closes the UIPickerView.

  #### Declaration

  Swift

      func didTapDone()

- `didTapCancel()`

  User tapped ‘Cancel’ button. Closes the UIPickerView.

  #### Declaration

  Swift

      func didTapCancel()

- `onBatteryThresholdReached(terminalName:batteryThreshold:)`

  Call back function definition to implement by the delegate. Called once when a terminal has hit the battery threshold

  #### Declaration

  Swift

      func onBatteryThresholdReached(terminalName: String, batteryThreshold: UInt8)

  #### Parameters

  `terminalName` | Name of card terminal hitting the threshold
  `batteryThreshold` | Battery threshold reached

- `onDisconnected(terminalName:)`

  Call back function definition to implement by the delegate. Called once when a terminal has disconnected

  #### Declaration

  Swift

      func onDisconnected(terminalName: String)

  #### Parameters

  `terminalName` | Name of card terminal that has disconnected

Copyright (c) 2018-2024, AB Circle Ltd. All rights reserved.

Generated by jazzy ♪♫ v0.14.3, a Realm project.