From b7c62f2328bc27141b5c677e50dd4b868bf0ae36 Mon Sep 17 00:00:00 2001 From: Fawkes100 Date: Mon, 6 Jan 2025 21:46:22 +0100 Subject: [PATCH] Implement Syncing Modifications in Songs (not tested yet) --- .../java/core/notevault/data/MusicNote.java | 12 +++++++ .../core/notevault/data/MusicNoteDAO.java | 5 +++ .../sync/synchronisation/SyncService.java | 5 +++ .../synchronisation/songs/SongSyncWorker.java | 31 ++++++++++++++++++- .../SongModificationBatchRequest.java | 11 +++++++ .../SongModificationBatchResponse.java | 19 ++++++++++++ .../modification/SongModificationRequest.java | 17 ++++++++++ 7 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/core/notevault/sync/synchronisation/songs/modification/SongModificationBatchRequest.java create mode 100644 app/src/main/java/core/notevault/sync/synchronisation/songs/modification/SongModificationBatchResponse.java create mode 100644 app/src/main/java/core/notevault/sync/synchronisation/songs/modification/SongModificationRequest.java diff --git a/app/src/main/java/core/notevault/data/MusicNote.java b/app/src/main/java/core/notevault/data/MusicNote.java index 36fb3ea..308e946 100644 --- a/app/src/main/java/core/notevault/data/MusicNote.java +++ b/app/src/main/java/core/notevault/data/MusicNote.java @@ -5,6 +5,8 @@ import androidx.room.Ignore; import androidx.room.PrimaryKey; import core.notevault.data.sync.SyncStatus; +import java.time.LocalDateTime; + @Entity(tableName = "music_notes") public class MusicNote { @@ -12,6 +14,7 @@ public class MusicNote { private long musicNoteId; private String serverID; private SyncStatus syncStatus; + private LocalDateTime last_sync; private String title; private String composer; @@ -92,4 +95,13 @@ public class MusicNote { public void setSyncStatus(SyncStatus syncStatus) { this.syncStatus = syncStatus; } + + public LocalDateTime getLast_sync() { + + return last_sync; + } + + public void setLast_sync(LocalDateTime last_sync) { + this.last_sync = last_sync; + } } diff --git a/app/src/main/java/core/notevault/data/MusicNoteDAO.java b/app/src/main/java/core/notevault/data/MusicNoteDAO.java index 470f7d8..580af49 100644 --- a/app/src/main/java/core/notevault/data/MusicNoteDAO.java +++ b/app/src/main/java/core/notevault/data/MusicNoteDAO.java @@ -62,4 +62,9 @@ public interface MusicNoteDAO { @Query("UPDATE music_notes SET syncStatus = :status, serverID = :serverID WHERE musicNoteId = :localID") void storeSongSyncResponse(long localID, SyncStatus status, String serverID); + + @Query("UPDATE music_notes SET syncStatus = :status, last_sync = :currentTime WHERE serverID IN (:serverIDs)") + void storeSongSyncModifyResponses(List serverIDs, SyncStatus status, LocalDateTime currentTime); + + } 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 7d967b5..c70096e 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.modification.SongModificationBatchRequest; +import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchResponse; import retrofit2.Call; import retrofit2.http.Body; import retrofit2.http.POST; @@ -11,4 +13,7 @@ public interface SyncService { @POST("/sync/songs/create") Call performSongCreation(@Body SongCreationBatchRequest syncRequest); + + @POST("/sync/songs/modify") + Call performSongModification(@Body SongModificationBatchRequest 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 2327a07..2ba1d39 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,9 @@ 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.modification.SongModificationBatchRequest; +import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchResponse; +import core.notevault.sync.synchronisation.songs.modification.SongModificationRequest; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -31,7 +34,7 @@ public class SongSyncWorker { this.context = context; } - public SongCreationBatchRequest buildCreationRequest() { + private SongCreationBatchRequest buildCreationRequest() { List created_songs = database.getSongsWithSyncStatus(SyncStatus.CREATED); List songCreationRequests = new ArrayList<>(); for(MusicNote musicNote : created_songs) { @@ -60,4 +63,30 @@ public class SongSyncWorker { } }); } + + private SongModificationBatchRequest buildModificationRequest() { + List modified_songs = database.getSongsWithSyncStatus(SyncStatus.MODIFIED); + List songModificationRequests = new ArrayList<>(); + for(MusicNote musicNote : modified_songs) { + songModificationRequests.add(new SongModificationRequest(musicNote.getServerID(), musicNote.getTitle(), musicNote.getComposer(), musicNote.getGenre(), musicNote.getYear())); + } + return new SongModificationBatchRequest(songModificationRequests); + } + + public void syncSongModifications() { + SongModificationBatchRequest request = buildModificationRequest(); + syncService.performSongModification(request).enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + executorService.execute(() -> { + + }); + } + + @Override + public void onFailure(Call call, Throwable throwable) { + Toast.makeText(context, "Sync of Modified Songs failed", Toast.LENGTH_LONG).show(); + } + }); + } } diff --git a/app/src/main/java/core/notevault/sync/synchronisation/songs/modification/SongModificationBatchRequest.java b/app/src/main/java/core/notevault/sync/synchronisation/songs/modification/SongModificationBatchRequest.java new file mode 100644 index 0000000..4f5f59e --- /dev/null +++ b/app/src/main/java/core/notevault/sync/synchronisation/songs/modification/SongModificationBatchRequest.java @@ -0,0 +1,11 @@ +package core.notevault.sync.synchronisation.songs.modification; + +import java.util.List; + +public class SongModificationBatchRequest { + private List songs; + + public SongModificationBatchRequest(List songs) { + this.songs = songs; + } +} diff --git a/app/src/main/java/core/notevault/sync/synchronisation/songs/modification/SongModificationBatchResponse.java b/app/src/main/java/core/notevault/sync/synchronisation/songs/modification/SongModificationBatchResponse.java new file mode 100644 index 0000000..86fa4a4 --- /dev/null +++ b/app/src/main/java/core/notevault/sync/synchronisation/songs/modification/SongModificationBatchResponse.java @@ -0,0 +1,19 @@ +package core.notevault.sync.synchronisation.songs.modification; + +import java.util.List; + +public class SongModificationBatchResponse { + private List updated_songs; + + public SongModificationBatchResponse(List updated_songs) { + this.updated_songs = updated_songs; + } + + public List getUpdated_songs() { + return updated_songs; + } + + public void setUpdated_songs(List updated_songs) { + this.updated_songs = updated_songs; + } +} diff --git a/app/src/main/java/core/notevault/sync/synchronisation/songs/modification/SongModificationRequest.java b/app/src/main/java/core/notevault/sync/synchronisation/songs/modification/SongModificationRequest.java new file mode 100644 index 0000000..bfb8d73 --- /dev/null +++ b/app/src/main/java/core/notevault/sync/synchronisation/songs/modification/SongModificationRequest.java @@ -0,0 +1,17 @@ +package core.notevault.sync.synchronisation.songs.modification; + +public class SongModificationRequest { + private String serverID; + private String title; + private String composer; + private String genre; + private int year; + + public SongModificationRequest(String serverID, String title, String composer, String genre, int year) { + this.serverID = serverID; + this.title = title; + this.composer = composer; + this.genre = genre; + this.year = year; + } +}