Skip to content

Commit

Permalink
fix(ad-hoc): avoid redundant native APM cell re-configuration (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-vysotskyi-cko authored Jul 17, 2023
1 parent 22801eb commit c4edd0b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ final class NativeAlternativePaymentMethodViewController<ViewModel: NativeAltern
}
}

func collectionView(_: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
let cell = cell as? NativeAlternativePaymentMethodCell
cell?.willDisplay()
}

func collectionView(
_: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath
) {
let cell = cell as? NativeAlternativePaymentMethodCell
cell?.didEndDisplaying()
}

// MARK: - UICollectionViewDelegateFlowLayout

func collectionView(
Expand Down

0 comments on commit c4edd0b

Please sign in to comment.