diff --git a/library/src/main/java/hotchemi/android/rate/AppRate.java b/library/src/main/java/hotchemi/android/rate/AppRate.java index b9a4907..2c6c6a5 100644 --- a/library/src/main/java/hotchemi/android/rate/AppRate.java +++ b/library/src/main/java/hotchemi/android/rate/AppRate.java @@ -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; @@ -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 { @@ -28,6 +32,8 @@ public final class AppRate { private int remindInterval = 1; + private final HashMap customEventCounts = new HashMap<>(); + private boolean isDebug = false; private AppRate(Context context) { @@ -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; @@ -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); @@ -190,7 +210,8 @@ public boolean shouldShowRateDialog() { return getIsAgreeShowDialog(context) && isOverLaunchTimes() && isOverInstallDate() && - isOverRemindDate(); + isOverRemindDate() && + isOverCustomEventRequirements(); } private boolean isOverLaunchTimes() { @@ -205,6 +226,16 @@ private boolean isOverRemindDate() { return isOverDate(getRemindInterval(context), remindInterval); } + private boolean isOverCustomEventRequirements() { + for(Map.Entry eventRequirement : customEventCounts.entrySet()) { + Long currentCount = getCustomEventCount(context, eventRequirement.getKey()); + if(currentCount < eventRequirement.getValue()) { + return false; + } + } + return true; + } + public boolean isDebug() { return isDebug; } diff --git a/library/src/main/java/hotchemi/android/rate/PreferenceHelper.java b/library/src/main/java/hotchemi/android/rate/PreferenceHelper.java index 6856a87..8e17a0f 100644 --- a/library/src/main/java/hotchemi/android/rate/PreferenceHelper.java +++ b/library/src/main/java/hotchemi/android/rate/PreferenceHelper.java @@ -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() { } @@ -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(); + } + } \ No newline at end of file