Skip to content

Commit c3f3c1b

Browse files
committed
Make IAM able to re display multiple times
* Changes on OSInAppMessage class, add two new params delay and limit * Make OSInAppMessageController handle the multiple re displays * Save on local database quantity of displays and last display time per IAM * Add unit test for new functionality
1 parent d33b5c2 commit c3f3c1b

18 files changed

+1226
-59
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSInAppMessage.java

Lines changed: 97 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@
88

99
import java.util.ArrayList;
1010
import java.util.HashMap;
11+
import java.util.HashSet;
1112
import java.util.Iterator;
13+
import java.util.Set;
1214

1315
class OSInAppMessage {
1416

17+
private static final String IAM_ID = "id";
18+
private static final String IAM_VARIANTS = "variants";
19+
private static final String IAM_TRIGGERS = "triggers";
20+
private static final String IAM_RE_DISPLAY = "redisplay";
21+
private static final String DISPLAY_DURATION = "display_duration";
22+
1523
/**
1624
* The unique identifier for this in-app message
1725
*/
@@ -34,24 +42,47 @@ class OSInAppMessage {
3442
@NonNull
3543
public ArrayList<ArrayList<OSTrigger>> triggers;
3644

37-
private double displayDuration;
45+
/**
46+
* IAM clicks associated to this IAM
47+
*/
48+
@NonNull
49+
private Set<String> clickedClickIds;
3850

51+
/**
52+
* Reference to redisplay properties
53+
*/
54+
private OSInAppMessageDisplayStats displayStats = new OSInAppMessageDisplayStats();
55+
56+
private double displayDuration;
57+
private boolean triggerChanged = false;
3958
private boolean actionTaken;
4059
boolean isPreview;
4160

4261
OSInAppMessage(boolean isPreview) {
4362
this.isPreview = isPreview;
4463
}
4564

65+
OSInAppMessage(@NonNull String messageId, @NonNull Set<String> clickIds, OSInAppMessageDisplayStats displayStats) {
66+
this.messageId = messageId;
67+
this.clickedClickIds = clickIds;
68+
69+
this.displayStats = displayStats;
70+
}
71+
4672
OSInAppMessage(JSONObject json) throws JSONException {
4773

4874
// initialize simple root properties
49-
this.messageId = json.getString("id");
50-
this.variants = parseVariants(json.getJSONObject("variants"));
51-
this.triggers = parseTriggerJson(json.getJSONArray("triggers"));
75+
this.messageId = json.getString(IAM_ID);
76+
this.variants = parseVariants(json.getJSONObject(IAM_VARIANTS));
77+
this.triggers = parseTriggerJson(json.getJSONArray(IAM_TRIGGERS));
78+
this.clickedClickIds = new HashSet<>();
79+
80+
if (json.has(IAM_RE_DISPLAY)) {
81+
this.displayStats = new OSInAppMessageDisplayStats(json.getJSONObject(IAM_RE_DISPLAY));
82+
}
5283
}
5384

54-
private static HashMap<String, HashMap<String, String>> parseVariants(JSONObject json) throws JSONException {
85+
private HashMap<String, HashMap<String, String>> parseVariants(JSONObject json) throws JSONException {
5586
HashMap<String, HashMap<String, String>> variantTypes = new HashMap<>();
5687

5788
Iterator<String> keyIterator = json.keys();
@@ -72,16 +103,17 @@ private static HashMap<String, HashMap<String, String>> parseVariants(JSONObject
72103
return variantTypes;
73104
}
74105

75-
ArrayList<ArrayList<OSTrigger>> parseTriggerJson(JSONArray triggersJson) throws JSONException {
106+
protected ArrayList<ArrayList<OSTrigger>> parseTriggerJson(JSONArray triggersJson) throws JSONException {
76107
// initialize triggers
77108
ArrayList<ArrayList<OSTrigger>> parsedTriggers = new ArrayList<>();
78109

79110
for (int i = 0; i < triggersJson.length(); i++) {
80111
JSONArray ands = triggersJson.getJSONArray(i);
81112
ArrayList<OSTrigger> converted = new ArrayList<>();
82-
for (int j = 0; j < ands.length(); j++)
83-
converted.add(new OSTrigger(ands.getJSONObject(j)));
84-
113+
for (int j = 0; j < ands.length(); j++) {
114+
OSTrigger trigger = new OSTrigger(ands.getJSONObject(j));
115+
converted.add(trigger);
116+
}
85117
parsedTriggers.add(converted);
86118
}
87119

@@ -92,7 +124,7 @@ JSONObject toJSONObject() {
92124
JSONObject json = new JSONObject();
93125

94126
try {
95-
json.put("id", this.messageId);
127+
json.put(IAM_ID, this.messageId);
96128

97129
JSONObject variants = new JSONObject();
98130
for (String key : this.variants.keySet()) {
@@ -105,8 +137,11 @@ JSONObject toJSONObject() {
105137
variants.put(key, converted);
106138
}
107139

108-
json.put("variants", variants);
109-
json.put("display_duration", this.displayDuration);
140+
json.put(IAM_VARIANTS, variants);
141+
json.put(DISPLAY_DURATION, this.displayDuration);
142+
143+
if (displayStats.isRedisplayEnabled())
144+
json.put(IAM_RE_DISPLAY, displayStats.toJSONObject());
110145

111146
JSONArray orConditions = new JSONArray();
112147
for (ArrayList<OSTrigger> andArray : this.triggers) {
@@ -118,7 +153,7 @@ JSONObject toJSONObject() {
118153
orConditions.put(andConditions);
119154
}
120155

121-
json.put("triggers", orConditions);
156+
json.put(IAM_TRIGGERS, orConditions);
122157

123158
} catch (JSONException exception) {
124159
exception.printStackTrace();
@@ -129,6 +164,7 @@ JSONObject toJSONObject() {
129164

130165
/**
131166
* Called when an action is taken to track uniqueness
167+
*
132168
* @return true if action taken was unique
133169
*/
134170
boolean takeActionAsUnique() {
@@ -137,22 +173,66 @@ boolean takeActionAsUnique() {
137173
return actionTaken = true;
138174
}
139175

140-
public double getDisplayDuration() {
176+
double getDisplayDuration() {
141177
return displayDuration;
142178
}
143179

144-
public void setDisplayDuration(double displayDuration) {
180+
void setDisplayDuration(double displayDuration) {
145181
this.displayDuration = displayDuration;
146182
}
147183

184+
boolean isTriggerChanged() {
185+
return triggerChanged;
186+
}
187+
188+
void setTriggerChanged(boolean triggerChanged) {
189+
this.triggerChanged = triggerChanged;
190+
}
191+
192+
@NonNull
193+
Set<String> getClickedClickIds() {
194+
return clickedClickIds;
195+
}
196+
197+
boolean isClickAvailable(String clickId) {
198+
return !clickedClickIds.contains(clickId);
199+
}
200+
201+
void clearClickIds() {
202+
clickedClickIds.clear();
203+
}
204+
205+
void addClickId(String clickId) {
206+
clickedClickIds.add(clickId);
207+
}
208+
209+
OSInAppMessageDisplayStats getDisplayStats() {
210+
return displayStats;
211+
}
212+
148213
@Override
149214
public String toString() {
150215
return "OSInAppMessage{" +
151216
"messageId='" + messageId + '\'' +
152-
", variants=" + variants +
153217
", triggers=" + triggers +
154-
", displayDuration=" + displayDuration +
218+
", clickedClickIds=" + clickedClickIds +
219+
", displayStats=" + displayStats +
155220
", actionTaken=" + actionTaken +
221+
", isPreview=" + isPreview +
156222
'}';
157223
}
224+
225+
@Override
226+
public boolean equals(Object o) {
227+
if (this == o) return true;
228+
if (o == null || getClass() != o.getClass()) return false;
229+
OSInAppMessage that = (OSInAppMessage) o;
230+
return messageId.equals(that.messageId);
231+
}
232+
233+
@Override
234+
public int hashCode() {
235+
int result = messageId.hashCode();
236+
return result;
237+
}
158238
}

0 commit comments

Comments
 (0)