Skip to content

Commit

Permalink
feature: optimize performance
Browse files Browse the repository at this point in the history
  • Loading branch information
sgpublic committed Nov 15, 2022
1 parent e80e02f commit eb0c931
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -197,7 +197,6 @@ object PreferenceCompiler {
ExPreferenceProcessor.ExConverters, type)
}
setter.addStatement("editor.apply()")
.addStatement("SharedPreferenceReference.clear()")

impl.addMethod(getter.build())
impl.addMethod(setter.build())
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
android.useAndroidX=true
android.nonTransitiveRClass=true

exsp.version=1.0.0-alpha06
exsp.version=1.0.0-alpha07
37 changes: 22 additions & 15 deletions runtime/src/main/java/io/github/sgpublic/exsp/ExPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand All @@ -54,5 +61,5 @@ object ExPreference {
}

operator fun ExPreference.Reference.getValue(thisRef: Any?, property: KProperty<*>): SharedPreferences {
return this.get()
return get()
}

0 comments on commit eb0c931

Please sign in to comment.