Search in sources :

Example 1 with PartyVersionLowerException

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

the class LobbyPresenter method joinParty.

void joinParty(String partyTitle, String partyPassword) {
    Party party = new Party(partyTitle, partyPassword);
    add(Observable.zip(partiesRepository.getParty(party.getTitle()), spotifyRepository.getMe(spotifyCommunicatorService.getWebApi()), (dbPartySnapshot, spotifyUser) -> {
        if (!dbPartySnapshot.exists())
            throw new PartyDoesNotExistException();
        Party dbParty = dbPartySnapshot.child(FirebaseConstants.CHILD_PARTYINFO).getValue(Party.class);
        if (dbParty.getPartyVersionCode() > VersionUtil.getCurrentAppVersionCode()) {
            throw new PartyVersionHigherException();
        } else if (dbParty.getPartyVersionCode() < VersionUtil.getCurrentAppVersionCode()) {
            throw new PartyVersionLowerException();
        }
        User user = new User(spotifyUser.id, spotifyUser.display_name, spotifyUser.images);
        user.setJoinedNowTimeStamp();
        boolean userAlreadyExists = dbPartySnapshot.child(FirebaseConstants.CHILD_USERS).hasChild(user.getUserId());
        return new UserPartyInformation(user, userAlreadyExists, dbParty);
    }).flatMap(userPartyInformation -> {
        if (!userPartyInformation.getParty().getPassword().equals(partyPassword))
            throw new PartyWrongPasswordException();
        if (userPartyInformation.userAlreadyExists()) {
            return Observable.just(true);
        } else {
            return partiesRepository.addUserToParty(userPartyInformation.getParty().getTitle(), userPartyInformation.getUser());
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).compose(this.deliverFirst()).subscribe(lobbyViewPartyDelivery -> lobbyViewPartyDelivery.split((lobbyView, userWasAdded) -> {
        navigateToParty(partyTitle);
    }, (lobbyView, exception) -> {
        if (exception instanceof PartyDoesNotExistException) {
            lobbyView.showMessage("Party does not exist, why not create it?");
        } else if (exception instanceof PartyWrongPasswordException) {
            lobbyView.showMessage("Invalid password");
        } else if (exception instanceof PartyVersionHigherException) {
            lobbyView.showMessage("This party was created with a newer version of SpotiQ");
        } else if (exception instanceof PartyVersionLowerException) {
            lobbyView.showMessage("This party was created with an older version of SpotiQ");
        } else {
            lobbyView.showMessage("Something went wrong when joining the party");
        }
        Log.d(LogTag.LOG_LOBBY, "Could not join 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) PartyWrongPasswordException(se.zinokader.spotiq.util.exception.PartyWrongPasswordException) Party(se.zinokader.spotiq.model.Party) User(se.zinokader.spotiq.model.User) PartyVersionLowerException(se.zinokader.spotiq.util.exception.PartyVersionLowerException) UserPartyInformation(se.zinokader.spotiq.model.UserPartyInformation) PartyDoesNotExistException(se.zinokader.spotiq.util.exception.PartyDoesNotExistException) PartyVersionHigherException(se.zinokader.spotiq.util.exception.PartyVersionHigherException)

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