Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public void onInboxUpdated() {

@Override
public void onListItemTapped(@NonNull IterableInAppMessage message) {
IterableApi.getInstance().getInAppManager().setRead(message, true);
IterableApi.getInstance().getInAppManager().setRead(message, true, null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this PR but inbox fragment can also have placeholders for success and failure callback make use of them..


if (inboxMode == InboxMode.ACTIVITY) {
startActivity(new Intent(getContext(), IterableInboxMessageActivity.class).putExtra(IterableInboxMessageActivity.ARG_MESSAGE_ID, message.getMessageId()));
Expand All @@ -255,7 +255,7 @@ public void onListItemTapped(@NonNull IterableInAppMessage message) {

@Override
public void onListItemDeleted(@NonNull IterableInAppMessage message, @NonNull IterableInAppDeleteActionType source) {
IterableApi.getInstance().getInAppManager().removeMessage(message, source, IterableInAppLocation.INBOX);
IterableApi.getInstance().getInAppManager().removeMessage(message, source, IterableInAppLocation.INBOX, null, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,26 @@ public void inAppConsume(@NonNull String messageId) {
IterableLogger.e(TAG, "inAppConsume: message is null");
return;
}
inAppConsume(message, null, null);
inAppConsume(message, null, null, null, null);
IterableLogger.printInfo();
}

/**
* Consumes an InApp message.
* @param messageId
* @param successHandler The callback which returns `success`.
* @param failureHandler The callback which returns `failure`.
*/
public void inAppConsume(@NonNull String messageId, @Nullable IterableHelper.SuccessHandler successHandler, @Nullable IterableHelper.FailureHandler failureHandler) {
IterableInAppMessage message = getInAppManager().getMessageById(messageId);
if (message == null) {
IterableLogger.e(TAG, "inAppConsume: message is null");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call failure callback here when we see message is not found.

if (failureHandler != null) {
failureHandler.onFailure("inAppConsume: message is null", null);
}
return;
}
inAppConsume(message, null, null, successHandler, failureHandler);
IterableLogger.printInfo();
}

Expand All @@ -742,8 +761,25 @@ public void inAppConsume(@NonNull IterableInAppMessage message, @Nullable Iterab
if (!checkSDKInitialization()) {
return;
}
apiClient.inAppConsume(message, source, clickLocation, inboxSessionId, null, null);
}

apiClient.inAppConsume(message, source, clickLocation, inboxSessionId);
/**
* Tracks InApp delete.
* This method from informs Iterable about inApp messages deleted with additional paramters.
* Call this method from places where inApp deletion are invoked by user. The messages can be swiped to delete or can be deleted using the link to delete button.
*
* @param message message object
* @param source An enum describing how the in App delete was triggered
* @param clickLocation The module in which the action happened
* @param successHandler The callback which returns `success`.
* @param failureHandler The callback which returns `failure`.
*/
public void inAppConsume(@NonNull IterableInAppMessage message, @Nullable IterableInAppDeleteActionType source, @Nullable IterableInAppLocation clickLocation, @Nullable IterableHelper.SuccessHandler successHandler, @Nullable IterableHelper.FailureHandler failureHandler) {
if (!checkSDKInitialization()) {
return;
}
apiClient.inAppConsume(message, source, clickLocation, inboxSessionId, successHandler, failureHandler);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ void trackInAppDelivery(@NonNull IterableInAppMessage message) {
}
}

public void inAppConsume(@NonNull IterableInAppMessage message, @Nullable IterableInAppDeleteActionType source, @Nullable IterableInAppLocation clickLocation, @Nullable String inboxSessionId) {
public void inAppConsume(@NonNull IterableInAppMessage message, @Nullable IterableInAppDeleteActionType source, @Nullable IterableInAppLocation clickLocation, @Nullable String inboxSessionId, @Nullable final IterableHelper.SuccessHandler successHandler, @Nullable final IterableHelper.FailureHandler failureHandler) {
JSONObject requestJSON = new JSONObject();

try {
Expand All @@ -335,8 +335,8 @@ public void inAppConsume(@NonNull IterableInAppMessage message, @Nullable Iterab
if (clickLocation == IterableInAppLocation.INBOX) {
addInboxSessionID(requestJSON, inboxSessionId);
}

sendPostRequest(IterableConstants.ENDPOINT_INAPP_CONSUME, requestJSON);
sendPostRequest(IterableConstants.ENDPOINT_INAPP_CONSUME, requestJSON, successHandler, failureHandler);
} catch (JSONException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ private void processMessageRemoval() {
}

if (message.isMarkedForDeletion() && !message.isConsumed()) {
IterableApi.sharedInstance.getInAppManager().removeMessage(message);
IterableApi.sharedInstance.getInAppManager().removeMessage(message, null, null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,13 @@ public synchronized int getUnreadInboxMessagesCount() {
* Set the read flag on an inbox message
* @param message Inbox message object retrieved from {@link IterableInAppManager#getInboxMessages()}
* @param read Read state flag. true = read, false = unread
* @param successHandler The callback which returns `success`.
*/
public synchronized void setRead(@NonNull IterableInAppMessage message, boolean read) {
public synchronized void setRead(@NonNull IterableInAppMessage message, boolean read, @Nullable IterableHelper.SuccessHandler successHandler) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why no failure callback for setRead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ayyanchira I checked the code but there is no scenario of failure here, that's why I removed an unused param for failure. Please let me know if I am wrong.

message.setRead(read);
if (successHandler != null) {
successHandler.onSuccess(new JSONObject()); // passing blank json object here as onSuccess is @Nonnull
}
notifyOnChange();
}

Expand Down Expand Up @@ -239,7 +243,7 @@ public void execute(Uri url) {
scheduleProcessing();
}
})) {
setRead(message, true);
setRead(message, true, null);
if (consume) {
message.markForDeletion(true);
}
Expand All @@ -249,17 +253,19 @@ public void execute(Uri url) {
/**
* Remove message from the list
* @param message The message to be removed
* @param successHandler The callback which returns `success`.
* @param failureHandler The callback which returns `failure`.
*/
public synchronized void removeMessage(@NonNull IterableInAppMessage message) {
public synchronized void removeMessage(@NonNull IterableInAppMessage message, @Nullable IterableHelper.SuccessHandler successHandler, @Nullable IterableHelper.FailureHandler failureHandler) {
message.setConsumed(true);
api.inAppConsume(message.getMessageId());
api.inAppConsume(message.getMessageId(), successHandler, failureHandler);
notifyOnChange();
}

public synchronized void removeMessage(@NonNull IterableInAppMessage message, @NonNull IterableInAppDeleteActionType source, @NonNull IterableInAppLocation clickLocation) {
public synchronized void removeMessage(@NonNull IterableInAppMessage message, @NonNull IterableInAppDeleteActionType source, @NonNull IterableInAppLocation clickLocation, @Nullable IterableHelper.SuccessHandler successHandler, @Nullable IterableHelper.FailureHandler failureHandler) {
IterableLogger.printInfo();
message.setConsumed(true);
api.inAppConsume(message, source, clickLocation);
api.inAppConsume(message, source, clickLocation, successHandler, failureHandler);
notifyOnChange();
}

Expand Down Expand Up @@ -432,7 +438,7 @@ private boolean canShowInAppAfterPrevious() {

private void handleIterableCustomAction(String actionName, IterableInAppMessage message) {
if (IterableConstants.ITERABLE_IN_APP_ACTION_DELETE.equals(actionName)) {
removeMessage(message, IterableInAppDeleteActionType.DELETE_BUTTON, IterableInAppLocation.IN_APP);
removeMessage(message, IterableInAppDeleteActionType.DELETE_BUTTON, IterableInAppLocation.IN_APP, null, null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import android.app.Activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.iterable.iterableapi.unit.PathBasedQueueDispatcher;

import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -78,18 +82,65 @@ public void testInboxMessageOrdering() throws Exception {
assertEquals("message4", inboxMessages.get(1).getMessageId());
}

@Test
public void testRemoveMessage() throws Exception {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌

dispatcher.enqueueResponse("/inApp/getMessages", new MockResponse().setBody(IterableTestUtils.getResourceString("inapp_payload_inbox_multiple.json")));
final IterableInAppManager inAppManager = IterableApi.getInstance().getInAppManager();
inAppManager.syncInApp();
shadowOf(getMainLooper()).idle();
List<IterableInAppMessage> inboxMessages = inAppManager.getInboxMessages();
assertEquals(2, inboxMessages.size());
assertEquals(1, inAppManager.getUnreadInboxMessagesCount());
inAppManager.removeMessage(inboxMessages.get(0), new IterableHelper.SuccessHandler() {
@Override
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverify this block to see if it works as expected as the callback has different handler in the definition above.

public void onSuccess(@NonNull JSONObject data) {
assertEquals(1, inAppManager.getInboxMessages().size());
assertEquals("message2", inAppManager.getInboxMessages().get(0).getMessageId());
}
}, new IterableHelper.FailureHandler() {
@Override
public void onFailure(@NonNull String reason, @Nullable JSONObject data) {
assertFalse(true);
}
});
assertEquals(1, inAppManager.getInboxMessages().size());
assertEquals("message2", inAppManager.getInboxMessages().get(0).getMessageId());
}

@Test
public void testSetRead() throws Exception {
// Set up mock response
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test set read can be part of next PR

dispatcher.enqueueResponse("/inApp/getMessages", new MockResponse().setBody(IterableTestUtils.getResourceString("inapp_payload_inbox_multiple.json")));

// Initialize in-app manager and wait for messages to be synced
IterableInAppManager inAppManager = IterableApi.getInstance().getInAppManager();
inAppManager.syncInApp();
shadowOf(getMainLooper()).idle();

// Get inbox messages
List<IterableInAppMessage> inboxMessages = inAppManager.getInboxMessages();

// Verify initial state
assertEquals(1, inAppManager.getUnreadInboxMessagesCount());
assertEquals(2, inboxMessages.size());
assertFalse(inboxMessages.get(0).isRead());
assertTrue(inboxMessages.get(1).isRead());
inAppManager.setRead(inboxMessages.get(0), true);

// Set first message as read with a callback
final boolean[] callbackCalled = { false };
inAppManager.setRead(inboxMessages.get(0), true, new IterableHelper.SuccessHandler() {
@Override
public void onSuccess(@NonNull JSONObject data) {
callbackCalled[0] = true;
assertTrue(callbackCalled[0]);
}
});

// Wait for callback to be called
shadowOf(getMainLooper()).idle();

// Verify that callback was called and that message is marked as read
assertTrue(callbackCalled[0]);
assertEquals(0, inAppManager.getUnreadInboxMessagesCount());
assertEquals(2, inAppManager.getInboxMessages().size());
assertTrue(inboxMessages.get(0).isRead());
Expand Down