Changed the order of deletion and saving of items at the time of synchronization

This commit is contained in:
CappielloAntonio 2021-05-02 12:20:43 +02:00
parent 01785b7bed
commit 86fcc8b479
13 changed files with 138 additions and 51 deletions

View file

@ -29,7 +29,7 @@ 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, AlbumArtistCross.class, SongArtistCross.class, PlaylistSongCross.class}, version = 12, exportSchema = false)
@Database(entities = {Album.class, Artist.class, Genre.class, Playlist.class, Song.class, RecentSearch.class, SongGenreCross.class, Queue.class, AlbumArtistCross.class, SongArtistCross.class, PlaylistSongCross.class}, version = 13, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
private static final String TAG = "AppDatabase";
private final static String DB_NAME = "play_db";
@ -38,7 +38,7 @@ public abstract class AppDatabase extends RoomDatabase {
public static synchronized AppDatabase getInstance(Context context) {
if (instance == null && context != null) {
instance = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, DB_NAME)
instance = Room.databaseBuilder(context, AppDatabase.class, DB_NAME)
.fallbackToDestructiveMigration()
.build();
}

View file

@ -17,9 +17,6 @@ public interface PlaylistSongCrossDao {
@Query("SELECT * FROM playlist_song_cross")
LiveData<List<PlaylistSongCross>> getAll();
@Query("SELECT EXISTS(SELECT * FROM playlist_song_cross WHERE id = :id)")
boolean exist(String id);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(PlaylistSongCross playlistSongCross);

View file

@ -5,16 +5,13 @@ import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity(tableName = "playlist_song_cross")
@Entity(tableName = "playlist_song_cross", primaryKeys = {"playlist_id","song_id"})
public class PlaylistSongCross {
@NonNull
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
private int id;
@ColumnInfo(name = "playlist_id")
private String playlistId;
@NonNull
@ColumnInfo(name = "song_id")
private String songId;
@ -23,14 +20,6 @@ public class PlaylistSongCross {
this.songId = songId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPlaylistId() {
return playlistId;
}

View file

@ -20,11 +20,11 @@ public class AlbumArtistRepository {
public void insertAll(List<AlbumArtistCross> crosses) {
try {
final Thread delete = new Thread(new DeleteAllAlbumArtistCrossThreadSafe(albumArtistCrossDao));
final Thread deleteAll = new Thread(new DeleteAllAlbumArtistCrossThreadSafe(albumArtistCrossDao));
final Thread insertAll = new Thread(new InsertAllThreadSafe(albumArtistCrossDao, crosses));
delete.start();
delete.join();
deleteAll.start();
deleteAll.join();
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {

View file

@ -64,9 +64,17 @@ public class AlbumRepository {
}
public void insertAll(ArrayList<Album> albums) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(albumDao, albums);
Thread thread = new Thread(insertAll);
thread.start();
try {
final Thread deleteAll = new Thread(new DeleteAllThreadSafe(albumDao));
final Thread insertAll = new Thread(new InsertAllThreadSafe(albumDao, albums));
deleteAll.start();
deleteAll.join();
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void deleteAll() {
@ -158,7 +166,6 @@ public class AlbumRepository {
@Override
public void run() {
albumDao.deleteAll();
albumDao.insertAll(albums);
}
}

View file

@ -56,9 +56,17 @@ public class ArtistRepository {
}
public void insertAll(ArrayList<Artist> artists) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(artistDao, artists);
Thread thread = new Thread(insertAll);
thread.start();
try {
final Thread deleteAll = new Thread(new DeleteAllThreadSafe(artistDao));
final Thread insertAll = new Thread(new InsertAllThreadSafe(artistDao, artists));
deleteAll.start();
deleteAll.join();
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void deleteAll() {
@ -117,7 +125,6 @@ public class ArtistRepository {
@Override
public void run() {
artistDao.deleteAll();
artistDao.insertAll(artists);
}
}

View file

@ -26,9 +26,17 @@ public class PlaylistRepository {
}
public void insertAll(ArrayList<Playlist> playlists) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(playlistDao, playlists);
Thread thread = new Thread(insertAll);
thread.start();
try {
final Thread deleteAll = new Thread(new DeleteAllThreadSafe(playlistDao));
final Thread insertAll = new Thread(new InsertAllThreadSafe(playlistDao, playlists));
deleteAll.start();
deleteAll.join();
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void deleteAll() {
@ -65,7 +73,6 @@ public class PlaylistRepository {
@Override
public void run() {
playlistDao.deleteAll();
playlistDao.insertAll(playlists);
}
}

View file

@ -24,12 +24,6 @@ public class PlaylistSongRepository {
thread.start();
}
public void deleteAll() {
DeleteAllPlaylistSongCrossThreadSafe delete = new DeleteAllPlaylistSongCrossThreadSafe(playlistSongCrossDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class InsertAllThreadSafe implements Runnable {
private PlaylistSongCrossDao playlistSongCrossDao;
private List<PlaylistSongCross> crosses;
@ -45,6 +39,12 @@ public class PlaylistSongRepository {
}
}
public void deleteAll() {
DeleteAllPlaylistSongCrossThreadSafe delete = new DeleteAllPlaylistSongCrossThreadSafe(playlistSongCrossDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class DeleteAllPlaylistSongCrossThreadSafe implements Runnable {
private PlaylistSongCrossDao playlistSongCrossDao;

View file

@ -20,11 +20,11 @@ public class SongArtistRepository {
public void insertAll(List<SongArtistCross> crosses) {
try {
final Thread delete = new Thread(new DeleteAllSongArtistCrossThreadSafe(songArtistCrossDao));
final Thread deleteAll = new Thread(new DeleteAllSongArtistCrossThreadSafe(songArtistCrossDao));
final Thread insertAll = new Thread(new InsertAllThreadSafe(songArtistCrossDao, crosses));
delete.start();
delete.join();
deleteAll.start();
deleteAll.join();
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {

View file

@ -0,0 +1,70 @@
package com.cappielloantonio.play.repository;
import android.app.Application;
import com.cappielloantonio.play.database.AppDatabase;
import com.cappielloantonio.play.database.dao.SongArtistCrossDao;
import com.cappielloantonio.play.database.dao.SongGenreCrossDao;
import com.cappielloantonio.play.model.SongArtistCross;
import com.cappielloantonio.play.model.SongGenreCross;
import java.util.List;
public class SongGenreRepository {
private static final String TAG = "AlbumArtistRepository";
private SongGenreCrossDao songGenreCrossDao;
public SongGenreRepository(Application application) {
AppDatabase database = AppDatabase.getInstance(application);
songGenreCrossDao = database.songGenreCrossDao();
}
public void insertAll(List<SongGenreCross> crosses) {
try {
final Thread delete = new Thread(new DeleteAllSongGenreCrossThreadSafe(songGenreCrossDao));
final Thread insertAll = new Thread(new InsertAllThreadSafe(songGenreCrossDao, crosses));
delete.start();
delete.join();
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void deleteAll() {
DeleteAllSongGenreCrossThreadSafe delete = new DeleteAllSongGenreCrossThreadSafe(songGenreCrossDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class InsertAllThreadSafe implements Runnable {
private SongGenreCrossDao songGenreCrossDao;
private List<SongGenreCross> crosses;
public InsertAllThreadSafe(SongGenreCrossDao songGenreCrossDao, List<SongGenreCross> crosses) {
this.songGenreCrossDao = songGenreCrossDao;
this.crosses = crosses;
}
@Override
public void run() {
songGenreCrossDao.insertAll(crosses);
}
}
private static class DeleteAllSongGenreCrossThreadSafe implements Runnable {
private SongGenreCrossDao songGenreCrossDao;
public DeleteAllSongGenreCrossThreadSafe(SongGenreCrossDao songGenreCrossDao) {
this.songGenreCrossDao = songGenreCrossDao;
}
@Override
public void run() {
songGenreCrossDao.deleteAll();
}
}
}

View file

@ -215,6 +215,18 @@ public class SongRepository {
}
public void insertAll(ArrayList<Song> songs) {
try {
final Thread deleteAll = new Thread(new DeleteAllSongThreadSafe(songDao));
final Thread insertAll = new Thread(new InsertAllThreadSafe(songDao, songGenreCrossDao, songs));
deleteAll.start();
deleteAll.join();
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(songDao, songGenreCrossDao, songs);
Thread thread = new Thread(insertAll);
thread.start();
@ -405,8 +417,6 @@ public class SongRepository {
@Override
public void run() {
songDao.deleteAll();
songGenreCrossDao.deleteAll();
songDao.insertAll(songs);
}
}

View file

@ -37,7 +37,6 @@ public class PlaylistPageFragment extends Fragment {
super.onActivityCreated(savedInstanceState);
initAppBar();
initMusicButton();
}
@Override
@ -50,6 +49,7 @@ public class PlaylistPageFragment extends Fragment {
init();
initBackCover();
initMusicButton();
initSongsView();
return view;

View file

@ -30,6 +30,7 @@ import com.cappielloantonio.play.repository.GenreRepository;
import com.cappielloantonio.play.repository.PlaylistRepository;
import com.cappielloantonio.play.repository.PlaylistSongRepository;
import com.cappielloantonio.play.repository.SongArtistRepository;
import com.cappielloantonio.play.repository.SongGenreRepository;
import com.cappielloantonio.play.repository.SongRepository;
import com.cappielloantonio.play.ui.activity.MainActivity;
import com.cappielloantonio.play.util.DownloadUtil;
@ -55,6 +56,7 @@ public class SyncFragment extends Fragment {
private ArtistRepository artistRepository;
private PlaylistRepository playlistRepository;
private GenreRepository genreRepository;
private SongGenreRepository songGenreRepository;
private SongArtistRepository songArtistRepository;
private AlbumArtistRepository albumArtistRepository;
private PlaylistSongRepository playlistSongRepository;
@ -82,6 +84,7 @@ public class SyncFragment extends Fragment {
artistRepository = new ArtistRepository(activity.getApplication());
playlistRepository = new PlaylistRepository(activity.getApplication());
genreRepository = new GenreRepository(activity.getApplication());
songGenreRepository = new SongGenreRepository(activity.getApplication());
songArtistRepository = new SongArtistRepository(activity.getApplication());
albumArtistRepository = new AlbumArtistRepository(activity.getApplication());
playlistSongRepository = new PlaylistSongRepository(activity.getApplication());
@ -244,6 +247,7 @@ public class SyncFragment extends Fragment {
@Override
public void onLoadMedia(List<?> media) {
syncViewModel.setPlaylistList((ArrayList<Playlist>) media);
playlistSongRepository.deleteAll();
playlistRepository.insertAll(syncViewModel.getPlaylistList());
loadSectorInfo(PLAYLISTS, "Found " + syncViewModel.getPlaylistList().size() + " elements", true);
}
@ -260,15 +264,11 @@ public class SyncFragment extends Fragment {
@Override
public void onLoadMedia(List<?> media) {
syncViewModel.setSongList((ArrayList<Song>) media);
songRepository.deleteAllSongGenreCross();
songGenreRepository.deleteAll();
songRepository.insertAll(syncViewModel.getSongList());
syncSongArtistCross(syncViewModel.getSongList());
syncDownloadedSong();
loadSectorInfo(SONGS, "Found " + syncViewModel.getSongList().size() + " elements", true);
PreferenceUtil.getInstance(requireContext()).setSongNumber(syncViewModel.getSongList().size());
}
});
@ -284,7 +284,7 @@ public class SyncFragment extends Fragment {
@Override
public void onLoadMedia(List<?> media) {
songRepository.insertSongPerGenre((ArrayList<SongGenreCross>) media);
songGenreRepository.insertAll((ArrayList<SongGenreCross>) media);
loadSectorInfo(SONG_X_GENRE, "Genre processed: " + genre.getName(), true);
}
}, genre.id);