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 f16a614..5aedc3b 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 @@ -48,4 +48,7 @@ public interface SongDao { @Query("DELETE FROM SONG WHERE localID IN (:localIDs)") void deleteSongsByLocalIDs(List localIDs); + + @Query("SELECT * FROM NoteSheet WHERE songID IN (:localSongIDs)") + List getNoteSheetsBySongIDs(List localSongIDs); } 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 2c98207..f4e702e 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 @@ -5,6 +5,7 @@ import android.os.Handler; import android.os.Looper; import com.stormtales.notevault.data.MusicDatabase; 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.sync.SyncStatus; import com.stormtales.notevault.network.sync.models.BatchCreateResponse; @@ -93,6 +94,14 @@ public class SongSyncRepository { }); } + public void getNoteSheetFilesBySongIDs(List songIDs, LoadDataCallback> callback) { + Executors.newSingleThreadExecutor().execute(() -> { + List noteSheets = songDao.getNoteSheetsBySongIDs(songIDs); + Handler mainHandler = new Handler(Looper.getMainLooper()); + mainHandler.post(()-> callback.onResult(noteSheets)); + }); + } + public interface LoadDataCallback { void onResult(T result); } diff --git a/app/src/main/java/com/stormtales/notevault/network/sync/SongSyncAPI.java b/app/src/main/java/com/stormtales/notevault/network/sync/SongSyncAPI.java index 5ec6449..ba93d7b 100644 --- a/app/src/main/java/com/stormtales/notevault/network/sync/SongSyncAPI.java +++ b/app/src/main/java/com/stormtales/notevault/network/sync/SongSyncAPI.java @@ -3,9 +3,13 @@ package com.stormtales.notevault.network.sync; import com.stormtales.notevault.network.auth.LoginRequest; import com.stormtales.notevault.network.auth.LoginResponse; import com.stormtales.notevault.network.sync.models.*; +import okhttp3.MultipartBody; +import okhttp3.RequestBody; import retrofit2.Call; import retrofit2.http.Body; +import retrofit2.http.Multipart; import retrofit2.http.POST; +import retrofit2.http.Part; public interface SongSyncAPI { @@ -17,4 +21,8 @@ public interface SongSyncAPI { @POST("/sync/songs/delete") Call syncDeletedSongs(@Body SongBatchDeleteRequest songBatchDeleteRequest); + + @Multipart + @POST("/sync/songs/note_sheet/upload") + Call uploadNoteSheet(@Part("serverID")RequestBody serverID, @Part("fileName") RequestBody fileName, @Part MultipartBody.Part image); } 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 f46d3c8..9bc045f 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 @@ -1,13 +1,16 @@ package com.stormtales.notevault.network.sync; import android.content.Context; +import com.stormtales.notevault.data.entities.Song; 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.ui.gallery.GalleryViewModel; +import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public class SongSyncModule { private SongRepository songRepository; @@ -26,7 +29,12 @@ public class SongSyncModule { songSyncRepository.loadCreatedSongs(result -> { songSyncService.syncCreatedSongs(result, response -> { songSyncRepository.markCreatedSongsAsSynced(response); - syncViewModel.finishCreateSongSyncing(); + List songIDs = result.stream().map(Song::getLocalID).collect(Collectors.toList()); + songSyncRepository.getNoteSheetFilesBySongIDs(songIDs, noteSheets -> { + songSyncService.uploadNoteSheetsOfCreatedSongs(noteSheets, response, uploadResponse -> { + 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 0aadb9b..92ef4c4 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 @@ -1,15 +1,20 @@ package com.stormtales.notevault.network.sync; import android.content.Context; +import com.stormtales.notevault.data.entities.NoteSheet; import com.stormtales.notevault.data.entities.Song; import com.stormtales.notevault.network.NetworkModule; import com.stormtales.notevault.network.auth.AuthAPI; import com.stormtales.notevault.network.sync.models.*; import com.stormtales.notevault.ui.gallery.GalleryViewModel; +import okhttp3.MediaType; +import okhttp3.MultipartBody; +import okhttp3.RequestBody; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; +import java.io.File; import java.util.ArrayList; import java.util.List; @@ -40,6 +45,38 @@ public class SongSyncService { }); } + public void uploadNoteSheetsOfCreatedSongs(List 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() { + @Override + public void onResponse(Call call, Response response) { + responses[0]++; + if(responses[0] == batchCreateResponse.getCreateResponses().size()) { + callback.finishSongSyncing(batchCreateResponse); + } + } + + @Override + public void onFailure(Call call, Throwable throwable) { + + } + }); + } + } + } + } + public void syncModifiedSongs(List songs, SongSyncModule.FinishSongSyncingCallback callback) { List modifyRequests = new ArrayList<>(); for(Song song : songs) { 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 new file mode 100644 index 0000000..84e1fc0 --- /dev/null +++ b/app/src/main/java/com/stormtales/notevault/network/sync/models/UploadResponse.java @@ -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; + } +}