use of com.fitpay.android.webview.events.UserReceived in project fitpay-android-sdk by fitpay.
the class WebViewCommunicatorImpl method getUserAndDevice.
private void getUserAndDevice(final String deviceId, final String callbackId) {
ApiManager.getInstance().getUser(new ApiCallback<User>() {
@Override
public void onSuccess(User result) {
if (result == null) {
onTaskError(EventCallback.USER_CREATED, callbackId, "getUser failed: result is null");
return;
}
WebViewCommunicatorImpl.this.user = result;
RxBus.getInstance().post(new UserReceived(user.getId(), user.getUsername()));
EventCallback eventCallback = new EventCallback.Builder().setCommand(EventCallback.USER_CREATED).setStatus(EventCallback.STATUS_OK).build();
eventCallback.send();
result.getDevice(deviceId, new ApiCallback<Device>() {
@Override
public void onSuccess(Device result) {
WebViewCommunicatorImpl.this.device = result;
String token = ApiManager.getPushToken();
String deviceToken = device.getNotificationToken();
final Runnable onSuccess = () -> onTaskSuccess(EventCallback.GET_USER_AND_DEVICE, callbackId);
boolean automaticallySubscribeToUserEventStream = true;
if (ApiManager.getConfig().containsKey(ApiManager.PROPERTY_AUTOMATICALLY_SUBSCRIBE_TO_USER_EVENT_STREAM)) {
automaticallySubscribeToUserEventStream = "true".equals(ApiManager.getConfig().get(ApiManager.PROPERTY_AUTOMATICALLY_SUBSCRIBE_TO_USER_EVENT_STREAM));
}
if (automaticallySubscribeToUserEventStream) {
try {
UserEventStreamManager.subscribe(user.getId());
} catch (IOException e) {
FPLog.e(e);
}
boolean automaticSyncThroughUserEventStream = true;
if (ApiManager.getConfig().containsKey(ApiManager.PROPERTY_AUTOMATICALLY_SYNC_FROM_USER_EVENT_STREAM)) {
automaticSyncThroughUserEventStream = "true".equals(ApiManager.getConfig().get(ApiManager.PROPERTY_AUTOMATICALLY_SYNC_FROM_USER_EVENT_STREAM));
}
if (automaticSyncThroughUserEventStream) {
userEventStreamSyncListener = new UserEventStreamListener() {
@Override
public void onUserEvent(UserStreamEvent event) {
if ("SYNC".equals(event.getType())) {
SyncInfo syncInfo = gson.fromJson(event.getPayload(), SyncInfo.class);
syncInfo.setInitiator(SyncInitiator.PLATFORM);
SyncRequest syncRequest = new SyncRequest.Builder().setSyncId(syncInfo.getSyncId()).setSyncInfo(syncInfo).setConnector(deviceService.getPaymentDeviceConnector()).setDevice(device).setUser(user).build();
RxBus.getInstance().post(syncRequest);
}
}
};
NotificationManager.getInstance().addListener(userEventStreamSyncListener);
}
}
if (deviceToken == null || !deviceToken.equals(token)) {
Device updatedDevice = new Device.Builder().setNotificationToken(token).build();
device.updateToken(updatedDevice, deviceToken == null, new ApiCallback<Device>() {
@Override
public void onSuccess(Device result) {
WebViewCommunicatorImpl.this.device = result;
onSuccess.run();
}
@Override
public void onFailure(@ResultCode.Code int errorCode, String errorMessage) {
onTaskError(EventCallback.GET_USER_AND_DEVICE, callbackId, "update device failed:" + errorMessage);
}
});
} else {
onSuccess.run();
}
}
@Override
public void onFailure(@ResultCode.Code int errorCode, String errorMessage) {
onTaskError(EventCallback.GET_USER_AND_DEVICE, callbackId, "getDevice failed " + errorMessage);
}
});
}
@Override
public void onFailure(@ResultCode.Code int errorCode, String errorMessage) {
onTaskError(EventCallback.USER_CREATED, callbackId, "getUser failed " + errorMessage);
}
});
}
Aggregations