From 74080d008fa7b600739bdbaf3360a2466594a7b5 Mon Sep 17 00:00:00 2001 From: Pablo Baxter Date: Thu, 26 Sep 2024 21:53:40 -0700 Subject: [PATCH 1/2] Consolidate Optional utility to the core library --- core/build.gradle | 2 ++ .../frybits/rx/preferences/core/Adapters.kt | 15 +++++++++++ .../rx/preferences/core/PreferenceUtil.kt | 12 +++++++++ .../rx2/app/SampleActivityJava.java | 3 ++- .../rx/preferences/rx2/app/SampleActivity.kt | 2 +- rx2/build.gradle | 1 - .../rx/preferences/rx2/Rx2Preference.kt | 27 ------------------- .../rx3/app/SampleActivityJava.java | 3 ++- .../rx/preferences/rx3/app/SampleActivity.kt | 2 +- rx3/build.gradle | 1 - .../rx/preferences/rx3/Rx3Preference.kt | 22 --------------- 11 files changed, 35 insertions(+), 55 deletions(-) create mode 100644 core/src/main/kotlin/com/frybits/rx/preferences/core/PreferenceUtil.kt diff --git a/core/build.gradle b/core/build.gradle index 451e8fd..f4072e8 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -9,6 +9,8 @@ dependencies { implementation libs.kotlin.stdlib implementation libs.androidx.annotation + api libs.guava + // Test testImplementation libs.junit testImplementation libs.kotlin.test diff --git a/core/src/main/kotlin/com/frybits/rx/preferences/core/Adapters.kt b/core/src/main/kotlin/com/frybits/rx/preferences/core/Adapters.kt index eedfdc4..6a00aab 100644 --- a/core/src/main/kotlin/com/frybits/rx/preferences/core/Adapters.kt +++ b/core/src/main/kotlin/com/frybits/rx/preferences/core/Adapters.kt @@ -1,6 +1,7 @@ package com.frybits.rx.preferences.core import android.content.SharedPreferences +import com.google.common.base.Optional /* * Copyright 2014 Prateek Srivastava @@ -149,3 +150,17 @@ object StringSetAdapter : Adapter?> { editor.putStringSet(key, value) } } + +internal class OptionalAdapter(private val adapter: Adapter) : Adapter> { + override fun get( + key: String?, + sharedPreference: SharedPreferences, + defaultValue: Optional + ): Optional { + return Optional.fromNullable(adapter.get(key, sharedPreference, defaultValue.orNull())) + } + + override fun set(key: String?, value: Optional, editor: SharedPreferences.Editor) { + adapter.set(key, value.orNull(), editor) + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/frybits/rx/preferences/core/PreferenceUtil.kt b/core/src/main/kotlin/com/frybits/rx/preferences/core/PreferenceUtil.kt new file mode 100644 index 0000000..7d427bc --- /dev/null +++ b/core/src/main/kotlin/com/frybits/rx/preferences/core/PreferenceUtil.kt @@ -0,0 +1,12 @@ +@file:JvmName("PreferenceUtil") + +package com.frybits.rx.preferences.core + +import com.google.common.base.Optional + +/** + * Converts a preference of a nullable type to be an [Optional] of that same type instead. + */ +fun Preference.asOptional(): Preference> { + return Preference(rxSharedPreferences, key, Optional.fromNullable(defaultValue), OptionalAdapter(adapter)) +} diff --git a/rx2/app/src/main/java/com/frybits/rx/preferences/rx2/app/SampleActivityJava.java b/rx2/app/src/main/java/com/frybits/rx/preferences/rx2/app/SampleActivityJava.java index d4838f2..9bc5998 100644 --- a/rx2/app/src/main/java/com/frybits/rx/preferences/rx2/app/SampleActivityJava.java +++ b/rx2/app/src/main/java/com/frybits/rx/preferences/rx2/app/SampleActivityJava.java @@ -29,6 +29,7 @@ import androidx.appcompat.app.AppCompatActivity; import com.frybits.rx.preferences.core.Preference; +import com.frybits.rx.preferences.core.PreferenceUtil; import com.frybits.rx.preferences.core.RxSharedPreferences; import com.frybits.rx.preferences.rx2.Rx2Preference; import com.frybits.rx.preferences.rx2.app.databinding.SampleLayoutBinding; @@ -52,7 +53,7 @@ public void onCreate(Bundle savedInstanceState) { RxSharedPreferences rxPreferences = RxSharedPreferences.create(getSharedPreferences("rx2", Context.MODE_PRIVATE)); Preference fooBool = rxPreferences.getBoolean("fooBool"); - Preference> fooString = Rx2Preference.asOptional(rxPreferences.getString("fooString")); + Preference> fooString = PreferenceUtil.asOptional(rxPreferences.getString("fooString")); bindPreference(binding.checkBox, fooBool); bindPreference(binding.checkBox2, fooBool); diff --git a/rx2/app/src/main/kotlin/com/frybits/rx/preferences/rx2/app/SampleActivity.kt b/rx2/app/src/main/kotlin/com/frybits/rx/preferences/rx2/app/SampleActivity.kt index 7b999a6..39463d5 100644 --- a/rx2/app/src/main/kotlin/com/frybits/rx/preferences/rx2/app/SampleActivity.kt +++ b/rx2/app/src/main/kotlin/com/frybits/rx/preferences/rx2/app/SampleActivity.kt @@ -9,10 +9,10 @@ import android.widget.EditText import androidx.appcompat.app.AppCompatActivity import com.frybits.rx.preferences.core.Preference import com.frybits.rx.preferences.core.RxSharedPreferences.Companion.asRxSharedPreferences +import com.frybits.rx.preferences.core.asOptional import com.frybits.rx.preferences.rx2.app.databinding.SampleLayoutBinding import com.frybits.rx.preferences.rx2.asConsumer import com.frybits.rx.preferences.rx2.asObservable -import com.frybits.rx.preferences.rx2.asOptional import com.google.common.base.Optional import io.reactivex.Observable import io.reactivex.disposables.CompositeDisposable diff --git a/rx2/build.gradle b/rx2/build.gradle index 9516cd2..1428aaa 100644 --- a/rx2/build.gradle +++ b/rx2/build.gradle @@ -14,7 +14,6 @@ dependencies { // RxJava api libs.reactivex.rx2 - api libs.guava // Test testImplementation libs.junit diff --git a/rx2/src/main/kotlin/com/frybits/rx/preferences/rx2/Rx2Preference.kt b/rx2/src/main/kotlin/com/frybits/rx/preferences/rx2/Rx2Preference.kt index 1fa2f02..9094139 100644 --- a/rx2/src/main/kotlin/com/frybits/rx/preferences/rx2/Rx2Preference.kt +++ b/rx2/src/main/kotlin/com/frybits/rx/preferences/rx2/Rx2Preference.kt @@ -4,7 +4,6 @@ package com.frybits.rx.preferences.rx2 import android.content.SharedPreferences import androidx.annotation.CheckResult -import com.frybits.rx.preferences.core.Adapter import com.frybits.rx.preferences.core.Preference import com.google.common.base.Optional import io.reactivex.Observable @@ -53,18 +52,6 @@ fun Preference.asConsumer(): Consumer { } } -/** - * Converts a preference of a nullable type to be an [Optional] of that same type instead. - */ -fun Preference.asOptional(): Preference> { - return Preference( - rxSharedPreferences, - key, - Optional.fromNullable(defaultValue), - OptionalAdapter(adapter) - ) -} - private val Preference.keysChanged: Observable> get() = rxSharedPreferences.getOrCreateKeyChangedStream(RX2_STREAM) { Observable.create> { emitter -> @@ -82,17 +69,3 @@ private val Preference.keysChanged: Observable> rxSharedPreferences.sharedPreferences.registerOnSharedPreferenceChangeListener(listener) }.share() } - -private class OptionalAdapter(private val adapter: Adapter) : Adapter> { - override fun get( - key: String?, - sharedPreference: SharedPreferences, - defaultValue: Optional - ): Optional { - return Optional.fromNullable(adapter.get(key, sharedPreference, defaultValue.get())) - } - - override fun set(key: String?, value: Optional, editor: SharedPreferences.Editor) { - adapter.set(key, value.orNull(), editor) - } -} diff --git a/rx3/app/src/main/java/com/frybits/rx/preferences/rx3/app/SampleActivityJava.java b/rx3/app/src/main/java/com/frybits/rx/preferences/rx3/app/SampleActivityJava.java index 91d7f2d..f822de2 100644 --- a/rx3/app/src/main/java/com/frybits/rx/preferences/rx3/app/SampleActivityJava.java +++ b/rx3/app/src/main/java/com/frybits/rx/preferences/rx3/app/SampleActivityJava.java @@ -29,6 +29,7 @@ import androidx.appcompat.app.AppCompatActivity; import com.frybits.rx.preferences.core.Preference; +import com.frybits.rx.preferences.core.PreferenceUtil; import com.frybits.rx.preferences.core.RxSharedPreferences; import com.frybits.rx.preferences.rx3.Rx3Preference; import com.frybits.rx.preferences.rx3.app.databinding.SampleLayoutBinding; @@ -52,7 +53,7 @@ public void onCreate(Bundle savedInstanceState) { RxSharedPreferences rxPreferences = RxSharedPreferences.create(getSharedPreferences("rx2", Context.MODE_PRIVATE)); Preference fooBool = rxPreferences.getBoolean("fooBool"); - Preference> fooString = Rx3Preference.asOptional(rxPreferences.getString("fooString")); + Preference> fooString = PreferenceUtil.asOptional(rxPreferences.getString("fooString")); bindPreference(binding.checkBox, fooBool); bindPreference(binding.checkBox2, fooBool); diff --git a/rx3/app/src/main/kotlin/com/frybits/rx/preferences/rx3/app/SampleActivity.kt b/rx3/app/src/main/kotlin/com/frybits/rx/preferences/rx3/app/SampleActivity.kt index 912132c..7f8da16 100644 --- a/rx3/app/src/main/kotlin/com/frybits/rx/preferences/rx3/app/SampleActivity.kt +++ b/rx3/app/src/main/kotlin/com/frybits/rx/preferences/rx3/app/SampleActivity.kt @@ -9,10 +9,10 @@ import android.widget.EditText import androidx.appcompat.app.AppCompatActivity import com.frybits.rx.preferences.core.Preference import com.frybits.rx.preferences.core.RxSharedPreferences.Companion.asRxSharedPreferences +import com.frybits.rx.preferences.core.asOptional import com.frybits.rx.preferences.rx3.app.databinding.SampleLayoutBinding import com.frybits.rx.preferences.rx3.asConsumer import com.frybits.rx.preferences.rx3.asObservable -import com.frybits.rx.preferences.rx3.asOptional import com.google.common.base.Optional import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.disposables.CompositeDisposable diff --git a/rx3/build.gradle b/rx3/build.gradle index bc957c4..dddfb27 100644 --- a/rx3/build.gradle +++ b/rx3/build.gradle @@ -14,7 +14,6 @@ dependencies { // RxJava api libs.reactivex.rx3 - api libs.guava // Test testImplementation libs.junit diff --git a/rx3/src/main/kotlin/com/frybits/rx/preferences/rx3/Rx3Preference.kt b/rx3/src/main/kotlin/com/frybits/rx/preferences/rx3/Rx3Preference.kt index 7b6ece0..b4f768f 100644 --- a/rx3/src/main/kotlin/com/frybits/rx/preferences/rx3/Rx3Preference.kt +++ b/rx3/src/main/kotlin/com/frybits/rx/preferences/rx3/Rx3Preference.kt @@ -4,7 +4,6 @@ package com.frybits.rx.preferences.rx3 import android.content.SharedPreferences import androidx.annotation.CheckResult -import com.frybits.rx.preferences.core.Adapter import com.frybits.rx.preferences.core.Preference import com.google.common.base.Optional import io.reactivex.rxjava3.core.Observable @@ -53,13 +52,6 @@ fun Preference.asConsumer(): Consumer { } } -/** - * Converts a preference of a nullable type to be an [Optional] of that same type instead. - */ -fun Preference.asOptional(): Preference> { - return Preference(rxSharedPreferences, key, Optional.fromNullable(defaultValue), OptionalAdapter(adapter)) -} - private val Preference.keysChanged: Observable> get() = rxSharedPreferences.getOrCreateKeyChangedStream(RX3_STREAM) { Observable.create> { emitter -> @@ -75,17 +67,3 @@ private val Preference.keysChanged: Observable> rxSharedPreferences.sharedPreferences.registerOnSharedPreferenceChangeListener(listener) }.share() } - -private class OptionalAdapter(private val adapter: Adapter) : Adapter> { - override fun get( - key: String?, - sharedPreference: SharedPreferences, - defaultValue: Optional - ): Optional { - return Optional.fromNullable(adapter.get(key, sharedPreference, defaultValue.get())) - } - - override fun set(key: String?, value: Optional, editor: SharedPreferences.Editor) { - adapter.set(key, value.orNull(), editor) - } -} From c1c03a40802b99198f0beb54fe316826294a3048 Mon Sep 17 00:00:00 2001 From: Pablo Baxter Date: Thu, 26 Sep 2024 21:55:32 -0700 Subject: [PATCH 2/2] Fix extra space after file --- .../src/main/kotlin/com/frybits/rx/preferences/core/Adapters.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/kotlin/com/frybits/rx/preferences/core/Adapters.kt b/core/src/main/kotlin/com/frybits/rx/preferences/core/Adapters.kt index 6a00aab..5112ad7 100644 --- a/core/src/main/kotlin/com/frybits/rx/preferences/core/Adapters.kt +++ b/core/src/main/kotlin/com/frybits/rx/preferences/core/Adapters.kt @@ -163,4 +163,4 @@ internal class OptionalAdapter(private val adapter: Adapter) : Adapter, editor: SharedPreferences.Editor) { adapter.set(key, value.orNull(), editor) } -} \ No newline at end of file +}