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 ) { @@ -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 ) { 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) -> Unit ) { - private val watcherEntryListType = object : TypeToken>() {}.type - private val watcherEntryType = object : TypeToken() {}.type + private val entryListType = object : TypeToken>() {}.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) { + fun writeLayout(list: List) { 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>(reader, watcherEntryListType) + val line = reader.readText() reader.close() - onWatcherEntriesUpdate.invoke(entries) + + val serializedEntries = gson.fromJson>(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() { - 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) { + companion object { + private fun serializeEntry(entry: WatcherEntry): SerializedWatcherEntry { + val outputs = arrayListOf() + 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() + + entry.outputs.forEach { path -> + outputs.add(Paths.get(path)) + } + + return WatcherEntry(file, outputs) + } + + fun fromUnserializedEntries(list: List): List { + val serialized = arrayListOf() + list.forEach { + serialized.add(serializeEntry(it)) + } + return serialized + } + + fun fromSerializedEntries(list: List): List { + val deserialized = arrayListOf() + 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() { - 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() - - 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) - } -}