use of se.zinokader.spotiq.util.exception.PartyExistsException 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");
})));
}
Aggregations