Better functioning selection.

This commit is contained in:
Kevin G Christiano 2019-11-22 09:20:35 -05:00
parent 64bda3cb1d
commit 21166bc5f0

View File

@ -22,7 +22,7 @@ import UIKit
private var switchFieldsAutomatically = false private var switchFieldsAutomatically = false
public var digitBoxes: [DigitBox] = [] public var digitBoxes: [DigitBox] = []
var selectedDigitField: DigitBox? var selectedDigitBox: DigitBox?
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Computed Properties // MARK: - Computed Properties
@ -273,10 +273,8 @@ import UIKit
var selectPreviousField = false var selectPreviousField = false
for (index, field) in Array(digitBoxes.reversed()).enumerated() { for (index, box) in Array(digitBoxes.reversed()).enumerated() {
if box.digitField == currentTextField {
if field.digitField == currentTextField {
field.isSelected = false
if index == digitBoxes.count - 1 { if index == digitBoxes.count - 1 {
return return
} else { } else {
@ -284,12 +282,11 @@ import UIKit
} }
} else if selectPreviousField { } else if selectPreviousField {
if !clear { if !clear {
self.switchFieldsAutomatically = true switchFieldsAutomatically = true
} }
field.digitField.becomeFirstResponder() box.digitField.becomeFirstResponder()
self.switchFieldsAutomatically = false switchFieldsAutomatically = false
field.isSelected = true UIAccessibility.post(notification: .layoutChanged, argument: box.digitField)
UIAccessibility.post(notification: .layoutChanged, argument: field.digitField)
return return
} }
} }
@ -299,24 +296,21 @@ import UIKit
var selectNextField = false var selectNextField = false
for (index, field) in digitBoxes.enumerated() { for (index, box) in digitBoxes.enumerated() {
if box.digitField == currentTextField {
if field.digitField == currentTextField {
if index == digitBoxes.count - 1 { if index == digitBoxes.count - 1 {
return return
} else { } else {
selectNextField = true selectNextField = true
field.isSelected = false
} }
} else if selectNextField { } else if selectNextField {
if !clear { if !clear {
self.switchFieldsAutomatically = true switchFieldsAutomatically = true
} }
field.digitField.becomeFirstResponder() box.digitField.becomeFirstResponder()
self.switchFieldsAutomatically = false switchFieldsAutomatically = false
field.isSelected = true UIAccessibility.post(notification: .layoutChanged, argument: box.digitField)
UIAccessibility.post(notification: .layoutChanged, argument: field.digitField)
return return
} }
} }
@ -324,14 +318,14 @@ import UIKit
@objc override func startEditing() { @objc override func startEditing() {
selectedDigitField?.isSelected = true selectedDigitBox?.isSelected = true
selectedDigitField?.digitField.becomeFirstResponder() selectedDigitBox?.digitField.becomeFirstResponder()
} }
@objc override open func resignFirstResponder() -> Bool { @objc override open func resignFirstResponder() -> Bool {
selectedDigitField?.digitField.resignFirstResponder() selectedDigitBox?.digitField.resignFirstResponder()
selectedDigitField?.isSelected = false selectedDigitBox?.isSelected = false
return true return true
} }
@ -407,17 +401,12 @@ extension DigitEntryField {
textField.text = "" textField.text = ""
valueChanged() valueChanged()
} }
for digitBox in digitBoxes { digitBoxes.forEach {
if $0.digitField == textField {
if digitBox.isSelected { selectedDigitBox = $0
digitBox.isSelected = false $0.isSelected = true
} return
if digitBox.digitField == textField {
selectedDigitField = digitBox
digitBox.isSelected = true
break
} }
} }
@ -426,9 +415,10 @@ extension DigitEntryField {
@objc public func textFieldDidEndEditing(_ textField: UITextField) { @objc public func textFieldDidEndEditing(_ textField: UITextField) {
digitBoxes.forEach { box in digitBoxes.forEach {
if box.digitField == textField { if $0.digitField == textField {
box.isSelected = false selectedDigitBox = nil
$0.isSelected = false
return return
} }
} }