Skip to content

Commit 1fe732b

Browse files
authored
feat(inbox): Callback for when Inbox was synced via forceContentUpdat… (#111)
* feat(inbox): Callback for when Inbox was synced via forceContentUpdate(LP-6295).
1 parent cffde82 commit 1fe732b

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

AndroidSDK/src/com/leanplum/Leanplum.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,8 @@ public void response(JSONObject response) {
19211921
applyContentInResponse(response, false);
19221922
if (response.optBoolean(Constants.Keys.SYNC_INBOX, false)) {
19231923
LeanplumInbox.getInstance().downloadMessages();
1924+
} else {
1925+
LeanplumInbox.getInstance().triggerInboxSyncedWithStatus(true);
19241926
}
19251927
if (response.optBoolean(Constants.Keys.LOGGING_ENABLED, false)) {
19261928
Constants.loggingEnabled = true;
@@ -1940,6 +1942,7 @@ public void error(Exception e) {
19401942
if (callback != null) {
19411943
OsHandler.getInstance().post(callback);
19421944
}
1945+
LeanplumInbox.getInstance().triggerInboxSyncedWithStatus(false);
19431946
}
19441947
});
19451948
req.sendIfConnected();

AndroidSDK/src/com/leanplum/LeanplumInbox.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import android.content.SharedPreferences;
2626

2727
import com.leanplum.callbacks.InboxChangedCallback;
28+
import com.leanplum.callbacks.InboxSyncedCallback;
2829
import com.leanplum.callbacks.VariablesChangedCallback;
2930
import com.leanplum.internal.AESCrypt;
3031
import com.leanplum.internal.CollectionUtil;
@@ -65,13 +66,15 @@ public class LeanplumInbox {
6566
private boolean didLoad = false;
6667

6768
private final List<InboxChangedCallback> changedCallbacks;
69+
private final List<InboxSyncedCallback> syncedCallbacks;
6870
private final Object updatingLock = new Object();
6971

7072
private LeanplumInbox() {
7173
this.unreadCount = 0;
7274
this.messages = new HashMap<>();
7375
this.didLoad = false;
7476
this.changedCallbacks = new ArrayList<>();
77+
this.syncedCallbacks = new ArrayList<>();
7578
downloadedImageUrls = new HashSet<>();
7679
}
7780

@@ -178,6 +181,29 @@ public void removeChangedHandler(InboxChangedCallback handler) {
178181
}
179182
}
180183

184+
/**
185+
* Add a callback for when the forceContentUpdate was called.
186+
*
187+
* @param handler InboxSyncedCallback callback that need to be added.
188+
*/
189+
public void addSyncedHandler(InboxSyncedCallback handler) {
190+
synchronized (syncedCallbacks) {
191+
syncedCallbacks.add(handler);
192+
}
193+
}
194+
195+
196+
/**
197+
* Removes a inbox synced callback.
198+
*
199+
* @param handler InboxSyncedCallback callback that need to be removed.
200+
*/
201+
public void removeSyncedHandler(InboxSyncedCallback handler) {
202+
synchronized (syncedCallbacks) {
203+
syncedCallbacks.remove(handler);
204+
}
205+
}
206+
181207
/**
182208
* Static 'getInstance' method.
183209
*/
@@ -241,6 +267,19 @@ void triggerChanged() {
241267
}
242268
}
243269

270+
/**
271+
* Trigger InboxSyncedCallback with status of forceContentUpdate.
272+
* @param success True if forceContentUpdate was successful.
273+
*/
274+
void triggerInboxSyncedWithStatus(boolean success) {
275+
synchronized (changedCallbacks) {
276+
for (InboxSyncedCallback callback : syncedCallbacks) {
277+
callback.setSuccess(success);
278+
OsHandler.getInstance().post(callback);
279+
}
280+
}
281+
}
282+
244283
void load() {
245284
if (Constants.isNoop()) {
246285
return;
@@ -353,6 +392,7 @@ public void response(JSONObject response) {
353392

354393
if (!willDownladImages) {
355394
update(messages, unreadCount, true);
395+
triggerInboxSyncedWithStatus(true);
356396
return;
357397
}
358398

@@ -362,13 +402,21 @@ public void response(JSONObject response) {
362402
@Override
363403
public void variablesChanged() {
364404
update(messages, totalUnreadCount, true);
405+
triggerInboxSyncedWithStatus(true);
365406
}
366407
});
367408
} catch (Throwable t) {
409+
triggerInboxSyncedWithStatus(false);
368410
Util.handleException(t);
369411
}
370412
}
371413
});
414+
req.onError(new Request.ErrorCallback() {
415+
@Override
416+
public void error(Exception e) {
417+
triggerInboxSyncedWithStatus(false);
418+
}
419+
});
372420
req.sendIfConnected();
373421
}
374422

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2017, Leanplum, Inc. All rights reserved.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package com.leanplum.callbacks;
23+
24+
/**
25+
* Callback that gets run when forceContentUpdate was called.
26+
*
27+
* @author Anna Orlova
28+
*/
29+
public abstract class InboxSyncedCallback implements Runnable {
30+
private boolean success;
31+
32+
public void setSuccess(boolean success) {
33+
this.success = success;
34+
}
35+
36+
public void run() {
37+
this.onForceContentUpdate(success);
38+
}
39+
40+
/**
41+
* Call when forceContentUpdate was called.
42+
* @param success True if syncing was successful.
43+
*/
44+
public abstract void onForceContentUpdate(boolean success);
45+
}

AndroidSDKTests/src/test/java/com/leanplum/LeanplumInboxTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import com.leanplum._whitebox.utilities.RequestHelper;
2525
import com.leanplum._whitebox.utilities.ResponseHelper;
2626
import com.leanplum.callbacks.InboxChangedCallback;
27+
import com.leanplum.callbacks.InboxSyncedCallback;
2728
import com.leanplum.internal.Constants;
29+
import com.leanplum.internal.Util;
2830

2931
import org.junit.Test;
3032

@@ -38,6 +40,7 @@
3840
import static org.junit.Assert.assertNotNull;
3941
import static org.junit.Assert.assertNull;
4042
import static org.junit.Assert.assertTrue;
43+
import static org.powermock.api.mockito.PowerMockito.doReturn;
4144

4245
/**
4346
* Tests the inbox.
@@ -59,9 +62,24 @@ public void onRequest(String httpMethod, String apiMethod, Map<String, Object> p
5962
assertEquals(Constants.Methods.GET_INBOX_MESSAGES, apiMethod);
6063
}
6164
});
65+
66+
// Validate inbox synced callback when messages changes.
67+
InboxSyncedCallback inboxSyncedCallback=new InboxSyncedCallback() {
68+
@Override
69+
public void onForceContentUpdate(boolean success) {
70+
assertTrue(success);
71+
}
72+
};
73+
74+
// Add synced callback to be able to validate.
75+
Leanplum.getInbox().addSyncedHandler(inboxSyncedCallback);
76+
6277
// Download messages.
6378
Leanplum.getInbox().downloadMessages();
6479

80+
// Remove synced callback afterwards so we don't get synced callbacks anymore.
81+
Leanplum.getInbox().removeSyncedHandler(inboxSyncedCallback);
82+
6583
// Validate inbox callback when messages changes.
6684
InboxChangedCallback callback = new InboxChangedCallback() {
6785
@Override
@@ -116,6 +134,26 @@ public void inboxChanged() {
116134

117135
Leanplum.getInbox().removeMessage(messageById.getMessageId());
118136
assertEquals(1, Leanplum.getInbox().allMessages().size());
137+
138+
// Validate inbox synced callback with false when there is no internet .
139+
InboxSyncedCallback inboxSyncedCallbackFalse=new InboxSyncedCallback() {
140+
@Override
141+
public void onForceContentUpdate(boolean success) {
142+
assertFalse(success);
143+
}
144+
};
145+
146+
// Add synced callback to be able to validate.
147+
Leanplum.getInbox().addSyncedHandler(inboxSyncedCallbackFalse);
148+
149+
// Return false when checks for internet connection.
150+
doReturn(false).when(Util.class, "isConnected");
151+
152+
// Download messages.
153+
Leanplum.getInbox().downloadMessages();
154+
155+
// Remove synced callback afterwards so we don't get synced callbacks anymore.
156+
Leanplum.getInbox().removeSyncedHandler(inboxSyncedCallbackFalse);
119157
}
120158

121159
@Test

0 commit comments

Comments
 (0)