Made the due distinction based on the server on the most listened to songs of the week

This commit is contained in:
antonio 2023-01-06 18:30:07 +01:00
parent 5eed437c5b
commit 1204716a65
6 changed files with 29 additions and 14 deletions

View file

@ -23,13 +23,11 @@ import com.cappielloantonio.play.model.Server;
@SuppressLint("RestrictedApi")
@Database(
version = 42,
version = 43,
entities = {Queue.class, Server.class, RecentSearch.class, Download.class, Playlist.class, Chronology.class},
autoMigrations = {@AutoMigration(from = 41, to = 42)}
autoMigrations = {@AutoMigration(from = 42, to = 43)}
)
public abstract class AppDatabase extends RoomDatabase {
private static final String TAG = "AppDatabase";
private final static String DB_NAME = "play_db";
private static AppDatabase instance;

View file

@ -12,8 +12,8 @@ import java.util.List;
@Dao
public interface ChronologyDao {
@Query("SELECT * FROM chronology WHERE timestamp >= :startDate AND timestamp < :endDate GROUP BY id ORDER BY COUNT(id) DESC LIMIT 9")
LiveData<List<Chronology>> getAllFrom(long startDate, long endDate);
@Query("SELECT * FROM chronology WHERE timestamp >= :startDate AND timestamp < :endDate AND server == :server GROUP BY id ORDER BY COUNT(id) DESC LIMIT 9")
LiveData<List<Chronology>> getAllFrom(long startDate, long endDate, String server);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(Chronology chronologyObject);

View file

@ -49,7 +49,10 @@ public class Chronology implements Parcelable {
@ColumnInfo(name = "timestamp")
private Long timestamp;
public Chronology(String trackId, String title, String albumId, String albumName, String artistId, String artistName, String coverArtId, long duration, String container, int bitrate, String extension) {
@ColumnInfo(name = "server")
private String server;
public Chronology(String trackId, String title, String albumId, String albumName, String artistId, String artistName, String coverArtId, long duration, String container, int bitrate, String extension, String server) {
this.trackId = trackId;
this.title = title;
this.albumId = albumId;
@ -62,6 +65,7 @@ public class Chronology implements Parcelable {
this.bitrate = bitrate;
this.extension = extension;
this.timestamp = System.currentTimeMillis();
this.server = server;
}
public int getUuid() {
@ -168,6 +172,14 @@ public class Chronology implements Parcelable {
this.timestamp = timestamp;
}
public String getServer() {
return server;
}
public void setServer(String server) {
this.server = server;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
@ -207,6 +219,7 @@ public class Chronology implements Parcelable {
dest.writeInt(this.bitrate);
dest.writeString(this.extension);
dest.writeLong(this.timestamp);
dest.writeString(this.server);
}
protected Chronology(Parcel in) {
@ -222,6 +235,7 @@ public class Chronology implements Parcelable {
this.bitrate = in.readInt();
this.extension = in.readString();
this.timestamp = in.readLong();
this.server = in.readString();
}
public static final Creator<Chronology> CREATOR = new Creator<Chronology>() {

View file

@ -21,7 +21,7 @@ public class ChronologyRepository {
chronologyDao = database.chronologyDao();
}
public LiveData<List<Chronology>> getThisWeek() {
public LiveData<List<Chronology>> getThisWeek(String server) {
Calendar calendar = Calendar.getInstance();
Calendar first = (Calendar) calendar.clone();
@ -30,10 +30,10 @@ public class ChronologyRepository {
Calendar last = (Calendar) first.clone();
last.add(Calendar.DAY_OF_YEAR, 6);
return chronologyDao.getAllFrom(first.getTime().getTime(), last.getTime().getTime());
return chronologyDao.getAllFrom(first.getTime().getTime(), last.getTime().getTime(), server);
}
public LiveData<List<Chronology>> getLastWeek() {
public LiveData<List<Chronology>> getLastWeek(String server) {
Calendar calendar = Calendar.getInstance();
Calendar first = (Calendar) calendar.clone();
@ -42,7 +42,7 @@ public class ChronologyRepository {
Calendar last = (Calendar) first.clone();
last.add(Calendar.DAY_OF_YEAR, 6);
return chronologyDao.getAllFrom(first.getTime().getTime(), last.getTime().getTime());
return chronologyDao.getAllFrom(first.getTime().getTime(), last.getTime().getTime(), server);
}
public void insert(Chronology item) {

View file

@ -217,6 +217,7 @@ public class MappingUtil {
bundle.putString("container", media.getContainer());
bundle.putInt("bitrate", media.getBitrate());
bundle.putString("extension", media.getExtension());
bundle.putString("server", PreferenceUtil.getInstance(context).getServerId());
return new MediaItem.Builder()
.setMediaId(media.getId())
@ -283,7 +284,8 @@ public class MappingUtil {
(long) item.mediaMetadata.extras.get("duration"),
item.mediaMetadata.extras.get("container").toString(),
(int) item.mediaMetadata.extras.get("bitrate"),
item.mediaMetadata.extras.get("extension").toString()
item.mediaMetadata.extras.get("extension").toString(),
item.mediaMetadata.extras.get("server").toString()
);
}

View file

@ -80,12 +80,13 @@ public class HomeViewModel extends AndroidViewModel {
public LiveData<List<Chronology>> getGridSongSample(LifecycleOwner owner) {
Calendar cal = Calendar.getInstance();
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
String server = PreferenceUtil.getInstance(App.getInstance()).getServerId();
if (thisGridTopSong.getValue() == null) {
if (dayOfMonth >= 7) {
chronologyRepository.getThisWeek().observe(owner, thisGridTopSong::postValue);
chronologyRepository.getThisWeek(server).observe(owner, thisGridTopSong::postValue);
} else {
chronologyRepository.getLastWeek().observe(owner, thisGridTopSong::postValue);
chronologyRepository.getLastWeek(server).observe(owner, thisGridTopSong::postValue);
}
}