From eb0c931fefb5adeeb347edf29fb3a344950dd445 Mon Sep 17 00:00:00 2001 From: Madray Haven Date: Tue, 15 Nov 2022 16:29:11 +0800 Subject: [PATCH] feature: optimize performance --- .../sgpublic/exsp/core/PreferenceCompiler.kt | 3 +- gradle.properties | 2 +- .../io/github/sgpublic/exsp/ExPreference.kt | 37 +++++++++++-------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/compiler/src/main/kotlin/io/github/sgpublic/exsp/core/PreferenceCompiler.kt b/compiler/src/main/kotlin/io/github/sgpublic/exsp/core/PreferenceCompiler.kt index 6023894..e937605 100644 --- a/compiler/src/main/kotlin/io/github/sgpublic/exsp/core/PreferenceCompiler.kt +++ b/compiler/src/main/kotlin/io/github/sgpublic/exsp/core/PreferenceCompiler.kt @@ -136,7 +136,7 @@ object PreferenceCompiler { .addModifiers(Modifier.PUBLIC) .addAnnotation(Override::class.java) .addParameter(type, "value") - .addStatement("SharedPreferences.Editor editor = SharedPreferenceReference.get().edit()") + .addStatement("SharedPreferences.Editor editor = SharedPreferenceReference.edit()") var convertedType = type @@ -197,7 +197,6 @@ object PreferenceCompiler { ExPreferenceProcessor.ExConverters, type) } setter.addStatement("editor.apply()") - .addStatement("SharedPreferenceReference.clear()") impl.addMethod(getter.build()) impl.addMethod(setter.build()) diff --git a/gradle.properties b/gradle.properties index f9ae5de..dc1a68f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,4 +4,4 @@ org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 android.useAndroidX=true android.nonTransitiveRClass=true -exsp.version=1.0.0-alpha06 \ No newline at end of file +exsp.version=1.0.0-alpha07 \ No newline at end of file diff --git a/runtime/src/main/java/io/github/sgpublic/exsp/ExPreference.kt b/runtime/src/main/java/io/github/sgpublic/exsp/ExPreference.kt index 3b80bef..da01ce6 100644 --- a/runtime/src/main/java/io/github/sgpublic/exsp/ExPreference.kt +++ b/runtime/src/main/java/io/github/sgpublic/exsp/ExPreference.kt @@ -15,29 +15,36 @@ object ExPreference { @JvmStatic fun getSharedPreference(name: String, mode: Int): Reference { - return Reference(context?.get()!!, name, mode) + return Reference(requiredContext(), name, mode) } - class Reference( + private fun requiredContext() = + context?.get() ?: throw IllegalStateException("Context are not initialized, did you call ExPreference.init(context)?") + + class Reference internal constructor( private val context: Context, private val name: String, private val mode: Int, ) { - private var SharedPreferences: SharedPreferences? = null + private var sp: SharedPreferences? = null fun get(): SharedPreferences { - synchronized(this) { - if (SharedPreferences == null) { - SharedPreferences = context.getSharedPreferences(name, mode) - } - return SharedPreferences!! + sp?.let { return it } + context.getSharedPreferences(name, mode).let { + sp = it + it.registerOnSharedPreferenceChangeListener(object : + SharedPreferences.OnSharedPreferenceChangeListener { + override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) { + it.unregisterOnSharedPreferenceChangeListener(this) + sharedPreferences.registerOnSharedPreferenceChangeListener(this) + sp = sharedPreferences + } + }) + return it } } - fun clear() { - synchronized(this) { - if (SharedPreferences != null) { - SharedPreferences = null - } - } + + fun edit(): SharedPreferences.Editor { + return get().edit() } } @@ -54,5 +61,5 @@ object ExPreference { } operator fun ExPreference.Reference.getValue(thisRef: Any?, property: KProperty<*>): SharedPreferences { - return this.get() + return get() } \ No newline at end of file