From 2736f12ce3dce208a702341641ef39cf5bb42beb Mon Sep 17 00:00:00 2001
From: aro <pantoine.rochas@gmail.com>
Date: Thu, 12 Jan 2023 18:31:18 +0100
Subject: [PATCH] feat: layout saving made right i guess

---
 .../xyz/atnrch/wrench/gui/WrenchScaffold.kt   |  6 +-
 .../wrench/gui/filemanager/top/TopBar.kt      |  6 +-
 .../gui/filemanager/top/TopBarButtons.kt      |  9 +--
 .../json/{JsonConfig.kt => JsonLayout.kt}     | 17 +++---
 .../xyz/atnrch/wrench/json/PathTypeAdapter.kt | 25 --------
 .../wrench/json/SerializedWatcherEntry.kt     | 47 ++++++++++++++
 .../atnrch/wrench/json/WatcherEntryAdapter.kt | 61 -------------------
 7 files changed, 66 insertions(+), 105 deletions(-)
 rename src/main/kotlin/xyz/atnrch/wrench/json/{JsonConfig.kt => JsonLayout.kt} (55%)
 delete mode 100644 src/main/kotlin/xyz/atnrch/wrench/json/PathTypeAdapter.kt
 create mode 100644 src/main/kotlin/xyz/atnrch/wrench/json/SerializedWatcherEntry.kt
 delete mode 100644 src/main/kotlin/xyz/atnrch/wrench/json/WatcherEntryAdapter.kt

diff --git a/src/main/kotlin/xyz/atnrch/wrench/gui/WrenchScaffold.kt b/src/main/kotlin/xyz/atnrch/wrench/gui/WrenchScaffold.kt
index ae1f2af..a191e88 100644
--- a/src/main/kotlin/xyz/atnrch/wrench/gui/WrenchScaffold.kt
+++ b/src/main/kotlin/xyz/atnrch/wrench/gui/WrenchScaffold.kt
@@ -11,7 +11,7 @@ import xyz.atnrch.wrench.data.SnackBarDataHolder
 import xyz.atnrch.wrench.gui.filemanager.bottom.FloatingButton
 import xyz.atnrch.wrench.gui.filemanager.top.TopBar
 import xyz.atnrch.wrench.gui.style.UIColors
-import xyz.atnrch.wrench.json.JsonConfig
+import xyz.atnrch.wrench.json.JsonLayout
 import xyz.atnrch.wrench.watcher.Watcher
 import xyz.atnrch.wrench.watcher.WatcherEntry
 import xyz.atnrch.wrench.watcher.WatcherManager
@@ -27,7 +27,7 @@ fun WrenchScaffold(state: WindowState) {
     val watcherManager = remember { WatcherManager(entries) }
     val watcher = remember { Watcher(watcherManager, snackBarDataHolder) }
     val tabTitles = listOf("File Manager", "Servers")
-    val jsonConfig = JsonConfig {
+    val jsonLayout = JsonLayout {
         it.forEach { entry ->
             watcherManager.addFile(entry.file, entry.outputs)
         }
@@ -43,7 +43,7 @@ fun WrenchScaffold(state: WindowState) {
     } else {
         Scaffold(
             scaffoldState = scaffoldState,
-            topBar = { TopBar(jsonConfig, tabIndex, entries.values) },
+            topBar = { TopBar(jsonLayout, tabIndex, entries.values) },
             floatingActionButton = { if (tabIndex == 0) FloatingButton(watcherManager) },
             isFloatingActionButtonDocked = true,
             backgroundColor = UIColors.PRIMARY,
diff --git a/src/main/kotlin/xyz/atnrch/wrench/gui/filemanager/top/TopBar.kt b/src/main/kotlin/xyz/atnrch/wrench/gui/filemanager/top/TopBar.kt
index 97fe436..26b9328 100644
--- a/src/main/kotlin/xyz/atnrch/wrench/gui/filemanager/top/TopBar.kt
+++ b/src/main/kotlin/xyz/atnrch/wrench/gui/filemanager/top/TopBar.kt
@@ -14,12 +14,12 @@ import androidx.compose.ui.graphics.Color
 import androidx.compose.ui.unit.dp
 import xyz.atnrch.wrench.gui.style.Fonts
 import xyz.atnrch.wrench.gui.style.UIColors
-import xyz.atnrch.wrench.json.JsonConfig
+import xyz.atnrch.wrench.json.JsonLayout
 import xyz.atnrch.wrench.watcher.WatcherEntry
 
 @Composable
 fun TopBar(
-    jsonConfig: JsonConfig,
+    jsonLayout: JsonLayout,
     tabIndex: Int,
     values: MutableCollection<WatcherEntry>
 ) {
@@ -42,7 +42,7 @@ fun TopBar(
         },
         actions = {
             if (tabIndex == 0) {
-                TopBarButtons(jsonConfig, values)
+                TopBarButtons(jsonLayout, values)
             }
         }
     )
diff --git a/src/main/kotlin/xyz/atnrch/wrench/gui/filemanager/top/TopBarButtons.kt b/src/main/kotlin/xyz/atnrch/wrench/gui/filemanager/top/TopBarButtons.kt
index 9b9d000..f8a9db4 100644
--- a/src/main/kotlin/xyz/atnrch/wrench/gui/filemanager/top/TopBarButtons.kt
+++ b/src/main/kotlin/xyz/atnrch/wrench/gui/filemanager/top/TopBarButtons.kt
@@ -7,23 +7,24 @@ import androidx.compose.material.Text
 import androidx.compose.runtime.Composable
 import androidx.compose.ui.Modifier
 import androidx.compose.ui.unit.dp
-import xyz.atnrch.wrench.json.JsonConfig
+import xyz.atnrch.wrench.json.JsonLayout
+import xyz.atnrch.wrench.json.SerializedWatcherEntry
 import xyz.atnrch.wrench.watcher.WatcherEntry
 
 @Composable
 fun TopBarButtons(
-    jsonConfig: JsonConfig,
+    jsonLayout: JsonLayout,
     values: MutableCollection<WatcherEntry>
 ) {
     Button(onClick = {
         println(values.toList().joinToString(","))
-        jsonConfig.writeLayout(values.toList())
+        jsonLayout.writeLayout(SerializedWatcherEntry.fromUnserializedEntries(values.toList()))
     }) {
         Text("Save")
     }
     Spacer(Modifier.width(10.dp))
     Button(onClick = {
-        jsonConfig.readLayout()
+        jsonLayout.readLayout()
     }) {
         Text("Load")
     }
diff --git a/src/main/kotlin/xyz/atnrch/wrench/json/JsonConfig.kt b/src/main/kotlin/xyz/atnrch/wrench/json/JsonLayout.kt
similarity index 55%
rename from src/main/kotlin/xyz/atnrch/wrench/json/JsonConfig.kt
rename to src/main/kotlin/xyz/atnrch/wrench/json/JsonLayout.kt
index d7d9c81..74ce301 100644
--- a/src/main/kotlin/xyz/atnrch/wrench/json/JsonConfig.kt
+++ b/src/main/kotlin/xyz/atnrch/wrench/json/JsonLayout.kt
@@ -6,21 +6,17 @@ import xyz.atnrch.wrench.watcher.WatcherEntry
 import java.io.File
 import java.io.FileReader
 import java.io.FileWriter
-import java.nio.file.Path
 
-class JsonConfig(
+class JsonLayout(
     private val onWatcherEntriesUpdate: (List<WatcherEntry>) -> Unit
 ) {
-    private val watcherEntryListType = object : TypeToken<List<WatcherEntry>>() {}.type
-    private val watcherEntryType = object : TypeToken<WatcherEntry>() {}.type
+    private val entryListType = object : TypeToken<List<SerializedWatcherEntry>>() {}.type
     private val gson: Gson = Gson().newBuilder()
-        .registerTypeAdapter(watcherEntryType, WatcherEntryAdapter())
-        .registerTypeAdapter(Path::class.java, PathTypeAdapter())
         .setPrettyPrinting()
         .create()
     private val file = File("layout.json")
 
-    fun writeLayout(list: List<WatcherEntry>) {
+    fun writeLayout(list: List<SerializedWatcherEntry>) {
         val writer = FileWriter(file)
         gson.toJson(list, writer)
         writer.flush()
@@ -29,8 +25,11 @@ class JsonConfig(
 
     fun readLayout() {
         val reader = FileReader(file)
-        val entries = gson.fromJson<List<WatcherEntry>>(reader, watcherEntryListType)
+        val line = reader.readText()
         reader.close()
-        onWatcherEntriesUpdate.invoke(entries)
+
+        val serializedEntries = gson.fromJson<List<SerializedWatcherEntry>>(line, entryListType)
+        val deserializedEntries = SerializedWatcherEntry.fromSerializedEntries(serializedEntries)
+        onWatcherEntriesUpdate.invoke(deserializedEntries)
     }
 }
\ No newline at end of file
diff --git a/src/main/kotlin/xyz/atnrch/wrench/json/PathTypeAdapter.kt b/src/main/kotlin/xyz/atnrch/wrench/json/PathTypeAdapter.kt
deleted file mode 100644
index bbee450..0000000
--- a/src/main/kotlin/xyz/atnrch/wrench/json/PathTypeAdapter.kt
+++ /dev/null
@@ -1,25 +0,0 @@
-package xyz.atnrch.wrench.json
-
-import com.google.gson.TypeAdapter
-import com.google.gson.stream.JsonReader
-import com.google.gson.stream.JsonWriter
-import java.nio.file.Path
-import java.nio.file.Paths
-
-class PathTypeAdapter : TypeAdapter<Path>() {
-    override fun write(out: JsonWriter?, value: Path?) {
-        if (out == null) return
-        out.beginObject()
-        out.name("path")
-        out.value(value.toString())
-        out.endObject()
-    }
-
-    override fun read(`in`: JsonReader?): Path? {
-        if (`in` == null) return null
-        `in`.beginObject()
-        val path = Paths.get(`in`.nextString())
-        `in`.endObject()
-        return path
-    }
-}
diff --git a/src/main/kotlin/xyz/atnrch/wrench/json/SerializedWatcherEntry.kt b/src/main/kotlin/xyz/atnrch/wrench/json/SerializedWatcherEntry.kt
new file mode 100644
index 0000000..4f61c72
--- /dev/null
+++ b/src/main/kotlin/xyz/atnrch/wrench/json/SerializedWatcherEntry.kt
@@ -0,0 +1,47 @@
+package xyz.atnrch.wrench.json
+
+import xyz.atnrch.wrench.watcher.WatcherEntry
+import java.io.File
+import java.nio.file.Path
+import java.nio.file.Paths
+import kotlin.io.path.absolutePathString
+
+data class SerializedWatcherEntry(val path: String, val outputs: ArrayList<String>) {
+    companion object {
+        private fun serializeEntry(entry: WatcherEntry): SerializedWatcherEntry {
+            val outputs = arrayListOf<String>()
+            entry.outputs.forEach {
+                outputs.add(it.absolutePathString())
+            }
+
+            return SerializedWatcherEntry(entry.file.path, outputs)
+        }
+
+        private fun deserializeEntry(entry: SerializedWatcherEntry): WatcherEntry {
+            val file = File(entry.path)
+            val outputs = arrayListOf<Path>()
+
+            entry.outputs.forEach { path ->
+                outputs.add(Paths.get(path))
+            }
+
+            return WatcherEntry(file, outputs)
+        }
+
+        fun fromUnserializedEntries(list: List<WatcherEntry>): List<SerializedWatcherEntry> {
+            val serialized = arrayListOf<SerializedWatcherEntry>()
+            list.forEach {
+                serialized.add(serializeEntry(it))
+            }
+            return serialized
+        }
+
+        fun fromSerializedEntries(list: List<SerializedWatcherEntry>): List<WatcherEntry> {
+            val deserialized = arrayListOf<WatcherEntry>()
+            list.forEach {
+                deserialized.add(deserializeEntry(it))
+            }
+            return deserialized
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/main/kotlin/xyz/atnrch/wrench/json/WatcherEntryAdapter.kt b/src/main/kotlin/xyz/atnrch/wrench/json/WatcherEntryAdapter.kt
deleted file mode 100644
index 621aa85..0000000
--- a/src/main/kotlin/xyz/atnrch/wrench/json/WatcherEntryAdapter.kt
+++ /dev/null
@@ -1,61 +0,0 @@
-package xyz.atnrch.wrench.json
-
-import com.google.gson.TypeAdapter
-import com.google.gson.stream.JsonReader
-import com.google.gson.stream.JsonToken
-import com.google.gson.stream.JsonWriter
-import xyz.atnrch.wrench.logger.Logger
-import xyz.atnrch.wrench.watcher.WatcherEntry
-import java.io.File
-import java.nio.file.Path
-import java.nio.file.Paths
-
-class WatcherEntryAdapter : TypeAdapter<WatcherEntry>() {
-    override fun write(writer: JsonWriter?, value: WatcherEntry?) {
-        if (writer == null) {
-            Logger.warn("JsonWriter is null?")
-            return
-        }
-
-        if (value == null) {
-            writer.nullValue()
-            return
-        }
-
-        writer.beginObject()
-        writer.name("file").value(value.file.path)
-
-        writer.name("outputs").beginArray()
-        value.outputs.forEach {
-            writer.beginObject()
-            writer.name("path").value(it.toString())
-            writer.endObject()
-        }
-        writer.endArray()
-        writer.endObject()
-    }
-
-    override fun read(reader: JsonReader?): WatcherEntry? {
-        if (reader == null) {
-            Logger.warn("JsonReader is null?")
-            Logger.warn("Failed to parse Layout.")
-            return null
-        }
-
-        reader.beginObject()
-        //val file = File(reader.nextString())
-        val outputs = arrayListOf<Path>()
-
-        reader.beginArray()
-        while(reader.peek() != JsonToken.END_ARRAY) {
-            while(reader.peek() != JsonToken.END_OBJECT) {
-                if(reader.peek() == JsonToken.STRING) {
-                    outputs.add(Paths.get(reader.nextString()))
-                }
-            }
-        }
-        reader.endArray()
-        reader.endObject()
-        return WatcherEntry(File("/home/aro/text"), outputs)
-    }
-}