Search in sources :

Example 1 with UserNotAddedException

use of se.zinokader.spotiq.util.exception.UserNotAddedException in project SpotiQ by ZinoKader.

the class LobbyPresenter method createParty.

void createParty(String partyTitle, String partyPassword) {
    Party party = new Party(partyTitle, partyPassword);
    Observable.zip(partiesRepository.getParty(party.getTitle()), spotifyRepository.getMe(spotifyCommunicatorService.getWebApi()), (dbParty, spotifyUser) -> {
        if (dbParty.exists()) {
            throw new PartyExistsException();
        } else {
            User user = new User(spotifyUser.id, spotifyUser.display_name, spotifyUser.images);
            party.setHostSpotifyId(user.getUserId());
            return new UserPartyInformation(user, party);
        }
    }).flatMap(userPartyInformation -> Observable.zip(partiesRepository.createNewParty(userPartyInformation.getParty()), partiesRepository.addUserToParty(userPartyInformation.getUser(), userPartyInformation.getParty().getTitle()), (partyWasCreated, userWasAdded) -> {
        if (!partyWasCreated)
            throw new PartyNotCreatedException();
        if (!userWasAdded)
            throw new UserNotAddedException();
        return userPartyInformation.getParty();
    })).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 PartyExistsException) {
                getView().showMessage("Party " + partyTitle + " already exists");
            } else if (exception instanceof UserNotAddedException) {
                getView().showMessage("Something went wrong when adding you to the party");
            } else {
                getView().showMessage("Something went wrong when creating the party");
            }
            Log.d(LogTag.LOG_LOBBY, "Could not create 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) Party(se.zinokader.spotiq.model.Party) User(se.zinokader.spotiq.model.User) PartyExistsException(se.zinokader.spotiq.util.exception.PartyExistsException) UserPartyInformation(se.zinokader.spotiq.model.UserPartyInformation) PartyNotCreatedException(se.zinokader.spotiq.util.exception.PartyNotCreatedException) UserNotAddedException(se.zinokader.spotiq.util.exception.UserNotAddedException)

Aggregations

Bundle (android.os.Bundle)1 Log (android.util.Log)1 Observable (io.reactivex.Observable)1 Observer (io.reactivex.Observer)1 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)1 Disposable (io.reactivex.disposables.Disposable)1 Schedulers (io.reactivex.schedulers.Schedulers)1 TimeUnit (java.util.concurrent.TimeUnit)1 Inject (javax.inject.Inject)1 UserPrivate (kaaes.spotify.webapi.android.models.UserPrivate)1 Callback (retrofit.Callback)1 RetrofitError (retrofit.RetrofitError)1 Response (retrofit.client.Response)1 ApplicationConstants (se.zinokader.spotiq.constant.ApplicationConstants)1 FirebaseConstants (se.zinokader.spotiq.constant.FirebaseConstants)1 LogTag (se.zinokader.spotiq.constant.LogTag)1 BasePresenter (se.zinokader.spotiq.feature.base.BasePresenter)1 Party (se.zinokader.spotiq.model.Party)1 User (se.zinokader.spotiq.model.User)1 UserPartyInformation (se.zinokader.spotiq.model.UserPartyInformation)1