From c23f188921e24b2dd9a1cda5da1213810b7623c2 Mon Sep 17 00:00:00 2001 From: Fawkes100 Date: Tue, 7 Jan 2025 17:56:14 +0100 Subject: [PATCH] Implement Marking as Deletion --- .../core/notevault/data/MusicNoteDAO.java | 7 ++++- .../java/core/notevault/sync/SyncWorker.java | 1 + .../sync/synchronisation/SyncService.java | 5 ++++ .../synchronisation/songs/SongSyncWorker.java | 28 +++++++++++++++++++ .../deletion/SongDeletionBatchRequest.java | 19 +++++++++++++ .../deletion/SongDeletionBatchResponse.java | 19 +++++++++++++ .../core/notevault/ui/home/HomeFragment.java | 4 ++- 7 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/core/notevault/sync/synchronisation/songs/deletion/SongDeletionBatchRequest.java create mode 100644 app/src/main/java/core/notevault/sync/synchronisation/songs/deletion/SongDeletionBatchResponse.java diff --git a/app/src/main/java/core/notevault/data/MusicNoteDAO.java b/app/src/main/java/core/notevault/data/MusicNoteDAO.java index 210cec8..ea09e17 100644 --- a/app/src/main/java/core/notevault/data/MusicNoteDAO.java +++ b/app/src/main/java/core/notevault/data/MusicNoteDAO.java @@ -17,7 +17,7 @@ public interface MusicNoteDAO { @Insert void insertNoteSheet(NoteSheet noteSheet); - @Query("SELECT * FROM music_notes") + @Query("SELECT * FROM music_notes WHERE syncStatus != 'DELETED'") List getAllNotes(); @Query("SELECT * FROM note_sheets WHERE musicNoteId = :musicNoteId") @@ -68,4 +68,9 @@ public interface MusicNoteDAO { @Update void updateSong(MusicNote musicNote); + + @Query("DELETE FROM music_notes WHERE serverID IN (:serverIDs)") + void deleteSongs(List serverIDs); + + } diff --git a/app/src/main/java/core/notevault/sync/SyncWorker.java b/app/src/main/java/core/notevault/sync/SyncWorker.java index 33fa9a5..330931c 100644 --- a/app/src/main/java/core/notevault/sync/SyncWorker.java +++ b/app/src/main/java/core/notevault/sync/SyncWorker.java @@ -40,6 +40,7 @@ public class SyncWorker extends Worker{ try { songSyncWorker.syncSongCreations(); songSyncWorker.syncSongModifications(); + songSyncWorker.syncSongDeletions(); return Result.success(); } catch (Exception e) { return Result.failure(); diff --git a/app/src/main/java/core/notevault/sync/synchronisation/SyncService.java b/app/src/main/java/core/notevault/sync/synchronisation/SyncService.java index c70096e..068f95e 100644 --- a/app/src/main/java/core/notevault/sync/synchronisation/SyncService.java +++ b/app/src/main/java/core/notevault/sync/synchronisation/SyncService.java @@ -3,6 +3,8 @@ package core.notevault.sync.synchronisation; import core.notevault.data.sync.SyncResponse; import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchRequest; import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchResponse; +import core.notevault.sync.synchronisation.songs.deletion.SongDeletionBatchRequest; +import core.notevault.sync.synchronisation.songs.deletion.SongDeletionBatchResponse; import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchRequest; import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchResponse; import retrofit2.Call; @@ -16,4 +18,7 @@ public interface SyncService { @POST("/sync/songs/modify") Call performSongModification(@Body SongModificationBatchRequest syncRequest); + + @POST("/sync/songs/delete") + Call performSongDeletion(@Body SongDeletionBatchRequest syncRequest); } diff --git a/app/src/main/java/core/notevault/sync/synchronisation/songs/SongSyncWorker.java b/app/src/main/java/core/notevault/sync/synchronisation/songs/SongSyncWorker.java index f48e7fd..3b9dd64 100644 --- a/app/src/main/java/core/notevault/sync/synchronisation/songs/SongSyncWorker.java +++ b/app/src/main/java/core/notevault/sync/synchronisation/songs/SongSyncWorker.java @@ -10,6 +10,8 @@ import core.notevault.sync.synchronisation.SyncService; import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchRequest; import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchResponse; import core.notevault.sync.synchronisation.songs.creation.SongCreationRequest; +import core.notevault.sync.synchronisation.songs.deletion.SongDeletionBatchRequest; +import core.notevault.sync.synchronisation.songs.deletion.SongDeletionBatchResponse; import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchRequest; import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchResponse; import core.notevault.sync.synchronisation.songs.modification.SongModificationRequest; @@ -22,6 +24,7 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.stream.Collectors; public class SongSyncWorker { private SyncService syncService; @@ -90,4 +93,29 @@ public class SongSyncWorker { } }); } + + private SongDeletionBatchRequest buildDeletionBatchRequest() { + List deleted_songs = database.getSongsWithSyncStatus(SyncStatus.DELETED); + List deleted_song_ids = deleted_songs.stream().map(MusicNote::getServerID).collect(Collectors.toList()); + return new SongDeletionBatchRequest(deleted_song_ids); + } + + public void syncSongDeletions() { + SongDeletionBatchRequest request = buildDeletionBatchRequest(); + syncService.performSongDeletion(request).enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if(response.isSuccessful() && response.body() != null) { + executorService.execute(() -> { + database.deleteSongs(response.body().getDeletedSongs()); + }); + } + } + + @Override + public void onFailure(Call call, Throwable throwable) { + Toast.makeText(context, "Sync of Deleted Songs failed", Toast.LENGTH_LONG).show(); + } + }); + } } diff --git a/app/src/main/java/core/notevault/sync/synchronisation/songs/deletion/SongDeletionBatchRequest.java b/app/src/main/java/core/notevault/sync/synchronisation/songs/deletion/SongDeletionBatchRequest.java new file mode 100644 index 0000000..76eeb37 --- /dev/null +++ b/app/src/main/java/core/notevault/sync/synchronisation/songs/deletion/SongDeletionBatchRequest.java @@ -0,0 +1,19 @@ +package core.notevault.sync.synchronisation.songs.deletion; + +import java.util.List; + +public class SongDeletionBatchRequest { + private List songs; + + public SongDeletionBatchRequest(List songs) { + this.songs = songs; + } + + public List getSongs() { + return songs; + } + + public void setSongs(List songs) { + this.songs = songs; + } +} diff --git a/app/src/main/java/core/notevault/sync/synchronisation/songs/deletion/SongDeletionBatchResponse.java b/app/src/main/java/core/notevault/sync/synchronisation/songs/deletion/SongDeletionBatchResponse.java new file mode 100644 index 0000000..61fa249 --- /dev/null +++ b/app/src/main/java/core/notevault/sync/synchronisation/songs/deletion/SongDeletionBatchResponse.java @@ -0,0 +1,19 @@ +package core.notevault.sync.synchronisation.songs.deletion; + +import java.util.List; + +public class SongDeletionBatchResponse { + private List deletedSongs; + + public SongDeletionBatchResponse(List deletedSongs) { + this.deletedSongs = deletedSongs; + } + + public List getDeletedSongs() { + return deletedSongs; + } + + public void setDeletedSongs(List deletedSongs) { + this.deletedSongs = deletedSongs; + } +} diff --git a/app/src/main/java/core/notevault/ui/home/HomeFragment.java b/app/src/main/java/core/notevault/ui/home/HomeFragment.java index 3718a48..016989c 100644 --- a/app/src/main/java/core/notevault/ui/home/HomeFragment.java +++ b/app/src/main/java/core/notevault/ui/home/HomeFragment.java @@ -23,6 +23,7 @@ import core.notevault.R; import core.notevault.data.MusicDatabase; import core.notevault.data.MusicNote; import core.notevault.data.MusicNoteDAO; +import core.notevault.data.sync.SyncStatus; import core.notevault.databinding.FragmentHomeBinding; import core.notevault.ui.metadatadialog.MetaDataDialog; import core.notevault.ui.metadatadialog.SongEditDialog; @@ -78,7 +79,8 @@ public class HomeFragment extends Fragment { new Thread(() -> { MusicDatabase musicDatabase = MusicDatabase.getDatabase(getContext()); MusicNoteDAO musicNoteDAO = musicDatabase.musicNoteDao(); - musicNoteDAO.deleteSong(musicNote); + musicNote.setSyncStatus(SyncStatus.DELETED); + musicNoteDAO.updateSong(musicNote); Log.d("HomeFragment", "Delete Song: " + musicNote.getTitle()); new Handler(Looper.getMainLooper()).post(()-> { homeViewModel.deleteSong(musicNote);