Search in sources :

Example 1 with Party

use of se.zinokader.spotiq.model.Party 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");
    })));
}
Also used : PartiesRepository(se.zinokader.spotiq.repository.PartiesRepository) Bundle(android.os.Bundle) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) Inject(javax.inject.Inject) VersionUtil(se.zinokader.spotiq.util.VersionUtil) SpotifyRepository(se.zinokader.spotiq.repository.SpotifyRepository) PartyVersionLowerException(se.zinokader.spotiq.util.exception.PartyVersionLowerException) PartyExistsException(se.zinokader.spotiq.util.exception.PartyExistsException) PartyVersionHigherException(se.zinokader.spotiq.util.exception.PartyVersionHigherException) FirebaseConstants(se.zinokader.spotiq.constant.FirebaseConstants) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) 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) SpotifyAuthenticationService(se.zinokader.spotiq.service.authentication.SpotifyAuthenticationService) TimeUnit(java.util.concurrent.TimeUnit) ApplicationConstants(se.zinokader.spotiq.constant.ApplicationConstants) PartyDoesNotExistException(se.zinokader.spotiq.util.exception.PartyDoesNotExistException) User(se.zinokader.spotiq.model.User) PartyWrongPasswordException(se.zinokader.spotiq.util.exception.PartyWrongPasswordException) PartyWrongPasswordException(se.zinokader.spotiq.util.exception.PartyWrongPasswordException) Party(se.zinokader.spotiq.model.Party) User(se.zinokader.spotiq.model.User) PartyVersionLowerException(se.zinokader.spotiq.util.exception.PartyVersionLowerException) UserPartyInformation(se.zinokader.spotiq.model.UserPartyInformation) PartyDoesNotExistException(se.zinokader.spotiq.util.exception.PartyDoesNotExistException) PartyVersionHigherException(se.zinokader.spotiq.util.exception.PartyVersionHigherException)

Example 2 with Party

use of se.zinokader.spotiq.model.Party in project SpotiQ by ZinoKader.

the class PartiesRepository method isHostOfParty.

public Observable<Boolean> isHostOfParty(String partyTitle, String spotifyUserId) {
    return Observable.create(subscriber -> databaseReference.child(partyTitle).child(FirebaseConstants.CHILD_PARTYINFO).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Party dbParty = dataSnapshot.getValue(Party.class);
            boolean isHost = dbParty != null && dbParty.getHostSpotifyId().equals(spotifyUserId);
            subscriber.onNext(isHost);
            subscriber.onComplete();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            subscriber.onError(databaseError.toException());
        }
    }));
}
Also used : Party(se.zinokader.spotiq.model.Party) DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 3 with Party

use of se.zinokader.spotiq.model.Party 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");
    })));
}
Also used : PartiesRepository(se.zinokader.spotiq.repository.PartiesRepository) Bundle(android.os.Bundle) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) Inject(javax.inject.Inject) VersionUtil(se.zinokader.spotiq.util.VersionUtil) SpotifyRepository(se.zinokader.spotiq.repository.SpotifyRepository) PartyVersionLowerException(se.zinokader.spotiq.util.exception.PartyVersionLowerException) PartyExistsException(se.zinokader.spotiq.util.exception.PartyExistsException) PartyVersionHigherException(se.zinokader.spotiq.util.exception.PartyVersionHigherException) FirebaseConstants(se.zinokader.spotiq.constant.FirebaseConstants) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) 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) SpotifyAuthenticationService(se.zinokader.spotiq.service.authentication.SpotifyAuthenticationService) TimeUnit(java.util.concurrent.TimeUnit) ApplicationConstants(se.zinokader.spotiq.constant.ApplicationConstants) PartyDoesNotExistException(se.zinokader.spotiq.util.exception.PartyDoesNotExistException) User(se.zinokader.spotiq.model.User) PartyWrongPasswordException(se.zinokader.spotiq.util.exception.PartyWrongPasswordException) 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)

Example 4 with Party

use of se.zinokader.spotiq.model.Party in project SpotiQ by ZinoKader.

the class SongSearchPresenter method searchTracks.

void searchTracks(String query) {
    Map<String, Object> searchOptions = new HashMap<>();
    searchOptions.put(SpotifyService.LIMIT, SpotifyConstants.TRACK_SEARCH_QUERY_RESPONSE_LIMIT);
    searchOptions.put(SpotifyService.OFFSET, 0);
    partiesRepository.getParty(partyTitle).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(partySnapshot -> {
        Party dbParty = partySnapshot.child(FirebaseConstants.CHILD_PARTYINFO).getValue(Party.class);
        searchOptions.put(SpotifyService.MARKET, dbParty.getHostMarket());
        searchTracksRecursively(query, searchOptions).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).map(tracksPager -> TrackMapper.tracksToSongs(tracksPager.tracks.items, user)).subscribe(songs -> {
            if (getView() != null) {
                if (songs.isEmpty())
                    getView().showMessage("No songs were found for the search query " + query);
                getView().updateSearch(songs);
            }
        }, throwable -> {
            Log.d(LogTag.LOG_SEARCH, "Something went wrong on searching for tracks");
            throwable.printStackTrace();
        });
    });
}
Also used : PartiesRepository(se.zinokader.spotiq.repository.PartiesRepository) Bundle(android.os.Bundle) HashMap(java.util.HashMap) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) TrackMapper(se.zinokader.spotiq.util.mapper.TrackMapper) SpotifyService(kaaes.spotify.webapi.android.SpotifyService) Inject(javax.inject.Inject) SpotifyRepository(se.zinokader.spotiq.repository.SpotifyRepository) Map(java.util.Map) PreviewPlayer(se.zinokader.spotiq.feature.search.preview.PreviewPlayer) FirebaseConstants(se.zinokader.spotiq.constant.FirebaseConstants) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) Log(android.util.Log) SpotifyConstants(se.zinokader.spotiq.constant.SpotifyConstants) LogTag(se.zinokader.spotiq.constant.LogTag) BasePresenter(se.zinokader.spotiq.feature.base.BasePresenter) Party(se.zinokader.spotiq.model.Party) SpotifyAuthenticationService(se.zinokader.spotiq.service.authentication.SpotifyAuthenticationService) TimeUnit(java.util.concurrent.TimeUnit) ApplicationConstants(se.zinokader.spotiq.constant.ApplicationConstants) List(java.util.List) TracksPager(kaaes.spotify.webapi.android.models.TracksPager) Song(se.zinokader.spotiq.model.Song) User(se.zinokader.spotiq.model.User) Party(se.zinokader.spotiq.model.Party) HashMap(java.util.HashMap)

Example 5 with Party

use of se.zinokader.spotiq.model.Party in project SpotiQ by ZinoKader.

the class PlaylistSearchPresenter method loadPlaylistSongs.

void loadPlaylistSongs(PlaylistSimple playlist) {
    Map<String, Object> searchOptions = new HashMap<>();
    searchOptions.put(SpotifyService.LIMIT, SpotifyConstants.PLAYLIST_TRACK_SEARCH_QUERY_RESPONSE_LIMIT);
    searchOptions.put(SpotifyService.OFFSET, 0);
    partiesRepository.getParty(partyTitle).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(partySnapshot -> {
        Party dbParty = partySnapshot.child(FirebaseConstants.CHILD_PARTYINFO).getValue(Party.class);
        searchOptions.put(SpotifyService.MARKET, dbParty.getHostMarket());
        findPlaylistTracksRecursively(playlist, searchOptions).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).map(playlistPager -> {
            List<PlaylistTrack> filteredTracks = new ArrayList<>();
            filteredTracks.addAll(playlistPager.items);
            for (PlaylistTrack playlistTrack : playlistPager.items) {
                if (playlistTrack.is_local || !playlistTrack.track.is_playable) {
                    filteredTracks.remove(playlistTrack);
                }
            }
            return filteredTracks;
        }).map(filteredTracks -> TrackMapper.playlistTracksToSongs(filteredTracks, user)).subscribe(songs -> {
            if (getView() != null) {
                if (songs.isEmpty())
                    getView().showMessage("Playlist is empty");
                getView().updateSongs(songs);
            }
        }, throwable -> {
            Log.d(LogTag.LOG_SEARCH, "Something went wrong on loading playlist songs");
            throwable.printStackTrace();
        });
    });
}
Also used : PartiesRepository(se.zinokader.spotiq.repository.PartiesRepository) Bundle(android.os.Bundle) HashMap(java.util.HashMap) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) TrackMapper(se.zinokader.spotiq.util.mapper.TrackMapper) SpotifyService(kaaes.spotify.webapi.android.SpotifyService) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) SpotifyRepository(se.zinokader.spotiq.repository.SpotifyRepository) Map(java.util.Map) PreviewPlayer(se.zinokader.spotiq.feature.search.preview.PreviewPlayer) FirebaseConstants(se.zinokader.spotiq.constant.FirebaseConstants) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) Log(android.util.Log) SpotifyConstants(se.zinokader.spotiq.constant.SpotifyConstants) Pager(kaaes.spotify.webapi.android.models.Pager) LogTag(se.zinokader.spotiq.constant.LogTag) BasePresenter(se.zinokader.spotiq.feature.base.BasePresenter) Party(se.zinokader.spotiq.model.Party) SpotifyAuthenticationService(se.zinokader.spotiq.service.authentication.SpotifyAuthenticationService) TimeUnit(java.util.concurrent.TimeUnit) ApplicationConstants(se.zinokader.spotiq.constant.ApplicationConstants) PlaylistSimple(kaaes.spotify.webapi.android.models.PlaylistSimple) List(java.util.List) TracklistRepository(se.zinokader.spotiq.repository.TracklistRepository) PlaylistTrack(kaaes.spotify.webapi.android.models.PlaylistTrack) User(se.zinokader.spotiq.model.User) Party(se.zinokader.spotiq.model.Party) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PlaylistTrack(kaaes.spotify.webapi.android.models.PlaylistTrack)

Aggregations

Party (se.zinokader.spotiq.model.Party)5 Bundle (android.os.Bundle)4 Log (android.util.Log)4 Observable (io.reactivex.Observable)4 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)4 Schedulers (io.reactivex.schedulers.Schedulers)4 TimeUnit (java.util.concurrent.TimeUnit)4 Inject (javax.inject.Inject)4 ApplicationConstants (se.zinokader.spotiq.constant.ApplicationConstants)4 FirebaseConstants (se.zinokader.spotiq.constant.FirebaseConstants)4 LogTag (se.zinokader.spotiq.constant.LogTag)4 BasePresenter (se.zinokader.spotiq.feature.base.BasePresenter)4 User (se.zinokader.spotiq.model.User)4 PartiesRepository (se.zinokader.spotiq.repository.PartiesRepository)4 SpotifyRepository (se.zinokader.spotiq.repository.SpotifyRepository)4 SpotifyAuthenticationService (se.zinokader.spotiq.service.authentication.SpotifyAuthenticationService)4 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 SpotifyService (kaaes.spotify.webapi.android.SpotifyService)2