mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 17:43:32 +00:00
Fix instant mix overriding added value in songs
This commit is contained in:
parent
7576fbb75b
commit
308ddf6c7c
9 changed files with 124 additions and 75 deletions
|
|
@ -93,15 +93,15 @@ public class DiscoverSongAdapter extends RecyclerView.Adapter<DiscoverSongAdapte
|
||||||
@Override
|
@Override
|
||||||
public void onLoadMedia(List<?> media) {
|
public void onLoadMedia(List<?> media) {
|
||||||
QueueRepository queueRepository = new QueueRepository(App.getInstance());
|
QueueRepository queueRepository = new QueueRepository(App.getInstance());
|
||||||
queueRepository.insertAllAndStartNew((ArrayList<Song>) media);
|
List<Song> mix = queueRepository.insertMix((ArrayList<Song>) media);
|
||||||
|
|
||||||
activity.isBottomSheetInPeek(true);
|
activity.isBottomSheetInPeek(true);
|
||||||
activity.setBottomSheetMusicInfo(((ArrayList<Song>) media).get(0));
|
activity.setBottomSheetMusicInfo(mix.get(0));
|
||||||
|
|
||||||
PlayerBottomSheetViewModel playerBottomSheetViewModel = new ViewModelProvider(activity).get(PlayerBottomSheetViewModel.class);
|
PlayerBottomSheetViewModel playerBottomSheetViewModel = new ViewModelProvider(activity).get(PlayerBottomSheetViewModel.class);
|
||||||
playerBottomSheetViewModel.setNowPlayingSong(((ArrayList<Song>) media).get(0));
|
playerBottomSheetViewModel.setNowPlayingSong(mix.get(0));
|
||||||
|
|
||||||
MusicPlayerRemote.openQueue((ArrayList<Song>) media, 0, true);
|
MusicPlayerRemote.openQueue(mix, 0, true);
|
||||||
}
|
}
|
||||||
}, SyncUtil.SONG, songs.get(getBindingAdapterPosition()).getId(), 50);
|
}, SyncUtil.SONG, songs.get(getBindingAdapterPosition()).getId(), 50);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,16 @@ public interface SongDao {
|
||||||
void deleteAll();
|
void deleteAll();
|
||||||
|
|
||||||
@Update
|
@Update
|
||||||
public void update(Song song);
|
void update(Song song);
|
||||||
|
|
||||||
|
@Query("UPDATE song SET play_count = :playCount AND last_play = :lastPlay WHERE id = :id")
|
||||||
|
void updatePlayCount(String id, int playCount, long lastPlay);
|
||||||
|
|
||||||
|
@Query("UPDATE song SET favorite = :isFavorite WHERE id = :id")
|
||||||
|
void updateFavorite(String id, boolean isFavorite);
|
||||||
|
|
||||||
|
@Query("SELECT * FROM song WHERE id IN (:ids)")
|
||||||
|
List<Song> getSongsByID(List<String> ids);
|
||||||
|
|
||||||
@Query("SELECT * FROM song ORDER BY RANDOM() LIMIT :number")
|
@Query("SELECT * FROM song ORDER BY RANDOM() LIMIT :number")
|
||||||
List<Song> random(int number);
|
List<Song> random(int number);
|
||||||
|
|
|
||||||
|
|
@ -230,13 +230,6 @@ public class Song implements Parcelable {
|
||||||
this.lastPlay = 0;
|
this.lastPlay = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Ignore
|
|
||||||
public Song(String title, String albumName) {
|
|
||||||
this.id = UUID.randomUUID().toString();
|
|
||||||
this.title = title;
|
|
||||||
this.albumName = albumName;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public String getId() {
|
public String getId() {
|
||||||
return id;
|
return id;
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import androidx.lifecycle.LiveData;
|
||||||
|
|
||||||
import com.cappielloantonio.play.database.AppDatabase;
|
import com.cappielloantonio.play.database.AppDatabase;
|
||||||
import com.cappielloantonio.play.database.dao.QueueDao;
|
import com.cappielloantonio.play.database.dao.QueueDao;
|
||||||
|
import com.cappielloantonio.play.database.dao.SongDao;
|
||||||
import com.cappielloantonio.play.model.Queue;
|
import com.cappielloantonio.play.model.Queue;
|
||||||
import com.cappielloantonio.play.model.Song;
|
import com.cappielloantonio.play.model.Song;
|
||||||
import com.cappielloantonio.play.util.QueueUtil;
|
import com.cappielloantonio.play.util.QueueUtil;
|
||||||
|
|
@ -17,11 +18,13 @@ import java.util.List;
|
||||||
public class QueueRepository {
|
public class QueueRepository {
|
||||||
private static final String TAG = "QueueRepository";
|
private static final String TAG = "QueueRepository";
|
||||||
|
|
||||||
|
private SongDao songDao;
|
||||||
private QueueDao queueDao;
|
private QueueDao queueDao;
|
||||||
private LiveData<List<Song>> listLiveQueue;
|
private LiveData<List<Song>> listLiveQueue;
|
||||||
|
|
||||||
public QueueRepository(Application application) {
|
public QueueRepository(Application application) {
|
||||||
AppDatabase database = AppDatabase.getInstance(application);
|
AppDatabase database = AppDatabase.getInstance(application);
|
||||||
|
songDao = database.songDao();
|
||||||
queueDao = database.queueDao();
|
queueDao = database.queueDao();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -59,6 +62,26 @@ public class QueueRepository {
|
||||||
thread.start();
|
thread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Song> insertMix(ArrayList<Song> media) {
|
||||||
|
List<String> IDs = QueueUtil.getIDsFromSongs(media);
|
||||||
|
List<Song> mix = new ArrayList<>();
|
||||||
|
|
||||||
|
GetSongsByIDThreadSafe getSongsByIDThreadSafe = new GetSongsByIDThreadSafe(songDao, IDs);
|
||||||
|
Thread thread = new Thread(getSongsByIDThreadSafe);
|
||||||
|
thread.start();
|
||||||
|
|
||||||
|
try {
|
||||||
|
thread.join();
|
||||||
|
mix = QueueUtil.orderSongByIdList(IDs, getSongsByIDThreadSafe.getSongs());
|
||||||
|
|
||||||
|
insertAllAndStartNew(mix);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return mix;
|
||||||
|
}
|
||||||
|
|
||||||
public void insertAllAndStartNew(List<Song> songs) {
|
public void insertAllAndStartNew(List<Song> songs) {
|
||||||
try {
|
try {
|
||||||
final Thread delete = new Thread(new DeleteAllThreadSafe(queueDao));
|
final Thread delete = new Thread(new DeleteAllThreadSafe(queueDao));
|
||||||
|
|
@ -218,4 +241,24 @@ public class QueueRepository {
|
||||||
return songs;
|
return songs;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class GetSongsByIDThreadSafe implements Runnable {
|
||||||
|
private SongDao songDao;
|
||||||
|
private List<String> IDs;
|
||||||
|
private List<Song> songs;
|
||||||
|
|
||||||
|
public GetSongsByIDThreadSafe(SongDao songDao, List<String> IDs) {
|
||||||
|
this.songDao = songDao;
|
||||||
|
this.IDs = IDs;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
songs = songDao.getSongsByID(IDs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Song> getSongs() {
|
||||||
|
return songs;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -46,11 +46,6 @@ public class SongRepository {
|
||||||
return searchListLiveSongs;
|
return searchListLiveSongs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LiveData<Song> getLiveDataSong(String id) {
|
|
||||||
liveDataSong = songDao.getOne(id);
|
|
||||||
return liveDataSong;
|
|
||||||
}
|
|
||||||
|
|
||||||
public LiveData<List<Song>> getListLiveRecentlyAddedSampleSong(int number) {
|
public LiveData<List<Song>> getListLiveRecentlyAddedSampleSong(int number) {
|
||||||
listLiveSampleRecentlyAddedSongs = songDao.getRecentlyAddedSample(number);
|
listLiveSampleRecentlyAddedSongs = songDao.getRecentlyAddedSample(number);
|
||||||
return listLiveSampleRecentlyAddedSongs;
|
return listLiveSampleRecentlyAddedSongs;
|
||||||
|
|
@ -240,9 +235,11 @@ public class SongRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void increasePlayCount(Song song) {
|
public void increasePlayCount(Song song) {
|
||||||
|
Log.i(TAG, "increasePlayCount: " + song.getId());
|
||||||
boolean isIncreased = song.nowPlaying();
|
boolean isIncreased = song.nowPlaying();
|
||||||
|
|
||||||
if(isIncreased) {
|
if(isIncreased) {
|
||||||
|
// UpdatePlayCountThreadSafe update = new UpdatePlayCountThreadSafe(songDao, song);
|
||||||
UpdateThreadSafe update = new UpdateThreadSafe(songDao, song);
|
UpdateThreadSafe update = new UpdateThreadSafe(songDao, song);
|
||||||
Thread thread = new Thread(update);
|
Thread thread = new Thread(update);
|
||||||
thread.start();
|
thread.start();
|
||||||
|
|
@ -250,6 +247,7 @@ public class SongRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFavoriteStatus(Song song) {
|
public void setFavoriteStatus(Song song) {
|
||||||
|
// UpdateFavoriteThreadSafe update = new UpdateFavoriteThreadSafe(songDao, song);
|
||||||
UpdateThreadSafe update = new UpdateThreadSafe(songDao, song);
|
UpdateThreadSafe update = new UpdateThreadSafe(songDao, song);
|
||||||
Thread thread = new Thread(update);
|
Thread thread = new Thread(update);
|
||||||
thread.start();
|
thread.start();
|
||||||
|
|
@ -296,23 +294,6 @@ public class SongRepository {
|
||||||
return sample;
|
return sample;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Song getSongByID(String id) {
|
|
||||||
Song song = null;
|
|
||||||
|
|
||||||
GetSongByIDThreadSafe songByID = new GetSongByIDThreadSafe(songDao, id);
|
|
||||||
Thread thread = new Thread(songByID);
|
|
||||||
thread.start();
|
|
||||||
|
|
||||||
try {
|
|
||||||
thread.join();
|
|
||||||
song = songByID.getSong();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return song;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class GetSongsByAlbumIDThreadSafe implements Runnable {
|
private static class GetSongsByAlbumIDThreadSafe implements Runnable {
|
||||||
private SongDao songDao;
|
private SongDao songDao;
|
||||||
private String albumID;
|
private String albumID;
|
||||||
|
|
@ -439,6 +420,36 @@ public class SongRepository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class UpdatePlayCountThreadSafe implements Runnable {
|
||||||
|
private SongDao songDao;
|
||||||
|
private Song song;
|
||||||
|
|
||||||
|
public UpdatePlayCountThreadSafe(SongDao songDao, Song song) {
|
||||||
|
this.songDao = songDao;
|
||||||
|
this.song = song;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
songDao.updatePlayCount(song.getId(), song.getPlayCount(), song.getLastPlay());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class UpdateFavoriteThreadSafe implements Runnable {
|
||||||
|
private SongDao songDao;
|
||||||
|
private Song song;
|
||||||
|
|
||||||
|
public UpdateFavoriteThreadSafe(SongDao songDao, Song song) {
|
||||||
|
this.songDao = songDao;
|
||||||
|
this.song = song;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
songDao.updateFavorite(song.getId(), song.isFavorite());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static class PickRandomThreadSafe implements Runnable {
|
private static class PickRandomThreadSafe implements Runnable {
|
||||||
private SongDao songDao;
|
private SongDao songDao;
|
||||||
private int elementNumber;
|
private int elementNumber;
|
||||||
|
|
@ -568,24 +579,4 @@ public class SongRepository {
|
||||||
return decades;
|
return decades;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class GetSongByIDThreadSafe implements Runnable {
|
|
||||||
private SongDao songDao;
|
|
||||||
private String id;
|
|
||||||
private Song song;
|
|
||||||
|
|
||||||
public GetSongByIDThreadSafe(SongDao songDao, String id) {
|
|
||||||
this.songDao = songDao;
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
song = songDao.getSongByID(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Song getSong() {
|
|
||||||
return song;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -95,15 +95,15 @@ public class AlbumBottomSheetDialog extends BottomSheetDialogFragment implements
|
||||||
@Override
|
@Override
|
||||||
public void onLoadMedia(List<?> media) {
|
public void onLoadMedia(List<?> media) {
|
||||||
QueueRepository queueRepository = new QueueRepository(App.getInstance());
|
QueueRepository queueRepository = new QueueRepository(App.getInstance());
|
||||||
queueRepository.insertAllAndStartNew((ArrayList<Song>) media);
|
List<Song> mix = queueRepository.insertMix((ArrayList<Song>) media);
|
||||||
|
|
||||||
activity.isBottomSheetInPeek(true);
|
activity.isBottomSheetInPeek(true);
|
||||||
activity.setBottomSheetMusicInfo(((ArrayList<Song>) media).get(0));
|
activity.setBottomSheetMusicInfo(mix.get(0));
|
||||||
|
|
||||||
PlayerBottomSheetViewModel playerBottomSheetViewModel = new ViewModelProvider(activity).get(PlayerBottomSheetViewModel.class);
|
PlayerBottomSheetViewModel playerBottomSheetViewModel = new ViewModelProvider(activity).get(PlayerBottomSheetViewModel.class);
|
||||||
playerBottomSheetViewModel.setNowPlayingSong(((ArrayList<Song>) media).get(0));
|
playerBottomSheetViewModel.setNowPlayingSong(mix.get(0));
|
||||||
|
|
||||||
MusicPlayerRemote.openQueue((ArrayList<Song>) media, 0, true);
|
MusicPlayerRemote.openQueue(mix, 0, true);
|
||||||
}
|
}
|
||||||
}, SyncUtil.SONG, album.getId(), 50);
|
}, SyncUtil.SONG, album.getId(), 50);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -84,15 +84,15 @@ public class ArtistBottomSheetDialog extends BottomSheetDialogFragment implement
|
||||||
@Override
|
@Override
|
||||||
public void onLoadMedia(List<?> media) {
|
public void onLoadMedia(List<?> media) {
|
||||||
QueueRepository queueRepository = new QueueRepository(App.getInstance());
|
QueueRepository queueRepository = new QueueRepository(App.getInstance());
|
||||||
queueRepository.insertAllAndStartNew((ArrayList<Song>) media);
|
List<Song> mix = queueRepository.insertMix((ArrayList<Song>) media);
|
||||||
|
|
||||||
activity.isBottomSheetInPeek(true);
|
activity.isBottomSheetInPeek(true);
|
||||||
activity.setBottomSheetMusicInfo(((ArrayList<Song>) media).get(0));
|
activity.setBottomSheetMusicInfo(mix.get(0));
|
||||||
|
|
||||||
PlayerBottomSheetViewModel playerBottomSheetViewModel = new ViewModelProvider(activity).get(PlayerBottomSheetViewModel.class);
|
PlayerBottomSheetViewModel playerBottomSheetViewModel = new ViewModelProvider(activity).get(PlayerBottomSheetViewModel.class);
|
||||||
playerBottomSheetViewModel.setNowPlayingSong(((ArrayList<Song>) media).get(0));
|
playerBottomSheetViewModel.setNowPlayingSong(mix.get(0));
|
||||||
|
|
||||||
MusicPlayerRemote.openQueue((ArrayList<Song>) media, 0, true);
|
MusicPlayerRemote.openQueue(mix, 0, true);
|
||||||
}
|
}
|
||||||
}, SyncUtil.SONG, artist.getId(), 50);
|
}, SyncUtil.SONG, artist.getId(), 50);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -102,15 +102,15 @@ public class SongBottomSheetDialog extends BottomSheetDialogFragment implements
|
||||||
@Override
|
@Override
|
||||||
public void onLoadMedia(List<?> media) {
|
public void onLoadMedia(List<?> media) {
|
||||||
QueueRepository queueRepository = new QueueRepository(App.getInstance());
|
QueueRepository queueRepository = new QueueRepository(App.getInstance());
|
||||||
queueRepository.insertAllAndStartNew((ArrayList<Song>) media);
|
List<Song> mix = queueRepository.insertMix((ArrayList<Song>) media);
|
||||||
|
|
||||||
activity.isBottomSheetInPeek(true);
|
activity.isBottomSheetInPeek(true);
|
||||||
activity.setBottomSheetMusicInfo(((ArrayList<Song>) media).get(0));
|
activity.setBottomSheetMusicInfo(mix.get(0));
|
||||||
|
|
||||||
PlayerBottomSheetViewModel playerBottomSheetViewModel = new ViewModelProvider(activity).get(PlayerBottomSheetViewModel.class);
|
PlayerBottomSheetViewModel playerBottomSheetViewModel = new ViewModelProvider(activity).get(PlayerBottomSheetViewModel.class);
|
||||||
playerBottomSheetViewModel.setNowPlayingSong(((ArrayList<Song>) media).get(0));
|
playerBottomSheetViewModel.setNowPlayingSong(mix.get(0));
|
||||||
|
|
||||||
MusicPlayerRemote.openQueue((ArrayList<Song>) media, 0, true);
|
MusicPlayerRemote.openQueue(mix, 0, true);
|
||||||
}
|
}
|
||||||
}, SyncUtil.SONG, song.getId(), 50);
|
}, SyncUtil.SONG, song.getId(), 50);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
package com.cappielloantonio.play.util;
|
package com.cappielloantonio.play.util;
|
||||||
|
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import com.cappielloantonio.play.model.Queue;
|
import com.cappielloantonio.play.model.Queue;
|
||||||
import com.cappielloantonio.play.model.Song;
|
import com.cappielloantonio.play.model.Song;
|
||||||
|
|
||||||
|
|
@ -7,9 +9,7 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class QueueUtil {
|
public class QueueUtil {
|
||||||
public static Queue getQueueElementFromSong(Song song) {
|
private static final String TAG = "QueueUtil";
|
||||||
return new Queue(0, song.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Queue getQueueElementFromSong(Song song, int startingPosition) {
|
public static Queue getQueueElementFromSong(Song song, int startingPosition) {
|
||||||
return new Queue(startingPosition, song.getId());
|
return new Queue(startingPosition, song.getId());
|
||||||
|
|
@ -27,15 +27,28 @@ public class QueueUtil {
|
||||||
return queue;
|
return queue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Queue> getQueueElementsFromSongs(List<Song> songs, int startingPosition) {
|
public static List<String> getIDsFromSongs(List<Song> songs) {
|
||||||
int counter = startingPosition;
|
List<String> IDs = new ArrayList<>();
|
||||||
List<Queue> queue = new ArrayList<>();
|
|
||||||
|
|
||||||
for(Song song: songs) {
|
for(Song song: songs) {
|
||||||
queue.add(new Queue(counter, song.getId()));
|
IDs.add(song.getId());
|
||||||
counter++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return queue;
|
return IDs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Song> orderSongByIdList(List<String> IDs, List<Song> songs) {
|
||||||
|
List<Song> orderedSong = new ArrayList<>();
|
||||||
|
|
||||||
|
for(String ID: IDs) {
|
||||||
|
for(Song song: songs) {
|
||||||
|
if(ID.equals(song.getId())) {
|
||||||
|
orderedSong.add(song);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return orderedSong;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue