0

I have an Android project that currently doesn't use View Models or rxJava. It's a fairly straightforward app that uses the Parse sdk with a parse-server. It's still early in the project and I've decided to start using VM and rxJava.

My question is mostly about structure, naming etc. Let's use Login for an example:

I have an interface calledAuthenticationService.java with a login(Authorization auth, ServiceCallback<User> callback) method.

My ParseAuthenticationService.java implements that interface and does all the parse stuff.

The whole app is set up like that, so my User.java is an interface used by MyParseUser.java where all the parse stuff happens.

So there's a login button, LoginActivity grabs the username/password, makes an Authorization object and calls the login function.

Is the change to rxJava as simple as wrapping the login function in an observable and subscribing to it in the activity?

What would that look like? The login function looks like this:

@Override
public void login(Authorization authorization, final ServiceCallback<User> callback)
{
    cachedUser = null;
    ParseUser.logInInBackground(authorization.getUsername(), authorization.getPassword(), new LogInCallback()
    {
        @Override
        public void done(ParseUser user, ParseException e)
        {
            if (callback != null)
            {
                callback.onServiceRequestComplete(Response.<User>from((MyParseUser) user, e));
            }
        }
    });
}

Can I keep the same Parse-class-using-generic-interface structure? Or should I restructure/rename my files?

Thanks for any help. This is all a bit overwhelming.

MayNotBe
  • 2,110
  • 3
  • 32
  • 47

1 Answers1

0

Using interface separate contract declaration from implementation. Since the Observable pattern is a nice way to represent asynchronous responses, replacing callback with Observables in your interfaces seems a good fit.

You could begin to put Observable version of your callback function alongside. For example, writing an Observable<User> loginObservable(Authorization auth) (or better Single<User> loginSingle(Authorization auth)) that essentially wrap login( ... ) with Observable.create() or Single.create(). You may need a refactoring afterward, but you will do it when you familiar with Rx. For me switching to Rx was a 2 steps process; I evolved my architecture but a lot later.

Since creating Observable like this is a little advanced Rx topic, I would recommend you to look for already existing Rx wrappers for Parse.

Geoffrey Marizy
  • 5,282
  • 2
  • 26
  • 29