Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix StripeEditText crash on instantiation #3905

Merged
merged 3 commits into from
Jun 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ open class StripeEditText @JvmOverloads constructor(
@ColorInt
private var externalErrorColor: Int? = null

private val textWatchers = mutableListOf<TextWatcher>()
private var textWatchers: MutableList<TextWatcher>? = null

/**
* Gets whether or not the text should be displayed in error mode.
Expand Down Expand Up @@ -96,6 +96,7 @@ open class StripeEditText @JvmOverloads constructor(
}

init {
textWatchers = mutableListOf()
mshafrir-stripe marked this conversation as resolved.
Show resolved Hide resolved
maxLines = 1

listenForTextChanges()
Expand Down Expand Up @@ -264,29 +265,34 @@ open class StripeEditText @JvmOverloads constructor(
@VisibleForTesting
internal fun getParentOnFocusChangeListener() = super.getOnFocusChangeListener()

/**
* Note: [addTextChangedListener] will potentially be called by a superclass constructor
*/
override fun addTextChangedListener(watcher: TextWatcher?) {
super.addTextChangedListener(watcher)

watcher?.let {
textWatchers.add(it)
textWatchers?.add(it)
}
}

override fun removeTextChangedListener(watcher: TextWatcher?) {
super.removeTextChangedListener(watcher)

watcher?.let {
textWatchers.remove(it)
textWatchers?.remove(it)
}
}

/**
* Set text without notifying its corresponding text watchers.
*/
internal fun setTextSilent(text: CharSequence?) {
textWatchers.forEach {
textWatchers?.forEach {
super.removeTextChangedListener(it)
}
setText(text)
textWatchers.forEach {
textWatchers?.forEach {
super.addTextChangedListener(it)
}
}
Expand Down