feat: layout saving made right i guess
This commit is contained in:
parent
5398aec1ed
commit
2736f12ce3
7 changed files with 66 additions and 105 deletions
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
Reference in a new issue