After finishing the last article, I started to refactor my Android project code intermittently, and now it's done. During the refactoring and carefully read the source of some open source projects and articles, and asked some great God ideas, in accordance with the understanding of their own completion of the MVP structure of the reconstruction, and the Google Samples project roughly consistent, but not completely copied. This article focuses on some of the refactoring process to think about the problem, the specific code can be viewed on GitHub, the source of this article is branch1.1, before the refactoring is master, it is best to compare the differences between refactoring.
Thinking on the logic of multiple callback
A large number of articles are only read once the network and then use a listener interface to handle access status, this is the most common network access rules, as shown in:
Get access results in a listening way
But where does the actual production environment simply get the data for the list, and if the business logic needs multiple network access? To take a look at the login process, I will follow each step as follows:
Get token based on Umeng SDK Access, then proceed to the next step
1. Obtain tokens based on Umeng SDK access, and perform the next step successfully.
Get user profile (avatar, nickname) based on Umeng SDK, then perform next step
2. Obtain the user profile (avatar, nickname) based on the Umeng SDK, then proceed to the next step.
Send data to the server for login verification
3. Send the data sending server for login verification to complete the login business.
The logic is simple, but from the point of view of code implementation is not elegant. The next action in Onsuccess, if not written in another method function, then looks like a layer of structure, readability is poor, I think there are several techniques to maximize readability:
1.添加充足的注释2.用Handler(之前我是这么做的)3.用EventBus4.用RxJava
In this refactoring, I gave up the handler way, if the annotations are sufficient, handler actually does not improve readability, and from the perspective of the MVP architecture, before Handler's writing did not separate the view logic from the presenter logic, All put in the handler, should try to avoid.
Give up handler.
Eventbus is also not used in refactoring, because Eventbus's subscription model is more appropriate for an operation that needs to notify multiple processes (such as receiving new messages), otherwise it is more readable than the listener interface. Did not ascend too much.
I tend to use Rxjava, although I'm ready to use it in the next refactoring, but I've read a lot of articles. In the case of sufficient annotations, now the callback set of callback has been able to read, but not negligible thread safety and memory overflow issues, can be used Rxjava a good solution (which is also a reason to give up handler, Do you really think that the actual production of new one out can be no matter? Interested friends can wait for my next branch, I will say how I feel.
Does the MVP refactoring always stick to the end?
In the TODO example of Google sample, to open a new page from the list page, you need to click the floating button, so a simple thing to do in the onclick is split into three parts, which I think is actually a bit too much. Take a look at the following code:
Menu Bar button click event
If you click the button on the message list, you need to first clear the notification badage, then clear the unread number of preference, and then make a page jump. In accordance with the MVP architecture, clear notification badage should be implemented in view (i.e., one more method in activity); Clear unread quantity should be in the model's local (that is, the class that is responsible for local data operations) The jump should be implemented in the view (more than one method is written in the activity).
So in fact, in the onclick 3 lines of code, you need to thank so many support framework code, is it necessary? Let's look at an example of the model layer:
In the TODO example of Google sample, network access is separated from local access, a logical judgment of whether a piece of data is read locally or read from a network is written in repository. So no problem, the idea is very clear. But if there is no need for localization, can you write the code directly in the repository? (In fact, you can ...) It doesn't affect reading anyway)
The hierarchy is clean after refactoring
In the TODO example, the network and local operations are consistent (base additions and deletions), using interfaces to define the method. In the actual production (such as the login process), the local and network operations are relatively large, so should be based on the actual determination of whether the interface needs to define the method.
The TODO example uses a lot of IOC ideas, and if you don't use a di library like dagger2 or you want to do unit testing, consider simplifying it just as I instantiate presenter code.
After refactoring, where are the member variables of the data?
The details page requires a member variable that describes the product data, and the other user's introductory page needs a member variable that describes the user's data, a list that requires an array to hold the data, and an int to save the page now. So after the MVP structure refactoring, these variables should not still be defined in the activity, then the data should be placed on the MVP level?
Install the TODO example in the introduction, which contains 2 aspects of refactoring. First, the data variables should be written in the P-layer, and then the array operation should be written to adapter for the ListView-containing adapter control.
Presenter's constructor
Take me a presenter for example, because the injection is not considered, so the repository is directly instantiated in the constructor itself. The current list is the first page of the MPage variable, is placed in the presenter, each time the data is fetched directly into the view, and then call the adapter custom method in view to update the list (see Code for details).
Data operations for ListView are defined in adapter
In adapter, a few of the commonly used data manipulation functions, such as remove, replace, AddAll, GetItem and so on, are not enough to be added. When customizing these methods, pay particular attention to whether you have implemented the Header-view function yourself, and if so, calculate the actual location (in fact, a simple, one-sentence thing).
Something
In fact, I would like to follow the reconstruction of the MVP long what kind of thinking to write this article, but in the process of refactoring constantly read the code of TODO example, feel hold several key points, the idea of refactoring will be very clear:
1.各种数据操作都要放到M层中去;2.V层中只需要定义方法的时候把数据设为参数,不管数据从哪来,到哪去;3.P层负责告诉M层要读哪些数据(get)或者要向服务器发送什么数据(set),根据数据访问情况(成功或失败),将数据传给V层;
This article is more about trying to write down some of the thoughts in the process of refactoring and discussing it, this is not the end of this refactoring, and I'll continue to talk about how to move from this step to rxjava functional programming. Each refactoring is how to change, the code also gave, the idea also gave, the project is also the application store on-line products (although there is no money to promote the use of a few people), this series should be more than other articles of the Toy Code reference value higher bar. If you are interested in the big God pointing twos, or also in the forward of friends to discuss, then I also think the late night code is not wasted.
App Shelf Migration Guide (iii)