feat: layout saving made right i guess

This commit is contained in:
aro 2023-01-12 18:31:18 +01:00
parent 5398aec1ed
commit 2736f12ce3
7 changed files with 66 additions and 105 deletions

View file

@ -11,7 +11,7 @@ import xyz.atnrch.wrench.data.SnackBarDataHolder
import xyz.atnrch.wrench.gui.filemanager.bottom.FloatingButton import xyz.atnrch.wrench.gui.filemanager.bottom.FloatingButton
import xyz.atnrch.wrench.gui.filemanager.top.TopBar import xyz.atnrch.wrench.gui.filemanager.top.TopBar
import xyz.atnrch.wrench.gui.style.UIColors 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.Watcher
import xyz.atnrch.wrench.watcher.WatcherEntry import xyz.atnrch.wrench.watcher.WatcherEntry
import xyz.atnrch.wrench.watcher.WatcherManager import xyz.atnrch.wrench.watcher.WatcherManager
@ -27,7 +27,7 @@ fun WrenchScaffold(state: WindowState) {
val watcherManager = remember { WatcherManager(entries) } val watcherManager = remember { WatcherManager(entries) }
val watcher = remember { Watcher(watcherManager, snackBarDataHolder) } val watcher = remember { Watcher(watcherManager, snackBarDataHolder) }
val tabTitles = listOf("File Manager", "Servers") val tabTitles = listOf("File Manager", "Servers")
val jsonConfig = JsonConfig { val jsonLayout = JsonLayout {
it.forEach { entry -> it.forEach { entry ->
watcherManager.addFile(entry.file, entry.outputs) watcherManager.addFile(entry.file, entry.outputs)
} }
@ -43,7 +43,7 @@ fun WrenchScaffold(state: WindowState) {
} else { } else {
Scaffold( Scaffold(
scaffoldState = scaffoldState, scaffoldState = scaffoldState,
topBar = { TopBar(jsonConfig, tabIndex, entries.values) }, topBar = { TopBar(jsonLayout, tabIndex, entries.values) },
floatingActionButton = { if (tabIndex == 0) FloatingButton(watcherManager) }, floatingActionButton = { if (tabIndex == 0) FloatingButton(watcherManager) },
isFloatingActionButtonDocked = true, isFloatingActionButtonDocked = true,
backgroundColor = UIColors.PRIMARY, backgroundColor = UIColors.PRIMARY,

View file

@ -14,12 +14,12 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import xyz.atnrch.wrench.gui.style.Fonts import xyz.atnrch.wrench.gui.style.Fonts
import xyz.atnrch.wrench.gui.style.UIColors 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 import xyz.atnrch.wrench.watcher.WatcherEntry
@Composable @Composable
fun TopBar( fun TopBar(
jsonConfig: JsonConfig, jsonLayout: JsonLayout,
tabIndex: Int, tabIndex: Int,
values: MutableCollection<WatcherEntry> values: MutableCollection<WatcherEntry>
) { ) {
@ -42,7 +42,7 @@ fun TopBar(
}, },
actions = { actions = {
if (tabIndex == 0) { if (tabIndex == 0) {
TopBarButtons(jsonConfig, values) TopBarButtons(jsonLayout, values)
} }
} }
) )

View file

@ -7,23 +7,24 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp 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 import xyz.atnrch.wrench.watcher.WatcherEntry
@Composable @Composable
fun TopBarButtons( fun TopBarButtons(
jsonConfig: JsonConfig, jsonLayout: JsonLayout,
values: MutableCollection<WatcherEntry> values: MutableCollection<WatcherEntry>
) { ) {
Button(onClick = { Button(onClick = {
println(values.toList().joinToString(",")) println(values.toList().joinToString(","))
jsonConfig.writeLayout(values.toList()) jsonLayout.writeLayout(SerializedWatcherEntry.fromUnserializedEntries(values.toList()))
}) { }) {
Text("Save") Text("Save")
} }
Spacer(Modifier.width(10.dp)) Spacer(Modifier.width(10.dp))
Button(onClick = { Button(onClick = {
jsonConfig.readLayout() jsonLayout.readLayout()
}) { }) {
Text("Load") Text("Load")
} }

View file

@ -6,21 +6,17 @@ import xyz.atnrch.wrench.watcher.WatcherEntry
import java.io.File import java.io.File
import java.io.FileReader import java.io.FileReader
import java.io.FileWriter import java.io.FileWriter
import java.nio.file.Path
class JsonConfig( class JsonLayout(
private val onWatcherEntriesUpdate: (List<WatcherEntry>) -> Unit private val onWatcherEntriesUpdate: (List<WatcherEntry>) -> Unit
) { ) {
private val watcherEntryListType = object : TypeToken<List<WatcherEntry>>() {}.type private val entryListType = object : TypeToken<List<SerializedWatcherEntry>>() {}.type
private val watcherEntryType = object : TypeToken<WatcherEntry>() {}.type
private val gson: Gson = Gson().newBuilder() private val gson: Gson = Gson().newBuilder()
.registerTypeAdapter(watcherEntryType, WatcherEntryAdapter())
.registerTypeAdapter(Path::class.java, PathTypeAdapter())
.setPrettyPrinting() .setPrettyPrinting()
.create() .create()
private val file = File("layout.json") private val file = File("layout.json")
fun writeLayout(list: List<WatcherEntry>) { fun writeLayout(list: List<SerializedWatcherEntry>) {
val writer = FileWriter(file) val writer = FileWriter(file)
gson.toJson(list, writer) gson.toJson(list, writer)
writer.flush() writer.flush()
@ -29,8 +25,11 @@ class JsonConfig(
fun readLayout() { fun readLayout() {
val reader = FileReader(file) val reader = FileReader(file)
val entries = gson.fromJson<List<WatcherEntry>>(reader, watcherEntryListType) val line = reader.readText()
reader.close() reader.close()
onWatcherEntriesUpdate.invoke(entries)
val serializedEntries = gson.fromJson<List<SerializedWatcherEntry>>(line, entryListType)
val deserializedEntries = SerializedWatcherEntry.fromSerializedEntries(serializedEntries)
onWatcherEntriesUpdate.invoke(deserializedEntries)
} }
} }

View file

@ -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
}
}

View file

@ -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
}
}
}

View file

@ -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)
}
}