diff --git a/app/src/main/java/com/stormtales/notevault/data/dao/SongDao.java b/app/src/main/java/com/stormtales/notevault/data/dao/SongDao.java index e3a8773..6b6744f 100644 --- a/app/src/main/java/com/stormtales/notevault/data/dao/SongDao.java +++ b/app/src/main/java/com/stormtales/notevault/data/dao/SongDao.java @@ -24,5 +24,6 @@ public interface SongDao { @Delete void deleteNoteSheets(List noteSheets); + @Query("SELECT * FROM NoteSheet WHERE songID = :songID") List getNoteSheetsBySong(int songID); } diff --git a/app/src/main/java/com/stormtales/notevault/data/repositories/SongRepository.java b/app/src/main/java/com/stormtales/notevault/data/repositories/SongRepository.java index 3c62e67..8eaea1e 100644 --- a/app/src/main/java/com/stormtales/notevault/data/repositories/SongRepository.java +++ b/app/src/main/java/com/stormtales/notevault/data/repositories/SongRepository.java @@ -10,7 +10,9 @@ import com.stormtales.notevault.data.dao.SongDao; import com.stormtales.notevault.data.entities.NoteSheet; import com.stormtales.notevault.data.entities.Song; import com.stormtales.notevault.data.sync.SyncStatus; +import com.stormtales.notevault.utils.NoteSheetsUtil; +import java.io.IOException; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -59,7 +61,19 @@ public class SongRepository { } public void deleteSong(Song song) { - song.setSyncStatus(SyncStatus.DELETED); - songDao.update(song); + Executors.newSingleThreadExecutor().execute(() -> { + song.setSyncStatus(SyncStatus.DELETED); + songDao.update(song); + + List noteSheets = songDao.getNoteSheetsBySong(song.getLocalID()); + for(NoteSheet noteSheet : noteSheets) { + try { + NoteSheetsUtil.deleteNoteSheet(noteSheet.getLocalFileName()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + songDao.deleteNoteSheets(noteSheets); + }); } } diff --git a/app/src/main/java/com/stormtales/notevault/utils/NoteSheetsUtil.java b/app/src/main/java/com/stormtales/notevault/utils/NoteSheetsUtil.java index ae21a14..d830504 100644 --- a/app/src/main/java/com/stormtales/notevault/utils/NoteSheetsUtil.java +++ b/app/src/main/java/com/stormtales/notevault/utils/NoteSheetsUtil.java @@ -84,4 +84,8 @@ public class NoteSheetsUtil { return null; } } + + public static void deleteNoteSheet(String filePath) throws IOException { + Files.delete(new File(filePath).toPath()); + } }