Skip to content

Commit

Permalink
Add custom scale support
Browse files Browse the repository at this point in the history
Add support for custom scale

Changes
- Refactor Score to store array of score rules for each type
- Update Settings to keep the surveyTypeScale
- Update tests
  • Loading branch information
diegoserranoa committed Jul 17, 2019
1 parent ba584f2 commit b63a94a
Show file tree
Hide file tree
Showing 19 changed files with 370 additions and 220 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.12.0 (2019-07-17)

- Add support for custom scale

## 2.11.0 (2019-02-04)

- Add new method showSurveyInActivity
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ If you use Maven, you can include this library as a dependency:
<dependency>
<groupId>com.wootric</groupId>
<artifactId>wootric-sdk-android</artifactId>
<version>2.11.0</version>
<version>2.12.0</version>
</dependency>
```

### Using Gradle

```xml
compile 'com.wootric:wootric-sdk-android:2.11.0'
compile 'com.wootric:wootric-sdk-android:2.12.0'
```

## Initializing Wootric
Expand Down
6 changes: 3 additions & 3 deletions androidsdk/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION_NAME=2.11.0
VERSION_CODE=2110
VERSION_NAME=2.12.0
VERSION_CODE=2120
GROUP=com.wootric

POM_DESCRIPTION=WootricSDK Android
Expand All @@ -15,4 +15,4 @@ POM_DEVELOPER_NAME=Diego Serrano

POM_NAME=WootricSDK Android
POM_ARTIFACT_ID=wootric-sdk-android
POM_PACKAGING=aar
POM_PACKAGING=aar
7 changes: 7 additions & 0 deletions androidsdk/src/main/java/com/wootric/androidsdk/Wootric.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,13 @@ public void shouldSkipFollowupScreenForPromoters(boolean shouldSkipFollowupScree
settings.setSkipFollowupScreenForPromoters(shouldSkipFollowupScreenForPromoters);
}

/**
*
*/
public void setSurveyTypeScale(int surveyTypeScale) {
settings.setCustomSurveyTypeScale(surveyTypeScale);
}

/**
* Starts the survey if configuration is correctly set and elibility returns true.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private String requestParams() {
}
builder.appendQueryParameter("os_name", "Android");
builder.appendQueryParameter("os_version", Build.VERSION.RELEASE);
builder.appendQueryParameter("sdk_version", BuildConfig.VERSION_NAME);
builder.appendQueryParameter("sdk_version", "android-" + BuildConfig.VERSION_NAME);

return builder.build().getEncodedQuery();
}
Expand Down
91 changes: 56 additions & 35 deletions androidsdk/src/main/java/com/wootric/androidsdk/objects/Score.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

package com.wootric.androidsdk.objects;

import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;

/**
Expand All @@ -30,78 +33,96 @@
public final class Score {
private final int score;
private final String surveyType;
private final int surveyTypeScale;

private static final HashMap<String, HashMap<String, Integer>> scoreRules = new HashMap<String, HashMap<String, Integer>>(){
private static final HashMap<String, ArrayList<HashMap<String, Integer>>> scoreRules = new HashMap<String, ArrayList<HashMap<String, Integer>>>(){
{
put("NPS", new HashMap<String, Integer>() {{
put("min", 0);
put("max", 10);
put("negative_type_max", 6);
put("neutral_type_max", 8);
put("NPS", new ArrayList<HashMap<String, Integer>>() {{
add(new HashMap<String, Integer>() {{
put("min", 0);
put("max", 10);
put("negative_type_max", 6);
put("neutral_type_max", 8);
}});
}});
put("CES", new HashMap<String, Integer>() {{
put("min", 1);
put("max", 7);
put("negative_type_max", 3);
put("neutral_type_max", 5);
put("CES", new ArrayList<HashMap<String, Integer>>() {{
add(new HashMap<String, Integer>() {{
put("min", 1);
put("max", 7);
put("negative_type_max", 3);
put("neutral_type_max", 5);
}});
}});
put("CSAT", new HashMap<String, Integer>() {{
put("min", 1);
put("max", 5);
put("negative_type_max", 2);
put("neutral_type_max", 3);
put("CSAT", new ArrayList<HashMap<String, Integer>>() {{
add(new HashMap<String, Integer>() {{
put("min", 1);
put("max", 5);
put("negative_type_max", 2);
put("neutral_type_max", 3);
}});
add(new HashMap<String, Integer>() {{
put("min", 1);
put("max", 10);
put("negative_type_max", 6);
put("neutral_type_max", 8);
}});
}});
}
};

public Score(int score, String surveyType) {
public Score(int score, String surveyType, int surveyTypeScale) {
this.score = score;
this.surveyType = surveyType;
if (scoreRules.containsKey(surveyType) && surveyTypeScale >= 0 && surveyTypeScale < scoreRules.get(surveyType).size()){
this.surveyTypeScale = surveyTypeScale;
} else {
this.surveyTypeScale = 0;
}
}

public boolean isDetractor() {
if (surveyType != null & scoreRules.containsKey(surveyType)){
return score >= scoreRules.get(surveyType).get("min") &&
score <= scoreRules.get(surveyType).get("negative_type_max");
return score >= scoreRules.get(surveyType).get(surveyTypeScale).get("min") &&
score <= scoreRules.get(surveyType).get(surveyTypeScale).get("negative_type_max");
} else {
return score >= scoreRules.get("NPS").get("min") &&
score <= scoreRules.get("NPS").get("negative_type_max");
return score >= scoreRules.get("NPS").get(surveyTypeScale).get("min") &&
score <= scoreRules.get("NPS").get(surveyTypeScale).get("negative_type_max");
}
}

public boolean isPassive() {
if (surveyType != null & scoreRules.containsKey(surveyType)){
return score > scoreRules.get(surveyType).get("negative_type_max") &&
score <= scoreRules.get(surveyType).get("neutral_type_max");
return score > scoreRules.get(surveyType).get(surveyTypeScale).get("negative_type_max") &&
score <= scoreRules.get(surveyType).get(surveyTypeScale).get("neutral_type_max");
} else {
return score > scoreRules.get("NPS").get("negative_type_max") &&
score <= scoreRules.get("NPS").get("neutral_type_max");
return score > scoreRules.get("NPS").get(surveyTypeScale).get("negative_type_max") &&
score <= scoreRules.get("NPS").get(surveyTypeScale).get("neutral_type_max");
}
}

public boolean isPromoter() {
if (surveyType != null & scoreRules.containsKey(surveyType)){
return score > scoreRules.get(surveyType).get("neutral_type_max") &&
score <= scoreRules.get(surveyType).get("max") ;
return score > scoreRules.get(surveyType).get(surveyTypeScale).get("neutral_type_max") &&
score <= scoreRules.get(surveyType).get(surveyTypeScale).get("max") ;
} else {
return score > scoreRules.get("NPS").get("neutral_type_max") &&
score <= scoreRules.get("NPS").get("max") ;
return score > scoreRules.get("NPS").get(surveyTypeScale).get("neutral_type_max") &&
score <= scoreRules.get("NPS").get(surveyTypeScale).get("max") ;
}
}

public static int maximumScore(String surveyType) {
public int maximumScore() {
if (surveyType != null & scoreRules.containsKey(surveyType)){
return scoreRules.get(surveyType).get("max");
return scoreRules.get(surveyType).get(surveyTypeScale).get("max");
} else {
return scoreRules.get("NPS").get("max");
return scoreRules.get("NPS").get(surveyTypeScale).get("max");
}
}

public static int minimumScore(String surveyType) {
public int minimumScore() {
if (surveyType != null & scoreRules.containsKey(surveyType)){
return scoreRules.get(surveyType).get("min");
return scoreRules.get(surveyType).get(surveyTypeScale).get("min");
} else {
return scoreRules.get("NPS").get("min");
return scoreRules.get("NPS").get(surveyTypeScale).get("min");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class Settings implements Parcelable {
private int scoreColor = Constants.NOT_SET;
private int thankYouButtonBackgroundColor = Constants.NOT_SET;
private int socialSharingColor = Constants.NOT_SET;
private int surveyTypeScale = 0;

// Sets values from the argument settings only if they are not provided yet
public void mergeWithSurveyServerSettings(Settings settings) {
Expand All @@ -92,7 +93,6 @@ public void mergeWithSurveyServerSettings(Settings settings) {
}

public boolean firstSurveyDelayPassed(long timeFrom) {

Boolean firstCriteria = timeFrom == Constants.NOT_SET;
Boolean secondCriteria;
if (getFirstSurveyDelay() != null) {
Expand Down Expand Up @@ -175,11 +175,11 @@ public String getFollowupQuestion(int score) {
String followupQuestion = null;

if(localCustomMessage != null) {
followupQuestion = localCustomMessage.getFollowupQuestionForScore(score, surveyType);
followupQuestion = localCustomMessage.getFollowupQuestionForScore(score, surveyType, surveyTypeScale);
}

if(followupQuestion == null && adminPanelCustomMessage != null) {
followupQuestion = adminPanelCustomMessage.getFollowupQuestionForScore(score, surveyType);
followupQuestion = adminPanelCustomMessage.getFollowupQuestionForScore(score, surveyType, surveyTypeScale);
}

if(followupQuestion == null) {
Expand All @@ -193,11 +193,11 @@ public String getFollowupPlaceholder(int score) {
String followupPlaceholder = null;

if(localCustomMessage != null) {
followupPlaceholder = localCustomMessage.getPlaceholderForScore(score, surveyType);
followupPlaceholder = localCustomMessage.getPlaceholderForScore(score, surveyType, surveyTypeScale);
}

if(followupPlaceholder == null && adminPanelCustomMessage != null) {
followupPlaceholder = adminPanelCustomMessage.getPlaceholderForScore(score, surveyType);
followupPlaceholder = adminPanelCustomMessage.getPlaceholderForScore(score, surveyType, surveyTypeScale);
}

if(followupPlaceholder == null) {
Expand All @@ -211,7 +211,7 @@ public String getCustomThankYouMessage(int score) {
String thankYouMessage = null;

if(customThankYou != null) {
thankYouMessage = customThankYou.getTextForScore(score, surveyType);
thankYouMessage = customThankYou.getTextForScore(score, surveyType, surveyTypeScale);
}

return thankYouMessage;
Expand All @@ -223,18 +223,18 @@ public String getThankYouMessage() {

public String getThankYouLinkText(int score) {
return (customThankYou == null) ?
null : customThankYou.getLinkTextForScore(score, surveyType);
null : customThankYou.getLinkTextForScore(score, surveyType, surveyTypeScale);
}

public Uri getThankYouLinkUri(int score, String comment) {
return (customThankYou == null) ?
null : customThankYou.getLinkUri(score, comment, surveyType);
null : customThankYou.getLinkUri(score, comment, surveyType, surveyTypeScale);
}

public boolean isThankYouActionConfigured(int score, String comment) {
return customThankYou != null &&
customThankYou.getLinkTextForScore(score, surveyType) != null &&
customThankYou.getLinkUri(score, comment, surveyType) != null;
customThankYou.getLinkTextForScore(score, surveyType, surveyTypeScale) != null &&
customThankYou.getLinkUri(score, comment, surveyType, surveyTypeScale) != null;
}

public String getSocialShareQuestion() {
Expand Down Expand Up @@ -379,6 +379,8 @@ public void setTwitterPage(String twitterPage) {

public String getSurveyType() { return surveyType; }

public int getSurveyTypeScale() { return surveyTypeScale; }

public void setSurveyType(String surveyType) {
this.surveyType = surveyType;
}
Expand All @@ -387,6 +389,11 @@ public void setCustomThankYou(WootricCustomThankYou customThankYou) {
this.customThankYou = customThankYou;
}

public void setCustomSurveyTypeScale(int customSurveyTypeScale) {
this.surveyTypeScale = customSurveyTypeScale < 0 ? 0 : customSurveyTypeScale;

}

public int getSurveyColor() {
if (surveyColor != Constants.NOT_SET){
return surveyColor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public void setPromoterPlaceholderText(String promoterPlaceholderText) {
this.placeholderTextsList.put(PROMOTER_TEXT_KEY, promoterPlaceholderText);
}

public String getFollowupQuestionForScore(int scoreValue, String surveyType) {
final Score score = new Score(scoreValue, surveyType);
public String getFollowupQuestionForScore(int scoreValue, String surveyType, int surveyTypeScale) {
final Score score = new Score(scoreValue, surveyType, surveyTypeScale);

if(score.isDetractor() && getDetractorQuestion() != null) {
return getDetractorQuestion();
Expand All @@ -131,8 +131,8 @@ public String getFollowupQuestionForScore(int scoreValue, String surveyType) {
}
}

public String getPlaceholderForScore(int scoreValue, String surveyType) {
final Score score = new Score(scoreValue, surveyType);
public String getPlaceholderForScore(int scoreValue, String surveyType, int surveyTypeScale) {
final Score score = new Score(scoreValue, surveyType, surveyTypeScale);

if(score.isDetractor() && getDetractorPlaceholder() != null) {
return getDetractorPlaceholder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public class WootricCustomThankYou implements Parcelable {
private boolean scoreInUrl;
private boolean commentInUrl;

public String getTextForScore(int scoreValue, String surveyType) {
Score score = new Score(scoreValue, surveyType);
public String getTextForScore(int scoreValue, String surveyType, int surveyTypeScale) {
Score score = new Score(scoreValue, surveyType, surveyTypeScale);

if(score.isDetractor() && detractorText != null) {
return detractorText;
Expand All @@ -63,8 +63,8 @@ public String getTextForScore(int scoreValue, String surveyType) {
}
}

public String getLinkTextForScore(int scoreValue, String surveyType) {
Score score = new Score(scoreValue, surveyType);
public String getLinkTextForScore(int scoreValue, String surveyType, int surveyTypeScale) {
Score score = new Score(scoreValue, surveyType, surveyTypeScale);

if(score.isDetractor() && detractorLinkText != null) {
return detractorLinkText;
Expand All @@ -77,8 +77,8 @@ public String getLinkTextForScore(int scoreValue, String surveyType) {
}
}

public Uri getLinkUri(int score, String comment, String surveyType) {
Uri uri = getLinkUriForScore(score, surveyType);
public Uri getLinkUri(int score, String comment, String surveyType, int surveyTypeScale) {
Uri uri = getLinkUriForScore(score, surveyType, surveyTypeScale);

if(uri != null) {
if(scoreInUrl) {
Expand All @@ -97,8 +97,8 @@ public Uri getLinkUri(int score, String comment, String surveyType) {
return uri;
}

private Uri getLinkUriForScore(int scoreValue, String surveyType) {
Score score = new Score(scoreValue, surveyType);
private Uri getLinkUriForScore(int scoreValue, String surveyType, int surveyTypeScale) {
Score score = new Score(scoreValue, surveyType, surveyTypeScale);

if(score.isDetractor() && detractorLinkUri != null) {
return detractorLinkUri;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class RatingBar extends View implements View.OnTouchListener {
private Context mContext;

private String mSurveyType;
private int mSurveyTypeScale;
private int mScaleMinimum;
private int mScaleMaximum;

Expand Down Expand Up @@ -99,9 +100,10 @@ private void init(Context context) {

private void initResources() {
Resources res = mContext.getResources();
Score score = new Score(-1, mSurveyType, mSurveyTypeScale);

mScaleMinimum = mSurveyType == null ? 0 : Score.minimumScore(mSurveyType);
mScaleMaximum = mSurveyType == null ? 10 : Score.maximumScore(mSurveyType);
mScaleMinimum = mSurveyType == null ? 0 : score.minimumScore();
mScaleMaximum = mSurveyType == null ? 10 : score.maximumScore();

mNotchTop = mScaleMaximum + 1;
mNotchCount = mNotchTop - mScaleMinimum;
Expand Down Expand Up @@ -232,8 +234,9 @@ private int getTouchedScore(float touchedXCoordinate) {
return touchedScore;
}

public void setScale(String surveyType) {
public void setScale(String surveyType, int surveyTypeScale) {
mSurveyType = surveyType;
mSurveyTypeScale = surveyTypeScale;
initResources();
}

Expand Down
Loading

0 comments on commit b63a94a

Please sign in to comment.