Skip to content

Commit

Permalink
feat(inapp) - check hasInAppFrequencyLimitsMaxedOut logic after remov…
Browse files Browse the repository at this point in the history
…ing in-app from queue and before display, remove old session based logic SDK-3385
  • Loading branch information
piyush-kukadiya committed Nov 6, 2023
1 parent b811271 commit 37abcfd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,22 @@

import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;

import com.clevertap.android.sdk.inapp.CTInAppNotification;
import com.clevertap.android.sdk.inapp.ImpressionManager;
import com.clevertap.android.sdk.inapp.SharedPreferencesMigration;
import com.clevertap.android.sdk.task.CTExecutorFactory;
import com.clevertap.android.sdk.task.Task;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Callable;

import kotlin.jvm.functions.Function1;
import kotlin.jvm.functions.Function2;
import org.json.JSONArray;
import org.json.JSONObject;

@RestrictTo(Scope.LIBRARY)
public class InAppFCManager {
Expand All @@ -37,23 +32,16 @@ public class InAppFCManager {

private String deviceId;

private final ArrayList<String> mDismissedThisSession = new ArrayList<>();

// private final HashMap<String, Integer> mShownThisSession = new HashMap<>();
//
// private int mShownThisSessionCount = 0;
//
private ImpressionManager impressionManager;


InAppFCManager(Context context, CleverTapInstanceConfig config, String deviceId/*, ImpressionManager impressionManager*/) {
this.config = config;
this.context = context;
this.deviceId = deviceId;
/*this.impressionManager = impressionManager;*/ // TODO

Task<Void> task = CTExecutorFactory.executors(config).postAsyncSafelyTask();
task.execute("initInAppFCManager",new Callable<Void>() {
task.execute("initInAppFCManager", new Callable<Void>() {
@Override
public Void call() {
init(InAppFCManager.this.deviceId);
Expand All @@ -62,7 +50,9 @@ public Void call() {
});
}

public boolean canShow(CTInAppNotification inapp) {

public boolean canShow(CTInAppNotification inapp,
Function2<JSONObject, String, Boolean> hasInAppFrequencyLimitsMaxedOut) {
try {
if (inapp == null) {
return false;
Expand All @@ -73,13 +63,20 @@ public boolean canShow(CTInAppNotification inapp) {
return true;
}


/* Evaluate frequency limits again (without Nth triggers)
in case the message was added multiple times before being displayed,
or queue was paused and the message was added multiple times in the meantime
*/
if (hasInAppFrequencyLimitsMaxedOut.invoke(inapp.getJsonDescription(), id)) {
return false;
}

// Exclude from all caps?
if (inapp.isExcludeFromCaps()) {
return true;
}

// TODO check new flag for exclude from caps

if (!hasSessionCapacityMaxedOut(inapp)
&& !hasLifetimeCapacityMaxedOut(inapp)
&& !hasDailyCapacityMaxedOut(inapp)) {
Expand All @@ -93,19 +90,13 @@ public boolean canShow(CTInAppNotification inapp) {

public void changeUser(String deviceId) {
// reset counters
// mShownThisSession.clear();
// mShownThisSessionCount = 0;
impressionManager.clearSessionData(); // TODO change deviceId in manager
mDismissedThisSession.clear();
this.deviceId = deviceId;
init(deviceId);
}

public void didDismiss(CTInAppNotification inapp) {
final Object id = inapp.getId();
if (id != null) {
mDismissedThisSession.add(id.toString());
}
// NO-OP
}

public void didShow(final Context context, CTInAppNotification inapp) {
Expand All @@ -115,14 +106,6 @@ public void didShow(final Context context, CTInAppNotification inapp) {
}

impressionManager.recordImpression(id);
// mShownThisSessionCount++;

// Integer count = mShownThisSession.get(id);
// if (count == null) {
// count = 1;
// }

// mShownThisSession.put(id, ++count);

incrementInAppCountsInPersistentStore(id);

Expand Down Expand Up @@ -229,7 +212,7 @@ private int[] getInAppCountsFromPersistentStore(String inappID) {
}
}

private String getInAppID(CTInAppNotification inapp) {
public String getInAppID(CTInAppNotification inapp) {
if (inapp.getId() == null) {
return null;
}
Expand Down Expand Up @@ -329,19 +312,12 @@ private boolean hasSessionCapacityMaxedOut(CTInAppNotification inapp) {
return false;
}

// TODO mDismissedThisSession should be removed
// 1. Has this been dismissed?
if (mDismissedThisSession.contains(id)) {
return true;
}

// 2. Has the session max count for this inapp been breached?
try {
final int maxPerSession = inapp.getMaxPerSession() >= 0 ? inapp.getMaxPerSession() : 1000;

//Integer c = mShownThisSession.get(id);
int c = impressionManager.perSession(id);
if (/*c != null && */c >= maxPerSession) {
if (c >= maxPerSession) {
return true;
}
} catch (Throwable t) {
Expand All @@ -352,7 +328,6 @@ private boolean hasSessionCapacityMaxedOut(CTInAppNotification inapp) {
final int c = getIntFromPrefs(getKeyWithDeviceId(Constants.INAPP_MAX_PER_SESSION_KEY, deviceId), 1);
int sessionTotal = impressionManager.perSessionTotal();
return (sessionTotal >= c);
// return (mShownThisSessionCount >= c);
}

private void incrementInAppCountsInPersistentStore(String inappID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
import com.clevertap.android.sdk.PushPermissionResponseListener;
import com.clevertap.android.sdk.StorageHelper;
import com.clevertap.android.sdk.Utils;
import com.clevertap.android.sdk.inapp.evaluation.EvaluationManager;
import com.clevertap.android.sdk.inapp.data.InAppResponseAdapter;
import com.clevertap.android.sdk.inapp.data.InAppServerSide;
import com.clevertap.android.sdk.inapp.evaluation.EvaluationManager;
import com.clevertap.android.sdk.inapp.evaluation.LimitAdapter;
import com.clevertap.android.sdk.task.CTExecutorFactory;
import com.clevertap.android.sdk.task.MainLooperHandler;
import com.clevertap.android.sdk.task.Task;
Expand All @@ -52,6 +54,7 @@
import java.util.concurrent.Callable;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function2;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -523,7 +526,13 @@ public void run() {
}

if (controllerManager.getInAppFCManager() != null) {
if (!controllerManager.getInAppFCManager().canShow(inAppNotification)) {

final Function2<JSONObject, String, Boolean> hasInAppFrequencyLimitsMaxedOut = (inAppJSON, inAppId) -> {
final List<LimitAdapter> listOfWhenLimits = InAppResponseAdapter.getListOfWhenLimits(inAppJSON);
return evaluationManager.matchWhenLimitsBeforeDisplay(listOfWhenLimits, inAppId);
};

if (!controllerManager.getInAppFCManager().canShow(inAppNotification, hasInAppFrequencyLimitsMaxedOut)) {
logger.verbose(config.getAccountId(),
"InApp has been rejected by FC, not showing " + inAppNotification.getCampaignId());
showInAppNotificationIfAny();
Expand Down

0 comments on commit 37abcfd

Please sign in to comment.