Using TalkJavaCompat

The Talk SDK is built using Kotlin as the primary language. While Kotlin is known for great interopability with Java, the SDK uses Kotlin coroutines, which are not native to Java. Coroutines are used for asynchronous operations. A Java application can't call the suspending methods directly. However, you can use TalkJavaCompat with your Java application to use a compatibility variant which works in Java.

TalkJavaCompat offers a replacement for two API from Talk interface:

  1. Talk.lineStatus
  2. Talk.createCall

Both of them are using Kotlin Coroutines which makes them impossible to use from Java. TalkJavaCompat provides similar APIs instead:

  1. TalkJavaCompat.lineStatus
  2. TalkJavaCompat.createCall

Rest of methods defined in Talk interface works in Java and TalkJavaCompat does not provide variant of them.

TalkListenableFuture

TalkListenableFuture is the type returned from TalkJavaCompat methods. It represents asynchronous operation and offers API for attaching the listener for result and canceling the work. If you are familiar with Guava ListenableFuture this API is simplified version of it.

Example

CallData callData = CallDataBuilder.create("your digital line name")        .recordingConsentAnswer(null)        .build();//assumes Zendesk.INSTANCE is properly configured at this pointTalk talk = Talk.create(Zendesk.INSTANCE);TalkListenableFuture<TalkCallResult> job = TalkJavaCompat.createCall(talk, callData);

Attaching listener

TalkListenableFuture.addListener allows to add a listener for result of asynchronous operation.

TalkListenableFuture.addListener accepts two arguments:

  1. Executor - an optional java.util.concurrent.Executor on which the listener will be called. If not provided the listener is called on main thread.
  2. TalkJavaCompatListener - listener for returning the asynchronously computed value

Examples

  1. Listener called on main thread:

    job.addListener(new TalkJavaCompatListener<TalkCallResult>() {    @Override    public void onSuccess(@NotNull TalkCallResult result) {
        }
        @Override    public void onError(@NotNull Throwable throwable) {
        }});
  2. Listener called on specified Executor:

    job.addListener(        Executors.newSingleThreadExecutor(),        new TalkJavaCompatListener<TalkCallResult>() {            @Override            public void onSuccess(@NotNull TalkCallResult result) {
                }
                @Override            public void onError(@NotNull Throwable throwable) {
                }        });

Canceling the work

To cancel ongoing work the TalkListenableFuture exposes the cancel method.

Example

TalkListenableFuture<TalkCallResult> job = TalkJavaCompat.createCall(talk, callData);job.addListener(new TalkJavaCompatListener<TalkCallResult>() {    @Override    public void onSuccess(@NotNull TalkCallResult result) {
    }
    @Override    public void onError(@NotNull Throwable throwable) {
    }});
job.cancel();