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.SongArtistCross;
import com.cappielloantonio.play.model.SongGenreCross; 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 { public abstract class AppDatabase extends RoomDatabase {
private static final String TAG = "AppDatabase"; private static final String TAG = "AppDatabase";
private final static String DB_NAME = "play_db"; 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) { public static synchronized AppDatabase getInstance(Context context) {
if (instance == null && context != null) { if (instance == null && context != null) {
instance = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, DB_NAME) instance = Room.databaseBuilder(context, AppDatabase.class, DB_NAME)
.fallbackToDestructiveMigration() .fallbackToDestructiveMigration()
.build(); .build();
} }

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -20,11 +20,11 @@ public class SongArtistRepository {
public void insertAll(List<SongArtistCross> crosses) { public void insertAll(List<SongArtistCross> crosses) {
try { 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)); final Thread insertAll = new Thread(new InsertAllThreadSafe(songArtistCrossDao, crosses));
delete.start(); deleteAll.start();
delete.join(); deleteAll.join();
insertAll.start(); insertAll.start();
insertAll.join(); insertAll.join();
} catch (InterruptedException e) { } 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) { 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); InsertAllThreadSafe insertAll = new InsertAllThreadSafe(songDao, songGenreCrossDao, songs);
Thread thread = new Thread(insertAll); Thread thread = new Thread(insertAll);
thread.start(); thread.start();
@ -405,8 +417,6 @@ public class SongRepository {
@Override @Override
public void run() { public void run() {
songDao.deleteAll();
songGenreCrossDao.deleteAll();
songDao.insertAll(songs); songDao.insertAll(songs);
} }
} }

View file

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