Fetch Song Server Changes (just serverID)

This commit is contained in:
Fawkes100 2025-01-07 21:04:14 +01:00
parent ceb5b5f671
commit 68a8e23862
15 changed files with 154 additions and 127 deletions

View File

@ -2,10 +2,10 @@ package core.notevault.data;
import android.content.Context;
import androidx.room.*;
import core.notevault.data.sync.SyncResponse;
import core.notevault.data.sync.SyncDataObject;
import core.notevault.data.sync.SyncStatusConverter;
@Database(entities = {MusicNote.class, NoteSheet.class, Concert.class, ConcertSong.class, SyncResponse.class}, version = 2, exportSchema = false)
@Database(entities = {MusicNote.class, NoteSheet.class, Concert.class, ConcertSong.class, SyncDataObject.class}, version = 2, exportSchema = false)
@TypeConverters(DateConverter.class)
public abstract class MusicDatabase extends RoomDatabase {
public abstract MusicNoteDAO musicNoteDao();

View File

@ -1,7 +1,8 @@
package core.notevault.data;
import androidx.room.*;
import core.notevault.data.sync.SyncResponse;
import core.notevault.data.sync.SyncDataObject;
import core.notevault.data.sync.SyncStatus;
import core.notevault.sync.synchronisation.songs.creation.SongCreationResponse;
@ -81,4 +82,8 @@ public interface MusicNoteDAO {
@Update
void updateNoteSheet(NoteSheet noteSheet);
@Transaction
@Insert(onConflict = OnConflictStrategy.REPLACE) // oder andere Strategie je nach Bedarf
void insertSyncDataObjects(List<SyncDataObject> syncDataObjects);
}

View File

@ -1,6 +0,0 @@
package core.notevault.data.sync;
public enum SyncAction {
TOBEUPLOADED,
TOBEDOWNLOADED
}

View File

@ -1,15 +0,0 @@
package core.notevault.data.sync;
import androidx.room.TypeConverter;
public class SyncActionConverter {
@TypeConverter
public static SyncAction fromInt(int value) {
return SyncAction.values()[value];
}
@TypeConverter
public static int toInt(SyncAction action) {
return action.ordinal();
}
}

View File

@ -0,0 +1,49 @@
package core.notevault.data.sync;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;
import androidx.room.TypeConverters;
@Entity(tableName = "sync_data_objects")
@TypeConverters(SyncDataObjectTypeConverter.class)
public class SyncDataObject {
@PrimaryKey(autoGenerate = true)
private int id;
private String serverID;
private SyncDataObjectType type;
@Ignore
public SyncDataObject(String serverID, SyncDataObjectType type) {
this.serverID = serverID;
this.type = type;
}
public SyncDataObject() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getServerID() {
return serverID;
}
public void setServerID(String serverID) {
this.serverID = serverID;
}
public SyncDataObjectType getType() {
return type;
}
public void setType(SyncDataObjectType type) {
this.type = type;
}
}

View File

@ -0,0 +1,5 @@
package core.notevault.data.sync;
public enum SyncDataObjectType {
MUSIC_NOTE
}

View File

@ -0,0 +1,15 @@
package core.notevault.data.sync;
import androidx.room.TypeConverter;
public class SyncDataObjectTypeConverter {
@TypeConverter
public static SyncDataObjectType fromInt(int value) {
return SyncDataObjectType.values()[value];
}
@TypeConverter
public static int toInt(SyncDataObjectType objectType) {
return objectType.ordinal();
}
}

View File

@ -1,5 +0,0 @@
package core.notevault.data.sync;
public enum SyncObject {
CONCERT
}

View File

@ -1,15 +0,0 @@
package core.notevault.data.sync;
import androidx.room.TypeConverter;
public class SyncObjectConverter {
@TypeConverter
public static SyncObject fromInt(int value) {
return SyncObject.values()[value];
}
@TypeConverter
public static int toInt(SyncObject object) {
return object.ordinal();
}
}

View File

@ -1,59 +0,0 @@
package core.notevault.data.sync;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;
import androidx.room.TypeConverters;
@Entity(tableName = "sync_actions")
@TypeConverters({SyncStatusConverter.class, SyncObjectConverter.class})
public class SyncResponse {
@PrimaryKey(autoGenerate = true)
private long id;
private String serverUUID;
private SyncAction syncAction;
private SyncObject syncObject;
@Ignore
public SyncResponse(String serverUUID, SyncAction syncAction, SyncObject syncObject) {
this.serverUUID = serverUUID;
this.syncAction = syncAction;
this.syncObject = syncObject;
}
public SyncResponse() {
}
public String getServerUUID() {
return serverUUID;
}
public void setServerUUID(String serverUUID) {
this.serverUUID = serverUUID;
}
public SyncAction getSyncAction() {
return syncAction;
}
public void setSyncAction(SyncAction syncAction) {
this.syncAction = syncAction;
}
public SyncObject getSyncObject() {
return syncObject;
}
public void setSyncObject(SyncObject syncObject) {
this.syncObject = syncObject;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}

View File

@ -1,29 +1,13 @@
package core.notevault.sync;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import core.notevault.data.Concert;
import core.notevault.data.MusicDatabase;
import core.notevault.data.MusicNoteDAO;
import core.notevault.data.sync.SyncAction;
import core.notevault.data.sync.SyncObject;
import core.notevault.data.sync.SyncResponse;
import core.notevault.data.sync.SyncStatus;
import core.notevault.sync.synchronisation.*;
import core.notevault.sync.synchronisation.songs.SongSyncWorker;
import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchResponse;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;
public class SyncWorker extends Worker{
private SongSyncWorker songSyncWorker;
@ -42,6 +26,7 @@ public class SyncWorker extends Worker{
songSyncWorker.syncSongModifications();
songSyncWorker.syncSongDeletions();
songSyncWorker.uploadNoteSheets();
songSyncWorker.fetchModifiedSongs();
return Result.success();
} catch (Exception e) {
e.printStackTrace();

View File

@ -0,0 +1,19 @@
package core.notevault.sync.synchronisation;
import java.time.LocalDateTime;
public class FetchRequest {
private LocalDateTime client_last_sync;
public FetchRequest(LocalDateTime client_last_sync) {
this.client_last_sync = client_last_sync;
}
public LocalDateTime getClient_last_sync() {
return client_last_sync;
}
public void setClient_last_sync(LocalDateTime client_last_sync) {
this.client_last_sync = client_last_sync;
}
}

View File

@ -0,0 +1,19 @@
package core.notevault.sync.synchronisation;
import java.util.List;
public class FetchResponse {
private List<String> serverIDs;
public FetchResponse(List<String> serverIDs) {
this.serverIDs = serverIDs;
}
public List<String> getServerIDs() {
return serverIDs;
}
public void setServerIDs(List<String> serverIDs) {
this.serverIDs = serverIDs;
}
}

View File

@ -1,6 +1,6 @@
package core.notevault.sync.synchronisation;
import core.notevault.data.sync.SyncResponse;
import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchRequest;
import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchResponse;
import core.notevault.sync.synchronisation.songs.deletion.SongDeletionBatchRequest;
@ -11,10 +11,9 @@ 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;
import retrofit2.http.*;
import java.time.LocalDateTime;
public interface SyncService {
@ -31,4 +30,7 @@ public interface SyncService {
@POST("/sync/songs/note_sheet/upload")
Call<UploadResponse> uploadNoteSheet(@Part("serverID") RequestBody serverID, @Part("fileName") RequestBody fileName,
@Part MultipartBody.Part image);
@GET("/sync/songs/fetch")
Call<FetchResponse> fetchModifiedSongs(@Query("last_client_sync") LocalDateTime last_client_sync);
}

View File

@ -6,7 +6,11 @@ import android.widget.Toast;
import core.notevault.data.MusicNote;
import core.notevault.data.MusicNoteDAO;
import core.notevault.data.NoteSheet;
import core.notevault.data.sync.SyncDataObject;
import core.notevault.data.sync.SyncDataObjectType;
import core.notevault.data.sync.SyncStatus;
import core.notevault.sync.synchronisation.FetchRequest;
import core.notevault.sync.synchronisation.FetchResponse;
import core.notevault.sync.synchronisation.SyncService;
import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchRequest;
import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchResponse;
@ -164,8 +168,32 @@ public class SongSyncWorker {
}
});
}
}
}
public void fetchModifiedSongs() {
FetchRequest request = new FetchRequest(LocalDateTime.MIN);
syncService.fetchModifiedSongs(LocalDateTime.MIN).enqueue(new Callback<FetchResponse>() {
@Override
public void onResponse(Call<FetchResponse> call, Response<FetchResponse> response) {
if(response.isSuccessful() && response.body() != null) {
executorService.execute(() -> {
List<SyncDataObject> syncDataObjects = new ArrayList<>();
for(String serverID: response.body().getServerIDs()) {
syncDataObjects.add(new SyncDataObject(serverID, SyncDataObjectType.MUSIC_NOTE));
}
database.insertSyncDataObjects(syncDataObjects);
});
} else {
Toast.makeText(context, "Fehler beim Fetch: " + response.code(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<FetchResponse> call, Throwable throwable) {
Toast.makeText(context, "Fehler beim Fetch: " + throwable.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}