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

Side effect while text changes #521

Open
orcchg opened this issue Aug 29, 2019 · 0 comments
Open

Side effect while text changes #521

orcchg opened this issue Aug 29, 2019 · 0 comments

Comments

@orcchg
Copy link

orcchg commented Aug 29, 2019

Suppose that i'm writing my own View class, that represents editable field and contains EditText tv_input inside:

class EditTextIconItemView : LinearLayout {
   fun setInputText(text: String?) {
        with (tv_input) {
            setText(text)
            setCharsCount(text?.length ?: 0)
            setSelection(text?.length ?: 0)
        }
    }

    fun setCharsCount(count: Int) {
        tv_chars_count?.text = "$count/$MAX_LENGTH"
    }
}

I'd like to delegate textChanges() to that internal EditText tv_input, so i'm writing the following code in my custom EditTextIconItemView:

fun EditTextIconItemView.textChanges() =
        tv_input.textChanges().doOnNext { setCharsCount(it.length) }

That works well, but now i want my client code to actually skip initial value, so in client code i have:

val et = EditTextIconItemView()
et.textChanges().skipInitialValue().subscribe { ... }

This requires me to explicitly specify the return type in EditTextIconItemView for textChanges():

fun EditTextIconItemView.textChanges(): InitialValueObservable<CharSequence> = 
         tv_input.textChanges().doOnNext { setCharsCount(it.length) }

But this won't compile since doOnNext returns Observable which cannot be cast to InitialValueObservable.

But i actually don't want the client code to handle that side effect and set up chars count on that View, this is the responsibility of EditTextIconItemView itself. But i' like to still be able to tell, whether to skip initial value or not on client's side.

How could i make it work?

Thank You!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant