Search in sources :

Example 1 with User

use of se.zinokader.spotiq.model.User 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) {
        }
    });
}
Also used : PartiesRepository(se.zinokader.spotiq.repository.PartiesRepository) Bundle(android.os.Bundle) UserPrivate(kaaes.spotify.webapi.android.models.UserPrivate) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) Inject(javax.inject.Inject) SpotifyRepository(se.zinokader.spotiq.repository.SpotifyRepository) PartyExistsException(se.zinokader.spotiq.util.exception.PartyExistsException) FirebaseConstants(se.zinokader.spotiq.constant.FirebaseConstants) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) Response(retrofit.client.Response) 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) TimeUnit(java.util.concurrent.TimeUnit) Callback(retrofit.Callback) ApplicationConstants(se.zinokader.spotiq.constant.ApplicationConstants) Disposable(io.reactivex.disposables.Disposable) PartyDoesNotExistException(se.zinokader.spotiq.util.exception.PartyDoesNotExistException) RetrofitError(retrofit.RetrofitError) Observer(io.reactivex.Observer) SpotifyCommunicatorService(se.zinokader.spotiq.service.SpotifyCommunicatorService) User(se.zinokader.spotiq.model.User) PartyWrongPasswordException(se.zinokader.spotiq.util.exception.PartyWrongPasswordException) Disposable(io.reactivex.disposables.Disposable) PartyWrongPasswordException(se.zinokader.spotiq.util.exception.PartyWrongPasswordException) Party(se.zinokader.spotiq.model.Party) User(se.zinokader.spotiq.model.User) UserPartyInformation(se.zinokader.spotiq.model.UserPartyInformation) PartyDoesNotExistException(se.zinokader.spotiq.util.exception.PartyDoesNotExistException)

Example 2 with User

use of se.zinokader.spotiq.model.User 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) {
        }
    });
}
Also used : PartiesRepository(se.zinokader.spotiq.repository.PartiesRepository) Bundle(android.os.Bundle) UserPrivate(kaaes.spotify.webapi.android.models.UserPrivate) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) Inject(javax.inject.Inject) SpotifyRepository(se.zinokader.spotiq.repository.SpotifyRepository) PartyExistsException(se.zinokader.spotiq.util.exception.PartyExistsException) FirebaseConstants(se.zinokader.spotiq.constant.FirebaseConstants) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) Response(retrofit.client.Response) 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) TimeUnit(java.util.concurrent.TimeUnit) Callback(retrofit.Callback) ApplicationConstants(se.zinokader.spotiq.constant.ApplicationConstants) Disposable(io.reactivex.disposables.Disposable) PartyDoesNotExistException(se.zinokader.spotiq.util.exception.PartyDoesNotExistException) RetrofitError(retrofit.RetrofitError) Observer(io.reactivex.Observer) SpotifyCommunicatorService(se.zinokader.spotiq.service.SpotifyCommunicatorService) User(se.zinokader.spotiq.model.User) PartyWrongPasswordException(se.zinokader.spotiq.util.exception.PartyWrongPasswordException) Disposable(io.reactivex.disposables.Disposable) Party(se.zinokader.spotiq.model.Party) User(se.zinokader.spotiq.model.User) PartyExistsException(se.zinokader.spotiq.util.exception.PartyExistsException) UserPartyInformation(se.zinokader.spotiq.model.UserPartyInformation) PartyNotCreatedException(se.zinokader.spotiq.util.exception.PartyNotCreatedException) UserNotAddedException(se.zinokader.spotiq.util.exception.UserNotAddedException)

Aggregations

Bundle (android.os.Bundle)2 Log (android.util.Log)2 Observable (io.reactivex.Observable)2 Observer (io.reactivex.Observer)2 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)2 Disposable (io.reactivex.disposables.Disposable)2 Schedulers (io.reactivex.schedulers.Schedulers)2 TimeUnit (java.util.concurrent.TimeUnit)2 Inject (javax.inject.Inject)2 UserPrivate (kaaes.spotify.webapi.android.models.UserPrivate)2 Callback (retrofit.Callback)2 RetrofitError (retrofit.RetrofitError)2 Response (retrofit.client.Response)2 ApplicationConstants (se.zinokader.spotiq.constant.ApplicationConstants)2 FirebaseConstants (se.zinokader.spotiq.constant.FirebaseConstants)2 LogTag (se.zinokader.spotiq.constant.LogTag)2 BasePresenter (se.zinokader.spotiq.feature.base.BasePresenter)2 Party (se.zinokader.spotiq.model.Party)2 User (se.zinokader.spotiq.model.User)2 UserPartyInformation (se.zinokader.spotiq.model.UserPartyInformation)2