How to uncouple gracefully? This is a big problem for all developers, and ele. me offers a new option — ToyRoom.

You must have encountered such a situation in the usual business development process: in business logic-oriented programming, the change of a specific business object may cause the change of each component, and the complexity of business logic will increase the coupling between modules. In order list page, for example, we call for an asynchronous network request data to get the order, after the network data back, need to deal with the data, then data saved to the database, and notify the multiple TAB page refresh the UI, we use EventBus to use notice before distribution, but when there are more and more business, We found that the EventBus onEvent method maintain uncontrolled, because we don’t know how many local response to this Event, the code becomes difficult to maintain, this time can we wondered how these response logic like line stretches out in one place, and to perform A, B, finally do C, be clear at A glance, Get the world back in our hands.

Based on this demand, ELE. me developed a set of business-logic-oriented programming library — ToyRoom. ToyRoom library provides a brand new programming mode, which expresses the influence of business object changes on each module through method chain. In the method chain, each method has an action parameter that performs the corresponding action to change the specific component. Methods, strung together, represent a series of actions on individual components. So you can see the whole world change in just one file. And those of you who have used RxJava will feel particularly familiar with it. ToyRoom is now open source, welcome to taste.

features

  1. It provides a new programming mode for business logic oriented programming.

  2. Code written using the ToyRoom library is easy to understand and maintain, no matter how the business logic changes.

  3. It is easy to send network requests and handle callbacks, especially to send concurrent and continuous requests.

  4. Easy to perform time-consuming tasks and handle callbacks.

  5. Provides powerful and rich APIS for data flow control and thread scheduling.

preview

The ToyRoom library is based on the Shelly library. Shelly is a business-logic-oriented programming library, which provides a brand new programming mode to express the influence of business object changes on each module through method chain.

With the ToyRoom library, you can create an object called “Domino” using a chain of methods. Each method in the method chain takes an action as an argument. Once the created Domino is invoked, each action is executed based on the action sequence in the method chain.

Before we get there, let’s look at an example.

Suppose you now want to print the names of all the files in your folder. Using the ToyRoom library, you can write the following code:

Shelly.createDomino("Print file names")
        .background()
        .flatMap((Function1) (input) -> {
                File[] files = new File(input).listFiles();
                List result = new ArrayList();
                for (File file : files) {
                    result.add(file.getName());
                return result;
        .perform((Action1) (input) -> {
                System.out.println(input);
        .commit();Copy the code

The above code prints the file name in the folder with a method chain. The path to the folder is passed in, and Function1 gets all the files in that path and passes the file names to Action1, which prints them out.

Let’s do a slightly more complicated example. Suppose now you want to send an HTTP request using Retrofit, and then

  1. If the response from the server is successful, the two functions in MyActivity and SecondActivity are called;

  2. If the server fails to respond, a toast is displayed on the screen;

  3. If an error or exception occurs while sending the request, print an error message.

Using the ToyRoom library, you can write the following code:

Shelly.createDomino("Sending request") .background() .beginRetrofitTask((RetrofitTask) (s) -> { return netInterface.test(s); .uiThread() .onSuccessResult(MainActivity.class, (TargetAction1) (mainActivity, input) -> { mainActivity.show(input.string()); .onSuccessResult(SecondActivity.class, (TargetAction1) (secondActivity, input) -> { secondActivity.show(input.string());  .onResponseFailure(MainActivity.class, (TargetAction1) (mainActivity, input) -> { Toast.makeText( mainActivity.getApplicationContext(), input.errorBody().string(), Toast.LENGTH_SHORT ).show(); .onFailure((Action1) (input) -> { Log.e("Eric Zhao", "Error", input); .endTask() .commit();Copy the code

A URL is passed in, Retrofit sends an HTTP request, and then performs an action based on the result.

There are some thread scheduling things in the code, such as background() and uiThread(). Background () means that the following operations are performed in the background. UiThread () means that the following operations are performed on the main thread (UI thread).

In the example above, you can see how MainActivity and SecondActivity change after sending the HTTP request. We can see the whole world change in one place.

Note that this code does not actually do anything if Domino is not called! All this code does is commit and store Domino for later use. In order for Domino to perform an operation, it must be called. Domino does not perform operations until it is called.

These are just simple examples. In fact, the ToyRoom library is quite powerful and will be covered in the next few sections.

Compared with RxJava

I discovered RxJva while developing the ToyRoom library. I then studied and learned about its ideas and implementation, so the ToyRoom library and RxJava are somewhat similar in style, but their ideas, implementation and usage are quite different, the details of which are described in the theory section.

Gradle

The compile 'me. Ele. Android: toyroom: while'Copy the code

usage

The specific usage is asynchronous – github.com/eleme/ToyRo… Here are the detailed DEMO and test cases.