FIXME: Save ServerFileName for NoteSheets

This commit is contained in:
Fawkes100 2025-01-19 11:56:42 +01:00
parent dd0bfc2672
commit d221502bef
6 changed files with 78 additions and 10 deletions

View File

@ -51,4 +51,13 @@ public interface SongDao {
@Query("SELECT * FROM NoteSheet WHERE songID IN (:localSongIDs)")
List<NoteSheet> getNoteSheetsBySongIDs(List<Integer> localSongIDs);
@Query("SELECT * FROM NoteSheet WHERE songID IN (:songIDs)")
List<NoteSheet> getNoteSheetsByLocalFiles(List<Integer> songIDs);
@Update
void updateNoteSheets(List<NoteSheet> uploadedNoteSheets);
@Query("SELECT localID FROM Song WHERE serverID IN (:serverIDs)")
List<Integer> getLocalSongIDsByServerIDs(List<String> serverIDs);
}

View File

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

View File

@ -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<UploadResponse> uploadResponses) {
Executors.newSingleThreadExecutor().execute(() -> {
List<String> serverSongIDs = uploadResponses.stream().map(UploadResponse::getServerID).collect(Collectors.toList());
List<Integer> localSongIDs = songDao.getLocalSongIDsByServerIDs(serverSongIDs);
List<NoteSheet> 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<T> {
void onResult(T result);
}

View File

@ -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);
if(response.getCreateResponses().isEmpty()) {
syncViewModel.finishCreateSongSyncing();
} else {
List<Integer> songIDs = result.stream().map(Song::getLocalID).collect(Collectors.toList());
songSyncRepository.getNoteSheetFilesBySongIDs(songIDs, noteSheets -> {
songSyncService.uploadNoteSheetsOfCreatedSongs(noteSheets, response, uploadResponse -> {
songSyncService.uploadNoteSheetsOfCreatedSongs(noteSheets, response, uploadResponses -> {
songSyncRepository.markCreatedNoteSheetsAsSynced(uploadResponses);
syncViewModel.finishCreateSongSyncing();
});
});
}
});
});
}

View File

@ -45,8 +45,8 @@ public class SongSyncService {
});
}
public void uploadNoteSheetsOfCreatedSongs(List<NoteSheet> noteSheets, BatchCreateResponse batchCreateResponse, SongSyncModule.FinishSongCreateSyncingCallback callback) {
final int[] responses = {0};
public void uploadNoteSheetsOfCreatedSongs(List<NoteSheet> noteSheets, BatchCreateResponse batchCreateResponse, UploadNoteSheetCallback callback) {
List<UploadResponse> 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<UploadResponse>() {
@Override
public void onResponse(Call<UploadResponse> call, Response<UploadResponse> 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<String> remoteDeletedSongs, List<Integer> localDeletedSongs);
}
public interface UploadNoteSheetCallback {
void finishUploadNoteSheets(List<UploadResponse> uploadResponses);
}
}

View File

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