From d221502bef58b13146c6f384839000df633be825 Mon Sep 17 00:00:00 2001 From: Fawkes100 Date: Sun, 19 Jan 2025 11:56:42 +0100 Subject: [PATCH] FIXME: Save ServerFileName for NoteSheets --- .../notevault/data/dao/SongDao.java | 9 +++++++++ .../notevault/data/entities/NoteSheet.java | 9 +++++++++ .../data/repositories/SongSyncRepository.java | 20 +++++++++++++++++++ .../network/sync/SongSyncModule.java | 16 ++++++++++----- .../network/sync/SongSyncService.java | 16 ++++++++++----- .../network/sync/models/UploadResponse.java | 18 +++++++++++++++++ 6 files changed, 78 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/stormtales/notevault/data/dao/SongDao.java b/app/src/main/java/com/stormtales/notevault/data/dao/SongDao.java index 5aedc3b..00d31ae 100644 --- a/app/src/main/java/com/stormtales/notevault/data/dao/SongDao.java +++ b/app/src/main/java/com/stormtales/notevault/data/dao/SongDao.java @@ -51,4 +51,13 @@ public interface SongDao { @Query("SELECT * FROM NoteSheet WHERE songID IN (:localSongIDs)") List getNoteSheetsBySongIDs(List localSongIDs); + + @Query("SELECT * FROM NoteSheet WHERE songID IN (:songIDs)") + List getNoteSheetsByLocalFiles(List songIDs); + + @Update + void updateNoteSheets(List uploadedNoteSheets); + + @Query("SELECT localID FROM Song WHERE serverID IN (:serverIDs)") + List getLocalSongIDsByServerIDs(List serverIDs); } diff --git a/app/src/main/java/com/stormtales/notevault/data/entities/NoteSheet.java b/app/src/main/java/com/stormtales/notevault/data/entities/NoteSheet.java index 4520136..12309d7 100644 --- a/app/src/main/java/com/stormtales/notevault/data/entities/NoteSheet.java +++ b/app/src/main/java/com/stormtales/notevault/data/entities/NoteSheet.java @@ -11,6 +11,7 @@ public class NoteSheet { private int localID; private int songID; private String localFileName; + private String serverFileName; private String hash; public NoteSheet(String localFileName, String hash) { @@ -42,6 +43,14 @@ public class NoteSheet { this.hash = hash; } + public String getServerFileName() { + return serverFileName; + } + + public void setServerFileName(String serverFileName) { + this.serverFileName = serverFileName; + } + @Override public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; diff --git a/app/src/main/java/com/stormtales/notevault/data/repositories/SongSyncRepository.java b/app/src/main/java/com/stormtales/notevault/data/repositories/SongSyncRepository.java index f4e702e..f9c52b8 100644 --- a/app/src/main/java/com/stormtales/notevault/data/repositories/SongSyncRepository.java +++ b/app/src/main/java/com/stormtales/notevault/data/repositories/SongSyncRepository.java @@ -11,7 +11,9 @@ import com.stormtales.notevault.data.sync.SyncStatus; import com.stormtales.notevault.network.sync.models.BatchCreateResponse; import com.stormtales.notevault.network.sync.models.BatchModifyResponse; import com.stormtales.notevault.network.sync.models.CreateResponse; +import com.stormtales.notevault.network.sync.models.UploadResponse; +import java.io.File; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; @@ -102,6 +104,24 @@ public class SongSyncRepository { }); } + public void markCreatedNoteSheetsAsSynced(List uploadResponses) { + Executors.newSingleThreadExecutor().execute(() -> { + List serverSongIDs = uploadResponses.stream().map(UploadResponse::getServerID).collect(Collectors.toList()); + List localSongIDs = songDao.getLocalSongIDsByServerIDs(serverSongIDs); + + List uploadedNoteSheets = songDao.getNoteSheetsBySongIDs(localSongIDs); + for(UploadResponse uploadResponse : uploadResponses) { + for(NoteSheet noteSheet : uploadedNoteSheets) { + if(new File(noteSheet.getLocalFileName()).getName().equals(uploadResponse.getLocalFile())) { + noteSheet.setServerFileName(uploadResponse.getServerFile()); + break; + } + } + } + songDao.updateNoteSheets(uploadedNoteSheets); + }); + } + public interface LoadDataCallback { void onResult(T result); } diff --git a/app/src/main/java/com/stormtales/notevault/network/sync/SongSyncModule.java b/app/src/main/java/com/stormtales/notevault/network/sync/SongSyncModule.java index 9bc045f..4e4abee 100644 --- a/app/src/main/java/com/stormtales/notevault/network/sync/SongSyncModule.java +++ b/app/src/main/java/com/stormtales/notevault/network/sync/SongSyncModule.java @@ -6,6 +6,7 @@ import com.stormtales.notevault.data.repositories.SongRepository; import com.stormtales.notevault.data.repositories.SongSyncRepository; import com.stormtales.notevault.network.sync.models.BatchCreateResponse; import com.stormtales.notevault.network.sync.models.BatchModifyResponse; +import com.stormtales.notevault.network.sync.models.UploadResponse; import com.stormtales.notevault.ui.gallery.GalleryViewModel; import java.util.ArrayList; @@ -29,12 +30,17 @@ public class SongSyncModule { songSyncRepository.loadCreatedSongs(result -> { songSyncService.syncCreatedSongs(result, response -> { songSyncRepository.markCreatedSongsAsSynced(response); - List songIDs = result.stream().map(Song::getLocalID).collect(Collectors.toList()); - songSyncRepository.getNoteSheetFilesBySongIDs(songIDs, noteSheets -> { - songSyncService.uploadNoteSheetsOfCreatedSongs(noteSheets, response, uploadResponse -> { - syncViewModel.finishCreateSongSyncing(); + if(response.getCreateResponses().isEmpty()) { + syncViewModel.finishCreateSongSyncing(); + } else { + List songIDs = result.stream().map(Song::getLocalID).collect(Collectors.toList()); + songSyncRepository.getNoteSheetFilesBySongIDs(songIDs, noteSheets -> { + songSyncService.uploadNoteSheetsOfCreatedSongs(noteSheets, response, uploadResponses -> { + songSyncRepository.markCreatedNoteSheetsAsSynced(uploadResponses); + syncViewModel.finishCreateSongSyncing(); + }); }); - }); + } }); }); } diff --git a/app/src/main/java/com/stormtales/notevault/network/sync/SongSyncService.java b/app/src/main/java/com/stormtales/notevault/network/sync/SongSyncService.java index 92ef4c4..7d1eeda 100644 --- a/app/src/main/java/com/stormtales/notevault/network/sync/SongSyncService.java +++ b/app/src/main/java/com/stormtales/notevault/network/sync/SongSyncService.java @@ -45,8 +45,8 @@ public class SongSyncService { }); } - public void uploadNoteSheetsOfCreatedSongs(List noteSheets, BatchCreateResponse batchCreateResponse, SongSyncModule.FinishSongCreateSyncingCallback callback) { - final int[] responses = {0}; + public void uploadNoteSheetsOfCreatedSongs(List noteSheets, BatchCreateResponse batchCreateResponse, UploadNoteSheetCallback callback) { + List uploadResponses = new ArrayList<>(); for(CreateResponse createResponse : batchCreateResponse.getCreateResponses()) { for(NoteSheet noteSheet : noteSheets) { if(noteSheet.getSongID() == createResponse.getLocalID()) { @@ -61,9 +61,11 @@ public class SongSyncService { songSyncAPI.uploadNoteSheet(serverID, fileName, image).enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { - responses[0]++; - if(responses[0] == batchCreateResponse.getCreateResponses().size()) { - callback.finishSongSyncing(batchCreateResponse); + if(response.isSuccessful() && response.body() != null) { + uploadResponses.add(response.body()); + if(uploadResponses.size() == noteSheets.size()) { + callback.finishUploadNoteSheets(uploadResponses); + } } } @@ -128,4 +130,8 @@ public class SongSyncService { public interface SyncDeletedSongsCallback { void finishSongSyncing(List remoteDeletedSongs, List localDeletedSongs); } + + public interface UploadNoteSheetCallback { + void finishUploadNoteSheets(List uploadResponses); + } } diff --git a/app/src/main/java/com/stormtales/notevault/network/sync/models/UploadResponse.java b/app/src/main/java/com/stormtales/notevault/network/sync/models/UploadResponse.java index 84e1fc0..92c23bd 100644 --- a/app/src/main/java/com/stormtales/notevault/network/sync/models/UploadResponse.java +++ b/app/src/main/java/com/stormtales/notevault/network/sync/models/UploadResponse.java @@ -2,6 +2,8 @@ package com.stormtales.notevault.network.sync.models; public class UploadResponse { private String serverID; + private String localFile; + private String serverFile; public String getServerID() { return serverID; @@ -10,4 +12,20 @@ public class UploadResponse { public void setServerID(String serverID) { this.serverID = serverID; } + + public String getLocalFile() { + return localFile; + } + + public void setLocalFile(String localFile) { + this.localFile = localFile; + } + + public String getServerFile() { + return serverFile; + } + + public void setServerFile(String serverFile) { + this.serverFile = serverFile; + } }