8
8
9
9
import java .util .ArrayList ;
10
10
import java .util .HashMap ;
11
+ import java .util .HashSet ;
11
12
import java .util .Iterator ;
13
+ import java .util .Set ;
12
14
13
15
class OSInAppMessage {
14
16
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
+
15
23
/**
16
24
* The unique identifier for this in-app message
17
25
*/
@@ -34,24 +42,47 @@ class OSInAppMessage {
34
42
@ NonNull
35
43
public ArrayList <ArrayList <OSTrigger >> triggers ;
36
44
37
- private double displayDuration ;
45
+ /**
46
+ * IAM clicks associated to this IAM
47
+ */
48
+ @ NonNull
49
+ private Set <String > clickedClickIds ;
38
50
51
+ /**
52
+ * Reference to redisplay properties
53
+ */
54
+ private OSInAppMessageDisplayStats displayStats = new OSInAppMessageDisplayStats ();
55
+
56
+ private double displayDuration ;
57
+ private boolean triggerChanged = false ;
39
58
private boolean actionTaken ;
40
59
boolean isPreview ;
41
60
42
61
OSInAppMessage (boolean isPreview ) {
43
62
this .isPreview = isPreview ;
44
63
}
45
64
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
+
46
72
OSInAppMessage (JSONObject json ) throws JSONException {
47
73
48
74
// 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
+ }
52
83
}
53
84
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 {
55
86
HashMap <String , HashMap <String , String >> variantTypes = new HashMap <>();
56
87
57
88
Iterator <String > keyIterator = json .keys ();
@@ -72,16 +103,17 @@ private static HashMap<String, HashMap<String, String>> parseVariants(JSONObject
72
103
return variantTypes ;
73
104
}
74
105
75
- ArrayList <ArrayList <OSTrigger >> parseTriggerJson (JSONArray triggersJson ) throws JSONException {
106
+ protected ArrayList <ArrayList <OSTrigger >> parseTriggerJson (JSONArray triggersJson ) throws JSONException {
76
107
// initialize triggers
77
108
ArrayList <ArrayList <OSTrigger >> parsedTriggers = new ArrayList <>();
78
109
79
110
for (int i = 0 ; i < triggersJson .length (); i ++) {
80
111
JSONArray ands = triggersJson .getJSONArray (i );
81
112
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
+ }
85
117
parsedTriggers .add (converted );
86
118
}
87
119
@@ -92,7 +124,7 @@ JSONObject toJSONObject() {
92
124
JSONObject json = new JSONObject ();
93
125
94
126
try {
95
- json .put ("id" , this .messageId );
127
+ json .put (IAM_ID , this .messageId );
96
128
97
129
JSONObject variants = new JSONObject ();
98
130
for (String key : this .variants .keySet ()) {
@@ -105,8 +137,11 @@ JSONObject toJSONObject() {
105
137
variants .put (key , converted );
106
138
}
107
139
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 ());
110
145
111
146
JSONArray orConditions = new JSONArray ();
112
147
for (ArrayList <OSTrigger > andArray : this .triggers ) {
@@ -118,7 +153,7 @@ JSONObject toJSONObject() {
118
153
orConditions .put (andConditions );
119
154
}
120
155
121
- json .put ("triggers" , orConditions );
156
+ json .put (IAM_TRIGGERS , orConditions );
122
157
123
158
} catch (JSONException exception ) {
124
159
exception .printStackTrace ();
@@ -129,6 +164,7 @@ JSONObject toJSONObject() {
129
164
130
165
/**
131
166
* Called when an action is taken to track uniqueness
167
+ *
132
168
* @return true if action taken was unique
133
169
*/
134
170
boolean takeActionAsUnique () {
@@ -137,22 +173,66 @@ boolean takeActionAsUnique() {
137
173
return actionTaken = true ;
138
174
}
139
175
140
- public double getDisplayDuration () {
176
+ double getDisplayDuration () {
141
177
return displayDuration ;
142
178
}
143
179
144
- public void setDisplayDuration (double displayDuration ) {
180
+ void setDisplayDuration (double displayDuration ) {
145
181
this .displayDuration = displayDuration ;
146
182
}
147
183
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
+
148
213
@ Override
149
214
public String toString () {
150
215
return "OSInAppMessage{" +
151
216
"messageId='" + messageId + '\'' +
152
- ", variants=" + variants +
153
217
", triggers=" + triggers +
154
- ", displayDuration=" + displayDuration +
218
+ ", clickedClickIds=" + clickedClickIds +
219
+ ", displayStats=" + displayStats +
155
220
", actionTaken=" + actionTaken +
221
+ ", isPreview=" + isPreview +
156
222
'}' ;
157
223
}
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
+ }
158
238
}
0 commit comments