Browse Source

#79: fixed bugs when loading autosaves + after startup: delete old autosaves

I. Dix 6 years ago
parent
commit
3383334145
2 changed files with 42 additions and 2 deletions
  1. 34 0
      src/ui/controller/Control.java
  2. 8 2
      src/ui/view/GUI.java

+ 34 - 0
src/ui/controller/Control.java

@@ -537,6 +537,32 @@ public class Control {
         }
     }
 
+    /**
+     * find all old auto save files (with a file-name, that does not contain the current rand)
+     *
+     * @return a list of files, that are not from the current run
+     */
+    public ArrayList<File> filterOldAutoSaveFiles() {
+        File[] files = new File(autosaveDir).listFiles();
+        ArrayList<File> oldAutoSaves = new ArrayList<>();
+
+        for (File file : files) {
+            if (!file.getName().contains(String.valueOf(rand)))
+                oldAutoSaves.add(file);
+        }
+
+        return oldAutoSaves;
+    }
+
+    /**
+     * deletes the old autosave files
+     */
+    public void deleteObsoleteAutoSaveFiles() {
+        for (File file : filterOldAutoSaveFiles()) {
+            file.delete();
+        }
+    }
+
     public void saveCategory() throws IOException {
         saveController.writeCategory(categoryDir + "Category.json");
     }
@@ -563,6 +589,14 @@ public class Control {
         autoSaveController.increaseAutoSaveNr();
         if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
             autoSaveController.decreaseAutoSaveNr();
+
+            // if it still does not exist, try old autosaves
+            if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
+                ArrayList<File> oldAutoSaves = filterOldAutoSaveFiles();
+                if (oldAutoSaves.size() > 0) {
+                    return autosaveDir + oldAutoSaves.get(oldAutoSaves.size() - 1).getName();
+                }
+            }
         }
         return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
     }

+ 8 - 2
src/ui/view/GUI.java

@@ -1805,13 +1805,16 @@ public class GUI implements CategoryListener {
 
         String autoPath = System.getProperty("user.home") + "/.config/HolonGUI/Autosave/";
         File dest = new File(autoPath);
-        if (dest.listFiles().length > 1) {
+        ArrayList<File> oldAutoSaves = controller.filterOldAutoSaveFiles();
+        int nrOfOldSaves = oldAutoSaves.size();
+
+        if (nrOfOldSaves > 0) {
             int dialogButton = JOptionPane.YES_NO_OPTION;
             int dialogResult = JOptionPane.showConfirmDialog(null, "Old autosave file was found, should it be loaded?",
                     warningText, dialogButton);
             if (dialogResult == JOptionPane.YES_OPTION) {
                 if (dest.exists()) {
-                    model.setAutoSaveNr(dest.listFiles().length - 1);
+                    model.setAutoSaveNr(nrOfOldSaves - 1);
                     mntmRedo.doClick();
                 } else {
                     JOptionPane.showMessageDialog(frmCyberPhysical, "Autosave could not be loaded.");
@@ -1820,6 +1823,9 @@ public class GUI implements CategoryListener {
             } else {
                 setUpAutoSave(dest);
             }
+
+            // after all: delete the obsolete/old autosave files from the directory
+            controller.deleteObsoleteAutoSaveFiles();
         }
 
         canvasSP.addComponentListener(new ComponentAdapter() {