ADD: Upload NoteSheets when Creating Songs

This commit is contained in:
Fawkes100 2025-01-19 11:13:46 +01:00
parent ebe09e0ba1
commit dd0bfc2672
6 changed files with 79 additions and 1 deletions

View File

@ -48,4 +48,7 @@ public interface SongDao {
@Query("DELETE FROM SONG WHERE localID IN (:localIDs)") @Query("DELETE FROM SONG WHERE localID IN (:localIDs)")
void deleteSongsByLocalIDs(List<Integer> localIDs); void deleteSongsByLocalIDs(List<Integer> localIDs);
@Query("SELECT * FROM NoteSheet WHERE songID IN (:localSongIDs)")
List<NoteSheet> getNoteSheetsBySongIDs(List<Integer> localSongIDs);
} }

View File

@ -5,6 +5,7 @@ import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import com.stormtales.notevault.data.MusicDatabase; import com.stormtales.notevault.data.MusicDatabase;
import com.stormtales.notevault.data.dao.SongDao; import com.stormtales.notevault.data.dao.SongDao;
import com.stormtales.notevault.data.entities.NoteSheet;
import com.stormtales.notevault.data.entities.Song; import com.stormtales.notevault.data.entities.Song;
import com.stormtales.notevault.data.sync.SyncStatus; import com.stormtales.notevault.data.sync.SyncStatus;
import com.stormtales.notevault.network.sync.models.BatchCreateResponse; import com.stormtales.notevault.network.sync.models.BatchCreateResponse;
@ -93,6 +94,14 @@ public class SongSyncRepository {
}); });
} }
public void getNoteSheetFilesBySongIDs(List<Integer> songIDs, LoadDataCallback<List<NoteSheet>> callback) {
Executors.newSingleThreadExecutor().execute(() -> {
List<NoteSheet> noteSheets = songDao.getNoteSheetsBySongIDs(songIDs);
Handler mainHandler = new Handler(Looper.getMainLooper());
mainHandler.post(()-> callback.onResult(noteSheets));
});
}
public interface LoadDataCallback<T> { public interface LoadDataCallback<T> {
void onResult(T result); void onResult(T result);
} }

View File

@ -3,9 +3,13 @@ package com.stormtales.notevault.network.sync;
import com.stormtales.notevault.network.auth.LoginRequest; import com.stormtales.notevault.network.auth.LoginRequest;
import com.stormtales.notevault.network.auth.LoginResponse; import com.stormtales.notevault.network.auth.LoginResponse;
import com.stormtales.notevault.network.sync.models.*; import com.stormtales.notevault.network.sync.models.*;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.http.Body; import retrofit2.http.Body;
import retrofit2.http.Multipart;
import retrofit2.http.POST; import retrofit2.http.POST;
import retrofit2.http.Part;
public interface SongSyncAPI { public interface SongSyncAPI {
@ -17,4 +21,8 @@ public interface SongSyncAPI {
@POST("/sync/songs/delete") @POST("/sync/songs/delete")
Call<BatchModifyResponse> syncDeletedSongs(@Body SongBatchDeleteRequest songBatchDeleteRequest); Call<BatchModifyResponse> syncDeletedSongs(@Body SongBatchDeleteRequest songBatchDeleteRequest);
@Multipart
@POST("/sync/songs/note_sheet/upload")
Call<UploadResponse> uploadNoteSheet(@Part("serverID")RequestBody serverID, @Part("fileName") RequestBody fileName, @Part MultipartBody.Part image);
} }

View File

@ -1,13 +1,16 @@
package com.stormtales.notevault.network.sync; package com.stormtales.notevault.network.sync;
import android.content.Context; import android.content.Context;
import com.stormtales.notevault.data.entities.Song;
import com.stormtales.notevault.data.repositories.SongRepository; import com.stormtales.notevault.data.repositories.SongRepository;
import com.stormtales.notevault.data.repositories.SongSyncRepository; import com.stormtales.notevault.data.repositories.SongSyncRepository;
import com.stormtales.notevault.network.sync.models.BatchCreateResponse; import com.stormtales.notevault.network.sync.models.BatchCreateResponse;
import com.stormtales.notevault.network.sync.models.BatchModifyResponse; import com.stormtales.notevault.network.sync.models.BatchModifyResponse;
import com.stormtales.notevault.ui.gallery.GalleryViewModel; import com.stormtales.notevault.ui.gallery.GalleryViewModel;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
public class SongSyncModule { public class SongSyncModule {
private SongRepository songRepository; private SongRepository songRepository;
@ -26,9 +29,14 @@ public class SongSyncModule {
songSyncRepository.loadCreatedSongs(result -> { songSyncRepository.loadCreatedSongs(result -> {
songSyncService.syncCreatedSongs(result, response -> { songSyncService.syncCreatedSongs(result, response -> {
songSyncRepository.markCreatedSongsAsSynced(response); songSyncRepository.markCreatedSongsAsSynced(response);
List<Integer> songIDs = result.stream().map(Song::getLocalID).collect(Collectors.toList());
songSyncRepository.getNoteSheetFilesBySongIDs(songIDs, noteSheets -> {
songSyncService.uploadNoteSheetsOfCreatedSongs(noteSheets, response, uploadResponse -> {
syncViewModel.finishCreateSongSyncing(); syncViewModel.finishCreateSongSyncing();
}); });
}); });
});
});
} }
public void syncModifiedSongs() { public void syncModifiedSongs() {

View File

@ -1,15 +1,20 @@
package com.stormtales.notevault.network.sync; package com.stormtales.notevault.network.sync;
import android.content.Context; import android.content.Context;
import com.stormtales.notevault.data.entities.NoteSheet;
import com.stormtales.notevault.data.entities.Song; import com.stormtales.notevault.data.entities.Song;
import com.stormtales.notevault.network.NetworkModule; import com.stormtales.notevault.network.NetworkModule;
import com.stormtales.notevault.network.auth.AuthAPI; import com.stormtales.notevault.network.auth.AuthAPI;
import com.stormtales.notevault.network.sync.models.*; import com.stormtales.notevault.network.sync.models.*;
import com.stormtales.notevault.ui.gallery.GalleryViewModel; import com.stormtales.notevault.ui.gallery.GalleryViewModel;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -40,6 +45,38 @@ public class SongSyncService {
}); });
} }
public void uploadNoteSheetsOfCreatedSongs(List<NoteSheet> noteSheets, BatchCreateResponse batchCreateResponse, SongSyncModule.FinishSongCreateSyncingCallback callback) {
final int[] responses = {0};
for(CreateResponse createResponse : batchCreateResponse.getCreateResponses()) {
for(NoteSheet noteSheet : noteSheets) {
if(noteSheet.getSongID() == createResponse.getLocalID()) {
File imageFile = new File(noteSheet.getLocalFileName());
RequestBody serverID = RequestBody.create(MediaType.parse("text/plain"), createResponse.getServerID());
RequestBody fileName = RequestBody.create(MediaType.parse("text/plain"), imageFile.getName());
RequestBody requestFile = RequestBody.create(MediaType.parse("image/**"), new File(noteSheet.getLocalFileName()));
MultipartBody.Part image = MultipartBody.Part.createFormData("image", noteSheet.getLocalFileName(), requestFile);
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);
}
}
@Override
public void onFailure(Call<UploadResponse> call, Throwable throwable) {
}
});
}
}
}
}
public void syncModifiedSongs(List<Song> songs, SongSyncModule.FinishSongSyncingCallback callback) { public void syncModifiedSongs(List<Song> songs, SongSyncModule.FinishSongSyncingCallback callback) {
List<SongModifyRequest> modifyRequests = new ArrayList<>(); List<SongModifyRequest> modifyRequests = new ArrayList<>();
for(Song song : songs) { for(Song song : songs) {

View File

@ -0,0 +1,13 @@
package com.stormtales.notevault.network.sync.models;
public class UploadResponse {
private String serverID;
public String getServerID() {
return serverID;
}
public void setServerID(String serverID) {
this.serverID = serverID;
}
}