Search in sources :

Example 91 with Disposable

use of io.reactivex.disposables.Disposable in project SpotiQ by ZinoKader.

the class LobbyPresenter method joinParty.

void joinParty(String partyTitle, String partyPassword) {
    Party party = new Party(partyTitle, partyPassword);
    Observable.zip(partiesRepository.getParty(party.getTitle()), spotifyRepository.getMe(spotifyCommunicatorService.getWebApi()), (dbPartySnapshot, spotifyUser) -> {
        if (dbPartySnapshot.exists()) {
            User user = new User(spotifyUser.id, spotifyUser.display_name, spotifyUser.images);
            boolean userAlreadyExists = dbPartySnapshot.child(FirebaseConstants.CHILD_USERS).hasChild(user.getUserId());
            Party dbParty = dbPartySnapshot.child(FirebaseConstants.CHILD_PARTYINFO).getValue(Party.class);
            return new UserPartyInformation(user, userAlreadyExists, dbParty);
        } else {
            throw new PartyDoesNotExistException();
        }
    }).map(userPartyInformation -> {
        if (userPartyInformation.getParty().getPassword().equals(partyPassword)) {
            if (!userPartyInformation.userAlreadyExists()) {
                partiesRepository.addUserToParty(userPartyInformation.getUser(), userPartyInformation.getParty().getTitle()).subscribe();
            }
            return userPartyInformation.getParty();
        } else {
            throw new PartyWrongPasswordException();
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Party>() {

        @Override
        public void onNext(Party party) {
            navigateToParty(party.getTitle());
        }

        @Override
        public void onError(Throwable exception) {
            if (exception instanceof PartyDoesNotExistException) {
                getView().showMessage("Party does not exist, why not create it?");
            } else if (exception instanceof PartyWrongPasswordException) {
                getView().showMessage("Password incorrect");
            } else {
                getView().showMessage("Something went wrong when joining the party");
            }
            Log.d(LogTag.LOG_LOBBY, "Could not join party");
            exception.printStackTrace();
        }

        @Override
        public void onComplete() {
        }

        @Override
        public void onSubscribe(Disposable d) {
        }
    });
}
Also used : PartiesRepository(se.zinokader.spotiq.repository.PartiesRepository) Bundle(android.os.Bundle) UserPrivate(kaaes.spotify.webapi.android.models.UserPrivate) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) Inject(javax.inject.Inject) SpotifyRepository(se.zinokader.spotiq.repository.SpotifyRepository) PartyExistsException(se.zinokader.spotiq.util.exception.PartyExistsException) FirebaseConstants(se.zinokader.spotiq.constant.FirebaseConstants) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) Response(retrofit.client.Response) Log(android.util.Log) PartyNotCreatedException(se.zinokader.spotiq.util.exception.PartyNotCreatedException) LogTag(se.zinokader.spotiq.constant.LogTag) BasePresenter(se.zinokader.spotiq.feature.base.BasePresenter) UserPartyInformation(se.zinokader.spotiq.model.UserPartyInformation) UserNotAddedException(se.zinokader.spotiq.util.exception.UserNotAddedException) Party(se.zinokader.spotiq.model.Party) TimeUnit(java.util.concurrent.TimeUnit) Callback(retrofit.Callback) ApplicationConstants(se.zinokader.spotiq.constant.ApplicationConstants) Disposable(io.reactivex.disposables.Disposable) PartyDoesNotExistException(se.zinokader.spotiq.util.exception.PartyDoesNotExistException) RetrofitError(retrofit.RetrofitError) Observer(io.reactivex.Observer) SpotifyCommunicatorService(se.zinokader.spotiq.service.SpotifyCommunicatorService) User(se.zinokader.spotiq.model.User) PartyWrongPasswordException(se.zinokader.spotiq.util.exception.PartyWrongPasswordException) Disposable(io.reactivex.disposables.Disposable) PartyWrongPasswordException(se.zinokader.spotiq.util.exception.PartyWrongPasswordException) Party(se.zinokader.spotiq.model.Party) User(se.zinokader.spotiq.model.User) UserPartyInformation(se.zinokader.spotiq.model.UserPartyInformation) PartyDoesNotExistException(se.zinokader.spotiq.util.exception.PartyDoesNotExistException)

Example 92 with Disposable

use of io.reactivex.disposables.Disposable in project Varis-Android by dkhmelenko.

the class BuildsDetailsPresenter method restartBuild.

/**
     * Restarts build process
     */
public void restartBuild() {
    RequestBody emptyBody = RequestBody.create(MediaType.parse("application/json"), "");
    Disposable subscription = mTravisRestClient.getApiService().restartBuild(mBuildId, emptyBody).onErrorReturn(throwable -> new Object()).flatMap(new Function<Object, SingleSource<BuildDetails>>() {

        @Override
        public SingleSource<BuildDetails> apply(@NonNull Object o) throws Exception {
            return mTravisRestClient.getApiService().getBuild(mRepoSlug, mBuildId);
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe((buildDetails, throwable) -> {
        if (throwable == null) {
            handleBuildDetails(buildDetails);
        } else {
            handleLoadingFailed(throwable);
        }
    });
    mSubscriptions.add(subscription);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Function(io.reactivex.functions.Function) BuildDetails(com.khmelenko.lab.varis.network.response.BuildDetails) NonNull(io.reactivex.annotations.NonNull) RequestBody(okhttp3.RequestBody)

Example 93 with Disposable

use of io.reactivex.disposables.Disposable in project Varis-Android by dkhmelenko.

the class BuildsDetailsPresenter method startLoadingLog.

/**
     * Starts loading log file
     *
     * @param jobId Job ID
     */
public void startLoadingLog(long jobId) {
    mJobId = jobId;
    String accessToken = AppSettings.getAccessToken();
    Single<String> responseSingle;
    if (TextUtils.isEmpty(accessToken)) {
        responseSingle = mRawClient.getApiService().getLog(String.valueOf(mJobId));
    } else {
        String auth = String.format("token %1$s", AppSettings.getAccessToken());
        responseSingle = mRawClient.getApiService().getLog(auth, String.valueOf(mJobId));
    }
    Disposable subscription = responseSingle.subscribeOn(Schedulers.io()).map(s -> mRawClient.getLogUrl(mJobId)).onErrorResumeNext(new Function<Throwable, SingleSource<String>>() {

        @Override
        public SingleSource<String> apply(@NonNull Throwable throwable) throws Exception {
            String redirectUrl = "";
            HttpException httpException = (HttpException) throwable;
            Headers headers = httpException.response().headers();
            for (String header : headers.names()) {
                if (header.equals("Location")) {
                    redirectUrl = headers.get(header);
                    break;
                }
            }
            return Single.just(redirectUrl);
        }
    }).retry(LOAD_LOG_MAX_ATTEMPT).observeOn(AndroidSchedulers.mainThread()).subscribe((logUrl, throwable) -> {
        if (throwable == null) {
            getView().setLogUrl(logUrl);
        } else {
            getView().showLogError();
            getView().showLoadingError(throwable.getMessage());
        }
    });
    mSubscriptions.add(subscription);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Function(io.reactivex.functions.Function) Headers(okhttp3.Headers) NonNull(io.reactivex.annotations.NonNull) HttpException(retrofit2.HttpException)

Example 94 with Disposable

use of io.reactivex.disposables.Disposable in project Varis-Android by dkhmelenko.

the class RepoDetailsPresenter method loadBuildsHistory.

/**
     * Starts loading build history
     */
public void loadBuildsHistory() {
    Disposable subscription = mTravisRestClient.getApiService().getBuilds(mRepoSlug).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe((buildHistory, throwable) -> {
        if (throwable == null) {
            getView().updateBuildHistory(buildHistory);
        } else {
            getView().showBuildHistoryLoadingError(throwable.getMessage());
        }
    });
    mSubscriptions.add(subscription);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable)

Example 95 with Disposable

use of io.reactivex.disposables.Disposable in project Varis-Android by dkhmelenko.

the class RepositoriesPresenter method reloadRepos.

/**
     * Starts loading repositories
     */
public void reloadRepos() {
    String accessToken = AppSettings.getAccessToken();
    if (TextUtils.isEmpty(accessToken)) {
        Disposable subscription = mTravisRestClient.getApiService().getRepos("").subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(reposHandler());
        mSubscriptions.add(subscription);
    } else {
        Disposable subscription = mTravisRestClient.getApiService().getUser().doOnSuccess(this::cacheUserData).flatMap(new Function<User, SingleSource<List<Repo>>>() {

            @Override
            public SingleSource<List<Repo>> apply(@NonNull User user) throws Exception {
                String loginName = mUser.getLogin();
                return mTravisRestClient.getApiService().getUserRepos(loginName);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(reposHandler());
        mSubscriptions.add(subscription);
    }
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Function(io.reactivex.functions.Function) User(com.khmelenko.lab.varis.network.response.User) NonNull(io.reactivex.annotations.NonNull) List(java.util.List)

Aggregations

Disposable (io.reactivex.disposables.Disposable)224 Test (org.junit.Test)95 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)31 Logger (chat.rocket.android.helper.Logger)19 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 Worker (io.reactivex.Scheduler.Worker)15 List (java.util.List)14 Single (io.reactivex.Single)13 Function (io.reactivex.functions.Function)13 NonNull (io.reactivex.annotations.NonNull)12 Nullable (android.support.annotation.Nullable)11 Bundle (android.os.Bundle)10 CountingRunnable (io.reactivex.android.testutil.CountingRunnable)10 EmptyDisposable (io.reactivex.internal.disposables.EmptyDisposable)10 TestSubscriber (io.reactivex.subscribers.TestSubscriber)10 Optional (com.fernandocejas.arrow.optional.Optional)9 Schedulers (io.reactivex.schedulers.Schedulers)9 TimeUnit (java.util.concurrent.TimeUnit)8 NonNull (android.support.annotation.NonNull)7