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

@ -8,34 +8,19 @@ import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity(tableName = "genre")
public class Genre implements Parcelable {
@NonNull
@PrimaryKey
@ColumnInfo(name = "id")
public String id;
@ColumnInfo(name = "name")
public String name;
@ColumnInfo(name = "songCount")
public int songCount;
public int albumCount;
@ColumnInfo(name = "primary")
public String primary;
@ColumnInfo(name = "blurHash")
public String blurHash;
public Genre(@NonNull String id, String name, int songCount, String primary, String blurHash) {
this.id = id;
this.name = name;
this.songCount = songCount;
this.primary = primary;
this.blurHash = blurHash;
public Genre(com.cappielloantonio.play.subsonic.models.Genre genre) {
this.id = genre.getGenre();
this.name = genre.getGenre();
this.songCount = genre.getSongCount();
this.albumCount = genre.getAlbumCount();
}
@NonNull
public String getId() {
return id;
}
@ -48,22 +33,6 @@ public class Genre implements Parcelable {
return songCount;
}
public String getPrimary() {
return primary;
}
public void setPrimary(String primary) {
this.primary = primary;
}
public String getBlurHash() {
return blurHash;
}
public void setBlurHash(String blurHash) {
this.blurHash = blurHash;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

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;
}
}

View file

@ -28,6 +28,7 @@ public class SubsonicResponse {
private NewestPodcasts newestPodcasts;
private Podcasts podcasts;
private Lyrics lyrics;
@Element(name = "songsByGenre")
private Songs songsByGenre;
@Element(name = "randomSongs")
private Songs randomSongs;

View file

@ -125,16 +125,6 @@ public class SongListPageFragment extends Fragment {
private void initButtons() {
songListPageViewModel.getSongList().observe(requireActivity(), songs -> {
if(bind != null) {
bind.songListPlayImageView.setOnClickListener(v -> {
QueueRepository queueRepository = new QueueRepository(App.getInstance());
queueRepository.insertAllAndStartNew(songs);
activity.isBottomSheetInPeek(true);
activity.setBottomSheetMusicInfo(songs.get(0));
MusicPlayerRemote.openQueue(songs, 0, true);
});
bind.songListShuffleImageView.setOnClickListener(v -> {
Collections.shuffle(songs);

View file

@ -12,6 +12,7 @@ import com.cappielloantonio.play.subsonic.models.ArtistID3;
import com.cappielloantonio.play.subsonic.models.ArtistInfo2;
import com.cappielloantonio.play.subsonic.models.ArtistWithAlbumsID3;
import com.cappielloantonio.play.subsonic.models.Child;
import com.cappielloantonio.play.subsonic.models.Genre;
import java.util.ArrayList;
import java.util.List;
@ -106,4 +107,14 @@ public class MappingUtil {
public static Download mapToDownload(Song song) {
return new Download(song);
}
public static ArrayList<com.cappielloantonio.play.model.Genre> mapGenre(List<Genre> genreList) {
ArrayList<com.cappielloantonio.play.model.Genre> genres = new ArrayList();
for (Genre genre : genreList) {
genres.add(new com.cappielloantonio.play.model.Genre(genre));
}
return genres;
}
}

View file

@ -26,7 +26,7 @@ public class FilterViewModel extends AndroidViewModel {
}
public LiveData<List<Genre>> getGenreList() {
// allGenres = genreRepository.getListLiveGenres();
allGenres = genreRepository.getGenres(false, -1);
return allGenres;
}

View file

@ -23,7 +23,6 @@ public class GenreCatalogueViewModel extends AndroidViewModel {
}
public LiveData<List<Genre>> getGenreList() {
// genreList = genreRepository.getListLiveGenres();
return genreList;
return genreRepository.getGenres(false, -1);
}
}

View file

@ -40,14 +40,9 @@ public class LibraryViewModel extends AndroidViewModel {
// Inizializzate all'interno del costruttore, in modo da rimanere immutabili per tutto il
// ciclo di vita dell'applicazione
// sampleAlbum = albumRepository.getListLiveSampleAlbum();
// sampleArtist = artistRepository.getListLiveSampleArtist();
// sampleGenres = genreRepository.getListLiveSampleGenre();
// playlistSample = playlistRepository.getRandomSample(5);
sampleAlbum = albumRepository.getAlbums("random", 20);
sampleArtist = artistRepository.getArtists(true, 20);
sampleGenres = new MutableLiveData<>(new ArrayList<>());
sampleGenres = genreRepository.getGenres(true, 15);
playlistSample = playlistRepository.getPlaylists(true, 10);
}

View file

@ -58,13 +58,13 @@ public class SongListPageViewModel extends AndroidViewModel {
// songList = songRepository.getListLiveRecentlyAddedSampleSong(100);
break;
case Song.BY_GENRE:
// songList = songRepository.getListLiveSongByGenre(genre.getId());
songList = songList = songRepository.getSongsByGenre(genre.getId());
break;
case Song.BY_ARTIST:
// songList = songRepository.getArtistListLiveTopSong(artist.getId());
break;
case Song.BY_GENRES:
// songList = songRepository.getFilteredListLiveSong(filters);
songList = songRepository.getSongsByGenres(filters);
break;
case Song.BY_YEAR:
// songList = songRepository.getSongByYearListLive(year);

View file

@ -45,20 +45,6 @@
app:layout_constraintEnd_toStartOf="@+id/song_list_shuffle_image_view"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/song_list_play_image_view"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="24dp"
android:background="@drawable/ic_play"
android:backgroundTint="@color/bottomNavIconColor"
android:foreground="?android:attr/selectableItemBackgroundBorderless"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<ImageView
android:id="@+id/song_list_shuffle_image_view"
android:layout_width="24dp"
@ -71,7 +57,7 @@
android:backgroundTint="@color/bottomNavIconColor"
android:foreground="?android:attr/selectableItemBackgroundBorderless"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/song_list_play_image_view" />
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>