From c7b3694035657af6b59588253791ed0aeaeb27d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linnea=20Gr=C3=A4f?= Date: Mon, 6 May 2024 20:21:20 +0200 Subject: [PATCH] Add auto saving to managed config --- .../java/io/github/notenoughupdates/moulconfig/Config.java | 5 +++++ .../notenoughupdates/moulconfig/managed/ManagedConfig.kt | 4 +++- .../notenoughupdates/moulconfig/managed/ManagedDataFile.kt | 5 ++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/io/github/notenoughupdates/moulconfig/Config.java b/common/src/main/java/io/github/notenoughupdates/moulconfig/Config.java index 605e9aec7..d9768994c 100644 --- a/common/src/main/java/io/github/notenoughupdates/moulconfig/Config.java +++ b/common/src/main/java/io/github/notenoughupdates/moulconfig/Config.java @@ -55,6 +55,9 @@ public String getTitle() { } public void saveNow() { + for (Runnable saveRunnable : saveRunnables) { + saveRunnable.run(); + } } public DescriptionRendereringBehaviour getDescriptionBehaviour(ProcessedOption option) { @@ -68,4 +71,6 @@ public boolean shouldAutoFocusSearchbar() { public boolean shouldSearchCategoryNames() { return true; } + + public transient List saveRunnables = new ArrayList<>(); } diff --git a/common/src/main/java/io/github/notenoughupdates/moulconfig/managed/ManagedConfig.kt b/common/src/main/java/io/github/notenoughupdates/moulconfig/managed/ManagedConfig.kt index c3483e74e..3468bf50d 100644 --- a/common/src/main/java/io/github/notenoughupdates/moulconfig/managed/ManagedConfig.kt +++ b/common/src/main/java/io/github/notenoughupdates/moulconfig/managed/ManagedConfig.kt @@ -29,11 +29,13 @@ class ManagedConfig(private val builder: ManagedConfigBuilder) : return ManagedConfig(ManagedConfigBuilder(file, clazz).apply(consumer)) } } - // TODO: enforce the save callback, somehow lateinit var processor: MoulConfigProcessor private set + override fun injectIntoInstance() { + instance.saveRunnables?.add(this::saveToFile) + } fun rebuildConfigProcessor() { rebuildConfigProcessor(builder) diff --git a/common/src/main/java/io/github/notenoughupdates/moulconfig/managed/ManagedDataFile.kt b/common/src/main/java/io/github/notenoughupdates/moulconfig/managed/ManagedDataFile.kt index 9d7d10759..0438cf178 100644 --- a/common/src/main/java/io/github/notenoughupdates/moulconfig/managed/ManagedDataFile.kt +++ b/common/src/main/java/io/github/notenoughupdates/moulconfig/managed/ManagedDataFile.kt @@ -58,9 +58,12 @@ open class ManagedDataFile internal constructor( } catch (ex: Exception) { loadFailed.accept(this, ex) } + injectIntoInstance() afterLoad.accept(this) } + open fun injectIntoInstance() {} + private fun createUniqueExtraFile(identifier: String, directory: File = file.parentFile): File { val jvmHash = ManagementFactory.getRuntimeMXBean().name.hashCode() val timestamp = System.currentTimeMillis().toString() @@ -86,4 +89,4 @@ open class ManagedDataFile internal constructor( } afterSave.accept(this) } -} \ No newline at end of file +}