1 minute to master the use of ViaBus architecture

Keywords: Android Gradle Fragment github

Copyright notice: This is the original article of blogger. Please indicate the author and link for reprint. More on KunMinX
https://blog.csdn.net/IamTheReal/article/details/82837568

0. Add the following dependencies to build.gradle of the module

implementation "com.kunminx.viabus:viabus-android:0.3.3"

1. Define an interface for sending requests.

The interface must inherit from IRequest, such as:

public interface INoteRequest extends IRequest{

    void queryList();

    void insert(NoteBean bean);

    ...
}

2. Define bus to support the access of request interface.

bus must inherit from BaseBus, such as:

public class NoteBus extends BaseBus {

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

    ...
}

3. Register the ui as the response recipient.

In the ui, data requests are sent over the bus. 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 management class of the module, register the business as the request handler.

public class MainActivity extends AppCompatActivity {

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

5. Asynchronous processing of various requests in the 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) -> { ... });
    }
    ...
}

More articles

Github : KunMinX / android-viabus-architecture
Horizontal comparison of Android architectures

Posted by kidbrax on Wed, 25 Dec 2019 11:41:39 -0800