使用RxJava2在ViewModel上进行EditText验证

时间:2017-10-20 19:33:06

标签: android rx-android rx-java2 rx-binding

当我点击按钮时,我正在尝试使用RxJava对EditText执行验证,但我很难将验证移动到ViewModel,这将使测试更容易。我正在使用Jake Wharton的RxBindings来获取UI输入,并使用PublishSubject使用RxJava2的Flowable.combineLatest来触发Flowable,当我单击AlertDialog上的按钮时。这是我到目前为止所得到的:

private Flowable<CharSequence> projectTitleObservable;
private final PublishSubject<CharSequence> createProjectClicked = PublishSubject.create();

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Context context = getActivity();

    View dialogView = LayoutInflater.from(context).inflate(R.layout.new_project, null);

    ButterKnife.bind(this, dialogView);

    projectTitleObservable = RxTextView.textChanges(projectNameEditText).toFlowable(BackpressureStrategy.LATEST);

    ConnectableFlowable <CharSequence> connectableFlowable = Flowable.combineLatest(createProjectClicked.toFlowable(BackpressureStrategy.LATEST), projectTitleObservable, (ignored, title) -> {
        boolean validTitle = !TextUtils.isEmpty(title);
        if (!validTitle) {
            projectNameEditText.setError("Project must have a name");
        }

        Timber.d("Hey, lambdas work! Look -> " + title);
        return title;
    })
            .publish();

    connectableFlowable.subscribe(test -> Timber.d(test.toString()));

    return new AlertDialog.Builder(context)
            .setTitle("Add new Project")
            .setView(dialogView)
            .setPositiveButton(android.R.string.ok, (dialogInterface, i) -> {
                Timber.d("Ok clicked!");
                createProjectClicked.onNext("It works!");
            })
            .setNegativeButton(android.R.string.cancel, (dialogInterface, i) -> Timber.d("Cancel clicked!"))
            .create();
}

我只是在同一个地方发布和订阅,以确保在将ConnectableFlowable移动到ViewModel之前它确实有效。我得到的唯一日志是“Ok clicked!”对我而言,这意味着它从未订阅过。对于为什么没有订阅的任何想法?

2 个答案:

答案 0 :(得分:0)

我可以使用flatMap实现此行为。以下是如果文本有效时如何显示Toast的示例:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val btnSubject = PublishSubject.create<Boolean>()
    val textObservable = RxTextView.textChangeEvents(editText)
            .filter { validateText(it.text().toString()) }

    btnSubject.flatMap { textObservable }
            .subscribe({
                Toast.makeText(this, "Text is correct", Toast.LENGTH_SHORT).show()
            })


    btn.setOnClickListener {
        btnSubject.onNext(true)
    }
}

private fun validateText(text: String) = text.length >= 5

如果您需要在文字不正确时显示一些反馈,您可以更改地图的过滤器并使用它:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val btnSubject = PublishSubject.create<Boolean>()
    val textObservable = RxTextView.textChangeEvents(editText)
            .map { validateText(it.text().toString()) }

    btnSubject.flatMap { textObservable }
            .subscribe({ isTextCorrect ->
                if (isTextCorrect) {
                    Toast.makeText(this, "Text is correct", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(this, "Text NOT is correct", Toast.LENGTH_SHORT).show()
                }
            })


    btn.setOnClickListener {
        btnSubject.onNext(true)
    }
}

private fun validateText(text: String) = text.length >= 5

我刚试过这段代码,效果很好。

快乐的代码!

答案 1 :(得分:0)

Java版:

如果代码有效,则显示Toast:

private Observable<TextViewTextChangeEvent> taskTitleObservable;
private final PublishSubject<Boolean> createTaskClicked = PublishSubject.create();

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    ButterKnife.bind(this, view);

    taskTitleObservable = RxTextView.textChangeEvents(taskTitle)
    .filter(event -> validateText(event.text().toString()));

    createTaskClicked.flatMap(test -> taskTitleObservable).subscribe( aVoid -> {
        Toast.makeText(getActivity(), "Text is correct", Toast.LENGTH_SHORT).show();
    });

    RxView.clicks(fab).subscribe(aVoid -> {
        createTaskClicked.onNext(true);
    });
}

private Boolean validateText (String text) {
    return text.length() >= 5;
}

如果文字不正确,请反馈:

private Observable<Boolean> taskTitleObservable;
private final PublishSubject<Boolean> createTaskClicked = PublishSubject.create();

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    ButterKnife.bind(this, view);

    taskTitleObservable = RxTextView.textChangeEvents(taskTitle)
            .map(event -> validateText(event.text().toString()));

    createTaskClicked.flatMap(test -> taskTitleObservable).subscribe( isTextCorrect -> {
        if (isTextCorrect) {
            Toast.makeText(getActivity(), "Text is correct", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getActivity(), "Text is incorrect", Toast.LENGTH_SHORT).show();
        }
    });

    RxView.clicks(fab).subscribe(aVoid -> {
        createTaskClicked.onNext(true);
    });
}

private Boolean validateText (String text) {
    return text.length() >= 5;
}