Skip to content

Commit 7891b8e

Browse files
Modified preference initialization mechanism to ignore the initialization if the preference is already available within the underlying shared preference.
1 parent 01d7201 commit 7891b8e

13 files changed

Lines changed: 272 additions & 215 deletions

File tree

PreferenceStore/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ dependencies {
3535
implementation 'com.google.code.gson:gson:2.9.1'
3636

3737
testImplementation 'junit:junit:4.13.2'
38-
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
39-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
38+
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
39+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
4040
}

PreferenceStore/src/androidTest/java/com/buggysofts/preferencestore/ExampleInstrumentedTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.buggysofts.preferencestore;
22

3+
import static org.junit.Assert.assertEquals;
4+
35
import android.content.Context;
46

5-
import androidx.test.platform.app.InstrumentationRegistry;
67
import androidx.test.ext.junit.runners.AndroidJUnit4;
8+
import androidx.test.platform.app.InstrumentationRegistry;
79

810
import org.junit.Test;
911
import org.junit.runner.RunWith;
1012

11-
import static org.junit.Assert.*;
12-
1313
/**
1414
* Instrumented test, which will execute on an Android device.
1515
*
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
2+
<manifest>
33

44
</manifest>

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
/**
77
* A preference which with a predefined set of non-null values, and a default value.
8-
* */
8+
*/
99

1010
public class BoundedPreference<T> {
1111
private final String key;
@@ -16,13 +16,13 @@ public class BoundedPreference<T> {
1616
/**
1717
* Create a bounded preference.
1818
* <br>
19-
* @param keyName Name of the preference. Should be of the form <b>pref_key_*</b>.
20-
* @param desc Description of the preference, its context, purpose & usability, and any other important information. This is not going to be used anywhere, the purpose of this is to remind the user what it is for and how to use it.
21-
* @param allValues All the supported values (by this preference). Each of the items must not be null.
22-
* @param defaultValueIndex Index of the value that should be used as default.
2319
*
20+
* @param keyName Name of the preference. Should be of the form <b>pref_key_*</b>.
21+
* @param desc Description of the preference, its context, purpose & usability, and any other important information. This is not going to be used anywhere, the purpose of this is to remind the user what it is for and how to use it.
22+
* @param allValues All the supported values (by this preference). Each of the items must not be null.
23+
* @param defaultValueIndex Index of the value that should be used as default.
2424
* @throws RuntimeException if any contract violation is found.
25-
* */
25+
*/
2626
public BoundedPreference(@NonNull String keyName,
2727
@NonNull String desc,
2828
@NonNull T[] allValues,
@@ -32,62 +32,63 @@ public BoundedPreference(@NonNull String keyName,
3232
this.allValues = allValues;
3333
this.defaultValueIndex = defaultValueIndex;
3434

35-
if(key == null){
35+
if (key == null) {
3636
throw new RuntimeException("Key must not be null.");
3737
}
38-
if(allValues == null || allValues.length == 0){
38+
if (allValues == null || allValues.length == 0) {
3939
throw new RuntimeException("Empty value list is not allowed.");
4040
} else {
4141
for (int i = 0; i < allValues.length; i++) {
42-
if(allValues[i] == null){
42+
if (allValues[i] == null) {
4343
throw new RuntimeException("Preference values can not be null.");
4444
}
4545
}
4646
}
47-
if(!(defaultValueIndex >= 0 && defaultValueIndex < allValues.length)){
47+
if (!(defaultValueIndex >= 0 && defaultValueIndex < allValues.length)) {
4848
throw new RuntimeException("Invalid default value index. Make sure the default value index lies between 0 and allValue.length - 1.");
4949
}
5050
}
5151

5252
/**
5353
* Get value at the specified index.
54+
*
5455
* @throws RuntimeException if index is invalid.
55-
* */
56+
*/
5657
@NonNull
57-
public T getValueAtIndex(@IntRange(from = 0) int index){
58-
if(index > 0 && index < allValues.length){
58+
public T getValueAtIndex(@IntRange(from = 0) int index) {
59+
if (index > 0 && index < allValues.length) {
5960
return allValues[index];
6061
}
6162
throw new RuntimeException("Invalid value index. Make sure the default value index lies between 0 and allValue.length - 1");
6263
}
6364

6465
/**
6566
* Get all the available values.
66-
* */
67+
*/
6768
@NonNull
6869
public T[] getAllValues() {
6970
return allValues;
7071
}
7172

7273
/**
7374
* Get the default value of this preference.
74-
* */
75+
*/
7576
@NonNull
7677
public T getDefaultValue() {
7778
return allValues[defaultValueIndex];
7879
}
7980

8081
/**
8182
* Get key string.
82-
* */
83+
*/
8384
@NonNull
8485
public String getKey() {
8586
return key;
8687
}
8788

8889
/**
8990
* Get key string.
90-
* */
91+
*/
9192
@NonNull
9293
public String getDescription() {
9394
return desc;

0 commit comments

Comments
 (0)