github地址:https://github.com/yuanfen7650/FinalMvp
使用各種架構(gòu)的目的無(wú)非是讓代碼變的簡(jiǎn)潔,易讀昧辽。并且在多人開發(fā)中可以展現(xiàn)出無(wú)限的魅力衙熔。不同的層可以讓不同的人開發(fā),互相獨(dú)立并互相影響搅荞!
框架就是將原本需要一大堆代碼的統(tǒng)一起來(lái),來(lái)簡(jiǎn)化代碼的編輯红氯。
# mvp
Model-View-Presenter的簡(jiǎn)寫,mvp就是一種設(shè)計(jì)模式咕痛,不懂的自己百度一下痢甘。
# Finalmvp
我們?cè)贏ndroid開發(fā)中用的最多的無(wú)非就是多線程,為了避免手機(jī)卡頓茉贡,我們?cè)谧龊臅r(shí)操作時(shí)必須在子線程中執(zhí)行产阱,而執(zhí)行完成后又需要在主線程中去做UI操作。
這樣就會(huì)需要寫很多的代碼块仆,并且還要考慮子線程主線程的關(guān)系构蹬,邏輯復(fù)雜了之后就容易產(chǎn)生一些意想不到的bug讓我們頭疼王暗。
這里需要感謝rxjava,讓多線程變得不用我們管庄敛,哈哈俗壹!沒(méi)錯(cuò),finalmvp使用了rxjava進(jìn)行多線程處理藻烤,并將rxjava和mvp完美結(jié)合绷雏。
我們寫代碼的時(shí)候無(wú)需關(guān)心rxjava,只需要根據(jù)
規(guī)則編輯代碼怖亭,便可以編輯出簡(jiǎn)潔易讀并且性能優(yōu)異的優(yōu)秀代碼涎显,哈哈- -
那就讓我們來(lái)看看這個(gè)神秘的finalmvp吧!
# 上代碼
這里做一個(gè)查詢天氣的demo
在使用前需要rxJava依賴
implementation 'io.reactivex.rxjava2:rxjava:2.2.2'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
1.MainView
public interface MainView {
/**
* 顯示天氣的文字
*/
void showWeatherText(String text);
}
2.MainActivity需要實(shí)現(xiàn)MainView接口,并且需要在class上加上@View注解以表示是View層
@View
public class MainActivity extends AppCompatActivity implements MainView{
@Autowired
MainPresenter mainPresenter;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.textView);
Finalmvp.init(this);//初始化finalmvp,框架自動(dòng)掃描注解
}
@Override
public void showWeatherText(String text) {
textView.setText(text);
}
}
3.Presenter
public interface MainPresenter {
void loadWeather();
}
4.MainPresenterImpl實(shí)現(xiàn)Presenter,并且需要在class上方加上注解@Presenter,以表示是Presenter層
@Presenter
public class MainPresenterImpl implements MainPresenter{
@Autowired
MainView mainView;
@Autowired
MainModel mainModel;
@Override
public void loadWeather() {
/**
* 此處做數(shù)據(jù)處理兴猩,處理完后期吓,在主動(dòng)讓view去修改UI
*/
Weather weather=mainModel.loadWeatherFromUrl();//獲取天氣預(yù)報(bào)信息
String reslut="";
if(weather!=null){
reslut="城市:"+weather.getCity()+" ?最低溫度:"+weather.getTemp1()+" ? 最高溫度:"+weather.getTemp2()+" ? ?天氣情況:"+weather.getWeather();
}
mainView.showWeatherText(reslut);
}
}
5.MainModel直接新建一個(gè)model類加上@Model注解
@Model
public class MainModel {
/**
* 從服務(wù)器獲取天氣數(shù)據(jù),對(duì)數(shù)據(jù)進(jìn)行基本處理倾芝,轉(zhuǎn)成需要的格式(如json讨勤,bean,string等)
* 不需要多線程晨另,直接同步執(zhí)行就行了
*/
public Weather loadWeatherFromUrl(){
String jsonStr=doHttp("http://www.weather.com.cn/data/cityinfo/101010100.html");
JSONObject jo=JSON.parseObject(jsonStr);
JSONObject weatherinfo=jo.getJSONObject("weatherinfo");
Weather weather=weatherinfo.toJavaObject(Weather.class);
return weather;
}
/**
* 執(zhí)行http請(qǐng)求
* 這里只是為演示使用潭千,建議自己使用okhttp等框架
* 需要同步執(zhí)行,不需要使用框架內(nèi)的異步操作
*/
public static String doHttp(String urlStr) {
try {
URL u = new URL(urlStr);
InputStream in = u.openStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
byte buf[] = new byte[1024];
int read;
while ((read = in.read(buf)) > 0) {
out.write(buf, 0, read);
}
} finally {
if (in != null) {
in.close();
}
}
byte b[] = out.toByteArray();
String result=new String(b, "utf-8");
return result;
}catch (Exception e){
e.printStackTrace();
}
return "aaaaa";
}
}
# 提醒
代碼看到這里有人就有疑問(wèn)了借尿,為什么請(qǐng)求http沒(méi)有在子線程中執(zhí)行刨晴,哈哈!告訴你們?cè)赑resenter運(yùn)行的時(shí)候就已經(jīng)在子線程了路翻。
# 完結(jié)
到這里就完成了割捅,這樣就層次很清晰了。并且不需要操作任何多線程的代碼帚桩,但實(shí)際在Presenter的時(shí)候就已經(jīng)在子線程執(zhí)行了!
# 使用方法:
1.Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
2.Add the dependency
dependencies {
implementation 'com.github.yuanfen7650:Finalmvp:1.9.5'
}