Created
January 14, 2019 16:50
-
-
Save cliffgr/c7c48bc094abea44700c262b941d3ebf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.wappier.wappierSDK.flush; | |
import android.text.TextUtils; | |
import com.wappier.wappierSDK.Env; | |
import com.wappier.wappierSDK.SessionHandler; | |
import com.wappier.wappierSDK.Wappier; | |
import com.wappier.wappierSDK.database.IDatabaseLayer; | |
import com.wappier.wappierSDK.logs.Logger; | |
import com.wappier.wappierSDK.loyalty.Loyalty; | |
import com.wappier.wappierSDK.network.NetworkResponse; | |
import com.wappier.wappierSDK.network.networkrequest.ContentType; | |
import com.wappier.wappierSDK.network.networkrequest.OnNetworkRequestResponseListener; | |
import com.wappier.wappierSDK.network.networkrequest.RequestType; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class FlushLayer implements IFlushLayer { | |
private IDatabaseLayer mdatabaseLayer; | |
private String mBaseUrl; | |
private int count = 0; | |
public void init(IDatabaseLayer databaseLayer) { | |
this.mdatabaseLayer = databaseLayer; | |
mBaseUrl = Env.WAPPIER_BASE_URL; | |
} | |
@Override | |
synchronized public void flushAll() { | |
if (TextUtils.isEmpty(Wappier.sSessionHandler.getStringPreference(SessionHandler.WAPPIER_ID))) { | |
Logger.d("** Wappier Id has not been set yet, will not flush events. **"); | |
return; | |
} | |
if (count == 0) { | |
count++; | |
JSONArray jsonArray = mdatabaseLayer.getEvents(); | |
if (jsonArray.length() == 0) { | |
Logger.d("No more events to send"); | |
return; | |
} | |
Logger.i("Sending database events..."); | |
Logger.d("DB Data : " + jsonArray.toString()); | |
Wappier.getNetworkRequest() | |
.baseUrl(mBaseUrl) | |
.endpoint("events") | |
.requestType(RequestType.POST) | |
.contentType(ContentType.JSON) | |
.bodyJsonObject(jsonArray) | |
.onNetworkRequestResponseListener(new OnNetworkRequestResponseListener() { | |
@Override | |
public void onSuccessResponse(NetworkResponse response) { | |
List<String> resultIds = new ArrayList<>(); | |
Logger.d("/events" + ": ** OK **"); | |
Logger.d("Network response " + response.getResponse()); | |
try { | |
JSONObject jsonObject = new JSONObject(response.getResponse()); | |
JSONArray eventsJsonArray = jsonObject.getJSONArray("events"); | |
for (int i = 0; i < eventsJsonArray.length(); i++) { | |
JSONObject responseJSONObject = eventsJsonArray.getJSONObject(i); | |
if (responseJSONObject.has("success") || responseJSONObject.has("error")) { | |
JSONObject successJSONObject = responseJSONObject.getJSONObject("success"); | |
if (successJSONObject.has("clientId")) { | |
resultIds.add(successJSONObject.getString("clientId")); | |
} | |
} | |
if (jsonObject.has("notifications")) { | |
Loyalty loyalty = Loyalty.getInstance(); | |
if (loyalty != null && loyalty.isLoyaltyEnabled()) | |
loyalty.setNotificationJson(jsonObject.getJSONObject("notifications")); | |
} | |
} | |
mdatabaseLayer.deleteSentEvents(resultIds); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} finally { | |
count--; | |
} | |
} | |
@Override | |
public void onErrorResponse(NetworkResponse errorResponse) { | |
Logger.d("/events" + ": Error :" + errorResponse.toString()); | |
count--; | |
} | |
}).fireRequest(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment