Skip to content

Commit fedfa58

Browse files
authored
Merge pull request #323 from Iterable/MOB-2606-read-state
[MOB-2606] persistent read state for inbox in-apps
2 parents 8ed57a0 + 23fe3f8 commit fedfa58

File tree

6 files changed

+116
-4
lines changed

6 files changed

+116
-4
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,7 @@ google-services.json
5454
# Freeline
5555
freeline.py
5656
freeline/
57-
freeline_project_description.json
57+
freeline_project_description.json
58+
59+
# MacOS generated files
60+
.DS_Store

iterableapi/src/main/java/com/iterable/iterableapi/IterableInAppManager.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,20 +307,38 @@ private boolean isMessageExpired(IterableInAppMessage message) {
307307
private void syncWithRemoteQueue(List<IterableInAppMessage> remoteQueue) {
308308
boolean changed = false;
309309
Map<String, IterableInAppMessage> remoteQueueMap = new HashMap<>();
310+
310311
for (IterableInAppMessage message : remoteQueue) {
311312
remoteQueueMap.put(message.getMessageId(), message);
312-
if (storage.getMessage(message.getMessageId()) == null) {
313+
314+
boolean isInAppStored = storage.getMessage(message.getMessageId()) != null;
315+
316+
if (!isInAppStored) {
313317
storage.addMessage(message);
314318
onMessageAdded(message);
315319
changed = true;
316320
}
321+
322+
if (isInAppStored) {
323+
IterableInAppMessage localMessage = storage.getMessage(message.getMessageId());
324+
325+
boolean shouldOverwriteInApp = !localMessage.isRead() && message.isRead();
326+
327+
if (shouldOverwriteInApp) {
328+
localMessage.setRead(message.isRead());
329+
330+
changed = true;
331+
}
332+
}
317333
}
334+
318335
for (IterableInAppMessage localMessage : storage.getMessages()) {
319336
if (!remoteQueueMap.containsKey(localMessage.getMessageId())) {
320337
storage.removeMessage(localMessage);
321338
changed = true;
322339
}
323340
}
341+
324342
scheduleProcessing();
325343
if (changed) {
326344
notifyOnChange();
@@ -358,7 +376,7 @@ private void processMessages() {
358376
List<IterableInAppMessage> messagesByPriorityLevel = getMessagesSortedByPriorityLevel(messages);
359377

360378
for (IterableInAppMessage message : messagesByPriorityLevel) {
361-
if (!message.isProcessed() && !message.isConsumed() && message.getTriggerType() == TriggerType.IMMEDIATE) {
379+
if (!message.isProcessed() && !message.isConsumed() && message.getTriggerType() == TriggerType.IMMEDIATE && !message.isRead()) {
362380
IterableLogger.d(TAG, "Calling onNewInApp on " + message.getMessageId());
363381
InAppResponse response = handler.onNewInApp(message);
364382
IterableLogger.d(TAG, "Response: " + response);

iterableapi/src/test/java/com/iterable/iterableapi/IterableInAppManagerTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import androidx.annotation.NonNull;
88
import androidx.fragment.app.FragmentActivity;
99

10+
import java.util.List;
11+
1012
import com.iterable.iterableapi.unit.PathBasedQueueDispatcher;
1113

1214
import org.json.JSONArray;
@@ -382,12 +384,30 @@ public void testInAppAutoDisplayPause() throws Exception {
382384
verify(inAppHandler, times(1)).onNewInApp(inAppMessageCaptor.capture());
383385
}
384386

387+
@Test
388+
public void testMessagePersistentReadStateFromServer() throws Exception {
389+
// load the in-app that has not been synchronized with the server yet (read state is set to false)
390+
dispatcher.enqueueResponse("/inApp/getMessages", new MockResponse().setBody(IterableTestUtils.getResourceString("inapp_payload_inbox_read_state_1.json")));
391+
IterableInAppManager inAppManager = IterableApi.getInstance().getInAppManager();
392+
inAppManager.syncInApp();
393+
shadowOf(getMainLooper()).idle();
394+
395+
List<IterableInAppMessage> inboxMessages = inAppManager.getInboxMessages();
396+
assertFalse(inboxMessages.get(0).isRead());
397+
398+
// now load the one that has the in-app with read state set to true
399+
dispatcher.enqueueResponse("/inApp/getMessages", new MockResponse().setBody(IterableTestUtils.getResourceString("inapp_payload_inbox_read_state_2.json")));
400+
inAppManager.syncInApp();
401+
shadowOf(getMainLooper()).idle();
402+
403+
assertTrue(inboxMessages.get(0).isRead());
404+
}
405+
385406
private static class IterableSkipInAppHandler implements IterableInAppHandler {
386407
@NonNull
387408
@Override
388409
public InAppResponse onNewInApp(@NonNull IterableInAppMessage message) {
389410
return InAppResponse.SKIP;
390411
}
391412
}
392-
393413
}

iterableapi/src/test/java/com/iterable/iterableapi/IterableInAppMessageTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ public void testInAppMessageOnChangeListener_consumed() throws Exception {
7979
verify(mockChangeListener).onInAppMessageChanged(testInAppMessage);
8080
}
8181

82+
@Test
83+
public void testInAppMessageOnChangeListener_read() throws Exception {
84+
IterableInAppMessage testInAppMessage = InAppTestUtils.getTestInAppMessage();
85+
IterableInAppMessage.OnChangeListener mockChangeListener = mock(IterableInAppMessage.OnChangeListener.class);
86+
testInAppMessage.setOnChangeListener(mockChangeListener);
87+
88+
testInAppMessage.setRead(true);
89+
verify(mockChangeListener).onInAppMessageChanged(testInAppMessage);
90+
}
8291

8392
@Test
8493
public void testStorageNotInvoked() throws Exception {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"inAppMessages": [
3+
{
4+
"messageId": "readMessage1",
5+
"read": false,
6+
"content": {
7+
"html": "<html><head></head><body>Test</body></html>",
8+
"inAppDisplaySettings": {
9+
"top": {
10+
"percentage": 0
11+
},
12+
"right": {
13+
"percentage": 0
14+
},
15+
"bottom": {
16+
"percentage": 0
17+
},
18+
"left": {
19+
"percentage": 0
20+
}
21+
}
22+
},
23+
"saveToInbox": true,
24+
"inboxMetadata": {
25+
"title": "Title",
26+
"subtitle": "Subtitle",
27+
"icon": "icon.png"
28+
}
29+
}
30+
]
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"inAppMessages": [
3+
{
4+
"messageId": "readMessage1",
5+
"read": true,
6+
"content": {
7+
"html": "<html><head></head><body>Test</body></html>",
8+
"inAppDisplaySettings": {
9+
"top": {
10+
"percentage": 0
11+
},
12+
"right": {
13+
"percentage": 0
14+
},
15+
"bottom": {
16+
"percentage": 0
17+
},
18+
"left": {
19+
"percentage": 0
20+
}
21+
}
22+
},
23+
"saveToInbox": true,
24+
"inboxMetadata": {
25+
"title": "Title",
26+
"subtitle": "Subtitle",
27+
"icon": "icon.png"
28+
}
29+
}
30+
]
31+
}

0 commit comments

Comments
 (0)