Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate Optional utility to the core library #20

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ dependencies {
implementation libs.kotlin.stdlib
implementation libs.androidx.annotation

api libs.guava

// Test
testImplementation libs.junit
testImplementation libs.kotlin.test
Expand Down
15 changes: 15 additions & 0 deletions core/src/main/kotlin/com/frybits/rx/preferences/core/Adapters.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.frybits.rx.preferences.core

import android.content.SharedPreferences
import com.google.common.base.Optional

/*
* Copyright 2014 Prateek Srivastava
Expand Down Expand Up @@ -149,3 +150,17 @@ object StringSetAdapter : Adapter<Set<String?>?> {
editor.putStringSet(key, value)
}
}

internal class OptionalAdapter<T>(private val adapter: Adapter<T?>) : Adapter<Optional<T>> {
override fun get(
key: String?,
sharedPreference: SharedPreferences,
defaultValue: Optional<T>
): Optional<T> {
return Optional.fromNullable(adapter.get(key, sharedPreference, defaultValue.orNull()))
}

override fun set(key: String?, value: Optional<T>, editor: SharedPreferences.Editor) {
adapter.set(key, value.orNull(), editor)
}
}
Original file line number Diff line number Diff line change
@@ -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 <T> Preference<T?>.asOptional(): Preference<Optional<T>> {
return Preference(rxSharedPreferences, key, Optional.fromNullable(defaultValue), OptionalAdapter(adapter))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,7 +53,7 @@ public void onCreate(Bundle savedInstanceState) {
RxSharedPreferences rxPreferences = RxSharedPreferences.create(getSharedPreferences("rx2", Context.MODE_PRIVATE));

Preference<Boolean> fooBool = rxPreferences.getBoolean("fooBool");
Preference<Optional<String>> fooString = Rx2Preference.asOptional(rxPreferences.getString("fooString"));
Preference<Optional<String>> fooString = PreferenceUtil.asOptional(rxPreferences.getString("fooString"));

bindPreference(binding.checkBox, fooBool);
bindPreference(binding.checkBox2, fooBool);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion rx2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dependencies {

// RxJava
api libs.reactivex.rx2
api libs.guava

// Test
testImplementation libs.junit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,18 +52,6 @@ fun <T : Any> Preference<T>.asConsumer(): Consumer<T> {
}
}

/**
* Converts a preference of a nullable type to be an [Optional] of that same type instead.
*/
fun <T> Preference<T?>.asOptional(): Preference<Optional<T>> {
return Preference(
rxSharedPreferences,
key,
Optional.fromNullable(defaultValue),
OptionalAdapter(adapter)
)
}

private val <T> Preference<T>.keysChanged: Observable<Optional<String?>>
get() = rxSharedPreferences.getOrCreateKeyChangedStream(RX2_STREAM) {
Observable.create<Optional<String?>> { emitter ->
Expand All @@ -82,17 +69,3 @@ private val <T> Preference<T>.keysChanged: Observable<Optional<String?>>
rxSharedPreferences.sharedPreferences.registerOnSharedPreferenceChangeListener(listener)
}.share()
}

private class OptionalAdapter<T>(private val adapter: Adapter<T?>) : Adapter<Optional<T>> {
override fun get(
key: String?,
sharedPreference: SharedPreferences,
defaultValue: Optional<T>
): Optional<T> {
return Optional.fromNullable(adapter.get(key, sharedPreference, defaultValue.get()))
}

override fun set(key: String?, value: Optional<T>, editor: SharedPreferences.Editor) {
adapter.set(key, value.orNull(), editor)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,7 +53,7 @@ public void onCreate(Bundle savedInstanceState) {
RxSharedPreferences rxPreferences = RxSharedPreferences.create(getSharedPreferences("rx2", Context.MODE_PRIVATE));

Preference<Boolean> fooBool = rxPreferences.getBoolean("fooBool");
Preference<Optional<String>> fooString = Rx3Preference.asOptional(rxPreferences.getString("fooString"));
Preference<Optional<String>> fooString = PreferenceUtil.asOptional(rxPreferences.getString("fooString"));

bindPreference(binding.checkBox, fooBool);
bindPreference(binding.checkBox2, fooBool);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion rx3/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dependencies {

// RxJava
api libs.reactivex.rx3
api libs.guava

// Test
testImplementation libs.junit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,13 +52,6 @@ fun <T : Any> Preference<T>.asConsumer(): Consumer<T> {
}
}

/**
* Converts a preference of a nullable type to be an [Optional] of that same type instead.
*/
fun <T> Preference<T?>.asOptional(): Preference<Optional<T>> {
return Preference(rxSharedPreferences, key, Optional.fromNullable(defaultValue), OptionalAdapter(adapter))
}

private val <T> Preference<T>.keysChanged: Observable<Optional<String?>>
get() = rxSharedPreferences.getOrCreateKeyChangedStream(RX3_STREAM) {
Observable.create<Optional<String?>> { emitter ->
Expand All @@ -75,17 +67,3 @@ private val <T> Preference<T>.keysChanged: Observable<Optional<String?>>
rxSharedPreferences.sharedPreferences.registerOnSharedPreferenceChangeListener(listener)
}.share()
}

private class OptionalAdapter<T>(private val adapter: Adapter<T?>) : Adapter<Optional<T>> {
override fun get(
key: String?,
sharedPreference: SharedPreferences,
defaultValue: Optional<T>
): Optional<T> {
return Optional.fromNullable(adapter.get(key, sharedPreference, defaultValue.get()))
}

override fun set(key: String?, value: Optional<T>, editor: SharedPreferences.Editor) {
adapter.set(key, value.orNull(), editor)
}
}