mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 09:33:33 +00:00
Added song/artist and album/artist synchronization
This commit is contained in:
parent
b577ce7e2e
commit
ef1deea33a
13 changed files with 476 additions and 20 deletions
|
|
@ -6,24 +6,28 @@ import androidx.room.Database;
|
|||
import androidx.room.Room;
|
||||
import androidx.room.RoomDatabase;
|
||||
|
||||
import com.cappielloantonio.play.database.dao.AlbumArtistCrossDao;
|
||||
import com.cappielloantonio.play.database.dao.AlbumDao;
|
||||
import com.cappielloantonio.play.database.dao.ArtistDao;
|
||||
import com.cappielloantonio.play.database.dao.GenreDao;
|
||||
import com.cappielloantonio.play.database.dao.PlaylistDao;
|
||||
import com.cappielloantonio.play.database.dao.QueueDao;
|
||||
import com.cappielloantonio.play.database.dao.RecentSearchDao;
|
||||
import com.cappielloantonio.play.database.dao.SongArtistCrossDao;
|
||||
import com.cappielloantonio.play.database.dao.SongDao;
|
||||
import com.cappielloantonio.play.database.dao.SongGenreCrossDao;
|
||||
import com.cappielloantonio.play.model.Album;
|
||||
import com.cappielloantonio.play.model.AlbumArtistCross;
|
||||
import com.cappielloantonio.play.model.Artist;
|
||||
import com.cappielloantonio.play.model.Genre;
|
||||
import com.cappielloantonio.play.model.Playlist;
|
||||
import com.cappielloantonio.play.model.Queue;
|
||||
import com.cappielloantonio.play.model.RecentSearch;
|
||||
import com.cappielloantonio.play.model.Song;
|
||||
import com.cappielloantonio.play.model.SongArtistCross;
|
||||
import com.cappielloantonio.play.model.SongGenreCross;
|
||||
|
||||
@Database(entities = {Album.class, Artist.class, Genre.class, Playlist.class, Song.class, RecentSearch.class, SongGenreCross.class, Queue.class}, version = 8, exportSchema = false)
|
||||
@Database(entities = {Album.class, Artist.class, Genre.class, Playlist.class, Song.class, RecentSearch.class, SongGenreCross.class, Queue.class, AlbumArtistCross.class, SongArtistCross.class}, version = 10, exportSchema = false)
|
||||
public abstract class AppDatabase extends RoomDatabase {
|
||||
private static final String TAG = "AppDatabase";
|
||||
|
||||
|
|
@ -54,5 +58,9 @@ public abstract class AppDatabase extends RoomDatabase {
|
|||
|
||||
public abstract SongGenreCrossDao songGenreCrossDao();
|
||||
|
||||
public abstract AlbumArtistCrossDao albumArtistCrossDao();
|
||||
|
||||
public abstract SongArtistCrossDao songArtistCrossDao();
|
||||
|
||||
public abstract QueueDao queueDao();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
package com.cappielloantonio.play.database.dao;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Delete;
|
||||
import androidx.room.Insert;
|
||||
import androidx.room.OnConflictStrategy;
|
||||
import androidx.room.Query;
|
||||
import androidx.room.Update;
|
||||
|
||||
import com.cappielloantonio.play.model.AlbumArtistCross;
|
||||
import com.cappielloantonio.play.model.SongGenreCross;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Dao
|
||||
public interface AlbumArtistCrossDao {
|
||||
@Query("SELECT * FROM album_artist_cross")
|
||||
LiveData<List<AlbumArtistCross>> getAll();
|
||||
|
||||
@Query("SELECT EXISTS(SELECT * FROM album_artist_cross WHERE id = :id)")
|
||||
boolean exist(String id);
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
void insert(AlbumArtistCross albumArtistCross);
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
void insertAll(List<AlbumArtistCross> albumArtistCrosses);
|
||||
|
||||
@Delete
|
||||
void delete(AlbumArtistCross albumArtistCross);
|
||||
|
||||
@Update
|
||||
void update(AlbumArtistCross albumArtistCross);
|
||||
|
||||
@Query("DELETE FROM album_artist_cross")
|
||||
void deleteAll();
|
||||
}
|
||||
|
|
@ -16,7 +16,8 @@ public interface AlbumDao {
|
|||
@Query("SELECT * FROM album")
|
||||
LiveData<List<Album>> getAll();
|
||||
|
||||
@Query("SELECT * FROM album WHERE artistId = :artistId;")
|
||||
// @Query("SELECT * FROM album WHERE artistId = :artistId;")
|
||||
@Query("SELECT album.* FROM album INNER JOIN album_artist_cross ON album.id = album_artist_cross.album_id AND album_artist_cross.artist_id = :artistId")
|
||||
LiveData<List<Album>> getArtistAlbums(String artistId);
|
||||
|
||||
@Query("SELECT * FROM album ORDER BY RANDOM() LIMIT :number;")
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
package com.cappielloantonio.play.database.dao;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Delete;
|
||||
import androidx.room.Insert;
|
||||
import androidx.room.OnConflictStrategy;
|
||||
import androidx.room.Query;
|
||||
import androidx.room.Update;
|
||||
|
||||
import com.cappielloantonio.play.model.AlbumArtistCross;
|
||||
import com.cappielloantonio.play.model.SongArtistCross;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Dao
|
||||
public interface SongArtistCrossDao {
|
||||
@Query("SELECT * FROM song_artist_cross")
|
||||
LiveData<List<SongArtistCross>> getAll();
|
||||
|
||||
@Query("SELECT EXISTS(SELECT * FROM song_artist_cross WHERE id = :id)")
|
||||
boolean exist(String id);
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
void insert(SongArtistCross songArtistCross);
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
void insertAll(List<SongArtistCross> songArtistCross);
|
||||
|
||||
@Delete
|
||||
void delete(SongArtistCross songArtistCross);
|
||||
|
||||
@Update
|
||||
void update(SongArtistCross songArtistCross);
|
||||
|
||||
@Query("DELETE FROM song_artist_cross")
|
||||
void deleteAll();
|
||||
}
|
||||
|
|
@ -36,13 +36,16 @@ public interface SongDao {
|
|||
@Query("SELECT * FROM song WHERE play_count != 0 ORDER BY play_count DESC LIMIT :number")
|
||||
LiveData<List<Song>> getMostPlayedSample(int number);
|
||||
|
||||
@Query("SELECT * FROM song WHERE artistId = :artistID ORDER BY play_count DESC LIMIT :number")
|
||||
// @Query("SELECT * FROM song WHERE artistId = :artistID ORDER BY play_count DESC LIMIT :number")
|
||||
@Query("SELECT song.* FROM song INNER JOIN song_artist_cross ON song.id = song_artist_cross.song_id AND song_artist_cross.artist_id = :artistID ORDER BY play_count DESC LIMIT :number")
|
||||
LiveData<List<Song>> getArtistTopSongsSample(String artistID, int number);
|
||||
|
||||
@Query("SELECT * FROM song WHERE artistId = :artistID ORDER BY play_count DESC")
|
||||
// @Query("SELECT * FROM song WHERE artistId = :artistID ORDER BY play_count DESC")
|
||||
@Query("SELECT song.* FROM song INNER JOIN song_artist_cross ON song.id = song_artist_cross.song_id AND song_artist_cross.artist_id = :artistID ORDER BY play_count DESC")
|
||||
LiveData<List<Song>> getArtistTopSongs(String artistID);
|
||||
|
||||
@Query("SELECT * FROM song WHERE artistId = :artistID ORDER BY RANDOM() LIMIT :number")
|
||||
// @Query("SELECT * FROM song WHERE artistId = :artistID ORDER BY RANDOM() LIMIT :number")
|
||||
@Query("SELECT song.* FROM song INNER JOIN song_artist_cross ON song.id = song_artist_cross.song_id AND song_artist_cross.artist_id = :artistID ORDER BY RANDOM() LIMIT :number")
|
||||
List<Song> getArtistRandomSongs(String artistID, int number);
|
||||
|
||||
@Query("SELECT * FROM song WHERE albumId = :albumID ORDER BY trackNumber ASC")
|
||||
|
|
|
|||
|
|
@ -21,6 +21,16 @@ public class Album implements Parcelable {
|
|||
@Ignore
|
||||
public List<Song> songs;
|
||||
|
||||
/*
|
||||
* TODO: Da capire chi tra albumArtist e artistItems sono i compositori e suonatori dell'album, oppure le comparse
|
||||
* In teoria AlbumArtist sono i creatori, mentre ArtistItems le comparse
|
||||
*/
|
||||
@Ignore
|
||||
public List<Artist> albumArtists;
|
||||
|
||||
@Ignore
|
||||
public List<Artist> artistItems;
|
||||
|
||||
@NonNull
|
||||
@PrimaryKey
|
||||
@ColumnInfo(name = "id")
|
||||
|
|
@ -60,12 +70,24 @@ public class Album implements Parcelable {
|
|||
this.title = itemDto.getName();
|
||||
this.year = itemDto.getProductionYear() != null ? itemDto.getProductionYear() : 0;
|
||||
|
||||
albumArtists = new ArrayList<>();
|
||||
artistItems = new ArrayList<>();
|
||||
|
||||
if (itemDto.getAlbumArtists().size() != 0) {
|
||||
this.artistId = itemDto.getAlbumArtists().get(0).getId();
|
||||
this.artistName = itemDto.getAlbumArtists().get(0).getName();
|
||||
} else if (itemDto.getArtistItems().size() != 0) {
|
||||
|
||||
itemDto.getAlbumArtists().forEach(artist -> {
|
||||
albumArtists.add(new Artist(artist.getId(), artist.getName()));
|
||||
});
|
||||
}
|
||||
else if (itemDto.getArtistItems().size() != 0) {
|
||||
this.artistId = itemDto.getArtistItems().get(0).getId();
|
||||
this.artistName = itemDto.getArtistItems().get(0).getName();
|
||||
|
||||
itemDto.getArtistItems().forEach(artist -> {
|
||||
artistItems.add(new Artist(artist.getId(), artist.getName()));
|
||||
});
|
||||
}
|
||||
|
||||
this.primary = itemDto.getImageTags().containsKey(ImageType.Primary) ? id : null;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,61 @@
|
|||
package com.cappielloantonio.play.model;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.room.ColumnInfo;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.PrimaryKey;
|
||||
|
||||
@Entity(tableName = "album_artist_cross")
|
||||
public class AlbumArtistCross {
|
||||
@NonNull
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
@ColumnInfo(name = "id")
|
||||
private int id;
|
||||
|
||||
@ColumnInfo(name = "album_id")
|
||||
private String albumId;
|
||||
|
||||
@ColumnInfo(name = "artist_id")
|
||||
private String artistId;
|
||||
|
||||
@ColumnInfo(name = "is_producer")
|
||||
private boolean isProducer;
|
||||
|
||||
public AlbumArtistCross(String albumId, String artistId, boolean isProducer) {
|
||||
this.albumId = albumId;
|
||||
this.artistId = artistId;
|
||||
this.isProducer = isProducer;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getAlbumId() {
|
||||
return albumId;
|
||||
}
|
||||
|
||||
public void setAlbumId(String albumId) {
|
||||
this.albumId = albumId;
|
||||
}
|
||||
|
||||
public String getArtistId() {
|
||||
return artistId;
|
||||
}
|
||||
|
||||
public void setArtistId(String artistId) {
|
||||
this.artistId = artistId;
|
||||
}
|
||||
|
||||
public boolean isProducer() {
|
||||
return isProducer;
|
||||
}
|
||||
|
||||
public void setProducer(boolean producer) {
|
||||
isProducer = producer;
|
||||
}
|
||||
}
|
||||
|
|
@ -87,6 +87,12 @@ public class Artist implements Parcelable {
|
|||
}
|
||||
}
|
||||
|
||||
@Ignore
|
||||
public Artist(String id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String getId() {
|
||||
return id;
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ import org.jellyfin.apiclient.model.entities.ImageType;
|
|||
import org.jellyfin.apiclient.model.entities.MediaStream;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity(tableName = "song")
|
||||
|
|
@ -46,6 +48,16 @@ public class Song implements Parcelable {
|
|||
@Ignore
|
||||
public static final String IS_FAVORITE = "IS_FAVORITE";
|
||||
|
||||
/*
|
||||
* TODO: Da capire chi tra albumArtist e artistItems sono i compositori e suonatori dell'album, oppure le comparse
|
||||
* In teoria AlbumArtist sono i creatori, mentre ArtistItems le comparse
|
||||
*/
|
||||
@Ignore
|
||||
public List<Artist> albumArtists;
|
||||
|
||||
@Ignore
|
||||
public List<Artist> artistItems;
|
||||
|
||||
@NonNull
|
||||
@PrimaryKey
|
||||
@ColumnInfo(name = "id")
|
||||
|
|
@ -164,12 +176,24 @@ public class Song implements Parcelable {
|
|||
this.albumId = itemDto.getAlbumId();
|
||||
this.albumName = itemDto.getAlbum();
|
||||
|
||||
albumArtists = new ArrayList<>();
|
||||
artistItems = new ArrayList<>();
|
||||
|
||||
if (itemDto.getAlbumArtists().size() != 0) {
|
||||
this.artistId = itemDto.getAlbumArtists().get(0).getId();
|
||||
this.artistName = itemDto.getAlbumArtists().get(0).getName();
|
||||
} else if (itemDto.getArtistItems().size() != 0) {
|
||||
|
||||
itemDto.getAlbumArtists().forEach(artist -> {
|
||||
albumArtists.add(new Artist(artist.getId(), artist.getName()));
|
||||
});
|
||||
}
|
||||
else if (itemDto.getArtistItems().size() != 0) {
|
||||
this.artistId = itemDto.getArtistItems().get(0).getId();
|
||||
this.artistName = itemDto.getArtistItems().get(0).getName();
|
||||
|
||||
itemDto.getArtistItems().forEach(artist -> {
|
||||
artistItems.add(new Artist(artist.getId(), artist.getName()));
|
||||
});
|
||||
}
|
||||
|
||||
this.primary = itemDto.getAlbumPrimaryImageTag() != null ? albumId : null;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
package com.cappielloantonio.play.model;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.room.ColumnInfo;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.PrimaryKey;
|
||||
|
||||
@Entity(tableName = "song_artist_cross")
|
||||
public class SongArtistCross {
|
||||
@NonNull
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
@ColumnInfo(name = "id")
|
||||
private int id;
|
||||
|
||||
@ColumnInfo(name = "song_id")
|
||||
private String songId;
|
||||
|
||||
@ColumnInfo(name = "artist_id")
|
||||
private String artistId;
|
||||
|
||||
public SongArtistCross(String songId, String artistId) {
|
||||
this.songId = songId;
|
||||
this.artistId = artistId;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getSongId() {
|
||||
return songId;
|
||||
}
|
||||
|
||||
public void setSongId(String songId) {
|
||||
this.songId = songId;
|
||||
}
|
||||
|
||||
public String getArtistId() {
|
||||
return artistId;
|
||||
}
|
||||
|
||||
public void setArtistId(String artistId) {
|
||||
this.artistId = artistId;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
package com.cappielloantonio.play.repository;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
|
||||
import com.cappielloantonio.play.database.AppDatabase;
|
||||
import com.cappielloantonio.play.database.dao.AlbumArtistCrossDao;
|
||||
import com.cappielloantonio.play.database.dao.QueueDao;
|
||||
import com.cappielloantonio.play.database.dao.SongArtistCrossDao;
|
||||
import com.cappielloantonio.play.model.AlbumArtistCross;
|
||||
import com.cappielloantonio.play.model.Queue;
|
||||
import com.cappielloantonio.play.model.Song;
|
||||
import com.cappielloantonio.play.util.QueueUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class AlbumArtistRepository {
|
||||
private static final String TAG = "AlbumArtistRepository";
|
||||
|
||||
private AlbumArtistCrossDao albumArtistCrossDao;
|
||||
|
||||
public AlbumArtistRepository(Application application) {
|
||||
AppDatabase database = AppDatabase.getInstance(application);
|
||||
albumArtistCrossDao = database.albumArtistCrossDao();
|
||||
}
|
||||
|
||||
public void insertAll(List<AlbumArtistCross> crosses) {
|
||||
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(albumArtistCrossDao, crosses);
|
||||
Thread thread = new Thread(insertAll);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
private static class InsertAllThreadSafe implements Runnable {
|
||||
private AlbumArtistCrossDao albumArtistCrossDao;
|
||||
private List<AlbumArtistCross> crosses;
|
||||
|
||||
public InsertAllThreadSafe(AlbumArtistCrossDao albumArtistCrossDao, List<AlbumArtistCross> crosses) {
|
||||
this.albumArtistCrossDao = albumArtistCrossDao;
|
||||
this.crosses = crosses;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
albumArtistCrossDao.insertAll(crosses);
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
DeleteAllAlbumArtistCrossThreadSafe delete = new DeleteAllAlbumArtistCrossThreadSafe(albumArtistCrossDao);
|
||||
Thread thread = new Thread(delete);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
private static class DeleteAllAlbumArtistCrossThreadSafe implements Runnable {
|
||||
private AlbumArtistCrossDao albumArtistCrossDao;
|
||||
|
||||
public DeleteAllAlbumArtistCrossThreadSafe(AlbumArtistCrossDao albumArtistCrossDao) {
|
||||
this.albumArtistCrossDao = albumArtistCrossDao;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
albumArtistCrossDao.deleteAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
package com.cappielloantonio.play.repository;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import com.cappielloantonio.play.database.AppDatabase;
|
||||
import com.cappielloantonio.play.database.dao.AlbumArtistCrossDao;
|
||||
import com.cappielloantonio.play.database.dao.SongArtistCrossDao;
|
||||
import com.cappielloantonio.play.database.dao.SongGenreCrossDao;
|
||||
import com.cappielloantonio.play.model.AlbumArtistCross;
|
||||
import com.cappielloantonio.play.model.SongArtistCross;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SongArtistRepository {
|
||||
private static final String TAG = "AlbumArtistRepository";
|
||||
|
||||
private SongArtistCrossDao songArtistCrossDao;
|
||||
|
||||
public SongArtistRepository(Application application) {
|
||||
AppDatabase database = AppDatabase.getInstance(application);
|
||||
songArtistCrossDao = database.songArtistCrossDao();
|
||||
}
|
||||
|
||||
public void insertAll(List<SongArtistCross> crosses) {
|
||||
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(songArtistCrossDao, crosses);
|
||||
Thread thread = new Thread(insertAll);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
private static class InsertAllThreadSafe implements Runnable {
|
||||
private SongArtistCrossDao songArtistCrossDao;
|
||||
private List<SongArtistCross> crosses;
|
||||
|
||||
public InsertAllThreadSafe(SongArtistCrossDao songArtistCrossDao, List<SongArtistCross> crosses) {
|
||||
this.songArtistCrossDao = songArtistCrossDao;
|
||||
this.crosses = crosses;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
songArtistCrossDao.insertAll(crosses);
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
DeleteAllSongArtistCrossThreadSafe delete = new DeleteAllSongArtistCrossThreadSafe(songArtistCrossDao);
|
||||
Thread thread = new Thread(delete);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
private static class DeleteAllSongArtistCrossThreadSafe implements Runnable {
|
||||
private SongArtistCrossDao songArtistCrossDao;
|
||||
|
||||
public DeleteAllSongArtistCrossThreadSafe(SongArtistCrossDao songArtistCrossDao) {
|
||||
this.songArtistCrossDao = songArtistCrossDao;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
songArtistCrossDao.deleteAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -16,15 +16,19 @@ import com.cappielloantonio.play.App;
|
|||
import com.cappielloantonio.play.databinding.FragmentSyncBinding;
|
||||
import com.cappielloantonio.play.interfaces.MediaCallback;
|
||||
import com.cappielloantonio.play.model.Album;
|
||||
import com.cappielloantonio.play.model.AlbumArtistCross;
|
||||
import com.cappielloantonio.play.model.Artist;
|
||||
import com.cappielloantonio.play.model.Genre;
|
||||
import com.cappielloantonio.play.model.Playlist;
|
||||
import com.cappielloantonio.play.model.Song;
|
||||
import com.cappielloantonio.play.model.SongArtistCross;
|
||||
import com.cappielloantonio.play.model.SongGenreCross;
|
||||
import com.cappielloantonio.play.repository.AlbumArtistRepository;
|
||||
import com.cappielloantonio.play.repository.AlbumRepository;
|
||||
import com.cappielloantonio.play.repository.ArtistRepository;
|
||||
import com.cappielloantonio.play.repository.GenreRepository;
|
||||
import com.cappielloantonio.play.repository.PlaylistRepository;
|
||||
import com.cappielloantonio.play.repository.SongArtistRepository;
|
||||
import com.cappielloantonio.play.repository.SongRepository;
|
||||
import com.cappielloantonio.play.ui.activities.MainActivity;
|
||||
import com.cappielloantonio.play.util.PreferenceUtil;
|
||||
|
|
@ -43,6 +47,14 @@ public class SyncFragment extends Fragment {
|
|||
private FragmentSyncBinding bind;
|
||||
private SyncViewModel syncViewModel;
|
||||
|
||||
private SongRepository songRepository;
|
||||
private AlbumRepository albumRepository;
|
||||
private ArtistRepository artistRepository;
|
||||
private PlaylistRepository playlistRepository;
|
||||
private GenreRepository genreRepository;
|
||||
private SongArtistRepository songArtistRepository;
|
||||
private AlbumArtistRepository albumArtistRepository;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
|
|
@ -52,6 +64,14 @@ public class SyncFragment extends Fragment {
|
|||
View view = bind.getRoot();
|
||||
syncViewModel = new ViewModelProvider(requireActivity()).get(SyncViewModel.class);
|
||||
|
||||
songRepository = new SongRepository(activity.getApplication());
|
||||
albumRepository = new AlbumRepository(activity.getApplication());
|
||||
artistRepository = new ArtistRepository(activity.getApplication());
|
||||
playlistRepository = new PlaylistRepository(activity.getApplication());
|
||||
genreRepository = new GenreRepository(activity.getApplication());
|
||||
songArtistRepository = new SongArtistRepository(activity.getApplication());
|
||||
albumArtistRepository = new AlbumArtistRepository(activity.getApplication());
|
||||
|
||||
init();
|
||||
syncLibraries();
|
||||
|
||||
|
|
@ -108,8 +128,8 @@ public class SyncFragment extends Fragment {
|
|||
|
||||
@Override
|
||||
public void onLoadMedia(List<?> media) {
|
||||
AlbumRepository repository = new AlbumRepository(activity.getApplication());
|
||||
repository.insertAll((ArrayList<Album>) media);
|
||||
albumRepository.insertAll((ArrayList<Album>) media);
|
||||
syncAlbumArtistCross((ArrayList<Album>) media);
|
||||
animateProgressBar(true);
|
||||
}
|
||||
});
|
||||
|
|
@ -125,8 +145,7 @@ public class SyncFragment extends Fragment {
|
|||
|
||||
@Override
|
||||
public void onLoadMedia(List<?> media) {
|
||||
ArtistRepository repository = new ArtistRepository(activity.getApplication());
|
||||
repository.insertAll((ArrayList<Artist>) media);
|
||||
artistRepository.insertAll((ArrayList<Artist>) media);
|
||||
animateProgressBar(true);
|
||||
}
|
||||
});
|
||||
|
|
@ -142,8 +161,7 @@ public class SyncFragment extends Fragment {
|
|||
|
||||
@Override
|
||||
public void onLoadMedia(List<?> media) {
|
||||
GenreRepository repository = new GenreRepository(activity.getApplication());
|
||||
repository.insertAll((ArrayList<Genre>) media);
|
||||
genreRepository.insertAll((ArrayList<Genre>) media);
|
||||
animateProgressBar(true);
|
||||
|
||||
if(syncViewModel.isCrossSyncSongGenre()) syncSongsPerGenre((ArrayList<Genre>) media);
|
||||
|
|
@ -162,8 +180,7 @@ public class SyncFragment extends Fragment {
|
|||
|
||||
@Override
|
||||
public void onLoadMedia(List<?> media) {
|
||||
PlaylistRepository repository = new PlaylistRepository(activity.getApplication());
|
||||
repository.insertAll((ArrayList<Playlist>) media);
|
||||
playlistRepository.insertAll((ArrayList<Playlist>) media);
|
||||
animateProgressBar(true);
|
||||
}
|
||||
});
|
||||
|
|
@ -179,9 +196,9 @@ public class SyncFragment extends Fragment {
|
|||
|
||||
@Override
|
||||
public void onLoadMedia(List<?> media) {
|
||||
SongRepository repository = new SongRepository(activity.getApplication());
|
||||
repository.deleteAllSongGenreCross();
|
||||
repository.insertAll((ArrayList<Song>) media);
|
||||
songRepository.deleteAllSongGenreCross();
|
||||
songRepository.insertAll((ArrayList<Song>) media);
|
||||
syncSongArtistCross((ArrayList<Song>) media);
|
||||
animateProgressBar(true);
|
||||
}
|
||||
});
|
||||
|
|
@ -197,8 +214,7 @@ public class SyncFragment extends Fragment {
|
|||
|
||||
@Override
|
||||
public void onLoadMedia(List<?> media) {
|
||||
SongRepository repository = new SongRepository(App.getInstance());
|
||||
repository.insertSongPerGenre((ArrayList<SongGenreCross>) media);
|
||||
songRepository.insertSongPerGenre((ArrayList<SongGenreCross>) media);
|
||||
}
|
||||
}, genre.id);
|
||||
}
|
||||
|
|
@ -227,4 +243,63 @@ public class SyncFragment extends Fragment {
|
|||
PreferenceUtil.getInstance(requireContext()).setSync(true);
|
||||
activity.goToHome();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
/*
|
||||
* Sincronizzazzione dell'album con gli artisti che lo hanno prodotto | isProduced = true
|
||||
* Sincronizzazzione dell'album con gli artisti che hanno collaborato per la sua produzione | isProduced = false
|
||||
*/
|
||||
private void syncAlbumArtistCross(ArrayList<Album> albums) {
|
||||
albumArtistRepository.deleteAll();
|
||||
|
||||
List<AlbumArtistCross> crosses = new ArrayList<>();
|
||||
|
||||
for(Album album: albums) {
|
||||
List<Artist> artists = new ArrayList<>();
|
||||
|
||||
if(album.albumArtists.size() > 0) {
|
||||
for (Artist artist: album.albumArtists) {
|
||||
artists.add(artist);
|
||||
crosses.add(new AlbumArtistCross(album.getId(), artist.getId(), true));
|
||||
}
|
||||
}
|
||||
|
||||
if(album.artistItems.size() > 0) {
|
||||
for (Artist artist: album.artistItems) {
|
||||
if(!artists.contains(artist)) {
|
||||
crosses.add(new AlbumArtistCross(album.getId(), artist.getId(), false));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
albumArtistRepository.insertAll(crosses);
|
||||
}
|
||||
|
||||
private void syncSongArtistCross(ArrayList<Song> songs) {
|
||||
songArtistRepository.deleteAll();
|
||||
|
||||
List<SongArtistCross> crosses = new ArrayList<>();
|
||||
|
||||
for(Song song: songs) {
|
||||
List<Artist> artists = new ArrayList<>();
|
||||
|
||||
if(song.albumArtists.size() > 0) {
|
||||
for (Artist artist: song.albumArtists) {
|
||||
artists.add(artist);
|
||||
crosses.add(new SongArtistCross(song.getId(), artist.getId()));
|
||||
}
|
||||
}
|
||||
|
||||
if(song.artistItems.size() > 0) {
|
||||
for (Artist artist: song.artistItems) {
|
||||
if(!artists.contains(artist)) {
|
||||
crosses.add(new SongArtistCross(song.getId(), artist.getId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
songArtistRepository.insertAll(crosses);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue