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")
|
@Query("SELECT * FROM song WHERE artistId = :artistID ORDER BY play_count DESC")
|
||||||
LiveData<List<Song>> getArtistTopSongs(String artistID);
|
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")
|
@Query("SELECT * FROM song WHERE albumId = :albumID ORDER BY trackNumber ASC")
|
||||||
LiveData<List<Song>> getLiveAlbumSong(String albumID);
|
LiveData<List<Song>> getLiveAlbumSong(String albumID);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -80,6 +80,23 @@ public class SongRepository {
|
||||||
return listLiveSampleArtistTopSongs;
|
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) {
|
public LiveData<List<Song>> getAlbumListLiveSong(String albumID) {
|
||||||
listLiveAlbumSongs = songDao.getLiveAlbumSong(albumID);
|
listLiveAlbumSongs = songDao.getLiveAlbumSong(albumID);
|
||||||
return listLiveAlbumSongs;
|
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 static class ExistThreadSafe implements Runnable {
|
||||||
private SongDao songDao;
|
private SongDao songDao;
|
||||||
private Song song;
|
private Song song;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package com.cappielloantonio.play.ui.fragment.bottomsheetdialog;
|
package com.cappielloantonio.play.ui.fragment.bottomsheetdialog;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
@ -11,16 +12,25 @@ import android.widget.Toast;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
|
|
||||||
|
import com.cappielloantonio.play.App;
|
||||||
import com.cappielloantonio.play.R;
|
import com.cappielloantonio.play.R;
|
||||||
import com.cappielloantonio.play.glide.CustomGlideRequest;
|
import com.cappielloantonio.play.glide.CustomGlideRequest;
|
||||||
|
import com.cappielloantonio.play.helper.MusicPlayerRemote;
|
||||||
import com.cappielloantonio.play.model.Artist;
|
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.cappielloantonio.play.viewmodel.ArtistBottomSheetViewModel;
|
||||||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
|
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class ArtistBottomSheetDialog extends BottomSheetDialogFragment implements View.OnClickListener {
|
public class ArtistBottomSheetDialog extends BottomSheetDialogFragment implements View.OnClickListener {
|
||||||
private static final String TAG = "AlbumBottomSheetDialog";
|
private static final String TAG = "AlbumBottomSheetDialog";
|
||||||
|
|
||||||
private ArtistBottomSheetViewModel artistBottomSheetViewModel;
|
private ArtistBottomSheetViewModel artistBottomSheetViewModel;
|
||||||
|
private SongRepository songRepository;
|
||||||
private Artist artist;
|
private Artist artist;
|
||||||
|
|
||||||
private ImageView coverArtist;
|
private ImageView coverArtist;
|
||||||
|
|
@ -39,6 +49,8 @@ public class ArtistBottomSheetDialog extends BottomSheetDialogFragment implement
|
||||||
artistBottomSheetViewModel = new ViewModelProvider(requireActivity()).get(ArtistBottomSheetViewModel.class);
|
artistBottomSheetViewModel = new ViewModelProvider(requireActivity()).get(ArtistBottomSheetViewModel.class);
|
||||||
artistBottomSheetViewModel.setArtist(artist);
|
artistBottomSheetViewModel.setArtist(artist);
|
||||||
|
|
||||||
|
songRepository = new SongRepository(App.getInstance());
|
||||||
|
|
||||||
init(view);
|
init(view);
|
||||||
|
|
||||||
return view;
|
return view;
|
||||||
|
|
@ -63,8 +75,17 @@ public class ArtistBottomSheetDialog extends BottomSheetDialogFragment implement
|
||||||
|
|
||||||
playRandom = view.findViewById(R.id.play_random_text_view);
|
playRandom = view.findViewById(R.id.play_random_text_view);
|
||||||
playRandom.setOnClickListener(v -> {
|
playRandom.setOnClickListener(v -> {
|
||||||
Toast.makeText(requireContext(), "Play random", Toast.LENGTH_SHORT).show();
|
List<Song> songs = songRepository.getArtistListLiveRandomSong(artist.getId());
|
||||||
dismissBottomSheet();
|
|
||||||
|
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