Validation in Robobinding framework (Android)

848 Views Asked by At

I'm newbie in Android develop. I use Robobinding (MVVM framework) to develop Android application and I didn't found any solution to create validation in presentation model (not in activity). Has anyone encountered a similar problem? Which approach is chosen? I need somthing like this:

public class LoginPM extends AbstractPresentationModel {
        private String login;
        public String getLogin() { return login; }
        public void setLogin(String value)
        {
            if (!StringComparator.IsEquals(this.login, value))
            {
                if(TextUtils.isEmpty(value))
                {
                 setError("login", "Field cannot be left blank.");
                 return;
                }
                this.login = value;
                firePropertyChange("login");
            }
        }
  }

Sample

2

There are 2 best solutions below

0
On BEST ANSWER

I have resolved my task using reflection to getting user controls from binding-objects maps (Robobindings). https://github.com/Barbanyaga/RobobindingValidation/blob/master/BasePresentationModel.java

Use like this:

public class LoginPM extends BasePresentationModel {
        private String login;
        public String getLogin() { return login; }
        public void setLogin(String value)
        {
            if (!StringComparator.IsEquals(this.login, value))
            {
                if(TextUtils.isEmpty(value))
                {
                 setError("login", "Field cannot be left blank.");
                 return;
                }
                this.login = value;
                firePropertyChange("login");
            }
        }
  }
6
On

Sorry for the late reply. I did not notice the question. Can you have a LoginView interface between your LoginActivity and LoginPM? In that way, you can do something like below:

public void login() {
  if(isInvalid(loginInfo)) {
    loginView.setLoginError("error info");
  } else {
    loginService.login(loginInfo);
  }
}

Alternatively, you can implement OnTextChange event for TextView, which is fairly easy to do. You can refer to text attribute binding implementation of TextView. And you can register a method to listen to the event. Once the event is fired, you can get hold of TextView from the event object.

Also, you can implement error binding attribute for TextView and update error info accordingly.

If you like, you can post into the RoboBinding google group to get a quick response.

Hope this helps, Cheng