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");
})));
}
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());
}
}));
}
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");
})));
}
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();
});
});
}
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();
});
});
}
Aggregations