Skip to content

Commit

Permalink
1.0.1: Using PrefsKey with tags for amplitude syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
Inderjeet Singh committed Jul 12, 2018
1 parent 3c51536 commit c8d7116
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 54 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Extension to automatically sync [prefs](https://github.com/PeelTechnologies/andr
maven { url "https://jitpack.io" }
}
```
* In your app build.gradle, add: ```implementation "com.github.PeelTechnologies:android-typesafe-prefs-amplitude:1.0.0"```
* In your app build.gradle, add: ```implementation "com.github.PeelTechnologies:android-typesafe-prefs-amplitude:1.0.1"```
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.github.PeelTechnologies</groupId>
<artifactId>android-typesafe-prefs-amplitude</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand All @@ -38,7 +38,7 @@
<dependency>
<groupId>com.github.PeelTechnologies</groupId>
<artifactId>android-typesafe-prefs</artifactId>
<version>[1.0.9,)</version>
<version>[1.1.0,)</version>
</dependency>
<dependency>
<groupId>com.amplitude</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,22 @@
*
* @author Inderjeet Singh
*/
public final class PrefsAmplitudeSyncListener implements Prefs.EventListener {
public class PrefsAmplitudeSyncListener implements Prefs.EventListener {
private final AmplitudeClient amplitudeClient;
private final String amplitudeTag;

public PrefsAmplitudeSyncListener() {
this(Amplitude.getInstance());
this(Amplitude.getInstance(), "amplitude");
}

public PrefsAmplitudeSyncListener(AmplitudeClient client) {
public PrefsAmplitudeSyncListener(AmplitudeClient client, String amplitudeTag) {
this.amplitudeClient = client;
this.amplitudeTag = amplitudeTag;
}

@Override
public <T> void onPut(PrefsKey<T> key, T value) {
if (key instanceof PrefsKeyAmplitudeSynced) {
if (key.containsTag(amplitudeTag)) {
try {
JSONObject props = new JSONObject();
props.put(key.getName(), value);
Expand All @@ -54,7 +56,7 @@ public <T> void onPut(PrefsKey<T> key, T value) {
@Override
public <T> void onRemove(PrefsKey<T> key) {
try {
if (key instanceof PrefsKeyAmplitudeSynced && key.getTypeOfValue() == Boolean.class) {
if (key.containsTag(amplitudeTag) && key.getTypeOfValue() == Boolean.class) {
// Only for boolean keys, set them to false in Amplitude
JSONObject props = new JSONObject();
props.put(key.getName(), false);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
@PrepareForTest({Context.class, SharedPreferences.class, PreferenceManager.class, Amplitude.class})
public class PrefsAmplitudeSyncFunctionalTest {

private static final String AMPLITUDE_SYNCED = "amplitudeSynced";
private Prefs prefs;
private AmplitudeClient amplitudeClient;
private JSONObject userProperties;
Expand All @@ -64,12 +65,12 @@ public Void answer(InvocationOnMock invocation) {
return null;
}
}).when(amplitudeClient).setUserProperties(Mockito.any(JSONObject.class));
prefs.addListener(new PrefsAmplitudeSyncListener(amplitudeClient));
prefs.addListener(new PrefsAmplitudeSyncListener(amplitudeClient, AMPLITUDE_SYNCED));
}

@Test
public void testAmplitudeSync() throws Exception {
PrefsKeyAmplitudeSynced<String> sync = new PrefsKeyAmplitudeSynced<>("sync", String.class);
PrefsKey<String> sync = new PrefsKey<>("sync", String.class, AMPLITUDE_SYNCED);
prefs.put(sync, "test");
assertEquals("test", userProperties.get("sync"));

Expand All @@ -81,7 +82,7 @@ public void testAmplitudeSync() throws Exception {

@Test
public void testAmplitudeUnsetOnBooleanPropertyRemoval() throws Exception {
PrefsKeyAmplitudeSynced<Boolean> bool = new PrefsKeyAmplitudeSynced<>("bool", Boolean.class);
PrefsKey<Boolean> bool = new PrefsKey<>("bool", Boolean.class, AMPLITUDE_SYNCED);
prefs.put(bool, true);
assertTrue(userProperties.getBoolean("bool"));

Expand Down

0 comments on commit c8d7116

Please sign in to comment.