Implement Syncing Modifications in Songs (not tested yet)

This commit is contained in:
Fawkes100 2025-01-06 21:46:22 +01:00
parent 7a7b483ac2
commit b7c62f2328
7 changed files with 99 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -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<String> serverIDs, SyncStatus status, LocalDateTime currentTime);
}

View File

@ -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<SongCreationBatchResponse> performSongCreation(@Body SongCreationBatchRequest syncRequest);
@POST("/sync/songs/modify")
Call<SongModificationBatchResponse> performSongModification(@Body SongModificationBatchRequest syncRequest);
}

View File

@ -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<MusicNote> created_songs = database.getSongsWithSyncStatus(SyncStatus.CREATED);
List<SongCreationRequest> songCreationRequests = new ArrayList<>();
for(MusicNote musicNote : created_songs) {
@ -60,4 +63,30 @@ public class SongSyncWorker {
}
});
}
private SongModificationBatchRequest buildModificationRequest() {
List<MusicNote> modified_songs = database.getSongsWithSyncStatus(SyncStatus.MODIFIED);
List<SongModificationRequest> 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<SongModificationBatchResponse>() {
@Override
public void onResponse(Call<SongModificationBatchResponse> call, Response<SongModificationBatchResponse> response) {
executorService.execute(() -> {
});
}
@Override
public void onFailure(Call<SongModificationBatchResponse> call, Throwable throwable) {
Toast.makeText(context, "Sync of Modified Songs failed", Toast.LENGTH_LONG).show();
}
});
}
}

View File

@ -0,0 +1,11 @@
package core.notevault.sync.synchronisation.songs.modification;
import java.util.List;
public class SongModificationBatchRequest {
private List<SongModificationRequest> songs;
public SongModificationBatchRequest(List<SongModificationRequest> songs) {
this.songs = songs;
}
}

View File

@ -0,0 +1,19 @@
package core.notevault.sync.synchronisation.songs.modification;
import java.util.List;
public class SongModificationBatchResponse {
private List<String> updated_songs;
public SongModificationBatchResponse(List<String> updated_songs) {
this.updated_songs = updated_songs;
}
public List<String> getUpdated_songs() {
return updated_songs;
}
public void setUpdated_songs(List<String> updated_songs) {
this.updated_songs = updated_songs;
}
}

View File

@ -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;
}
}