I have this class in java

public abstract class SimpleApiCallback<T> implements ApiCallback<T> {

    private static final String LOG_TAG = "SimpleApiCallback";

    private Activity mActivity;

    private Context mContext = null;
    private View mPostView = null;

    /**
     * Failure callback to pass on failures to.
     */
    private ApiFailureCallback failureCallback = null;

    /**
     * Constructor
     */
    public SimpleApiCallback() {
    }

    /**
     * Constructor
     *
     * @param activity The context.
     */
    public SimpleApiCallback(Activity activity) {
        mActivity = activity;
    }

    /**
     * Constructor
     *
     * @param context    The context.
     * @param postOnView the view to post the code to execute
     */
    public SimpleApiCallback(Context context, View postOnView) {
        mContext = context;
        mPostView = postOnView;
    }

    /**
     * Constructor to delegate failure callback to another object. This allows us to stack failure callback implementations
     * in a decorator-type approach.
     *
     * @param failureCallback the failure callback implementation to delegate to
     */
    public SimpleApiCallback(ApiFailureCallback failureCallback) {
        this.failureCallback = failureCallback;
    }

    private void displayToast(final String message) {
        if (null != mActivity) {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(mActivity, message, Toast.LENGTH_SHORT).show();
                }
            });
        } else if ((null != mContext) && (null != mPostView)) {
            mPostView.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    @Override
    public void onNetworkError(Exception e) {
        if (failureCallback != null) {
            try {
                failureCallback.onNetworkError(e);
            } catch (Exception exception) {
                Log.e(LOG_TAG, "## onNetworkError() failed" + exception.getMessage(), exception);
            }
        } else {
            displayToast("Network Error");
        }
    }

    @Override
    public void onMatrixError(final MatrixError e) {
        if (failureCallback != null) {
            try {
                failureCallback.onMatrixError(e);
            } catch (Exception exception) {
                Log.e(LOG_TAG, "## onMatrixError() failed" + exception.getMessage(), exception);
            }
        } else {
            displayToast("Matrix Error : " + e.getLocalizedMessage());
        }
    }

    @Override
    public void onUnexpectedError(final Exception e) {
        if (failureCallback != null) {
            try {
                failureCallback.onUnexpectedError(e);
            } catch (Exception exception) {
                Log.e(LOG_TAG, "## onUnexpectedError() failed" + exception.getMessage(), exception);
            }
        } else {
            displayToast(e.getLocalizedMessage());
        }
    }
}

I can call it in java like that

new SimpleApiCallback<Void>(this) {
            @Override
            public void onSuccess(Void avoid) {
               ///
            }

            @Override
            public void onNetworkError(Exception e) {
                ///
            }

            @Override
            public void onUnexpectedError(Exception e) {
                ///
            }

            @Override
            public void onMatrixError(MatrixError e) {
            //
            }
        }

but in kotlin I tried many formats to call it but it doesn't work

like

val callback = object : SimpleApiCallback<>(activity){
            fun onSuccess(avoid: Void) {
                ///
            }

            fun onNetworkError(e: Exception) {
                ///
            }

            fun onUnexpectedError(e: Exception) {
                ///
            }

            fun onMatrixError(e: MatrixError) {
                //
            }
        }

can anyone advice please ?

1 Answers

0
Giorgos Neokleous On

Since you are using Java.lang.Void, you can still use it in Kotlin as it's not the same as a function's declarion void.

val callback = object : SimpleApiCallback<Void>(activity) {
    fun onSuccess(avoid: Void) {
        ///
    }

    override fun onNetworkError(e: Exception) {
        ///
    }

    override fun onUnexpectedError(e: Exception) {
        ///
    }

    override fun onMatrixError(e: MatrixError) {
        //
    }
}

The above should work, but your onSuccess() is not part of the SimpleApiCallback if it is then you should override that too.