Search in sources :

Example 1 with UserAccount

use of org.eyeseetea.malariacare.domain.entity.UserAccount in project pictureapp by EyeSeeTea.

the class AuthenticationDhisSDKDataSource method login.

@Override
public void login(final Credentials credentials, final IDataSourceCallback<UserAccount> callback) {
    boolean isNetworkAvailable = isNetworkAvailable();
    if (!isNetworkAvailable) {
        callback.onError(new NetworkException());
    } else {
        Configuration configuration = new Configuration(credentials.getServerURL());
        D2.configure(configuration).flatMap(new Func1<Void, Observable<org.hisp.dhis.client.sdk.models.user.UserAccount>>() {

            @Override
            public Observable<org.hisp.dhis.client.sdk.models.user.UserAccount> call(Void aVoid) {
                return D2.me().signIn(credentials.getUsername(), credentials.getPassword());
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<org.hisp.dhis.client.sdk.models.user.UserAccount>() {

            @Override
            public void call(org.hisp.dhis.client.sdk.models.user.UserAccount dhisUserAccount) {
                UserAccount userAccount = new UserAccount(credentials.getUsername(), credentials.isDemoCredentials());
                callback.onSuccess(userAccount);
            }
        }, new Action1<Throwable>() {

            @Override
            public void call(Throwable throwable) {
                Throwable throwableResult = mapThrowable(throwable);
                callback.onError(throwableResult);
            }
        });
    }
}
Also used : Configuration(org.hisp.dhis.client.sdk.core.common.network.Configuration) Func1(rx.functions.Func1) NetworkException(org.eyeseetea.malariacare.domain.exception.NetworkException) UserAccount(org.eyeseetea.malariacare.domain.entity.UserAccount)

Aggregations

UserAccount (org.eyeseetea.malariacare.domain.entity.UserAccount)1 NetworkException (org.eyeseetea.malariacare.domain.exception.NetworkException)1 Configuration (org.hisp.dhis.client.sdk.core.common.network.Configuration)1 Func1 (rx.functions.Func1)1