use of se.zinokader.spotiq.util.exception.PartyWrongPasswordException in project SpotiQ by ZinoKader.
the class LobbyPresenter method joinParty.
void joinParty(String partyTitle, String partyPassword) {
Party party = new Party(partyTitle, partyPassword);
Observable.zip(partiesRepository.getParty(party.getTitle()), spotifyRepository.getMe(spotifyCommunicatorService.getWebApi()), (dbPartySnapshot, spotifyUser) -> {
if (dbPartySnapshot.exists()) {
User user = new User(spotifyUser.id, spotifyUser.display_name, spotifyUser.images);
boolean userAlreadyExists = dbPartySnapshot.child(FirebaseConstants.CHILD_USERS).hasChild(user.getUserId());
Party dbParty = dbPartySnapshot.child(FirebaseConstants.CHILD_PARTYINFO).getValue(Party.class);
return new UserPartyInformation(user, userAlreadyExists, dbParty);
} else {
throw new PartyDoesNotExistException();
}
}).map(userPartyInformation -> {
if (userPartyInformation.getParty().getPassword().equals(partyPassword)) {
if (!userPartyInformation.userAlreadyExists()) {
partiesRepository.addUserToParty(userPartyInformation.getUser(), userPartyInformation.getParty().getTitle()).subscribe();
}
return userPartyInformation.getParty();
} else {
throw new PartyWrongPasswordException();
}
}).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 PartyDoesNotExistException) {
getView().showMessage("Party does not exist, why not create it?");
} else if (exception instanceof PartyWrongPasswordException) {
getView().showMessage("Password incorrect");
} else {
getView().showMessage("Something went wrong when joining the party");
}
Log.d(LogTag.LOG_LOBBY, "Could not join party");
exception.printStackTrace();
}
@Override
public void onComplete() {
}
@Override
public void onSubscribe(Disposable d) {
}
});
}
Aggregations