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