Using TalkJavaCompat
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:
Talk.lineStatusTalk.createCall
Both of them are using Kotlin Coroutines which makes them impossible to use from Java. TalkJavaCompat provides similar APIs instead:
TalkJavaCompat.lineStatusTalkJavaCompat.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:
Executor- an optionaljava.util.concurrent.Executoron which the listener will be called. If not provided the listener is called on main thread.TalkJavaCompatListener- listener for returning the asynchronously computed value
Examples
-
Listener called on main thread:
job.addListener(new TalkJavaCompatListener<TalkCallResult>() {@Overridepublic void onSuccess(@NotNull TalkCallResult result) {}@Overridepublic void onError(@NotNull Throwable throwable) {}}); -
Listener called on specified
Executor:job.addListener(Executors.newSingleThreadExecutor(),new TalkJavaCompatListener<TalkCallResult>() {@Overridepublic void onSuccess(@NotNull TalkCallResult result) {}@Overridepublic 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>() {@Overridepublic void onSuccess(@NotNull TalkCallResult result) {}@Overridepublic void onError(@NotNull Throwable throwable) {}});job.cancel();