Remove NoteSheets when Deleting Song

This commit is contained in:
Fawkes100 2025-01-18 17:54:45 +01:00
parent c04dbe565a
commit 7e04b7c9f3
3 changed files with 21 additions and 2 deletions

View File

@ -24,5 +24,6 @@ public interface SongDao {
@Delete
void deleteNoteSheets(List<NoteSheet> noteSheets);
@Query("SELECT * FROM NoteSheet WHERE songID = :songID")
List<NoteSheet> getNoteSheetsBySong(int songID);
}

View File

@ -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<NoteSheet> noteSheets = songDao.getNoteSheetsBySong(song.getLocalID());
for(NoteSheet noteSheet : noteSheets) {
try {
NoteSheetsUtil.deleteNoteSheet(noteSheet.getLocalFileName());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
songDao.deleteNoteSheets(noteSheets);
});
}
}

View File

@ -84,4 +84,8 @@ public class NoteSheetsUtil {
return null;
}
}
public static void deleteNoteSheet(String filePath) throws IOException {
Files.delete(new File(filePath).toPath());
}
}