Sync Images

This commit is contained in:
Fawkes100 2025-01-07 19:35:14 +01:00
parent c23f188921
commit ceb5b5f671
6 changed files with 86 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import core.notevault.sync.synchronisation.songs.creation.SongCreationResponse;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
@Dao
public interface MusicNoteDAO {
@ -72,5 +73,12 @@ public interface MusicNoteDAO {
@Query("DELETE FROM music_notes WHERE serverID IN (: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);
}

View File

@ -2,19 +2,21 @@ package core.notevault.data;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import core.notevault.data.sync.SyncStatus;
@Entity(tableName = "note_sheets")
public class NoteSheet {
@PrimaryKey(autoGenerate = true)
private int id;
private SyncStatus syncStatus;
private long musicNoteId;
private String filePath;
public NoteSheet(long musicNoteID, String filePath) {
this.musicNoteId = musicNoteID;
this.filePath = filePath;
this.syncStatus = SyncStatus.CREATED;
}
public NoteSheet() {
@ -43,4 +45,12 @@ public class NoteSheet {
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public SyncStatus getSyncStatus() {
return syncStatus;
}
public void setSyncStatus(SyncStatus syncStatus) {
this.syncStatus = syncStatus;
}
}

View File

@ -41,8 +41,10 @@ public class SyncWorker extends Worker{
songSyncWorker.syncSongCreations();
songSyncWorker.syncSongModifications();
songSyncWorker.syncSongDeletions();
songSyncWorker.uploadNoteSheets();
return Result.success();
} catch (Exception e) {
e.printStackTrace();
return Result.failure();
}
}

View File

@ -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.modification.SongModificationBatchRequest;
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.http.Body;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
public interface SyncService {
@ -21,4 +26,9 @@ public interface SyncService {
@POST("/sync/songs/delete")
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);
}

View File

@ -5,6 +5,7 @@ import android.os.AsyncTask;
import android.widget.Toast;
import core.notevault.data.MusicNote;
import core.notevault.data.MusicNoteDAO;
import core.notevault.data.NoteSheet;
import core.notevault.data.sync.SyncStatus;
import core.notevault.sync.synchronisation.SyncService;
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.SongModificationBatchResponse;
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.Callback;
import retrofit2.Response;
import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
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();
}
});
}
}
}
}

View File

@ -0,0 +1,5 @@
package core.notevault.sync.synchronisation.songs.notesheets;
public class UploadResponse {
private String serverID;
}