Fix genre retrieval and filtering

This commit is contained in:
CappielloAntonio 2021-07-31 18:43:40 +02:00
parent 37e45e7957
commit fd4250b6f7
11 changed files with 139 additions and 73 deletions

View file

@ -2,7 +2,62 @@ package com.cappielloantonio.play.repository;
import android.app.Application;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.App;
import com.cappielloantonio.play.model.Genre;
import com.cappielloantonio.play.subsonic.models.ResponseStatus;
import com.cappielloantonio.play.subsonic.models.SubsonicResponse;
import com.cappielloantonio.play.util.MappingUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class GenreRepository {
private static final String TAG = "GenreRepository";
private Application application;
private MutableLiveData<List<Genre>> genres = new MutableLiveData<>();
public GenreRepository(Application application) {
this.application = application;
}
public MutableLiveData<List<Genre>> getGenres(boolean random, int size) {
App.getSubsonicClientInstance(application, false)
.getBrowsingClient()
.getGenres()
.enqueue(new Callback<SubsonicResponse>() {
@Override
public void onResponse(Call<SubsonicResponse> call, Response<SubsonicResponse> response) {
if (response.body().getStatus().getValue().equals(ResponseStatus.OK)) {
List<Genre> genreList = new ArrayList<>(MappingUtil.mapGenre(response.body().getGenres().getGenres()));
if(random) {
Collections.shuffle(genreList);
}
if(size != -1) {
genres.setValue(genreList.subList(0, Math.min(size, genreList.size())));
}
else {
genres.setValue(genreList);
}
}
}
@Override
public void onFailure(Call<SubsonicResponse> call, Throwable t) {
}
});
return genres;
}
}

View file

@ -12,6 +12,8 @@ import com.cappielloantonio.play.subsonic.models.SubsonicResponse;
import com.cappielloantonio.play.util.MappingUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import retrofit2.Call;
@ -168,4 +170,62 @@ public class SongRepository {
}
});
}
public MutableLiveData<List<Song>> getSongsByGenre(String id) {
MutableLiveData<List<Song>> songsByGenre = new MutableLiveData<>();
App.getSubsonicClientInstance(application, false)
.getAlbumSongListClient()
.getSongsByGenre(id, 500, 0)
.enqueue(new Callback<SubsonicResponse>() {
@Override
public void onResponse(Call<SubsonicResponse> call, Response<SubsonicResponse> response) {
if (response.body().getStatus().getValue().equals(ResponseStatus.OK)) {
List<Song> newSongs = new ArrayList<>(MappingUtil.mapSong(response.body().getSongsByGenre().getSongs()));
List<Song> songs = songsByGenre.getValue();
songs.addAll(newSongs);
Collections.shuffle(songs);
LinkedHashSet<Song> hashSet = new LinkedHashSet<>(songs);
ArrayList<Song> songsWithoutDuplicates = new ArrayList<>(hashSet);
songsByGenre.setValue(songsWithoutDuplicates);
}
}
@Override
public void onFailure(Call<SubsonicResponse> call, Throwable t) {
}
});
return songsByGenre;
}
public MutableLiveData<List<Song>> getSongsByGenres(ArrayList<String> genresId) {
MutableLiveData<List<Song>> songsByGenre = new MutableLiveData<>();
for(String id: genresId)
App.getSubsonicClientInstance(application, false)
.getAlbumSongListClient()
.getSongsByGenre(id, 500, 0)
.enqueue(new Callback<SubsonicResponse>() {
@Override
public void onResponse(Call<SubsonicResponse> call, Response<SubsonicResponse> response) {
if (response.body().getStatus().getValue().equals(ResponseStatus.OK)) {
List<Song> songs = new ArrayList<>(MappingUtil.mapSong(response.body().getSongsByGenre().getSongs()));
songsByGenre.setValue(songs);
}
}
@Override
public void onFailure(Call<SubsonicResponse> call, Throwable t) {
}
});
return songsByGenre;
}
}