mirror of
https://github.com/rhunk/SnapEnhance.git
synced 2025-06-12 13:17:42 +02:00
refactor: ModConfig lateInit
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
package me.rhunk.snapenhance.common.config
|
||||
|
||||
import android.content.Context
|
||||
import com.google.gson.JsonObject
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
@ -83,6 +84,12 @@ open class ConfigContainer(
|
||||
}
|
||||
}
|
||||
|
||||
open fun lateInit(context: Context) {
|
||||
properties.values.filter { it.getNullable() is ConfigContainer }.forEach {
|
||||
(it.get() as ConfigContainer).lateInit(context)
|
||||
}
|
||||
}
|
||||
|
||||
operator fun getValue(t: Any?, property: KProperty<*>) = this.globalState
|
||||
operator fun setValue(t: Any?, property: KProperty<*>, t1: Boolean?) { this.globalState = t1 }
|
||||
}
|
@ -12,7 +12,9 @@ import me.rhunk.snapenhance.common.config.impl.RootConfig
|
||||
import me.rhunk.snapenhance.common.logger.AbstractLogger
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
class ModConfig {
|
||||
class ModConfig(
|
||||
private val context: Context
|
||||
) {
|
||||
var locale: String = LocaleWrapper.DEFAULT_LOCALE
|
||||
|
||||
private val gson: Gson = GsonBuilder().setPrettyPrinting().create()
|
||||
@ -24,8 +26,10 @@ class ModConfig {
|
||||
lateinit var root: RootConfig
|
||||
private set
|
||||
|
||||
private fun createRootConfig() = RootConfig().apply { lateInit(context) }
|
||||
|
||||
private fun load() {
|
||||
root = RootConfig()
|
||||
root = createRootConfig()
|
||||
wasPresent = file.isFileExists()
|
||||
if (!file.isFileExists()) {
|
||||
writeConfig()
|
||||
@ -95,7 +99,7 @@ class ModConfig {
|
||||
|
||||
configStateListener?.also {
|
||||
runCatching {
|
||||
compareDiff(RootConfig().apply {
|
||||
compareDiff(createRootConfig().apply {
|
||||
fromJson(gson.fromJson(file.read().toString(Charsets.UTF_8), JsonObject::class.java))
|
||||
}, root)
|
||||
|
||||
|
Reference in New Issue
Block a user