Skip to content
This repository has been archived by the owner on Oct 30, 2022. It is now read-only.

How to use ViaBus

KunMinX edited this page Nov 20, 2018 · 24 revisions

0. Add the following dependencies in module build.gradle.

implementation "com.kunminx.viabus:viabus-core:0.4.5"

1. Define the interface for sending requests.

The interface must be inherited from IRequest, such as:

public interface INoteRequest extends IRequest{

    void queryList();

    void insert(NoteBean bean);

    ...
}

2. Bus is defined to support access to request interfaces.

Bus must be inherited from BaseBus, such as:

public class NoteBus extends BaseBus {

    public static INoteRequest note() {
        return (INoteRequest) getRequest(INoteRequest.class);
    }

    ...
}

image

3. Register UI as a response recipient.

In UI, data requests are sent through bus. And in onResult, the UI logic is processed according to the response code.

public class NoteListFragment extends Fragment implements IResponse {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        ...
        NoteBus.registerResponseObserver(this);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        ...
	NoteBus.note().queryList();
    }

    @Override 
    public void onResult(Result testResult) {

        String resultCode = (String) testResult.getResultCode();

        switch (resultCode) {
            case NoteResultCode.QUERY_LIST:
                if (testResult.getResultObject() != null) {
                    mAdapter.setList((List<NoteBean>) testResult.getResultObject());
                    mAdapter.notifyDataSetChanged();
                }
                break;
            case NoteResultCode.INSERTED:
            ...
        }
    }

    @Override
    public void onDestroy() {
        ...
        NoteBus.unregisterResponseObserver(this);
    }
}

4. In the module management class, business is registered as request handler.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        NoteBusiness noteBusiness = new NoteBusiness();
        noteBusiness.init(getApplicationContext());
        NoteBus.registerRequestHandler(noteBusiness);
        ...
    }
}

5. Asynchronously process various requests in business.

The progress message can be sent during the period, and finally the result data can be returned. Business must be inherited from BaseBusiness.

public class NoteBusiness extends BaseBusiness<NoteBus> implements INoteRequest {

    @Override
    public void queryList() {
        handleRequest((e) -> {
            List<NoteBean> list = mDataBase.getList(null, null);
	    ... 
	    sendMessage(e, new Result(NoteResultCode.PROGRESS, bean.getId()));
	    ... 
            return new Result(NoteResultCode.QUERY_LIST, list); 
        });
    }

    @Override
    public void insert(NoteBean bean) {
        handleRequest((e) -> { ... });
    }
    ...
}