Class Library Introduction Easyandroid
- Android is now a lot of frameworks, third-party libraries are also many, as a project, will consider how to integrate so many resources to achieve the rapid development of reuse.
- The framework for today is to integrate the mainstream Retrofit2 network layer encapsulation, fresco image loading, all using OKHTTP3 for network processing, using the MVP architecture, to quickly and flexibly develop and reuse our projects.
Frame Description Retrofit2
Why use it? Let's think about what we need to do if we don't use it, we can directly manipulate the OKHTTP3 network request. When sending the request, to encapsulate to be sent in a GET or post, and then come back Gjson parse into the entity object we need, but also look at the data corresponding to return code and so on, to properly parse our data, then RETROFIT2 is to effectively solve these things, Of course Retrofit2 more than these features, specific people can see the API.
Fresco
Compare some of the mainstream picture loading and a variety of comparisons, Fresco is a comprehensive comparison of good, support more, updated timely.
OKHTTP3
No, we're all using it.
Other class libraries
Are some of the more used, cookie automatic management, view injection butterknife, and Recycleview drop-down refresh Baserecyclerviewadapterhelper, Utilcode ToolPak, logger Log Library We used to be able to focus on the use of flexible
MVP Architecture Ideas
The following focus on the MVP of the clean mode, now MVP is very hot, big line, do not engage in a set of MVP architecture are feeling missing something, haha, why? is the MVP simply adding a presenter layer? Obviously not, it is a thought, if the simple one application each activity class in 500 rows to be done, still need MVP? I don't think it's a good fit. The larger the app, the more complex the app will make you feel like MVP. How do you get your code to work?
MVP Clean Mode
This is the clean architecture, why do we need to do it, we think, we put the business logic part of the presenter layer, if the page to do a lot of logic, more than 10 business operations (request data, database operations, etc.), This presenter will also be very large, since bloated, we continue to abstract, under the presenter layer in addition to use Cases domain layer, specialized in handling additions and deletions and other tasks are not clear, presenter only need to perform related usecases tasks , the callback processing will be OK after the operation.
MVP Dagger2 Mode
What is Dagger2 again? I want to say you wrote activity or fragment rely on Prensenter, then the clean mode Let presenter rely on Usecases, each time new an instance into, and then pass AH and so on, think all tired, So Dagger2 can help you build these instances, what you depend on, what I inject, that's the essence of it.
Dependency Injection, not only these, third-party libraries, and other providers can be injected, as long as you need, there are instances of our presenter object life cycle how you manage, usecases layer of objects, all this Dagger2 can help you do, you can customize the scopes, Java automatically handles it for you
And a little bit about why it's called Dagger2, version number 2, because it's 2, haha, because Dagger1 uses the runtime reflection mechanism, which affects performance, Dagger2 uses compile-time to generate the corresponding code automatically, the direct running performance will not be affected.
How to use this library demo code structure diagram
- Com.wjj.easy.easyandroidHelper
- Common
- Base
- Baseactivity
- Basefragment
- Basepresenter
- Baseview
- Simpleactivity
- Simplefragment
- Di
- Activitycommoncomponent
- Appcommoncomponent
- Appcommonmodule
- Fragmentcommoncomponent
- Net
- Model
- Module
- Login
- Loginactivity
- Logincontract
- Loginpresenter
- Utils
- Appapplication
Inside the app module, the common package contains a code demonstration that the business logic layer needs to implement, and you can copy it to your project, and the base package contains the basic activity, the fragment package, When we use it, we will automatically generate this presenter instance as long as we specify the required presenter at the time of inheritance.
publicclass LoginActivity extends BaseActivity<LoginPresenter>
DI package, is our Dagger2 use package, in here to provide our example, generate our universal injector, do not understand it does not matter, do not care, take the past with the line, only to do some presenter, task, global instance injection, other large can not use this, In order to increase the robustness of code, improve efficiency can not be the cart before the horse. All you have to do is inject the activity or fragment that we want to add in Activitycommoncomponent or fragmentcommoncomponent.
/** * Activity注入器 * @author wujiajun */@ActivityScope@Component(dependencies = AppCommonComponent.class, modules = {ActivityModule.class})publicinterface ActivityCommonComponent extends ActivityComponent { void inject(LoginActivity activity); void inject(MainActivity activity);}
NET is the Retrofit2 package Apiservice contains all the interface requests, including request parameters, request method, address, file upload, download, request format, can be annotated operation.
/** * 获取注册验证码 */ @FormUrlEncoded @POST"app/sendSMS.do") Call<BaseStatus> getVerifyCode(@Field("username"@Field("password") String pwd);
Apphttp is the invocation process of our interface.
publicvoidgetVerifyCodefinal Http.HttpCallback<BaseStatus> callback) { Call<BaseStatus> call = apiService.getVerifyCode(username, pwd); call.enqueue(new Http.CallbackDefault(callback)); call.request(); }
In the module, let's take login for example. Loginactivity's reliance on Loginpresenter, and Loginpresenter to the Logincontract.view, all in the class inheritance when the designation is good, will automatically help you build, do not need to pass.
/** * Login Activity * * @author wujiajun */ Public class loginactivity extends baseactivity<loginpresenter> Implements logincontract. View { @BindView(R.id.set_user_name) EditText Setusername;@BindView(R.ID.SET_PWD) EditText setpwd;@BindView(R.id.set_verify_code) EditText Setverifycode;@BindView(R.id.get_verify_code) TextView Getverifycode;@BindView(R.id.tv_login) TextView Tvlogin;@BindView(R.id.tool_bar) Toolbar Toolbar;@Override protected void Initinject() {getactivitycomponent (). Inject ( This); }@Override protected void Initeventanddata() {Toolbar.setlogo (R.mipmap.icon_app); Toolbar.settitle (R.string.app_name); Toolbar.settitletextcolor (Contextcompat.getcolor ( This, R.color.white)); Toolbar.settitlemarginstart (Getresources (). Getdimensionpixelsize (r.dimen.padding_size_30)); Setsupportactionbar (toolbar); }@Override protected int Getcontentview() {returnR.layout.activity_login; }@OnClick({r.id.get_verify_code, r.id.tv_login}) Public void OnClick(View view) {Switch(View.getid ()) { CaseR.id.get_verify_code:getpresenter (). Getverifycode (Setusername.gettext (). toString (), Setpwd.gettext (). toStr ing ()); StartActivity (NewIntent ( This, Mainactivity.class)); Break; CaseR.id.tv_login:getpresenter (). Login (Setusername.gettext (). ToString (), Setpwd.gettext (). ToString (), Setverif Ycode.gettext (). toString ()); Break; } }}
- The same is true of presenter, focusing on task execution
/** * Login Presenter * Created by Wujiajun on 17/4/7. * * Public class loginpresenter extends basepresenter<logincontract. View> implements logincontract. Presenter { @InjectGetverifycodetask Mgetverifycodetask;@InjectLogintask Mlogintask;@Inject Public Loginpresenter() { }@Override Public void Getverifycode(String userName, string pwd) {mgetverifycodetask.setusername (userName); Mgetverifycodetask.setpwd (PWD); Mgetverifycodetask.setcallback (NewUsecase.callback<basestatus> () {@Override Public void Success(Basestatus basestatus) {GetView (). Toast ("Getverifycode success!"); }@Override Public void fail() {GetView (). Toast ("Getverifycode fail!"); } }); Getthreadexecutor (). Execute (mgetverifycodetask); }@Override Public void Login(String userName, string pwd, String verifycode) {GetView (). showloading (); Mlogintask.setusername (UserName); Mlogintask.setpwd (PWD); Mlogintask.setverifycode (Verifycode); Mlogintask.setcallback (NewUsecase.callback<loginresponse> () {@Override Public void Success(Loginresponse loginresponse) {GetView (). Toast ("Login success!"); }@Override Public void fail() {GetView (). Toast ("Login fail!"); } }); Getthreadexecutor (). Execute (mlogintask); }}
Domain layer only focuses on how to complete this task request verification code or login operation
/** * Get Verification Code Task * * @author wujiajun */ Public class getverifycodetask extends abstractusecase {String PublicKey ="One"; String UserName; String pwd;@InjectApphttp apphttp;@Inject Public Getverifycodetask() { }@Override Public void Run() {Apphttp.getverifycode (UserName, Secretutils.encryptbypublickey (pwd, PublicKey),NewHttp.httpcallback<basestatus> () {@Override Public void Onresponse(Basestatus basestatus) {Getcallback (). Success (Basestatus); }@Override Public void onfailure(Throwable t) {Getcallback (). fail (); } }); } Public void Setusername(String userName) { This. userName = UserName; } Public void setpwd(String pwd) { This. pwd = pwd; }}
Epilogue easyandroid The main parts of the framework are introduced one by one, as well as its ideas, about the MVP of these I also refer to a lot, a lot of simple demo, really can use very little, write this also want to everyone less detours, hope that the framework can be perfected, at least the content of the demo is very little, Interested can fork modify, what good advice can be private chat me. GitHub Address
Https://github.com/wu928320442/EasyAndroid
The environment you need
- JDK1.8
- Sdk
- Androidstudio Development Tools
Characteristics
- A fast and efficient development framework that integrates the mainstream HTTP network, image loading, MVP (CLEAN+DAGGER2) architecture
- Includes app library two module modular development
Third-party framework used
- SDK comes with extended dependency package
- RETROFIT2 Network layer processing using OKHTTP3 processing
- Fresco picture loading processing using OKHTTP3 processing
- OKHTTP3 HTTP Base library for Network layer processing and picture loading
- Persistentcookiejar Fast cookie persistence and cache library
- DAGGER2 relies on injection libraries to integrate dependencies between Activity,fragment,presenter,task
- Butterknife View's injection library
- Baserecyclerviewadapterhelper recycler drop-down loading library
- Utilcode Utility Library
- Logger a simple and beautiful log print library
Class Library Import
‘com.wjj.easy:easyandroid:1.0.0‘
Contact information
- QQ 928320442
- Email [email protected]
Resources
- Https://github.com/googlesamples/android-architecture
- Https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html
- https://google.github.io/dagger/
- Https://github.com/Blankj/AndroidUtilCode
- Https://github.com/franmontiel/PersistentCookieJar
- Https://github.com/codeestX/GeekNews
- Https://github.com/JakeWharton/butterknife
- Https://github.com/square/retrofit
- Https://github.com/CymChad/BaseRecyclerViewAdapterHelper
- Https://github.com/android10/Android-CleanArchitecture
- Http://www.jianshu.com/p/cd2c1c9f68d4
[Android Open source] a fast and efficient development framework Easyandroid