Search in sources :

Example 1 with PartyNotCreatedException

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

the class LobbyPresenter method createParty.

void createParty(String partyTitle, String partyPassword) {
    Party party = new Party(partyTitle, partyPassword);
    add(Observable.zip(partiesRepository.getParty(party.getTitle()), spotifyRepository.getMe(spotifyCommunicatorService.getWebApi()), (dbParty, spotifyUser) -> {
        if (dbParty.exists()) {
            throw new PartyExistsException();
        }
        User user = new User(spotifyUser.id, spotifyUser.display_name, spotifyUser.images);
        party.setCreatedNowTimeStamp();
        party.setPartyVersionCode(VersionUtil.getCurrentAppVersionCode());
        party.setHostSpotifyId(user.getUserId());
        party.setHostMarket(spotifyUser.country);
        user.setJoinedNowTimeStamp();
        user.setHasHostPrivileges();
        return new UserPartyInformation(user, party);
    }).flatMap(userPartyInformation -> Observable.zip(partiesRepository.createNewParty(userPartyInformation.getParty()), partiesRepository.addUserToParty(userPartyInformation.getParty().getTitle(), userPartyInformation.getUser()), (partyWasCreated, userWasAdded) -> {
        if (!partyWasCreated)
            throw new PartyNotCreatedException();
        if (!userWasAdded)
            throw new UserNotAddedException();
        return userPartyInformation.getParty();
    })).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).compose(this.deliverFirst()).subscribe(lobbyViewPartyDelivery -> lobbyViewPartyDelivery.split((lobbyView, confirmedParty) -> {
        navigateToParty(confirmedParty.getTitle());
    }, (lobbyView, exception) -> {
        if (exception instanceof PartyExistsException) {
            lobbyView.showMessage("Party " + partyTitle + " already exists");
        } else if (exception instanceof UserNotAddedException) {
            lobbyView.showMessage("Something went wrong when adding you to the party");
        } else {
            lobbyView.showMessage("Something went wrong when creating the party");
        }
        Log.d(LogTag.LOG_LOBBY, "Could not create party");
    })));
}
Also used : PartiesRepository(se.zinokader.spotiq.repository.PartiesRepository) Bundle(android.os.Bundle) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) Inject(javax.inject.Inject) VersionUtil(se.zinokader.spotiq.util.VersionUtil) SpotifyRepository(se.zinokader.spotiq.repository.SpotifyRepository) PartyVersionLowerException(se.zinokader.spotiq.util.exception.PartyVersionLowerException) PartyExistsException(se.zinokader.spotiq.util.exception.PartyExistsException) PartyVersionHigherException(se.zinokader.spotiq.util.exception.PartyVersionHigherException) FirebaseConstants(se.zinokader.spotiq.constant.FirebaseConstants) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) 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) SpotifyAuthenticationService(se.zinokader.spotiq.service.authentication.SpotifyAuthenticationService) TimeUnit(java.util.concurrent.TimeUnit) ApplicationConstants(se.zinokader.spotiq.constant.ApplicationConstants) PartyDoesNotExistException(se.zinokader.spotiq.util.exception.PartyDoesNotExistException) User(se.zinokader.spotiq.model.User) PartyWrongPasswordException(se.zinokader.spotiq.util.exception.PartyWrongPasswordException) 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 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)1 Schedulers (io.reactivex.schedulers.Schedulers)1 TimeUnit (java.util.concurrent.TimeUnit)1 Inject (javax.inject.Inject)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 PartiesRepository (se.zinokader.spotiq.repository.PartiesRepository)1 SpotifyRepository (se.zinokader.spotiq.repository.SpotifyRepository)1 SpotifyAuthenticationService (se.zinokader.spotiq.service.authentication.SpotifyAuthenticationService)1 VersionUtil (se.zinokader.spotiq.util.VersionUtil)1 PartyDoesNotExistException (se.zinokader.spotiq.util.exception.PartyDoesNotExistException)1 PartyExistsException (se.zinokader.spotiq.util.exception.PartyExistsException)1