nextNoteVault #23
@ -48,4 +48,7 @@ public interface SongDao {
 | 
			
		||||
 | 
			
		||||
    @Query("DELETE FROM SONG WHERE localID IN (:localIDs)")
 | 
			
		||||
    void deleteSongsByLocalIDs(List<Integer> localIDs);
 | 
			
		||||
 | 
			
		||||
    @Query("SELECT * FROM NoteSheet WHERE songID IN (:localSongIDs)")
 | 
			
		||||
    List<NoteSheet> getNoteSheetsBySongIDs(List<Integer> localSongIDs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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<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> {
 | 
			
		||||
        void onResult(T result);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -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<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);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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<Integer> songIDs = result.stream().map(Song::getLocalID).collect(Collectors.toList());
 | 
			
		||||
                songSyncRepository.getNoteSheetFilesBySongIDs(songIDs, noteSheets -> {
 | 
			
		||||
                    songSyncService.uploadNoteSheetsOfCreatedSongs(noteSheets, response, uploadResponse -> {
 | 
			
		||||
                        syncViewModel.finishCreateSongSyncing();
 | 
			
		||||
                    });
 | 
			
		||||
                });
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -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<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) {
 | 
			
		||||
        List<SongModifyRequest> modifyRequests = new ArrayList<>();
 | 
			
		||||
        for(Song song : songs) {
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user