From 7e04b7c9f399585acdfa4c7f46db7804a6002d01 Mon Sep 17 00:00:00 2001 From: Fawkes100 Date: Sat, 18 Jan 2025 17:54:45 +0100 Subject: [PATCH] Remove NoteSheets when Deleting Song --- .../stormtales/notevault/data/dao/SongDao.java | 1 + .../data/repositories/SongRepository.java | 18 ++++++++++++++++-- .../notevault/utils/NoteSheetsUtil.java | 4 ++++ 3 files changed, 21 insertions(+), 2 deletions(-) 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()); + } }