diff --git a/Sources/ProcessOut/Sources/UI/Modules/NativeAlternativePaymentMethod/View/Cells/NativeAlternativePaymentMethodCell.swift b/Sources/ProcessOut/Sources/UI/Modules/NativeAlternativePaymentMethod/View/Cells/NativeAlternativePaymentMethodCell.swift index 792bfd50d..38c69e6e3 100644 --- a/Sources/ProcessOut/Sources/UI/Modules/NativeAlternativePaymentMethod/View/Cells/NativeAlternativePaymentMethodCell.swift +++ b/Sources/ProcessOut/Sources/UI/Modules/NativeAlternativePaymentMethod/View/Cells/NativeAlternativePaymentMethodCell.swift @@ -9,6 +9,12 @@ import UIKit protocol NativeAlternativePaymentMethodCell: UICollectionViewCell { + /// Tells the cell that it is about to be displayed. + func willDisplay() + + /// Tells the cell that it was removed from the collection view. + func didEndDisplaying() + /// Should return input responder if any. var inputResponder: UIResponder? { get } diff --git a/Sources/ProcessOut/Sources/UI/Modules/NativeAlternativePaymentMethod/View/Cells/NativeAlternativePaymentMethodCodeInputCell.swift b/Sources/ProcessOut/Sources/UI/Modules/NativeAlternativePaymentMethod/View/Cells/NativeAlternativePaymentMethodCodeInputCell.swift index 4da445e02..a7e7d64ed 100644 --- a/Sources/ProcessOut/Sources/UI/Modules/NativeAlternativePaymentMethod/View/Cells/NativeAlternativePaymentMethodCodeInputCell.swift +++ b/Sources/ProcessOut/Sources/UI/Modules/NativeAlternativePaymentMethod/View/Cells/NativeAlternativePaymentMethodCodeInputCell.swift @@ -24,12 +24,31 @@ final class NativeAlternativePaymentMethodCodeInputCell: UICollectionViewCell, N codeTextField.keyboardType = .numberPad codeTextField.textContentType = .oneTimeCode codeTextFieldCenterXConstraint.isActive = item.isCentered - observeChanges(item: item, style: style) self.item = item + self.style = style } // MARK: - NativeAlternativePaymentMethodCell + func willDisplay() { + guard let item, let style else { + return + } + let isInvalidObserver = item.value.$isInvalid.addObserver { [weak self] isInvalid in + self?.codeTextField.configure(isInvalid: isInvalid, style: style, animated: true) + } + let valueObserver = item.value.$text.addObserver { [weak self] updatedValue in + if self?.codeTextField.text != updatedValue { + self?.codeTextField.text = item.value.text + } + } + self.observations = [isInvalidObserver, valueObserver] + } + + func didEndDisplaying() { + observations = [] + } + var inputResponder: UIResponder? { codeTextField } @@ -50,6 +69,7 @@ final class NativeAlternativePaymentMethodCodeInputCell: UICollectionViewCell, N // swiftlint:enable implicitly_unwrapped_optional private var item: NativeAlternativePaymentMethodViewModelState.CodeInputItem? + private var style: POInputStyle? private var observations: [AnyObject] = [] // MARK: - Private Methods @@ -79,18 +99,6 @@ final class NativeAlternativePaymentMethodCodeInputCell: UICollectionViewCell, N private func textFieldEditingChanged() { item?.value.text = codeTextField.text ?? "" } - - private func observeChanges(item: NativeAlternativePaymentMethodViewModelState.CodeInputItem, style: POInputStyle) { - let isInvalidObserver = item.value.$isInvalid.addObserver { [weak self] isInvalid in - self?.codeTextField.configure(isInvalid: isInvalid, style: style, animated: true) - } - let valueObserver = item.value.$text.addObserver { [weak self] updatedValue in - if self?.codeTextField.text != updatedValue { - self?.codeTextField.text = item.value.text - } - } - self.observations = [isInvalidObserver, valueObserver] - } } extension NativeAlternativePaymentMethodCodeInputCell: CodeTextFieldDelegate { diff --git a/Sources/ProcessOut/Sources/UI/Modules/NativeAlternativePaymentMethod/View/Cells/NativeAlternativePaymentMethodInputCell.swift b/Sources/ProcessOut/Sources/UI/Modules/NativeAlternativePaymentMethod/View/Cells/NativeAlternativePaymentMethodInputCell.swift index 3e0a187d8..9af30930b 100644 --- a/Sources/ProcessOut/Sources/UI/Modules/NativeAlternativePaymentMethod/View/Cells/NativeAlternativePaymentMethodInputCell.swift +++ b/Sources/ProcessOut/Sources/UI/Modules/NativeAlternativePaymentMethod/View/Cells/NativeAlternativePaymentMethodInputCell.swift @@ -36,12 +36,31 @@ final class NativeAlternativePaymentMethodInputCell: UICollectionViewCell, Nativ textField.returnKeyType = item.isLast ? .done : .next textField.keyboardType = keyboardType(parameterType: item.type) textField.textContentType = textContentType(parameterType: item.type) - observeChanges(item: item, style: style) self.item = item + self.style = style } // MARK: - NativeAlternativePaymentMethodCell + func willDisplay() { + guard let item, let style else { + return + } + let isInvalidObserver = item.value.$isInvalid.addObserver { [weak self] isInvalid in + self?.textFieldContainer.configure(isInvalid: isInvalid, style: style, animated: true) + } + let valueObserver = item.value.$text.addObserver { [weak self] updatedValue in + if self?.textFieldContainer.textField.text != updatedValue { + self?.textFieldContainer.textField.text = updatedValue + } + } + self.observations = [isInvalidObserver, valueObserver] + } + + func didEndDisplaying() { + observations = [] + } + var inputResponder: UIResponder? { textFieldContainer.textField } @@ -65,6 +84,7 @@ final class NativeAlternativePaymentMethodInputCell: UICollectionViewCell, Nativ }() private var item: NativeAlternativePaymentMethodViewModelState.InputItem? + private var style: POInputStyle? private var observations: [AnyObject] // MARK: - Private Methods @@ -101,18 +121,6 @@ final class NativeAlternativePaymentMethodInputCell: UICollectionViewCell, Nativ private func textFieldEditingChanged() { item?.value.text = textFieldContainer.textField.text ?? "" } - - private func observeChanges(item: NativeAlternativePaymentMethodViewModelState.InputItem, style: POInputStyle) { - let isInvalidObserver = item.value.$isInvalid.addObserver { [weak self] isInvalid in - self?.textFieldContainer.configure(isInvalid: isInvalid, style: style, animated: true) - } - let valueObserver = item.value.$text.addObserver { [weak self] updatedValue in - if self?.textFieldContainer.textField.text != updatedValue { - self?.textFieldContainer.textField.text = updatedValue - } - } - self.observations = [isInvalidObserver, valueObserver] - } } extension NativeAlternativePaymentMethodInputCell: UITextFieldDelegate { diff --git a/Sources/ProcessOut/Sources/UI/Modules/NativeAlternativePaymentMethod/View/NativeAlternativePaymentMethodViewController.swift b/Sources/ProcessOut/Sources/UI/Modules/NativeAlternativePaymentMethod/View/NativeAlternativePaymentMethodViewController.swift index f504049bb..98df9f338 100644 --- a/Sources/ProcessOut/Sources/UI/Modules/NativeAlternativePaymentMethod/View/NativeAlternativePaymentMethodViewController.swift +++ b/Sources/ProcessOut/Sources/UI/Modules/NativeAlternativePaymentMethod/View/NativeAlternativePaymentMethodViewController.swift @@ -104,6 +104,18 @@ final class NativeAlternativePaymentMethodViewController