Skip to content

Commit

Permalink
Changed the OnboardingTracker to follow a builder pattern to allow th…
Browse files Browse the repository at this point in the history
…e setting of the first and last time it will be set to show based on the number of times the tracker is built
  • Loading branch information
Ryan John committed Jan 11, 2015
1 parent 3a9ab3e commit a3fc023
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ private void addPurpleToolTipView() {
}

private void addOrangeToolTipView() {
OnboardingTracker tracker = new OnboardingTracker(this, getString(R.string.tracker_app_launches));
OnboardingTracker tracker = new OnboardingTracker(this, getString(R.string.tracker_first_button))
.withFirstShow(1)
.withLastShow(3)
.build();

ToolTip toolTip = new ToolTip()
.withText("Tap me!")
.withColor(getResources().getColor(R.color.holo_orange));
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
<string name="blue">Blue</string>
<string name="purple">Purple</string>
<string name="orange">Orange</string>
<string name="tracker_app_launches">APP_LAUNCHES</string>

<string name="tracker_first_button">FIRST_BUTTON</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,60 @@ public class OnboardingTracker {

private final static String DEFAULT_PREFS = "DEFAULT_PREFS";
private final static String DISMISSED = "_DISMISSED";
private final static int MAX_VIEWS = 3;

private boolean show = false;
private Context context;
private String id;
private int lastShow = 1;
private int firstShow = 0;

/**
* Creates a new OnboardingTracker with an id used to save the history in shared preferences.
*/
public OnboardingTracker (Context context, String id) {
this.context = context;
this.id = id;
init();
}

private void init() {
/**
* Set the number of builds of this Tracker before the Tracker will set to show (zero-indexed).
*
* @return this OnboardingTracker to build upon.
*/
public OnboardingTracker withFirstShow(int firstShow) {
this.firstShow = firstShow;
return this;
}

/**
* Set the number of builds of this Tracker before the Tracker will stop showing (zero-indexed).
*
* @return this OnboardingTracker to build upon.
*/
public OnboardingTracker withLastShow(int lastShow) {
this.lastShow = lastShow;
return this;
}

/**
* The Tracker will calculate whether to show when built based on its history and the first and
* last show settings.
*
* @return this OnboardingTracker to build upon.
*/
public OnboardingTracker build() {
int currentCount = getTrackerCounter();
boolean dismissed = getDismissedPref();
show = shouldViewShow(currentCount, dismissed);
incrementTrackerCounter(currentCount);
return this;
}

private boolean shouldViewShow(int currentCount, boolean dismissed) {
if (dismissed) {
return false;
} else {
return !(currentCount >= MAX_VIEWS);
return ((firstShow <= currentCount) && (currentCount <= lastShow));
}
}

Expand Down

0 comments on commit a3fc023

Please sign in to comment.