Skip to content

Commit

Permalink
Update PrefView to support default true
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Oct 6, 2023
1 parent 39370ed commit af3a008
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

import com.eveningoutpost.dexdrip.adapters.ObservableArrayMapNoNotify;

import lombok.val;

/**
* Created by jamorham on 05/10/2017.
*
* <p>
* Implementation of PrefsView
*/

Expand All @@ -15,12 +17,14 @@ public class PrefsViewImpl extends ObservableArrayMapNoNotify<String, Boolean> i
private Runnable runnable;

public boolean getbool(String name) {
return Pref.getBooleanDefaultFalse(name);
if (name == null) return false;
return PrefHandle.parse(name).getBoolean();
}

public void setbool(String name, boolean value) {
Pref.setBoolean(name, value);
super.put(name, value);
val handle = PrefHandle.parse(name);
Pref.setBoolean(handle.key, value);
super.put(handle.key, value);
doRunnable();
}

Expand All @@ -42,27 +46,57 @@ private void doRunnable() {
@NonNull
@Override
public Boolean get(Object key) {
Boolean value = super.get(key);
val handle = PrefHandle.parse((String) key);
Boolean value = super.get(handle.key);
if (value == null) {
value = getbool((String) key);
super.putNoNotify((String) key, value);
super.putNoNotify(handle.key, value);
}
return value;
}

@Override
public Boolean put(String key, Boolean value) {
if (!(super.get(key).equals(value))) {
Pref.setBoolean(key, value);
super.put(key, value);
val handle = PrefHandle.parse(key);
if (!(super.get(handle.key).equals(value))) {
Pref.setBoolean(handle.key, value);
super.put(handle.key, value);
doRunnable();
}
return value;
}

public void put(Object key, boolean value) {
if (!(super.get(key).equals(value))) {
super.put((String) key, value);
val handle = PrefHandle.parse((String) key);
if (!(super.get(handle.key).equals(value))) {
super.put(handle.key, value);
}
}

public static class PrefHandle {
public final String key;
public final boolean defaultValue;

public PrefHandle(final String key, final boolean defaultValue) {
this.key = key;
this.defaultValue = defaultValue;
}

boolean getBoolean() {
return Pref.getBoolean(key, defaultValue);
}

public static PrefHandle parse(final String identifier) {
if (identifier == null) {
return null;
}

final String[] parts = identifier.split(":", 2);
if (parts.length == 2) {
return new PrefHandle(parts[0], Boolean.parseBoolean(parts[1]));
} else {
return new PrefHandle(identifier, false);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.eveningoutpost.dexdrip.utilitymodels;

import static com.google.common.truth.Truth.assertWithMessage;

import com.eveningoutpost.dexdrip.RobolectricTestWithConfig;

import org.junit.Test;

public class PrefsViewImplTest extends RobolectricTestWithConfig {


private final PrefsViewImpl view = new PrefsViewImpl();

@Test
public void getboolTest() {

assertWithMessage("test default plain false").that(view.getbool("plain-example-no-default")).isFalse();
assertWithMessage("test default specified false").that(view.getbool("plain-example-default:false")).isFalse();
assertWithMessage("test default specified true").that(view.getbool("plain-example-default:true")).isTrue();
assertWithMessage("test default specified invalid 1").that(view.getbool("plain-example-default:")).isFalse();
assertWithMessage("test default specified invalid 2").that(view.getbool("plain-example-default:blah")).isFalse();
assertWithMessage("test default specified invalid 3").that(view.getbool("plain-example-default:true:")).isFalse();

}

@Test
public void setboolTest() {

String pref1 = "test-bool-pref1:true";

Pref.removeItem(pref1);
Pref.removeItem(PrefsViewImpl.PrefHandle.parse(pref1).key);
assertWithMessage("test 1 default before").that(view.getbool(pref1)).isTrue();
view.setbool(pref1, false);
assertWithMessage("test 1 false after").that(view.getbool(pref1)).isFalse();
view.setbool(pref1, true);
assertWithMessage("test 1 true after").that(view.getbool(pref1)).isTrue();

String pref2 = "test-bool-pref2:false";

Pref.removeItem(pref2);
Pref.removeItem(PrefsViewImpl.PrefHandle.parse(pref2).key);
assertWithMessage("test 2 default before").that(view.getbool(pref2)).isFalse();
view.setbool(pref2, false);
assertWithMessage("test 2 false after").that(view.getbool(pref2)).isFalse();
view.setbool(pref2, true);
assertWithMessage("test 2 true after").that(view.getbool(pref2)).isTrue();
assertWithMessage("test 2 false low level").that(Pref.getBooleanDefaultFalse(pref2)).isFalse();
assertWithMessage("test 2 false low level").that(Pref.getBooleanDefaultFalse(PrefsViewImpl.PrefHandle.parse(pref2).key)).isTrue();

String pref3 = "test-bool-pref3";

Pref.removeItem(pref3);
Pref.removeItem(PrefsViewImpl.PrefHandle.parse(pref3).key);
assertWithMessage("test 3 default before").that(view.getbool(pref3)).isFalse();
view.setbool(pref3, false);
assertWithMessage("test 3 false after").that(view.getbool(pref3)).isFalse();
view.setbool(pref3, true);
assertWithMessage("test 3 true after").that(view.getbool(pref3)).isTrue();

Pref.removeItem(pref1);
Pref.removeItem(pref2);
Pref.removeItem(pref3);
Pref.removeItem(PrefsViewImpl.PrefHandle.parse(pref1).key);
Pref.removeItem(PrefsViewImpl.PrefHandle.parse(pref2).key);
Pref.removeItem(PrefsViewImpl.PrefHandle.parse(pref3).key);
}


@Test
public void getTest() {
assertWithMessage("test default plain false").that(view.get("plain-example-no-default1")).isFalse();
assertWithMessage("test default specified false").that(view.get("plain-example-default2:false")).isFalse();
assertWithMessage("test default specified false 2").that(view.get("plain-example-default2:true")).isFalse(); // cached
assertWithMessage("test default specified true").that(view.get("plain-example-default3:true")).isTrue();
assertWithMessage("test default specified true 2").that(view.get("plain-example-default3:false")).isTrue(); // cached
assertWithMessage("test default specified invalid 1").that(view.get("plain-example-default4:")).isFalse();
assertWithMessage("test default specified invalid 2").that(view.get("plain-example-default5:blah")).isFalse();
assertWithMessage("test default specified invalid 3").that(view.get("plain-example-default6:true:")).isFalse();
}

@Test
public void putTest() {

assertWithMessage("test default plain false 1").that(view.get("plain-example-put-test-1")).isFalse();
view.put("plain-example-put-test-1", false);
assertWithMessage("test default plain false 2").that(view.get("plain-example-put-test-1")).isFalse();
view.put("plain-example-put-test-1", true);
assertWithMessage("test default plain false 3").that(view.get("plain-example-put-test-1")).isTrue();

String pref = "plain-example-put-test-2:true";
assertWithMessage("test default plain true 1").that(view.get(pref)).isTrue();
view.put(pref, false);
assertWithMessage("test default plain true 2").that(view.get(pref)).isFalse();
view.put(pref, true);
assertWithMessage("test default plain true 3").that(view.get(pref)).isTrue();
}

}

0 comments on commit af3a008

Please sign in to comment.