Skip to content

Commit 07d6b5f

Browse files
Modified preference initialization mechanism to ignore the initialization if the preference is already available within the underlying shared preference.
1 parent 88c0292 commit 07d6b5f

4 files changed

Lines changed: 32 additions & 11 deletions

File tree

.idea/.name

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PreferenceStore/src/main/java/com/buggysofts/preferencestore/PreferenceHandler.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public boolean getPreferenceValue(@NonNull BoundedPreference<Boolean> preference
6565
/**
6666
* Get value of the specified preference.
6767
* @param preference The preference from which we want our value.
68-
* @param clazz Data type of <b>preference</b>. It is a dummy parameter used to avoid method erasure.
6968
* @param defaultOverride Optional value to override the actual return from underlying {@link SharedPreferences} instance.
7069
* Although it is declared as a vararg, you should pass at most one value. Others will be discarded.
7170
*
@@ -677,7 +676,7 @@ public <T extends Serializable> T setPreferenceValue(@NonNull UnBoundedPreferenc
677676

678677
// initializers
679678
/**
680-
* Initialize the bounded preference.
679+
* Initialize the bounded preference. This call will return the passed preference immediately (without any further operation) if the preference is already available within the current underlying shared preference.
681680
* @param preference The preference which we are initializing.
682681
* @param overrideDefault Optional value to override the default of the specified bounded preference.
683682
* If it is not provided, the default value of the specified bounded preference will be used to initialize the preference.
@@ -687,6 +686,9 @@ public <T extends Serializable> T setPreferenceValue(@NonNull UnBoundedPreferenc
687686
@NonNull
688687
public BoundedPreference<Boolean> initializePreference(@NonNull BoundedPreference<Boolean> preference,
689688
@NonNull Boolean... overrideDefault) throws RuntimeException {
689+
// if already available, do not overwrite
690+
if(contains(preference)) return preference;
691+
690692
Boolean defaultValue = preference.getDefaultValue();
691693
if(overrideDefault.length > 0){
692694
// this will not be null here
@@ -703,7 +705,7 @@ public BoundedPreference<Boolean> initializePreference(@NonNull BoundedPreferenc
703705
return preference;
704706
}
705707
/**
706-
* Initialize the bounded preference.
708+
* Initialize the bounded preference. This call will return the passed preference immediately (without any further operation) if the preference is already available within the current underlying shared preference.
707709
* @param preference The preference which we are initializing.
708710
* @param overrideDefault Optional value to override the default of the specified bounded preference.
709711
* If it is not provided, the default value of the specified bounded preference will be used to initialize the preference.
@@ -713,6 +715,9 @@ public BoundedPreference<Boolean> initializePreference(@NonNull BoundedPreferenc
713715
@NonNull
714716
public BoundedPreference<Integer> initializePreference(@NonNull BoundedPreference<Integer> preference,
715717
@NonNull Integer... overrideDefault) throws RuntimeException {
718+
// if already available, do not overwrite
719+
if(contains(preference)) return preference;
720+
716721
Integer defaultValue = preference.getDefaultValue();
717722
if(overrideDefault.length > 0){
718723
// this will not be null here
@@ -729,7 +734,7 @@ public BoundedPreference<Integer> initializePreference(@NonNull BoundedPreferenc
729734
return preference;
730735
}
731736
/**
732-
* Initialize the bounded preference.
737+
* Initialize the bounded preference. This call will return the passed preference immediately (without any further operation) if the preference is already available within the current underlying shared preference.
733738
* @param preference The preference which we are initializing.
734739
* @param overrideDefault Optional value to override the default of the specified bounded preference.
735740
* If it is not provided, the default value of the specified bounded preference will be used to initialize the preference.
@@ -739,6 +744,9 @@ public BoundedPreference<Integer> initializePreference(@NonNull BoundedPreferenc
739744
@NonNull
740745
public BoundedPreference<Long> initializePreference(@NonNull BoundedPreference<Long> preference,
741746
@NonNull Long... overrideDefault) throws RuntimeException {
747+
// if already available, do not overwrite
748+
if(contains(preference)) return preference;
749+
742750
Long defaultValue = preference.getDefaultValue();
743751
if(overrideDefault.length > 0){
744752
// this will not be null here
@@ -755,7 +763,7 @@ public BoundedPreference<Long> initializePreference(@NonNull BoundedPreference<L
755763
return preference;
756764
}
757765
/**
758-
* Initialize the bounded preference.
766+
* Initialize the bounded preference. This call will return the passed preference immediately (without any further operation) if the preference is already available within the current underlying shared preference.
759767
* @param preference The preference which we are initializing.
760768
* @param overrideDefault Optional value to override the default of the specified bounded preference.
761769
* If it is not provided, the default value of the specified bounded preference will be used to initialize the preference.
@@ -765,6 +773,9 @@ public BoundedPreference<Long> initializePreference(@NonNull BoundedPreference<L
765773
@NonNull
766774
public BoundedPreference<Float> initializePreference(@NonNull BoundedPreference<Float> preference,
767775
@NonNull Float... overrideDefault) throws RuntimeException {
776+
// if already available, do not overwrite
777+
if(contains(preference)) return preference;
778+
768779
Float defaultValue = preference.getDefaultValue();
769780
if(overrideDefault.length > 0){
770781
// this will not be null here
@@ -781,7 +792,7 @@ public BoundedPreference<Float> initializePreference(@NonNull BoundedPreference<
781792
return preference;
782793
}
783794
/**
784-
* Initialize the bounded preference.
795+
* Initialize the bounded preference. This call will return the passed preference immediately (without any further operation) if the preference is already available within the current underlying shared preference.
785796
* @param preference The preference which we are initializing.
786797
* @param overrideDefault Optional value to override the default of the specified bounded preference.
787798
* If it is not provided, the default value of the specified bounded preference will be used to initialize the preference.
@@ -791,6 +802,9 @@ public BoundedPreference<Float> initializePreference(@NonNull BoundedPreference<
791802
@NonNull
792803
public BoundedPreference<String> initializePreference(@NonNull BoundedPreference<String> preference,
793804
@NonNull String... overrideDefault) throws RuntimeException {
805+
// if already available, do not overwrite
806+
if(contains(preference)) return preference;
807+
794808
String defaultValue = preference.getDefaultValue();
795809
if(overrideDefault.length > 0){
796810
// this will not be null here
@@ -807,7 +821,7 @@ public BoundedPreference<String> initializePreference(@NonNull BoundedPreference
807821
return preference;
808822
}
809823
/**
810-
* Initialize the bounded preference.
824+
* Initialize the bounded preference. This call will return the passed preference immediately (without any further operation) if the preference is already available within the current underlying shared preference.
811825
* @param preference The preference which we are initializing.
812826
* @param overrideDefault Optional value to override the default of the specified bounded preference.
813827
* If it is not provided, the default value of the specified bounded preference will be used to initialize the preference.
@@ -818,6 +832,9 @@ public BoundedPreference<String> initializePreference(@NonNull BoundedPreference
818832
@NonNull
819833
public final BoundedPreference<Set<String>> initializePreference(@NonNull BoundedPreference<Set<String>> preference,
820834
@NonNull Set<String>... overrideDefault) throws RuntimeException {
835+
// if already available, do not overwrite
836+
if(contains(preference)) return preference;
837+
821838
Set<String> defaultValue = preference.getDefaultValue();
822839
if(overrideDefault.length > 0){
823840
// this will not be null here
@@ -834,7 +851,7 @@ public final BoundedPreference<Set<String>> initializePreference(@NonNull Bounde
834851
return preference;
835852
}
836853
/**
837-
* Initialize the bounded preference.
854+
* Initialize the bounded preference. This call will return the passed preference immediately (without any further operation) if the preference is already available within the current underlying shared preference.
838855
* @param preference The preference which we are initializing.
839856
* @param overrideDefault Optional value to override the default of the specified bounded preference.
840857
* If it is not provided, the default value of the specified bounded preference will be used to initialize the preference.
@@ -845,6 +862,9 @@ public final BoundedPreference<Set<String>> initializePreference(@NonNull Bounde
845862
@NonNull
846863
public final <T extends Serializable> BoundedPreference<T> initializePreference(BoundedPreference<T> preference,
847864
@NonNull T... overrideDefault) throws RuntimeException {
865+
// if already available, do not overwrite
866+
if(contains(preference)) return preference;
867+
848868
T defaultValue = preference.getDefaultValue();
849869
if(overrideDefault.length > 0){
850870
// this will not be null here

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Finally, add these two dependencies to your app/module level build.gradle file
3434
dependencies {
3535
...
3636
implementation 'com.google.code.gson:gson:2.9.1'
37-
implementation 'com.github.buggysofts-com:PreferenceStore:v1.0.0'
37+
implementation 'com.github.buggysofts-com:PreferenceStore:v1.0.1'
3838
}
3939
```
4040
And you are done importing the library.

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22
plugins {
3-
id 'com.android.application' version '7.3.0' apply false
4-
id 'com.android.library' version '7.3.0' apply false
3+
id 'com.android.application' version '7.3.1' apply false
4+
id 'com.android.library' version '7.3.1' apply false
55
}

0 commit comments

Comments
 (0)