Skip to content

Commit

Permalink
Remove Preview request code from Controller
Browse files Browse the repository at this point in the history
* Move request code to Repository
  • Loading branch information
Jeasmine committed Jul 26, 2021
1 parent 33228f7 commit d4d400e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,18 @@ protected OSInAppMessageController(OneSignalDbHelper dbHelper, OSTaskController
if (tempClickedMessageIdsSet != null)
clickedClickIds.addAll(tempClickedMessageIdsSet);

initRedisplayData(dbHelper);
initRedisplayData(dbHelper, logger);
}

OSInAppMessageRepository getInAppMessageRepository(OneSignalDbHelper dbHelper) {
OSInAppMessageRepository getInAppMessageRepository(OneSignalDbHelper dbHelper, OSLogger logger) {
if (inAppMessageRepository == null)
inAppMessageRepository = new OSInAppMessageRepository(dbHelper);
inAppMessageRepository = new OSInAppMessageRepository(dbHelper, logger);

return inAppMessageRepository;
}

protected void initRedisplayData(OneSignalDbHelper dbHelper) {
inAppMessageRepository = getInAppMessageRepository(dbHelper);
protected void initRedisplayData(OneSignalDbHelper dbHelper, OSLogger logger) {
inAppMessageRepository = getInAppMessageRepository(dbHelper, logger);
Runnable getCachedIAMRunnable = new BackgroundRunnable() {
@Override
public void run() {
Expand Down Expand Up @@ -906,17 +906,9 @@ void displayPreviewMessage(@NonNull String previewUUID) {
final OSInAppMessage message = new OSInAppMessage(true);
getTagsForLiquidTemplating(message, true);

String htmlPath = "in_app_messages/device_preview?preview_id=" + previewUUID + "&app_id=" + OneSignal.appId;
OneSignalRestClient.get(htmlPath, new ResponseHandler() {
@Override
void onFailure(int statusCode, String response, Throwable throwable) {
printHttpErrorForInAppMessageRequest("html", statusCode, response);

dismissCurrentMessage(null);
}

inAppMessageRepository.getIAMPreviewData(OneSignal.appId, previewUUID, new OSInAppMessageRepository.OSInAppMessageRequestResponse() {
@Override
void onSuccess(String response) {
public void onSuccess(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
String htmlStr = jsonResponse.getString("html");
Expand All @@ -932,7 +924,12 @@ void onSuccess(String response) {
e.printStackTrace();
}
}
}, null);

@Override
public void onFailure(String response) {
dismissCurrentMessage(null);
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class OSInAppMessageDummyController extends OSInAppMessageController {
}

@Override
public void initRedisplayData(OneSignalDbHelper dbHelper) {
public void initRedisplayData(OneSignalDbHelper dbHelper, OSLogger logger) {
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -17,9 +18,27 @@ class OSInAppMessageRepository {
final static long IAM_CACHE_DATA_LIFETIME = 15_552_000L; // 6 months in seconds

private final OneSignalDbHelper dbHelper;
private final OSLogger logger;

OSInAppMessageRepository(OneSignalDbHelper dbHelper) {
OSInAppMessageRepository(OneSignalDbHelper dbHelper, OSLogger logger) {
this.dbHelper = dbHelper;
this.logger = logger;
}

void getIAMPreviewData(String appId, String previewUUID, final OSInAppMessageRequestResponse requestResponse){
String htmlPath = "in_app_messages/device_preview?preview_id=" + previewUUID + "&app_id=" + appId;
OneSignalRestClient.get(htmlPath, new OneSignalRestClient.ResponseHandler() {
@Override
void onFailure(int statusCode, String response, Throwable throwable) {
printHttpErrorForInAppMessageRequest("html", statusCode, response);
requestResponse.onFailure(response);
}

@Override
void onSuccess(String response) {
requestResponse.onSuccess(response);
}
}, null);
}

@WorkerThread
Expand Down Expand Up @@ -205,4 +224,13 @@ private void cleanInAppMessageClickedClickIds(Set<String> oldClickedClickIds) {
}
}

private void printHttpErrorForInAppMessageRequest(String requestType, int statusCode, String response) {
logger.error("Encountered a " + statusCode + " error while attempting in-app message " + requestType + " request: " + response);
}

interface OSInAppMessageRequestResponse {
void onSuccess(String response);
void onFailure(String response);
}

}

0 comments on commit d4d400e

Please sign in to comment.