feat: still figuring out layout saving
This commit is contained in:
parent
a42eb486e4
commit
60be35d302
6 changed files with 103 additions and 6 deletions
|
@ -12,7 +12,6 @@ import xyz.atnrch.wrench.gui.filemanager.center.empty.DefaultDisplay
|
|||
import xyz.atnrch.wrench.gui.filemanager.center.input.InputEntries
|
||||
import xyz.atnrch.wrench.gui.filemanager.center.output.OutputEntries
|
||||
import xyz.atnrch.wrench.watcher.WatcherManager
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
|
||||
@Composable
|
||||
|
@ -51,7 +50,7 @@ fun DisplayEntries(
|
|||
onEntryClick: (id: Int) -> Unit
|
||||
) {
|
||||
if (watcherManager.getEntries().isEmpty()) {
|
||||
watcherManager.addFile(File("/home/aro/IdeaProjects/Wrench/dummy"))
|
||||
//watcherManager.addFile(File("/home/aro/IdeaProjects/Wrench/dummy"))
|
||||
DefaultDisplay()
|
||||
} else {
|
||||
InputEntries(minmode, watcherManager, onEntryClick)
|
||||
|
|
|
@ -16,6 +16,7 @@ fun TopBarButtons(
|
|||
values: MutableCollection<WatcherEntry>
|
||||
) {
|
||||
Button(onClick = {
|
||||
println(values.toList().joinToString(","))
|
||||
jsonConfig.writeLayout(values.toList())
|
||||
}) {
|
||||
Text("Save")
|
||||
|
|
|
@ -6,20 +6,31 @@ 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(
|
||||
private val onWatcherEntriesUpdate: (List<WatcherEntry>) -> Unit
|
||||
) {
|
||||
private val gson: Gson = Gson().newBuilder().serializeNulls().setPrettyPrinting().create()
|
||||
private val watcherEntryListType = object : TypeToken<List<WatcherEntry>>() {}.type
|
||||
private val watcherEntryType = object : TypeToken<WatcherEntry>() {}.type
|
||||
private val gson: Gson = Gson().newBuilder()
|
||||
.registerTypeAdapter(watcherEntryType, WatcherEntryAdapter())
|
||||
.registerTypeAdapter(Path::class.java, PathTypeAdapter())
|
||||
.setPrettyPrinting()
|
||||
.create()
|
||||
private val file = File("layout.json")
|
||||
private val watcherEntryType = object : TypeToken<List<WatcherEntry>>() {}.type
|
||||
|
||||
fun writeLayout(list: List<WatcherEntry>) {
|
||||
gson.toJson(list, FileWriter(file))
|
||||
val writer = FileWriter(file)
|
||||
gson.toJson(list, writer)
|
||||
writer.flush()
|
||||
writer.close()
|
||||
}
|
||||
|
||||
fun readLayout() {
|
||||
val entries = gson.fromJson<List<WatcherEntry>>(FileReader(file), watcherEntryType)
|
||||
val reader = FileReader(file)
|
||||
val entries = gson.fromJson<List<WatcherEntry>>(reader, watcherEntryListType)
|
||||
reader.close()
|
||||
onWatcherEntriesUpdate.invoke(entries)
|
||||
}
|
||||
}
|
25
src/main/kotlin/xyz/atnrch/wrench/json/PathTypeAdapter.kt
Normal file
25
src/main/kotlin/xyz/atnrch/wrench/json/PathTypeAdapter.kt
Normal file
|
@ -0,0 +1,25 @@
|
|||
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
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
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)
|
||||
}
|
||||
}
|
Reference in a new issue