From a20865331226699a5112f0a65fa168b874349612 Mon Sep 17 00:00:00 2001
From: Ivan Olexyn <ivan@olexyn.com>
Date: Sat, 7 Mar 2020 18:45:03 +0100
Subject: [PATCH] - Connected UI to logic. - Added Threads for UI/logic. -
 Removing SyncDir(s) causes Exceptions due to ConcurrentMod.

---
 src/com/olexyn/ensync/Flow.java          | 51 +++++++++++-------------
 src/com/olexyn/ensync/Main.java          | 24 +++++++++--
 src/com/olexyn/ensync/ui/Controller.java |  1 +
 src/com/olexyn/ensync/ui/UI.java         | 10 ++---
 4 files changed, 50 insertions(+), 36 deletions(-)

diff --git a/src/com/olexyn/ensync/Flow.java b/src/com/olexyn/ensync/Flow.java
index 800a102..3c77a46 100644
--- a/src/com/olexyn/ensync/Flow.java
+++ b/src/com/olexyn/ensync/Flow.java
@@ -6,46 +6,42 @@ import com.olexyn.ensync.artifacts.SyncMap;
 import java.io.File;
 import java.util.Map;
 
-public class Flow {
+public class Flow implements Runnable{
 
 
 
 
 
 
-    public void start() {
+    public void run() {
 
 
-        Tools tools = new Tools();
-        Execute x = new Execute();
 
 
-        SyncMap syncMap = new SyncMap("test");
-        syncMap.addDirectory("/home/user/test/a");
-        syncMap.addDirectory("/home/user/test/b");
-        //syncMap.addDirectory("/home/user/test/c");
 
+        for (Map.Entry<String, SyncMap> mapEntry : Main.sync.syncMaps.entrySet()) {
 
-        for (Map.Entry<String, SyncDirectory> entry : syncMap.syncDirectories.entrySet()) {
-            SyncDirectory syncDirectory = entry.getValue();
-            String path = syncDirectory.path;
-            String stateFilePath = syncDirectory.stateFilePath(path);
-            
-            if (new File(stateFilePath).exists()) {
-                syncDirectory.readStateFile();
-            } else {
-                syncDirectory.writeStateFile(path);
+
+            for (Map.Entry<String, SyncDirectory> entry : mapEntry.getValue().syncDirectories.entrySet()) {
+                SyncDirectory syncDirectory = entry.getValue();
+                String path = syncDirectory.path;
+                String stateFilePath = syncDirectory.stateFilePath(path);
+
+                if (new File(stateFilePath).exists()) {
+                    syncDirectory.readStateFile();
+                } else {
+                    syncDirectory.writeStateFile(path);
+                }
             }
-        }
 
 
-        while (true) {
+            while (true) {
 
-            for (Map.Entry<String, SyncDirectory> entry : syncMap.syncDirectories.entrySet()) {
+                for (Map.Entry<String, SyncDirectory> entry : mapEntry.getValue().syncDirectories.entrySet()) {
 
-                SyncDirectory syncDirectory = entry.getValue();
+                    SyncDirectory syncDirectory = entry.getValue();
 
-                String path = syncDirectory.path;
+                    String path = syncDirectory.path;
 
                     syncDirectory.readState();
 
@@ -58,20 +54,19 @@ public class Flow {
                     syncDirectory.doModify();
 
 
-
                     syncDirectory.writeStateFile(path);
 
 
-
                 }
 
-            try {
-                Thread.sleep(1000);
-            } catch (InterruptedException e) {
+                try {
+                    System.out.println("Pausing...");
+                    Thread.sleep(1000);
+                } catch (InterruptedException e) {
 
+                }
             }
         }
     }
 
-
 }
diff --git a/src/com/olexyn/ensync/Main.java b/src/com/olexyn/ensync/Main.java
index 54570cf..e29643e 100644
--- a/src/com/olexyn/ensync/Main.java
+++ b/src/com/olexyn/ensync/Main.java
@@ -16,12 +16,15 @@ import javafx.stage.Stage;
 import javafx.stage.Window;
 
 
-public class Main extends UI {
+public class Main{
 
 
 
-    public static Sync sync = new Sync();
+    final public static Sync sync = new Sync();
 
+    final private static Flow flow = new Flow();
+
+    final private static UI ui = new UI();
 
 
 
@@ -34,7 +37,22 @@ public class Main extends UI {
         sync.syncMaps.put("test", new SyncMap("test"));
 
 
-        UI.main(args);
+        Thread uiThread = new Thread(ui, "ui");
+        uiThread.start();
+
+
+        Thread flowThread = new Thread(flow, "flow");
+        flowThread.start();
+
+
+
+
+
+
 
     }
+
+
+
+
 }
diff --git a/src/com/olexyn/ensync/ui/Controller.java b/src/com/olexyn/ensync/ui/Controller.java
index f51ceac..614d035 100644
--- a/src/com/olexyn/ensync/ui/Controller.java
+++ b/src/com/olexyn/ensync/ui/Controller.java
@@ -127,6 +127,7 @@ public class Controller implements Initializable {
 
         nodeList.addAll(gridPane.getChildren());
 
+        //TODO fix ConcurrentModificationException. This will possibly resolve further errors.
 
         for (Node node : nodeList) {
 
diff --git a/src/com/olexyn/ensync/ui/UI.java b/src/com/olexyn/ensync/ui/UI.java
index 81940d5..6e1c1a0 100644
--- a/src/com/olexyn/ensync/ui/UI.java
+++ b/src/com/olexyn/ensync/ui/UI.java
@@ -7,7 +7,7 @@ import javafx.scene.Scene;
 import javafx.stage.Stage;
 
 
-public class UI extends Application {
+public class UI extends Application implements Runnable {
 
 
 
@@ -29,11 +29,11 @@ public class UI extends Application {
         primaryStage.show();
     }
 
-    public static void main(String[] args) {
-        UI.launch(args);
-
-    }
 
 
 
+    @Override
+    public void run() {
+        UI.launch();
+    }
 }