Skip to content

Commit

Permalink
Merge pull request #146 from yprie/96-implementation-of-auto-saving-v2
Browse files Browse the repository at this point in the history
[FIX] add new auto saving feature
  • Loading branch information
BaptisteDxm authored Apr 2, 2024
2 parents 640a9c4 + 85d1bd7 commit aca69b9
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/main/java/application/UPMTApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import utils.GlobalVariables;
import java.io.IOException;
import java.util.UUID;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class UPMTApp {

Expand All @@ -24,15 +26,15 @@ public class UPMTApp {
private static Project currentProject;
private String currentProjectPath;
private UUID lastSavedCommandId;
private long autoSaveIntervalMillis;


public UPMTApp(Stage primaryStage) throws IOException {


this.primaryStage = primaryStage;
this.appCommandFactory = new ApplicationCommandFactory(this);
this.rootLayoutController = new RootLayoutController(appCommandFactory);

this.autoSaveIntervalMillis = 30000;

Configuration.loadAppConfiguration();
HistoryManager.init(appCommandFactory);
Expand All @@ -56,14 +58,35 @@ public UPMTApp(Stage primaryStage) throws IOException {
appCommandFactory.openProjectManagerCommand().execute();
}

startAutoSave();

FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/views/MainView/MainView.fxml"));
loader.setResources(Configuration.langBundle);
loader.setClassLoader(getClass().getClassLoader());
loader.load();
}


private void startAutoSave() {
if (currentProject != null) {
// Créez et démarrez un nouveau thread pour la sauvegarde automatique
Thread autoSaveThread = new Thread(() -> {
while (true) {
try {
// Utilisez Platform.runLater() pour exécuter l'opération sur le thread de l'interface utilisateur
Platform.runLater(() -> appCommandFactory.saveProject().execute());

// Pause pour l'intervalle spécifié
Thread.sleep(autoSaveIntervalMillis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
autoSaveThread.setDaemon(true); // Le thread s'exécutera en arrière-plan et se terminera lorsque le programme principal se termine
autoSaveThread.start();
}
}

public Stage getPrimaryStage() {
return primaryStage;
Expand Down

0 comments on commit aca69b9

Please sign in to comment.