This commit is contained in:
Fawkes100 2025-01-19 19:44:17 +01:00
parent d059f7ed8e
commit da53e4507c
7 changed files with 101 additions and 18 deletions

View File

@ -78,4 +78,10 @@ public interface SongDao {
@Query("DELETE FROM NoteSheet WHERE songID = :localID") @Query("DELETE FROM NoteSheet WHERE songID = :localID")
void deleteNoteSheetsByLocalSongID(int localID); void deleteNoteSheetsByLocalSongID(int localID);
@Update
void updateNoteSheet(NoteSheet noteSheet);
@Query("SELECT * FROM NoteSheet WHERE serverFileName =:serverFileName")
NoteSheet getNoteSheetByServerFileName(String serverFileName);
} }

View File

@ -1,6 +1,7 @@
package com.stormtales.notevault.data.entities; package com.stormtales.notevault.data.entities;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
import com.stormtales.notevault.data.sync.SyncStatus; import com.stormtales.notevault.data.sync.SyncStatus;
@ -17,12 +18,27 @@ public class NoteSheet {
private SyncStatus syncStatus; private SyncStatus syncStatus;
@Ignore
public NoteSheet(String localFileName, String hash) { public NoteSheet(String localFileName, String hash) {
this.localFileName = localFileName; this.localFileName = localFileName;
this.hash = hash; this.hash = hash;
this.syncStatus = SyncStatus.CREATED; this.syncStatus = SyncStatus.CREATED;
} }
@Ignore
public NoteSheet(int songID, String localFileName, String serverFileName, String hash) {
this.songID = songID;
this.localFileName = localFileName;
this.serverFileName = serverFileName;
this.hash = hash;
this.syncStatus = SyncStatus.REMOTE_MODIFIED;
}
public NoteSheet() {
}
public int getLocalID() { public int getLocalID() {
return localID; return localID;
} }

View File

@ -47,6 +47,12 @@ public class Song {
public Song() { public Song() {
} }
@Ignore
public Song(String serverID) {
this.serverID = serverID;
this.syncStatus = SyncStatus.REMOTE_MODIFIED;
}
public int getLocalID() { public int getLocalID() {
return localID; return localID;
} }

View File

@ -168,10 +168,27 @@ public class SongSyncRepository {
public void markSongsAsRemotelyModified(List<String> serverIDs) { public void markSongsAsRemotelyModified(List<String> serverIDs) {
Executors.newSingleThreadExecutor().execute(() -> { Executors.newSingleThreadExecutor().execute(() -> {
List<Song> songs = songDao.getSongsByServerIDs(serverIDs); List<Song> songs = songDao.getSongsByServerIDs(serverIDs);
List<Song> createdSongs = new ArrayList<>();
for(Song song : songs) { for(Song song : songs) {
song.setSyncStatus(SyncStatus.REMOTE_MODIFIED); song.setSyncStatus(SyncStatus.REMOTE_MODIFIED);
} }
for(String serverID : serverIDs) {
boolean foundServerSong = false;
for(Song song : songs) {
if(song.getServerID().equals(serverID)) {
foundServerSong = true;
break;
}
}
if(!foundServerSong) {
Song song = new Song(serverID);
createdSongs.add(song);
}
}
songDao.updateSongs(songs); songDao.updateSongs(songs);
songDao.insertSongs(createdSongs);
}); });
} }
@ -192,6 +209,7 @@ public class SongSyncRepository {
List<Song> onlyRemoteExistingSongs = new ArrayList<>(); List<Song> onlyRemoteExistingSongs = new ArrayList<>();
List<NoteSheet> outdatedNoteSheets = new ArrayList<>(); List<NoteSheet> outdatedNoteSheets = new ArrayList<>();
List<NoteSheet> onlyRemoteNoteSheets = new ArrayList<>();
for(SongModel songModel : remoteSongs) { for(SongModel songModel : remoteSongs) {
boolean found = false; boolean found = false;
for(Song song : songs) { for(Song song : songs) {
@ -206,16 +224,32 @@ public class SongSyncRepository {
song.setGenre(songModel.getGenre()); song.setGenre(songModel.getGenre());
song.setYear(songModel.getYear()); song.setYear(songModel.getYear());
for(NoteSheet noteSheet : noteSheets) {
for(RemotelyModifiedNoteSheetModel remoteNoteSheet : songModel.getNote_sheets()) { for(RemotelyModifiedNoteSheetModel remoteNoteSheet : songModel.getNote_sheets()) {
if(noteSheets.isEmpty()) {
NoteSheet noteSheet = new NoteSheet(song.getLocalID(), remoteNoteSheet.getFilename(), remoteNoteSheet.getServer_filename(), null);
onlyRemoteNoteSheets.add(noteSheet);
outdatedNoteSheets.add(noteSheet);
} else {
boolean foundNoteSheet = false;
for(NoteSheet noteSheet : outdatedNoteSheets) {
if(remoteNoteSheet.getServer_filename().equals(noteSheet.getServerFileName())) { if(remoteNoteSheet.getServer_filename().equals(noteSheet.getServerFileName())) {
foundNoteSheet = true;
if(!remoteNoteSheet.getHash().equals(noteSheet.getHash())) { if(!remoteNoteSheet.getHash().equals(noteSheet.getHash())) {
outdatedNoteSheets.add(noteSheet); outdatedNoteSheets.add(noteSheet);
break; break;
} }
} }
} }
if(!foundNoteSheet) {
NoteSheet noteSheet = new NoteSheet(song.getLocalID(), remoteNoteSheet.getFilename(), remoteNoteSheet.getServer_filename(), null);
onlyRemoteNoteSheets.add(noteSheet);
outdatedNoteSheets.add(noteSheet);
} }
}
}
} else { } else {
removeSong(song); removeSong(song);
} }
@ -230,6 +264,7 @@ public class SongSyncRepository {
songDao.updateSongs(songs); songDao.updateSongs(songs);
songDao.insertSongs(onlyRemoteExistingSongs); songDao.insertSongs(onlyRemoteExistingSongs);
songDao.insert(onlyRemoteNoteSheets);
Handler mainHandler = new Handler(Looper.getMainLooper()); Handler mainHandler = new Handler(Looper.getMainLooper());
mainHandler.post(()-> callback.onResult(outdatedNoteSheets)); mainHandler.post(()-> callback.onResult(outdatedNoteSheets));
@ -250,6 +285,17 @@ public class SongSyncRepository {
} }
public void saveUpdatedNoteSheet(String serverFileName, String localFileName, String hash) {
Executors.newSingleThreadExecutor().execute(()-> {
NoteSheet noteSheet = songDao.getNoteSheetByServerFileName(serverFileName);
noteSheet.setHash(hash);
noteSheet.setLocalFileName(localFileName);
songDao.updateNoteSheet(noteSheet);
});
}
public interface LoadDataCallback<T> { public interface LoadDataCallback<T> {
void onResult(T result); void onResult(T result);
} }

View File

@ -10,7 +10,7 @@ import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory; import retrofit2.converter.gson.GsonConverterFactory;
public class NetworkModule { public class NetworkModule {
private static final String BASE_URL = "https://192.168.178.30:8000/"; private static final String BASE_URL = "https://notevault.fawkes100.de/";
private static Retrofit retrofit; private static Retrofit retrofit;
public static Retrofit getRetrofitInstance(Context context) { public static Retrofit getRetrofitInstance(Context context) {
@ -25,7 +25,7 @@ public class NetworkModule {
.build(); .build();
retrofit = new Retrofit.Builder() retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.178.30:8000/") .baseUrl(BASE_URL)
.client(client) .client(client)
.addConverterFactory(GsonConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create())
.build(); .build();

View File

@ -33,7 +33,7 @@ public interface SongSyncAPI {
@GET("/sync/songs/fetch") @GET("/sync/songs/fetch")
Call<FetchResponse> fetchRemoteModifiedSongs(@Query(value = "last_client_sync") String last_client_sync); Call<FetchResponse> fetchRemoteModifiedSongs(@Query(value = "last_client_sync") String last_client_sync);
@GET("/sync/songs/get") @GET("/sync/songs/get/")
Call<SongModel> fetchRemotelyModifiedSongData(@Query(value = "songID") String songID); Call<SongModel> fetchRemotelyModifiedSongData(@Query(value = "songID") String songID);
@GET("/sync/songs/get/notesheet/") @GET("/sync/songs/get/notesheet/")

View File

@ -9,6 +9,7 @@ import com.stormtales.notevault.data.repositories.SongSyncRepository;
import com.stormtales.notevault.network.sync.models.*; import com.stormtales.notevault.network.sync.models.*;
import com.stormtales.notevault.ui.gallery.GalleryViewModel; import com.stormtales.notevault.ui.gallery.GalleryViewModel;
import com.stormtales.notevault.utils.NoteSheetsUtil; import com.stormtales.notevault.utils.NoteSheetsUtil;
import com.stormtales.notevault.utils.Tupel;
import java.io.File; import java.io.File;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@ -97,10 +98,17 @@ public class SongSyncModule {
public void getRemotelyModifiedSongData() { public void getRemotelyModifiedSongData() {
songSyncRepository.loadRemotelyModifiedSongs(songs -> { songSyncRepository.loadRemotelyModifiedSongs(songs -> {
if(songs.isEmpty()) {
syncViewModel.finishFetching();
} else {
songSyncService.getRemotelyModifiedSongData(songs, remoteSongs -> { songSyncService.getRemotelyModifiedSongData(songs, remoteSongs -> {
if(remoteSongs.isEmpty()) {
syncViewModel.finishFetching();
} else {
songSyncRepository.saveRemoteSongs(remoteSongs, this::downloadNoteSheets); songSyncRepository.saveRemoteSongs(remoteSongs, this::downloadNoteSheets);
}
}); });
}
}); });
} }
@ -110,7 +118,8 @@ public class SongSyncModule {
songSyncService.downloadNoteSheet(noteSheet, responseBody -> { songSyncService.downloadNoteSheet(noteSheet, responseBody -> {
String localFilename = noteSheet.getServerFileName().substring(36); String localFilename = noteSheet.getServerFileName().substring(36);
NoteSheetsUtil.saveImageFromServer(responseBody, context.getFilesDir()); Tupel<String, String> result = NoteSheetsUtil.saveImageFromServer(responseBody, context.getFilesDir());
songSyncRepository.saveUpdatedNoteSheet(noteSheet.getServerFileName(), result.getValue00(), result.getValue01());
}); });
} }
syncViewModel.finishFetching(); syncViewModel.finishFetching();