Search in sources :

Example 1 with PartyWrongPasswordException

use of se.zinokader.spotiq.util.exception.PartyWrongPasswordException 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)

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