Skip to content

Commit

Permalink
Add android-mvp demo
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengxiaopeng committed Feb 7, 2015
1 parent bb23855 commit bcba4cd
Show file tree
Hide file tree
Showing 21 changed files with 721 additions and 52 deletions.
7 changes: 7 additions & 0 deletions android-mvp/android-mvp.iml
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,19 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 21 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="gson-2.1-javadoc" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-21.0.3" level="project" />
<orderEntry type="library" exported="" name="gson-2.1-sources" level="project" />
<orderEntry type="library" exported="" name="support-annotations-21.0.3" level="project" />
<orderEntry type="library" exported="" name="dagger-1.2.2" level="project" />
<orderEntry type="library" exported="" name="support-v4-21.0.3" level="project" />
<orderEntry type="library" exported="" name="javax.inject-1" level="project" />
<orderEntry type="library" exported="" name="gson-2.1" level="project" />
<orderEntry type="library" exported="" name="library-aar-1.0.0" level="project" />
</component>
</module>

2 changes: 2 additions & 0 deletions android-mvp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.mcxiaoke.volley:library-aar:1.0.0'
compile 'com.squareup.dagger:dagger:1.2.2'
}
Binary file added android-mvp/libs/gson-2.1-javadoc.jar
Binary file not shown.
Binary file added android-mvp/libs/gson-2.1-sources.jar
Binary file not shown.
Binary file added android-mvp/libs/gson-2.1.jar
Binary file not shown.
10 changes: 6 additions & 4 deletions android-mvp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.rocko.demos.mvp" >
package="org.rocko.demos.mvp">

<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".app.MVPDemoApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
android:name=".ui.activity.WeatherActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
39 changes: 0 additions & 39 deletions android-mvp/src/main/java/org/rocko/demos/mvp/MainActivity.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.rocko.demos.mvp.app;

import android.app.Application;

import com.android.volley.toolbox.Volley;

import org.rocko.demos.mvp.util.volley.VolleyRequest;

/**
* Created by Administrator on 2015/2/6.
* 替换默认的Application实现
*/
public class MVPDemoApplication extends Application {
private static MVPDemoApplication instance;

public MVPDemoApplication() {
instance = this;
}

public static Application getContext() {
return instance;
}

@Override
public void onCreate() {
super.onCreate();
VolleyRequest.buildRequestQueue(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.rocko.demos.mvp.model;

import org.rocko.demos.mvp.presenter.OnWeatherListener;

/**
* Created by Administrator on 2015/2/6.
* 天气Model接口
*/
public interface WeatherModel {
void loadWeather(String cityNO, OnWeatherListener listener);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.rocko.demos.mvp.model.entity;

/**
* Created by Administrator on 2015/2/6.
* 天气实体类
*/
public class Weather {
private WeatherInfo weatherinfo;

public WeatherInfo getWeatherinfo() {
return weatherinfo;
}

public void setWeatherinfo(WeatherInfo weatherinfo) {
this.weatherinfo = weatherinfo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.rocko.demos.mvp.model.entity;

/**
* Created by Administrator on 2015/2/6.
*/
public class WeatherInfo {
private String city;
private String cityid;
private String temp;
private String WD;
private String WS;
private String SD;
private String WSE;
private String time;
private String njd;

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getCityid() {
return cityid;
}

public void setCityid(String cityid) {
this.cityid = cityid;
}

public String getTemp() {
return temp;
}

public void setTemp(String temp) {
this.temp = temp;
}

public String getWD() {
return WD;
}

public void setWD(String WD) {
this.WD = WD;
}

public String getWS() {
return WS;
}

public void setWS(String WS) {
this.WS = WS;
}

public String getSD() {
return SD;
}

public void setSD(String SD) {
this.SD = SD;
}

public String getWSE() {
return WSE;
}

public void setWSE(String WSE) {
this.WSE = WSE;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}

public String getNjd() {
return njd;
}

public void setNjd(String njd) {
this.njd = njd;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.rocko.demos.mvp.model.impl;

import android.util.Log;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;

import org.rocko.demos.mvp.model.WeatherModel;
import org.rocko.demos.mvp.model.entity.Weather;
import org.rocko.demos.mvp.presenter.OnWeatherListener;
import org.rocko.demos.mvp.util.volley.VolleyRequest;

/**
* Created by Administrator on 2015/2/6.
* 天气Model实现
*/
public class WeatherModelImpl implements WeatherModel {
@Override
public void loadWeather(String cityNO, final OnWeatherListener listener) {
/*数据层操作*/
VolleyRequest.newInstance().newGsonRequest("http://www.weather.com.cn/data/sk/" + cityNO + ".html",
Weather.class, new Response.Listener<Weather>() {
@Override
public void onResponse(Weather weather) {
if (weather != null) {
listener.onSuccess(weather);
} else {
listener.onError();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
listener.onError();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.rocko.demos.mvp.presenter;

import org.rocko.demos.mvp.model.entity.Weather;

/**
* Created by Administrator on 2015/2/7.
* 在Presenter层实现,给Model层回调,更改View层的状态,确保Model层不直接操作View层
*/
public interface OnWeatherListener {
/**
* 成功时回调
*
* @param weather
*/
void onSuccess(Weather weather);
/**
* 失败时回调,简单处理,没做什么
*/
void onError();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.rocko.demos.mvp.presenter;

/**
* Created by Administrator on 2015/2/6.
* 天气 Presenter接口
*/
public interface WeatherPresenter {
/**
* 获取天气的逻辑
*/
void getWeather(String cityNO);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.rocko.demos.mvp.presenter.impl;

import org.rocko.demos.mvp.model.WeatherModel;
import org.rocko.demos.mvp.model.entity.Weather;
import org.rocko.demos.mvp.model.impl.WeatherModelImpl;
import org.rocko.demos.mvp.presenter.OnWeatherListener;
import org.rocko.demos.mvp.presenter.WeatherPresenter;
import org.rocko.demos.mvp.ui.view.WeatherView;

/**
* Created by Administrator on 2015/2/6.
* 天气 Prestener实现
*/
public class WeatherPresenterImpl implements WeatherPresenter, OnWeatherListener {
/*Presenter作为中间层,持有View和Model的引用*/
private WeatherView weatherView;
private WeatherModel weatherModel;

public WeatherPresenterImpl(WeatherView weatherView) {
this.weatherView = weatherView;
weatherModel = new WeatherModelImpl();
}

@Override
public void getWeather(String cityNO) {
weatherView.showLoading();
weatherModel.loadWeather(cityNO, this);
}

@Override
public void onSuccess(Weather weather) {
weatherView.hideLoading();
weatherView.setWeatherInfo(weather);
}

@Override
public void onError() {
weatherView.hideLoading();
weatherView.showError();
}
}
Loading

0 comments on commit bcba4cd

Please sign in to comment.