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) {
}
});
}
Aggregations