Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to re-prompt at intervals after user accept/ignore #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ protected void onCreate(Bundle savedInstanceState) {
* setStoreType(Intent...) (Any custom intent/intents, Intent... - an intent or array of intents) */
.setTimeToWait(Time.DAY, (short) 0) // default is 10 days, 0 means install millisecond, 10 means app is launched 10 or more time units later than installation
.setLaunchTimes((byte) 3) // default is 10, 3 means app is launched 3 or more times
.setTimeToReprompt(Time.MONTH, (short) 3) // default is off, 3 means prompt is shown again 3 months after last user accept/ignore action. Setting this value enables the check.
.setRemindTimeToWait(Time.DAY, (short) 2) // default is 1 day, 1 means app is launched 1 or more time units after neutral button clicked
.setRemindLaunchesNumber((byte) 1) // default is 0, 1 means app is launched 1 or more times after neutral button clicked
.setSelectedAppLaunches((byte) 1) // default is 1, 1 means each launch, 2 means every 2nd launch, 3 means every 3rd launch, etc
Expand Down Expand Up @@ -165,6 +166,7 @@ Default options of the Rate Dialog are as below:
10. Don't re-enable the Rate Dialog if a new version of app with different version name is installed. Change via `AppRate#setVersionNameCheck(boolean)`.
11. Setting `AppRate#setDebug(boolean)` to `true` ensures that the Rate Dialog will be shown each time the app is launched. **This feature is for development only!**.
12. There is no default callback when the button of Rate Dialog is pressed. Change via `AppRate.with(this).setOnClickButtonListener(OnClickButtonListener)`.
13. Prompt is not shown again after user action (accept/ignore).

### OnClickButtonListener interface

Expand Down
34 changes: 32 additions & 2 deletions library/src/main/java/com/vorlonsoft/android/rate/AppRate.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.ArrayMap;
import android.util.Log;
import android.view.View;
Expand All @@ -36,6 +35,7 @@
import static com.vorlonsoft.android.rate.PreferenceHelper.getCustomEventCount;
import static com.vorlonsoft.android.rate.PreferenceHelper.getInstallDate;
import static com.vorlonsoft.android.rate.PreferenceHelper.getIsAgreeShowDialog;
import static com.vorlonsoft.android.rate.PreferenceHelper.getLastAgreeShowFalseDate;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong lexicographical order for 'com.vorlonsoft.android.rate.PreferenceHelper.getLastAgreeShowFalseDate' import. Should be before 'java.util.Map'.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matching existing imports in alphabetical order

import static com.vorlonsoft.android.rate.PreferenceHelper.getLaunchTimes;
import static com.vorlonsoft.android.rate.PreferenceHelper.getRemindInterval;
import static com.vorlonsoft.android.rate.PreferenceHelper.getRemindLaunchesNumber;
Expand Down Expand Up @@ -76,6 +76,8 @@ public final class AppRate {
private boolean isVersionCodeCheck = false;
private boolean isVersionNameCheck = false;
private long installDate = Time.DAY * 10L;
private long repromptDate = Time.MONTH * 6L;
private boolean repromptCheck = false;
private byte appLaunchTimes = (byte) 10;
private long remindInterval = Time.DAY;
private byte remindLaunchesNumber = (byte) 0;
Expand Down Expand Up @@ -215,6 +217,24 @@ public AppRate setInstallDays(byte installDate) {
return setTimeToWait(Time.DAY, installDate);
}

/**
* <p>Sets the minimal number of time units until the Rate Dialog appears after it has already
* been actioned.</p>
* <p>Default is off (no check), calling this method enables the check, 0 means user
* accept/ignore millisecond, 10 means prompt is re-shown 10 or more time units later than the
* last accept/ignore.</p>
* @param timeUnit one of the values defined by {@link Time.TimeUnits}
* @param timeUnitsNumber time units number
* @return the {@link AppRate} singleton object
* @see Time.TimeUnits
*/
@SuppressWarnings("unused")
public AppRate setTimeToReprompt(@Time.TimeUnits long timeUnit, short timeUnitsNumber) {
this.repromptDate = timeUnit * timeUnitsNumber;
this.repromptCheck = true;
return this;
}

/**
* <p>Sets the minimal number of time units until the Rate Dialog pops up for the first time.</p>
* <p>Default is 10 {@link Time#DAY days}, 0 means install millisecond, 10 means app is launched
Expand Down Expand Up @@ -952,7 +972,9 @@ public void rateNow(Activity activity) {
*/
@SuppressWarnings("WeakerAccess")
public boolean shouldShowRateDialog() {
return getAgreeShowDialog() &&
// If Agree show is false (user has ignored/accepted) then return false unless it has
// been `repromptDate` time since the time when the user accepted/ignored the prompt
return (getAgreeShowDialog() || isOverLastAgreeShowFalseDate()) &&
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'&&' should be on a new line.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matching existing style as much as possible while combining the tightly related ||

isOverLaunchTimes() &&
isSelectedAppLaunch() &&
isOverInstallDate() &&
Expand All @@ -962,6 +984,14 @@ public boolean shouldShowRateDialog() {
isBelow365DayPeriodMaxNumberDialogLaunchTimes();
}

private boolean isOverLastAgreeShowFalseDate() {
if (!repromptCheck) {
return false;
}

return ((repromptDate == 0L)) || isOverDate(getLastAgreeShowFalseDate(context), repromptDate);
sampengilly marked this conversation as resolved.
Show resolved Hide resolved
sampengilly marked this conversation as resolved.
Show resolved Hide resolved
}

private boolean isOverLaunchTimes() {
return ((appLaunchTimes == 0) || (getLaunchTimes(context) >= appLaunchTimes));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ final class PreferenceHelper {

private static final String PREF_KEY_IS_AGREE_SHOW_DIALOG = "androidrate_is_agree_show_dialog";

private static final String PREF_KEY_LAST_AGREE_SHOW_FALSE_DATE = "androidrate_last_agree_show_false_date";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is longer than 100 characters (found 111).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the only one among these constants


private static final String PREF_KEY_LAUNCH_TIMES = "androidrate_launch_times";

private static final String PREF_KEY_REMIND_INTERVAL = "androidrate_remind_interval";
Expand Down Expand Up @@ -216,12 +218,22 @@ static void setIsAgreeShowDialog(final Context context, final boolean isAgree) {
getPreferencesEditor(context)
.putBoolean(PREF_KEY_IS_AGREE_SHOW_DIALOG, isAgree)
.apply();

if (!isAgree) {
getPreferencesEditor(context)
.putLong(PREF_KEY_LAST_AGREE_SHOW_FALSE_DATE, new Date().getTime())
.apply();
}
}

static boolean getIsAgreeShowDialog(final Context context) {
return getPreferences(context).getBoolean(PREF_KEY_IS_AGREE_SHOW_DIALOG, true);
}

static long getLastAgreeShowFalseDate(final Context context) {
return getPreferences(context).getLong(PREF_KEY_LAST_AGREE_SHOW_FALSE_DATE, 0L);
}

/**
* <p>Sets number of times the app has been launched.</p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ protected void onCreate(Bundle savedInstanceState) {
* setStoreType(Intent...) (Any custom intent/intents, Intent... - an intent or array of intents) */
.setTimeToWait(Time.DAY, (short) 3) // default is 10 days, 0 means install millisecond, 10 means app is launched 10 or more time units later than installation
.setLaunchTimes((byte) 10) // default is 10, 3 means app is launched 3 or more times
.setTimeToReprompt(Time.MONTH, (short) 3) // default is off, 3 means prompt is shown again 3 months after last user accept/ignore action. Setting this value enables the check.
.setRemindTimeToWait(Time.DAY, (short) 2) // default is 1 day, 1 means app is launched 1 or more time units after neutral button clicked
.setRemindLaunchesNumber((byte) 1) // default is 0, 1 means app is launched 1 or more times after neutral button clicked
.setSelectedAppLaunches((byte) 4) // default is 1, 1 means each launch, 2 means every 2nd launch, 3 means every 3rd launch, etc
Expand Down