Sync Images
This commit is contained in:
parent
c23f188921
commit
ceb5b5f671
@ -7,6 +7,7 @@ import core.notevault.sync.synchronisation.songs.creation.SongCreationResponse;
|
|||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Dao
|
@Dao
|
||||||
public interface MusicNoteDAO {
|
public interface MusicNoteDAO {
|
||||||
@ -72,5 +73,12 @@ public interface MusicNoteDAO {
|
|||||||
@Query("DELETE FROM music_notes WHERE serverID IN (:serverIDs)")
|
@Query("DELETE FROM music_notes WHERE serverID IN (:serverIDs)")
|
||||||
void deleteSongs(List<String> serverIDs);
|
void deleteSongs(List<String> serverIDs);
|
||||||
|
|
||||||
|
@Query("SELECT * FROM note_sheets ns WHERE ns.syncStatus = :status")
|
||||||
|
List<NoteSheet> getNoteSheetsWithStatus(SyncStatus status);
|
||||||
|
|
||||||
|
@Query("SELECT mn.serverID FROM music_notes mn WHERE mn.musicNoteId =:musicNoteId")
|
||||||
|
Optional<String> getMusicNoteByLocalID(long musicNoteId);
|
||||||
|
|
||||||
|
@Update
|
||||||
|
void updateNoteSheet(NoteSheet noteSheet);
|
||||||
}
|
}
|
||||||
|
@ -2,19 +2,21 @@ package core.notevault.data;
|
|||||||
|
|
||||||
import androidx.room.Entity;
|
import androidx.room.Entity;
|
||||||
import androidx.room.PrimaryKey;
|
import androidx.room.PrimaryKey;
|
||||||
|
import core.notevault.data.sync.SyncStatus;
|
||||||
|
|
||||||
@Entity(tableName = "note_sheets")
|
@Entity(tableName = "note_sheets")
|
||||||
public class NoteSheet {
|
public class NoteSheet {
|
||||||
|
|
||||||
@PrimaryKey(autoGenerate = true)
|
@PrimaryKey(autoGenerate = true)
|
||||||
private int id;
|
private int id;
|
||||||
|
private SyncStatus syncStatus;
|
||||||
private long musicNoteId;
|
private long musicNoteId;
|
||||||
private String filePath;
|
private String filePath;
|
||||||
|
|
||||||
public NoteSheet(long musicNoteID, String filePath) {
|
public NoteSheet(long musicNoteID, String filePath) {
|
||||||
this.musicNoteId = musicNoteID;
|
this.musicNoteId = musicNoteID;
|
||||||
this.filePath = filePath;
|
this.filePath = filePath;
|
||||||
|
this.syncStatus = SyncStatus.CREATED;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NoteSheet() {
|
public NoteSheet() {
|
||||||
@ -43,4 +45,12 @@ public class NoteSheet {
|
|||||||
public void setFilePath(String filePath) {
|
public void setFilePath(String filePath) {
|
||||||
this.filePath = filePath;
|
this.filePath = filePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SyncStatus getSyncStatus() {
|
||||||
|
return syncStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSyncStatus(SyncStatus syncStatus) {
|
||||||
|
this.syncStatus = syncStatus;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,8 +41,10 @@ public class SyncWorker extends Worker{
|
|||||||
songSyncWorker.syncSongCreations();
|
songSyncWorker.syncSongCreations();
|
||||||
songSyncWorker.syncSongModifications();
|
songSyncWorker.syncSongModifications();
|
||||||
songSyncWorker.syncSongDeletions();
|
songSyncWorker.syncSongDeletions();
|
||||||
|
songSyncWorker.uploadNoteSheets();
|
||||||
return Result.success();
|
return Result.success();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
return Result.failure();
|
return Result.failure();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,14 @@ import core.notevault.sync.synchronisation.songs.deletion.SongDeletionBatchReque
|
|||||||
import core.notevault.sync.synchronisation.songs.deletion.SongDeletionBatchResponse;
|
import core.notevault.sync.synchronisation.songs.deletion.SongDeletionBatchResponse;
|
||||||
import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchRequest;
|
import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchRequest;
|
||||||
import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchResponse;
|
import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchResponse;
|
||||||
|
import core.notevault.sync.synchronisation.songs.notesheets.UploadResponse;
|
||||||
|
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 SyncService {
|
public interface SyncService {
|
||||||
|
|
||||||
@ -21,4 +26,9 @@ public interface SyncService {
|
|||||||
|
|
||||||
@POST("/sync/songs/delete")
|
@POST("/sync/songs/delete")
|
||||||
Call<SongDeletionBatchResponse> performSongDeletion(@Body SongDeletionBatchRequest syncRequest);
|
Call<SongDeletionBatchResponse> performSongDeletion(@Body SongDeletionBatchRequest syncRequest);
|
||||||
|
|
||||||
|
@Multipart
|
||||||
|
@POST("/sync/songs/note_sheet/upload")
|
||||||
|
Call<UploadResponse> uploadNoteSheet(@Part("serverID") RequestBody serverID, @Part("fileName") RequestBody fileName,
|
||||||
|
@Part MultipartBody.Part image);
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import android.os.AsyncTask;
|
|||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
import core.notevault.data.MusicNote;
|
import core.notevault.data.MusicNote;
|
||||||
import core.notevault.data.MusicNoteDAO;
|
import core.notevault.data.MusicNoteDAO;
|
||||||
|
import core.notevault.data.NoteSheet;
|
||||||
import core.notevault.data.sync.SyncStatus;
|
import core.notevault.data.sync.SyncStatus;
|
||||||
import core.notevault.sync.synchronisation.SyncService;
|
import core.notevault.sync.synchronisation.SyncService;
|
||||||
import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchRequest;
|
import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchRequest;
|
||||||
@ -15,13 +16,19 @@ import core.notevault.sync.synchronisation.songs.deletion.SongDeletionBatchRespo
|
|||||||
import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchRequest;
|
import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchRequest;
|
||||||
import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchResponse;
|
import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchResponse;
|
||||||
import core.notevault.sync.synchronisation.songs.modification.SongModificationRequest;
|
import core.notevault.sync.synchronisation.songs.modification.SongModificationRequest;
|
||||||
|
import core.notevault.sync.synchronisation.songs.notesheets.UploadResponse;
|
||||||
|
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.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -118,4 +125,47 @@ public class SongSyncWorker {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void uploadNoteSheets() {
|
||||||
|
List<NoteSheet> noteSheets = database.getNoteSheetsWithStatus(SyncStatus.CREATED);
|
||||||
|
for(NoteSheet noteSheet : noteSheets) {
|
||||||
|
Optional<String> serverID = database.getMusicNoteByLocalID(noteSheet.getMusicNoteId());
|
||||||
|
|
||||||
|
if(serverID.isPresent()) {
|
||||||
|
File imageFile = new File(noteSheet.getFilePath());
|
||||||
|
// Erstelle Request-Bodies für serverID und fileName
|
||||||
|
RequestBody serverIDBody = RequestBody.create(MediaType.parse("text/plain"), serverID.get());
|
||||||
|
RequestBody fileNameBody = RequestBody.create(MediaType.parse("text/plain"), imageFile.getName());
|
||||||
|
|
||||||
|
// Erstelle den Multipart-Body für die Bilddatei
|
||||||
|
RequestBody imageRequestBody = RequestBody.create(MediaType.parse("image/*"), imageFile);
|
||||||
|
MultipartBody.Part imagePart = MultipartBody.Part.createFormData("image", imageFile.getName(), imageRequestBody);
|
||||||
|
|
||||||
|
syncService.uploadNoteSheet(serverIDBody, fileNameBody, imagePart).enqueue(new Callback<UploadResponse>() {
|
||||||
|
@Override
|
||||||
|
public void onResponse(Call<UploadResponse> call, Response<UploadResponse> response) {
|
||||||
|
if (response.isSuccessful() && response.body() != null) {
|
||||||
|
// Erfolg: Antwort verarbeiten
|
||||||
|
Toast.makeText(context, "Upload erfolgreich", Toast.LENGTH_LONG).show();
|
||||||
|
executorService.execute(() -> {
|
||||||
|
noteSheet.setSyncStatus(SyncStatus.SYNCED);
|
||||||
|
database.updateNoteSheet(noteSheet);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Fehler: Antwort prüfen
|
||||||
|
Toast.makeText(context, "Fehler beim Upload: " + response.code(), Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(Call<UploadResponse> call, Throwable throwable) {
|
||||||
|
Toast.makeText(context, "Fehler beim Upload: " + throwable.toString(), Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
package core.notevault.sync.synchronisation.songs.notesheets;
|
||||||
|
|
||||||
|
public class UploadResponse {
|
||||||
|
private String serverID;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user