Simple analysis on the method of Rxjava to deal with complex form verification _java

Source: Internet
Author: User
Tags closure throwable

Whether it's a simple login page or a complex order submission page, front-end validation of a form (such as a login and password that meets the basic requirements to light the login button) is an essential step. This article shows how to use Rxjava to facilitate the processing of form submission before the verification problem, examples of Android on a simple login page

Content Summary

Traditional methods of verification

Combinelatest operator

Handling form validation with Combinelatest

The difference between combinelatest and zip

The example sample code shown in this article is located in Rxandroiddemo, see loginactivity this file

Traditional methods of verification

Here we use the simplest example to illustrate, as shown above, an email input and a password input, below is a login button. Only when the email input box contains the @ character, the password input box content is greater than 4, only the button below is lit.

First of all, you use EditText or inherited from the EditText control, generally listening to its content, are used Addtextchangedlistener. But how obviously the login button to enable or not at the same time to determine the email and password, two are set up to light. So we in the textwatcher of email in addition to judge whether the email meets the conditions, but also to determine whether the password meet the conditions, so it is easy to cause multiple judgments.
Just imagine how painful it would be if you were submitting a form for an order with more than 10 input boxes, and each input was eligible to light up the "submit" button ———— Each input box will be changed to determine whether the contents of the other more than 10 input boxes are met (in fact, the other more than 10 input boxes do not change at this time)

Combinelatest operator

Combinelatest is a common operator provided by the Rxjava itself, which accepts two or more observable and a funcx closure. When any one of the data in the incoming observable is emitted, the Combinelatest combines the most recent value (lastest) of each observable (combine) to the FUNCX closure for processing. The point is

1.combineLatest is the most recent value that will store each observable

2. Any observable launching a new value will trigger the Operation-> "combine all observable ' s lastest value together and send to Function"


Handling form validation with Combinelatest

First we write an email and password verification method, one that needs to contain the @ character, and one that requires more than 4 characters:

Private Boolean isemailvalid (String email) {
  //todo:replace this and your own logic return
  email.contains ("@");
 }
Private Boolean ispasswordvalid (String password) {
 //todo:replace this and your own logic return
 Password.lengt H () > 4;
}

Subsequently, we create observable for email and password, and the values that are emitted are the contents of the respective EditText changes, and the return value of the call callback method is the passed-in parameter of the Aftertextchanged method in Textwatcher:

observable<string> Observableemail = observable.create (new observable.onsubscribe<string> () {@Override public void call (Final subscriber<? Super String> Subscriber) {Memailview.addtextchangedlistener (new Textwatch ER () {@Override public void beforetextchanged (charsequence s, int start, int count, int after) {} @O Verride public void ontextchanged (charsequence s, int start, int before, int count) {} @Override Publ
     IC void aftertextchanged (Editable s) {Subscriber.onnext (s.tostring ());
   }
    });
}
  }); observable<string> Observablepassword = observable.create (new observable.onsubscribe<string> () {@  Override public void call (Final subscriber<? Super String> Subscriber) {Mpasswordview.addtextchangedlistener (new
   Textwatcher () {@Override public void beforetextchanged (charsequence s, int start, int count, int after) {} @Override public void ontextchanged (Charsequence s, int start, int before, int count) {} @Override public void aftertextchanged (Editable s) {Subscriber.onnex
   T (s.tostring ());
 }
  }); }
});

Finally, use Combinelastest to combine observableemail and Observablepassword for verification:

Observable.combinelatest (Observableemail, Observablepassword, New func2<string, String, boolean> () {
   @ Override public
   Boolean Call (string e-mail, string password) {return
    isemailvalid (email) && Ispasswordvalid (password);
   }
  ). Subscribe (new subscriber<boolean> () {
   @Override public
   void oncompleted () {
   }
   @Override
   public void OnError (Throwable e) {
   }
   @Override public
   void OnNext (Boolean verify) {
    if (verify) { C15/>memailsigninbutton.setenabled (true);
    else {
     memailsigninbutton.setenabled (false);}}
  );

The verify in the OnNext is the result of a combination of combinelastest after the verification of both.

See BindView () Method of loginactivity

Here, even if the form is very complex, you actually need to expand it to be easy:

1. Encapsulate a observable for each edittext

2. Rewrite this phrase to add new logic:

return isemailvalid (email) && ispasswordvalid (password);

Think for each edittext package a observable to write a lot of duplicate code? Don't worry, Jake Wharton the Big God already thought, rxbinding in the Rxtextview can solve this problem:

observable<charsequence> Observableemail = rxtextview.textchanges (Memailview);
observable<charsequence> Observablepassword = rxtextview.textchanges (Mpasswordview);
Observable.combinelatest (Observableemail, Observablepassword, New func2<charsequence, CharSequence, Boolean> ( {
 @Override public
 Boolean Call (charsequence e-mail, charsequence password) {return
  isemailvalid ( Email.tostring ()) && Ispasswordvalid (password.tostring ());
 }
). Subscribe (new subscriber<boolean> () {
 @Override public
 void oncompleted () {
 }
 @Override Public
 void OnError (Throwable e) {
 }
 @Override public
 void OnNext (Boolean verify) {
  if ( Verify) {
   memailsigninbutton.setenabled (true);
  } else {
   memailsigninbutton.setenabled (false);
  }
 }
});

See Bindviewbyrxbinding () Method of loginactivity

The difference between combinelatest and zip

A zip is an operator that is somewhat like a combinelatest, and the accepted parameter is two or more observable and a closure. But the difference is:

1.zip is strictly in order to combine each observable, such as Observablea's first data and Observableb of the first data combined to be sent to the funcx to deal with, the nth data combination of the two sent to the funcx to deal with, Analogy.

2.zip is not any one observable launch data to trigger the closure process, but wait for each observable of the nth data is launched to trigger

Zip is typically used to consolidate data in sequential order.


The above is a small series to introduce the analysis of the Rxjava to deal with complex forms to verify the problem of the method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.