mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 17:43:32 +00:00
Added artist's random-song playing
This commit is contained in:
parent
7adeac1e01
commit
9bc48c7800
3 changed files with 65 additions and 2 deletions
|
|
@ -42,6 +42,9 @@ public interface SongDao {
|
|||
@Query("SELECT * FROM song WHERE artistId = :artistID ORDER BY play_count DESC")
|
||||
LiveData<List<Song>> getArtistTopSongs(String artistID);
|
||||
|
||||
@Query("SELECT * FROM song WHERE artistId = :artistID ORDER BY RANDOM() LIMIT :number")
|
||||
List<Song> getArtistRandomSongs(String artistID, int number);
|
||||
|
||||
@Query("SELECT * FROM song WHERE albumId = :albumID ORDER BY trackNumber ASC")
|
||||
LiveData<List<Song>> getLiveAlbumSong(String albumID);
|
||||
|
||||
|
|
|
|||
|
|
@ -80,6 +80,23 @@ public class SongRepository {
|
|||
return listLiveSampleArtistTopSongs;
|
||||
}
|
||||
|
||||
public List<Song> getArtistListLiveRandomSong(String artistID) {
|
||||
List<Song> songs = new ArrayList<>();
|
||||
|
||||
GetRandomSongsByArtistIDThreadSafe randomArtistSongThread = new GetRandomSongsByArtistIDThreadSafe(songDao, artistID, 100);
|
||||
Thread thread = new Thread(randomArtistSongThread);
|
||||
thread.start();
|
||||
|
||||
try {
|
||||
thread.join();
|
||||
songs = randomArtistSongThread.getSongs();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return songs;
|
||||
}
|
||||
|
||||
public LiveData<List<Song>> getAlbumListLiveSong(String albumID) {
|
||||
listLiveAlbumSongs = songDao.getLiveAlbumSong(albumID);
|
||||
return listLiveAlbumSongs;
|
||||
|
|
@ -310,6 +327,28 @@ public class SongRepository {
|
|||
}
|
||||
}
|
||||
|
||||
private static class GetRandomSongsByArtistIDThreadSafe implements Runnable {
|
||||
private SongDao songDao;
|
||||
private String artistID;
|
||||
private int limit;
|
||||
private List<Song> songs = new ArrayList<>();
|
||||
|
||||
public GetRandomSongsByArtistIDThreadSafe(SongDao songDao, String artistID, int limit) {
|
||||
this.songDao = songDao;
|
||||
this.artistID = artistID;
|
||||
this.limit = limit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
songs = songDao.getArtistRandomSongs(artistID, limit);
|
||||
}
|
||||
|
||||
public List<Song> getSongs() {
|
||||
return songs;
|
||||
}
|
||||
}
|
||||
|
||||
private static class ExistThreadSafe implements Runnable {
|
||||
private SongDao songDao;
|
||||
private Song song;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.cappielloantonio.play.ui.fragment.bottomsheetdialog;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
|
@ -11,16 +12,25 @@ import android.widget.Toast;
|
|||
import androidx.annotation.Nullable;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import com.cappielloantonio.play.App;
|
||||
import com.cappielloantonio.play.R;
|
||||
import com.cappielloantonio.play.glide.CustomGlideRequest;
|
||||
import com.cappielloantonio.play.helper.MusicPlayerRemote;
|
||||
import com.cappielloantonio.play.model.Artist;
|
||||
import com.cappielloantonio.play.model.Song;
|
||||
import com.cappielloantonio.play.repository.QueueRepository;
|
||||
import com.cappielloantonio.play.repository.SongRepository;
|
||||
import com.cappielloantonio.play.ui.activities.MainActivity;
|
||||
import com.cappielloantonio.play.viewmodel.ArtistBottomSheetViewModel;
|
||||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ArtistBottomSheetDialog extends BottomSheetDialogFragment implements View.OnClickListener {
|
||||
private static final String TAG = "AlbumBottomSheetDialog";
|
||||
|
||||
private ArtistBottomSheetViewModel artistBottomSheetViewModel;
|
||||
private SongRepository songRepository;
|
||||
private Artist artist;
|
||||
|
||||
private ImageView coverArtist;
|
||||
|
|
@ -39,6 +49,8 @@ public class ArtistBottomSheetDialog extends BottomSheetDialogFragment implement
|
|||
artistBottomSheetViewModel = new ViewModelProvider(requireActivity()).get(ArtistBottomSheetViewModel.class);
|
||||
artistBottomSheetViewModel.setArtist(artist);
|
||||
|
||||
songRepository = new SongRepository(App.getInstance());
|
||||
|
||||
init(view);
|
||||
|
||||
return view;
|
||||
|
|
@ -63,8 +75,17 @@ public class ArtistBottomSheetDialog extends BottomSheetDialogFragment implement
|
|||
|
||||
playRandom = view.findViewById(R.id.play_random_text_view);
|
||||
playRandom.setOnClickListener(v -> {
|
||||
Toast.makeText(requireContext(), "Play random", Toast.LENGTH_SHORT).show();
|
||||
dismissBottomSheet();
|
||||
List<Song> songs = songRepository.getArtistListLiveRandomSong(artist.getId());
|
||||
|
||||
if(songs.size() > 0) {
|
||||
QueueRepository queueRepository = new QueueRepository(App.getInstance());
|
||||
queueRepository.insertAllAndStartNew(songs);
|
||||
|
||||
MusicPlayerRemote.openQueue(songs, 0, true);
|
||||
((MainActivity) requireActivity()).isBottomSheetInPeek(true);
|
||||
dismissBottomSheet();
|
||||
}
|
||||
else Toast.makeText(requireContext(), "Error retrieving artist's songs", Toast.LENGTH_SHORT).show();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue