Callbacks between java and kotlin

二次信任 提交于 2021-02-11 12:23:59

问题


Initially, I had API calls in an adapter class which is in Java, I moved the API calls from the adapter to a presenter(Kotlin file).

Adapter(java file) requests to do an API call, from adapter, code moves to presenter (kotlin file) presenter does the actual API call when the response comes or error, adapter methods need to be called.

I implemented callbacks/interface, to do this

Interface:

interface ServiceOrderAdapterInterface {
    fun flOrderOfflineAcceptSuccess(flOrder: FlOrder)
    fun flOrderOfflineAcceptFailure(viewHolder: ServiceOrderAdapter.ViewHolder)
}

Adapter implements the above interface

public class ServiceOrderAdapter extends ArrayAdapter<ServiceOrder> implements ServiceOrderAdapterInterface {
    public class ServiceOrderAdapter extends ArrayAdapter<ServiceOrder> implements ServiceOrderAdapterInterface {
           @Override
        public void flOrderOfflineAcceptSuccess(@NotNull FlOrder flOrder) {
            // Set the current fl activity
            getApp().getFlOrderStateMachine().setCurrentServiceOrder(flOrder);
    
            // Move on to the next screen in the fl activity state machine
            getApp().getFlOrderStateMachine().trigger(FlOrderStateMachine.Events.onAcceptedJob);
        }
    
        @Override
        public void flOrderOfflineAcceptFailure(@NotNull ServiceOrderAdapter.ViewHolder viewHolder) {
            viewHolder.hideLoader();
            app.getServiceOrderBus().post(new ServiceOrderAcceptingEvent(false));
        }
    }
}

Presenter layer - want to call the interface methods

private fun handleFlOrderOfflinceAcceptFailure(failure: Failure) {
    //call the interface method fun flOrderOfflineAcceptSuccess(flOrder: FlOrder)
}

private fun handleFlOrderOfflinceAcceptSuccess(response: GenericResponse) {
    //want to call the interface method fun flOrderOfflineAcceptFailure(viewHolder: ServiceOrderAdapter.ViewHolder)
}

How can I do this?

来源:https://stackoverflow.com/questions/65947391/callbacks-between-java-and-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!