Skip to content

DistinctUntilChanged doesn't work on API level 29 #7834

Closed
@Jacek-Gawel

Description

@Jacek-Gawel

Running this code on API level 25, t1 and t2 are different when typing in the edit text.
However, on API level 29, the t1 == t2 condition always evaluates to true.

fun TextInputLayout.priceInput(
    maxValue: Long = Constants.max_value_receipt.toLong(),
    skipInitialValue: Boolean = false): Observable<CharSequence> {

    var animation: ValueAnimator? = null

    errorIconDrawable = null

    editText!!.inputType =
        EditorInfo.TYPE_NUMBER_FLAG_DECIMAL
    editText!!.keyListener =
        DigitsKeyListener.getInstance("0123456789,")

    editText!!.setOnTouchListener { _, motionEvent ->
        if (motionEvent.action == MotionEvent.ACTION_UP) {
            editText!!.showSoftKeyboard()
        }
        return@setOnTouchListener true
    }

    editText!!.setOnFocusChangeListener { _, hasFocus ->
        if (hasFocus) editText!!.selectAtTheEnd()
    }

    val afterTextChangeEvents = editText!!.textChanges().let {
        if (skipInitialValue)
            it.skipInitialValue()
        else
            it.skip(0)
    }
    return afterTextChangeEvents
        .distinctUntilChanged { t1, t2 ->
            t1 == t2
        }
        .concatMap {
            Observable.fromCallable {
                val toString = it.toEmptyIfNull().toString()
                if (toString.isEmpty())
                    return@fromCallable toString
                val withoutComma = toString.replace(",", "")
                val value = try {
                    withoutComma.toLong()
                } catch (e: Exception) {
                    return@fromCallable (editText!!).text.delete(
                            editText!!.text.length - 1,
                            editText!!.text.length)
                }
                if (value > maxValue) {
                    if (animation == null || animation!!.isRunning.not()) {
                        animation = createAnimationError(editText!!)
                        animation!!.start()
                    }
                    return@fromCallable editText!!.text.delete(
                            editText!!.text.length - 1,
                            editText!!.text.length)
                }
                val result = Formats.convertPrice_LongToStringWithComma(value)
                Logger.d { "formatted: $result" }
                if (toString != result)
                    editText!!.text.replace(0, editText!!.text.length, result)
                return@fromCallable result
            }
                .onErrorReturnItem("")
        }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions