Skip to content

Commit 8a833b8

Browse files
authored
Add method for downloading inbox messages (#434)
* Add method for downloading inbox messages * Simplify implementation * Move public methods to LeanplumInbox
1 parent f60ffc5 commit 8a833b8

File tree

4 files changed

+116
-9
lines changed

4 files changed

+116
-9
lines changed

AndroidSDKCore/src/main/java/com/leanplum/Leanplum.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016, Leanplum, Inc. All rights reserved.
2+
* Copyright 2020, Leanplum, Inc. All rights reserved.
33
*
44
* Licensed to the Apache Software Foundation (ASF) under one
55
* or more contributor license agreements. See the NOTICE file

AndroidSDKCore/src/main/java/com/leanplum/LeanplumInbox.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import android.content.Context;
2525
import android.content.SharedPreferences;
2626

27+
import androidx.annotation.Nullable;
2728
import com.leanplum.callbacks.InboxChangedCallback;
2829
import com.leanplum.callbacks.InboxSyncedCallback;
2930
import com.leanplum.callbacks.VariablesChangedCallback;
@@ -284,6 +285,19 @@ void triggerInboxSyncedWithStatus(boolean success) {
284285
}
285286
}
286287

288+
private void triggerInboxSyncedWithStatus(
289+
boolean success,
290+
@Nullable InboxSyncedCallback oneTimeCallback) {
291+
292+
if (oneTimeCallback != null) {
293+
addSyncedHandler(oneTimeCallback);
294+
triggerInboxSyncedWithStatus(success);
295+
removeSyncedHandler(oneTimeCallback);
296+
} else {
297+
triggerInboxSyncedWithStatus(success);
298+
}
299+
}
300+
287301
void load() {
288302
if (Constants.isNoop()) {
289303
return;
@@ -346,7 +360,21 @@ void save() {
346360
SharedPreferencesUtil.commitChanges(editor);
347361
}
348362

349-
void downloadMessages() {
363+
/**
364+
* Forces downloading of inbox messages from the server. After messages are downloaded the
365+
* appropriate callbacks will fire.
366+
*/
367+
public void downloadMessages() {
368+
downloadMessages(null);
369+
}
370+
371+
/**
372+
* Forces downloading of inbox messages from the server. After messages are downloaded the
373+
* appropriate callbacks will fire.
374+
*
375+
* @param callback The callback to invoke when messages are downloaded.
376+
*/
377+
public void downloadMessages(@Nullable InboxSyncedCallback callback) {
350378
if (Constants.isNoop()) {
351379
return;
352380
}
@@ -399,7 +427,7 @@ public void response(JSONObject response) {
399427

400428
if (!willDownladImages) {
401429
update(messages, unreadCount, true);
402-
triggerInboxSyncedWithStatus(true);
430+
triggerInboxSyncedWithStatus(true, callback);
403431
return;
404432
}
405433

@@ -409,19 +437,19 @@ public void response(JSONObject response) {
409437
@Override
410438
public void variablesChanged() {
411439
update(messages, totalUnreadCount, true);
412-
triggerInboxSyncedWithStatus(true);
440+
triggerInboxSyncedWithStatus(true, callback);
413441
}
414442
});
415443
} catch (Throwable t) {
416-
triggerInboxSyncedWithStatus(false);
444+
triggerInboxSyncedWithStatus(false, callback);
417445
Log.exception(t);
418446
}
419447
}
420448
});
421449
req.onError(new Request.ErrorCallback() {
422450
@Override
423451
public void error(Exception e) {
424-
triggerInboxSyncedWithStatus(false);
452+
triggerInboxSyncedWithStatus(false, callback);
425453
}
426454
});
427455
RequestSender.getInstance().send(req);

AndroidSDKCore/src/main/java/com/leanplum/callbacks/InboxSyncedCallback.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017, Leanplum, Inc. All rights reserved.
2+
* Copyright 2020, Leanplum, Inc. All rights reserved.
33
*
44
* Licensed to the Apache Software Foundation (ASF) under one
55
* or more contributor license agreements. See the NOTICE file
@@ -22,7 +22,7 @@
2222
package com.leanplum.callbacks;
2323

2424
/**
25-
* Callback that gets run when forceContentUpdate was called.
25+
* Callback that gets run when inbox finishes syncing of messages.
2626
*
2727
* @author Anna Orlova
2828
*/
@@ -38,7 +38,7 @@ public void run() {
3838
}
3939

4040
/**
41-
* Call when forceContentUpdate was called.
41+
* Called when inbox finishes syncing of messages.
4242
* @param success True if syncing was successful.
4343
*/
4444
public abstract void onForceContentUpdate(boolean success);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2020, 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;
23+
24+
import static org.junit.Assert.assertNotNull;
25+
import static org.junit.Assert.assertTrue;
26+
27+
import com.leanplum.__setup.AbstractTest;
28+
import com.leanplum._whitebox.utilities.ResponseHelper;
29+
import com.leanplum.callbacks.InboxSyncedCallback;
30+
import org.junit.Test;
31+
32+
/**
33+
* Unit tests for {@link LeanplumInbox}.
34+
*/
35+
public class LeanplumInboxTest extends AbstractTest {
36+
37+
/**
38+
* Test for {@link LeanplumInbox#downloadMessages()}.
39+
*/
40+
@Test
41+
public void testDownloadMessages() {
42+
setupSDK(mContext, "/responses/simple_start_response.json");
43+
44+
ResponseHelper.seedResponse("/responses/newsfeed_response.json");
45+
Boolean[] inboxSyncResult = new Boolean[1]; // boolean holder
46+
47+
LeanplumInbox.getInstance().addSyncedHandler(new InboxSyncedCallback() {
48+
@Override
49+
public void onForceContentUpdate(boolean success) {
50+
inboxSyncResult[0] = success;
51+
}
52+
});
53+
LeanplumInbox.getInstance().downloadMessages();
54+
55+
assertNotNull(inboxSyncResult[0]);
56+
assertTrue(inboxSyncResult[0]);
57+
}
58+
59+
/**
60+
* Test for {@link LeanplumInbox#downloadMessages(InboxSyncedCallback)}.
61+
*/
62+
@Test
63+
public void testDownloadMessagesWithCallback() {
64+
setupSDK(mContext, "/responses/simple_start_response.json");
65+
66+
ResponseHelper.seedResponse("/responses/newsfeed_response.json");
67+
Boolean[] inboxSyncResult = new Boolean[1]; // boolean holder
68+
69+
LeanplumInbox.getInstance().downloadMessages(new InboxSyncedCallback() {
70+
@Override
71+
public void onForceContentUpdate(boolean success) {
72+
inboxSyncResult[0] = success;
73+
}
74+
});
75+
assertNotNull(inboxSyncResult[0]);
76+
assertTrue(inboxSyncResult[0]);
77+
}
78+
79+
}

0 commit comments

Comments
 (0)