Response

Response (Deprecated)

This class is considered legacy. It will remain as long API’s will use it. But it is considered to be replaced by the Result class. Which is lightweight and without any LiveData support.

Response is a container class that stores a generic data, a status and a generic progress All asynchronous operations on the SDK are wrapped in a LiveData<Response<Type, Progress>> and can have their values observed

Status for a response are:

  • EXECUTING: SDK is doing some action regarding to this Response. This can also be triggered by a call to sdk.refresh()
  • SUCCESS: Action was completed successfully. For non-Void types, the value can be accessed on the .data field.
  • FAIL: Action Failed. An exception with more details can be found on .exception field.

Progress

The generic parameter Progress is only used in the SDK for the Opening command.
Refer to the opening documentation on its usage here

For other commands the progress type is Void. And a Kotlin typealias have been created for convenience to the type SimpleResponse<Type>.

Helpers

To facilitate handling of this LiveData on different use cases and scenarios there a few helpers and extensions

Rx Styled Observe

Observing from Kotlin, from a lifeCycle ownwer

val data : LiveData<Response<User, Void>> // example data
data.observe(this) // lifecycleOwner (Activity or Fragment)
    .onExecuting { showProgressbar(true) }
    .onFail { exception ->
        showProgressbar(false)
        showError(exception)
     }
     .onSuccess { user ->
        showProgressbar(false)
        showSuccess(user)
     }

Or from an element that is not part of the lifecycle

val data : LiveData<Response<User, Void>> // example data
data.observeResult()
    .onFail { exception ->    }
    .onSuccess { user ->      }

// internally observeResult will use observeForever and remove the observer onSuccess or onFail

Divide the live-data per state

Divided data can be easily integrated into DataBinding

val onSuccess: LiveData<User> = data.onSuccess()
val onFail: LiveData<Exception> = data.onFail()
val onExecuting: LiveData<Boolean> = data.onExecuting()

Mapping

Maybe the application needs to map some of the SDK data types to some UI view model type, or for DataBinding

// example extracting the full name from the user type
val fullName: LiveData<SimpleResponse<String>> = data.map { it?.getFullName() }

Some examples in Java


LiveData<Response<User, Void>> data;  // example data

LiveData<User> onSuccess = ResponseExtensionsKt.onSuccess(data);
LiveData<Exception> onFail = ResponseExtensionsKt.onFail(data);
LiveData<Boolean> onExecuting = ResponseExtensionsKt.onExecuting(data);

data.observe(this, new ResponseObserver<User, Void>() {
  @Override public void onSuccess(@Nullable User user) {

  }

  @Override public void onExecuting(@Nullable Void aVoid) {

  }

  @Override public void onFail(@Nullable Exception exception) {

  }
});

RxObserverKt.observeResult(data)
      .onSuccess(new Function1<User, Unit>() {
        @Override public Unit invoke(User user) {
          return null;
        }
      })
      .onFail(new Function1<Exception, Unit>() {
        @Override public Unit invoke(Exception e) {
          return null;
        }
      });