Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 32 additions & 1 deletion library/src/main/java/hotchemi/android/rate/AppRate.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.view.View;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static hotchemi.android.rate.DialogManager.create;
import static hotchemi.android.rate.PreferenceHelper.getInstallDate;
Expand All @@ -13,6 +15,8 @@
import static hotchemi.android.rate.PreferenceHelper.getRemindInterval;
import static hotchemi.android.rate.PreferenceHelper.isFirstLaunch;
import static hotchemi.android.rate.PreferenceHelper.setInstallDate;
import static hotchemi.android.rate.PreferenceHelper.getCustomEventCount;
import static hotchemi.android.rate.PreferenceHelper.setCustomEventCount;

public final class AppRate {

Expand All @@ -28,6 +32,8 @@ public final class AppRate {

private int remindInterval = 1;

private final HashMap<String, Long> customEventCounts = new HashMap<>();

private boolean isDebug = false;

private AppRate(Context context) {
Expand Down Expand Up @@ -72,6 +78,11 @@ public AppRate setRemindInterval(int remindInterval) {
return this;
}

public AppRate setMinimumEventCount(String eventName, long minimumCount) {
this.customEventCounts.put(eventName, minimumCount);
return this;
}

public AppRate setShowLaterButton(boolean isShowNeutralButton) {
options.setShowNeutralButton(isShowNeutralButton);
return this;
Expand Down Expand Up @@ -173,6 +184,15 @@ public AppRate setStoreType(StoreType appstore) {
return this;
}

public AppRate incrementEventCount(String eventName) {
return setEventCountValue(eventName, getCustomEventCount(context,eventName) + 1);
}

public AppRate setEventCountValue(String eventName, long countValue) {
setCustomEventCount(context, eventName, countValue);
return this;
}

public void monitor() {
if (isFirstLaunch(context)) {
setInstallDate(context);
Expand All @@ -190,7 +210,8 @@ public boolean shouldShowRateDialog() {
return getIsAgreeShowDialog(context) &&
isOverLaunchTimes() &&
isOverInstallDate() &&
isOverRemindDate();
isOverRemindDate() &&
isOverCustomEventRequirements();
}

private boolean isOverLaunchTimes() {
Expand All @@ -205,6 +226,16 @@ private boolean isOverRemindDate() {
return isOverDate(getRemindInterval(context), remindInterval);
}

private boolean isOverCustomEventRequirements() {
for(Map.Entry<String, Long> eventRequirement : customEventCounts.entrySet()) {
Long currentCount = getCustomEventCount(context, eventRequirement.getKey());
if(currentCount < eventRequirement.getValue()) {
return false;
}
}
return true;
}

public boolean isDebug() {
return isDebug;
}
Expand Down
18 changes: 18 additions & 0 deletions library/src/main/java/hotchemi/android/rate/PreferenceHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ final class PreferenceHelper {

private static final String PREF_KEY_REMIND_INTERVAL = "android_rate_remind_interval";

private static final String PREF_CUSTOM_EVENT_KEY_PREFIX = "android_rate_custom_event_prefix_";

private PreferenceHelper() {
}

Expand Down Expand Up @@ -93,4 +95,20 @@ static boolean isFirstLaunch(Context context) {
return getPreferences(context).getLong(PREF_KEY_INSTALL_DATE, 0) == 0L;
}

/**
* Add a prefix for the key for each custom event,
* so that there is no clash with existing keys (PREF_KEY_LAUNCH_TIME, PREF_KEY_INSTALL_DATE, etc.)
*/
static long getCustomEventCount(Context context, String eventName) {
String eventKey = PREF_CUSTOM_EVENT_KEY_PREFIX + eventName;
return getPreferences(context).getLong(eventKey, 0);
}

static void setCustomEventCount(Context context, String eventName, long eventCount) {
String eventKey = PREF_CUSTOM_EVENT_KEY_PREFIX + eventName;
SharedPreferences.Editor editor = getPreferencesEditor(context);
editor.putLong(eventKey, eventCount);
editor.apply();
}

}