Implement Marking as Deletion

This commit is contained in:
Fawkes100 2025-01-07 17:56:14 +01:00
parent a550ff0ac8
commit c23f188921
7 changed files with 81 additions and 2 deletions

View File

@ -17,7 +17,7 @@ public interface MusicNoteDAO {
@Insert @Insert
void insertNoteSheet(NoteSheet noteSheet); void insertNoteSheet(NoteSheet noteSheet);
@Query("SELECT * FROM music_notes") @Query("SELECT * FROM music_notes WHERE syncStatus != 'DELETED'")
List<MusicNote> getAllNotes(); List<MusicNote> getAllNotes();
@Query("SELECT * FROM note_sheets WHERE musicNoteId = :musicNoteId") @Query("SELECT * FROM note_sheets WHERE musicNoteId = :musicNoteId")
@ -68,4 +68,9 @@ public interface MusicNoteDAO {
@Update @Update
void updateSong(MusicNote musicNote); void updateSong(MusicNote musicNote);
@Query("DELETE FROM music_notes WHERE serverID IN (:serverIDs)")
void deleteSongs(List<String> serverIDs);
} }

View File

@ -40,6 +40,7 @@ public class SyncWorker extends Worker{
try { try {
songSyncWorker.syncSongCreations(); songSyncWorker.syncSongCreations();
songSyncWorker.syncSongModifications(); songSyncWorker.syncSongModifications();
songSyncWorker.syncSongDeletions();
return Result.success(); return Result.success();
} catch (Exception e) { } catch (Exception e) {
return Result.failure(); return Result.failure();

View File

@ -3,6 +3,8 @@ package core.notevault.sync.synchronisation;
import core.notevault.data.sync.SyncResponse; import core.notevault.data.sync.SyncResponse;
import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchRequest; import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchRequest;
import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchResponse; 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.SongModificationBatchRequest;
import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchResponse; import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchResponse;
import retrofit2.Call; import retrofit2.Call;
@ -16,4 +18,7 @@ public interface SyncService {
@POST("/sync/songs/modify") @POST("/sync/songs/modify")
Call<SongModificationBatchResponse> performSongModification(@Body SongModificationBatchRequest syncRequest); Call<SongModificationBatchResponse> performSongModification(@Body SongModificationBatchRequest syncRequest);
@POST("/sync/songs/delete")
Call<SongDeletionBatchResponse> performSongDeletion(@Body SongDeletionBatchRequest syncRequest);
} }

View File

@ -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.SongCreationBatchRequest;
import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchResponse; import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchResponse;
import core.notevault.sync.synchronisation.songs.creation.SongCreationRequest; 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.SongModificationBatchRequest;
import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchResponse; import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchResponse;
import core.notevault.sync.synchronisation.songs.modification.SongModificationRequest; import core.notevault.sync.synchronisation.songs.modification.SongModificationRequest;
@ -22,6 +24,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.stream.Collectors;
public class SongSyncWorker { public class SongSyncWorker {
private SyncService syncService; private SyncService syncService;
@ -90,4 +93,29 @@ public class SongSyncWorker {
} }
}); });
} }
private SongDeletionBatchRequest buildDeletionBatchRequest() {
List<MusicNote> deleted_songs = database.getSongsWithSyncStatus(SyncStatus.DELETED);
List<String> 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<SongDeletionBatchResponse>() {
@Override
public void onResponse(Call<SongDeletionBatchResponse> call, Response<SongDeletionBatchResponse> response) {
if(response.isSuccessful() && response.body() != null) {
executorService.execute(() -> {
database.deleteSongs(response.body().getDeletedSongs());
});
}
}
@Override
public void onFailure(Call<SongDeletionBatchResponse> call, Throwable throwable) {
Toast.makeText(context, "Sync of Deleted Songs failed", Toast.LENGTH_LONG).show();
}
});
}
} }

View File

@ -0,0 +1,19 @@
package core.notevault.sync.synchronisation.songs.deletion;
import java.util.List;
public class SongDeletionBatchRequest {
private List<String> songs;
public SongDeletionBatchRequest(List<String> songs) {
this.songs = songs;
}
public List<String> getSongs() {
return songs;
}
public void setSongs(List<String> songs) {
this.songs = songs;
}
}

View File

@ -0,0 +1,19 @@
package core.notevault.sync.synchronisation.songs.deletion;
import java.util.List;
public class SongDeletionBatchResponse {
private List<String> deletedSongs;
public SongDeletionBatchResponse(List<String> deletedSongs) {
this.deletedSongs = deletedSongs;
}
public List<String> getDeletedSongs() {
return deletedSongs;
}
public void setDeletedSongs(List<String> deletedSongs) {
this.deletedSongs = deletedSongs;
}
}

View File

@ -23,6 +23,7 @@ import core.notevault.R;
import core.notevault.data.MusicDatabase; import core.notevault.data.MusicDatabase;
import core.notevault.data.MusicNote; import core.notevault.data.MusicNote;
import core.notevault.data.MusicNoteDAO; import core.notevault.data.MusicNoteDAO;
import core.notevault.data.sync.SyncStatus;
import core.notevault.databinding.FragmentHomeBinding; import core.notevault.databinding.FragmentHomeBinding;
import core.notevault.ui.metadatadialog.MetaDataDialog; import core.notevault.ui.metadatadialog.MetaDataDialog;
import core.notevault.ui.metadatadialog.SongEditDialog; import core.notevault.ui.metadatadialog.SongEditDialog;
@ -78,7 +79,8 @@ public class HomeFragment extends Fragment {
new Thread(() -> { new Thread(() -> {
MusicDatabase musicDatabase = MusicDatabase.getDatabase(getContext()); MusicDatabase musicDatabase = MusicDatabase.getDatabase(getContext());
MusicNoteDAO musicNoteDAO = musicDatabase.musicNoteDao(); MusicNoteDAO musicNoteDAO = musicDatabase.musicNoteDao();
musicNoteDAO.deleteSong(musicNote); musicNote.setSyncStatus(SyncStatus.DELETED);
musicNoteDAO.updateSong(musicNote);
Log.d("HomeFragment", "Delete Song: " + musicNote.getTitle()); Log.d("HomeFragment", "Delete Song: " + musicNote.getTitle());
new Handler(Looper.getMainLooper()).post(()-> { new Handler(Looper.getMainLooper()).post(()-> {
homeViewModel.deleteSong(musicNote); homeViewModel.deleteSong(musicNote);