Skip to content

Commit

Permalink
1.1发布 加快首页列表加载速度
Browse files Browse the repository at this point in the history
  • Loading branch information
iMeiji committed Nov 25, 2016
1 parent 8a50aca commit 0671b6e
Show file tree
Hide file tree
Showing 91 changed files with 537 additions and 209 deletions.
14 changes: 7 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
buildToolsVersion '25.0.1'
defaultConfig {
applicationId "com.meiji.daily"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
versionName "1.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down Expand Up @@ -38,11 +38,11 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
compile 'com.android.support:recyclerview-v7:25.0.0'
compile 'com.android.support:cardview-v7:25.0.0'
compile 'com.android.support:support-v4:25.0.0'
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:design:25.0.1'
compile 'com.android.support:recyclerview-v7:25.0.1'
compile 'com.android.support:cardview-v7:25.0.1'
compile 'com.android.support:support-v4:25.0.1'
compile 'com.squareup.okhttp3:okhttp:3.4.2'
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
Expand Down
82 changes: 82 additions & 0 deletions app/src/main/java/com/meiji/daily/dao/ZhuanlanDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.meiji.daily.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.meiji.daily.db.ZhuanlanHelper;
import com.meiji.daily.mvp.zhuanlan.ZhuanlanBean;

import java.util.ArrayList;
import java.util.List;

import static com.meiji.daily.mvp.zhuanlan.ZhuanlanBean.ZHUANLANBEAN_AVATARId;
import static com.meiji.daily.mvp.zhuanlan.ZhuanlanBean.ZHUANLANBEAN_AVATARURL;
import static com.meiji.daily.mvp.zhuanlan.ZhuanlanBean.ZHUANLANBEAN_FOLLOWERSCOUNT;
import static com.meiji.daily.mvp.zhuanlan.ZhuanlanBean.ZHUANLANBEAN_INTRO;
import static com.meiji.daily.mvp.zhuanlan.ZhuanlanBean.ZHUANLANBEAN_NAME;
import static com.meiji.daily.mvp.zhuanlan.ZhuanlanBean.ZHUANLANBEAN_POSTSCOUNT;
import static com.meiji.daily.mvp.zhuanlan.ZhuanlanBean.ZHUANLANBEAN_SLUG;
import static com.meiji.daily.mvp.zhuanlan.ZhuanlanBean.ZHUANLANBEAN_TYPE;

/**
* Created by Meiji on 2016/11/25.
*/

public class ZhuanlanDao {

private Context mContext;

public ZhuanlanDao(Context mContext) {
this.mContext = mContext;
}

public boolean add(
String type,
String avatarUrl,
String avatarId,
String name,
String followersCount,
String postsCount,
String intro,
String slug) {
ZhuanlanHelper helper = new ZhuanlanHelper(mContext, 1);
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ZHUANLANBEAN_TYPE, type);
values.put(ZHUANLANBEAN_AVATARURL, avatarUrl);
values.put(ZHUANLANBEAN_AVATARId, avatarId);
values.put(ZHUANLANBEAN_NAME, name);
values.put(ZHUANLANBEAN_FOLLOWERSCOUNT, followersCount);
values.put(ZHUANLANBEAN_POSTSCOUNT, postsCount);
values.put(ZHUANLANBEAN_INTRO, intro);
values.put(ZHUANLANBEAN_SLUG, slug);
long id = db.insert("zhuanlan", null, values);
db.close();
return id != -1;
}

public List<ZhuanlanBean> query(int type) {
ZhuanlanHelper helper = new ZhuanlanHelper(mContext, 1);
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.query("zhuanlan", null, "type=?", new String[]{type + ""}, null, null, null);
List<ZhuanlanBean> list = new ArrayList<>();
while (cursor.moveToNext()) {
ZhuanlanBean bean = new ZhuanlanBean();
ZhuanlanBean.AvatarBeanX avatarBeanX = new ZhuanlanBean.AvatarBeanX();
avatarBeanX.setTemplate(cursor.getString(2));
avatarBeanX.setId(cursor.getString(3));
bean.setAvatar(avatarBeanX);
bean.setName(cursor.getString(4));
bean.setFollowersCount(Integer.parseInt(cursor.getString(5)));
bean.setPostsCount(Integer.parseInt(cursor.getString(6)));
bean.setIntro(cursor.getString(7));
bean.setSlug(cursor.getString(8));
list.add(bean);
}
cursor.close();
db.close();
return list;
}
}
50 changes: 50 additions & 0 deletions app/src/main/java/com/meiji/daily/db/ZhuanlanHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.meiji.daily.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import static com.meiji.daily.mvp.zhuanlan.ZhuanlanBean.ZHUANLANBEAN_AVATARId;
import static com.meiji.daily.mvp.zhuanlan.ZhuanlanBean.ZHUANLANBEAN_AVATARURL;
import static com.meiji.daily.mvp.zhuanlan.ZhuanlanBean.ZHUANLANBEAN_FOLLOWERSCOUNT;
import static com.meiji.daily.mvp.zhuanlan.ZhuanlanBean.ZHUANLANBEAN_INTRO;
import static com.meiji.daily.mvp.zhuanlan.ZhuanlanBean.ZHUANLANBEAN_NAME;
import static com.meiji.daily.mvp.zhuanlan.ZhuanlanBean.ZHUANLANBEAN_POSTSCOUNT;
import static com.meiji.daily.mvp.zhuanlan.ZhuanlanBean.ZHUANLANBEAN_SLUG;
import static com.meiji.daily.mvp.zhuanlan.ZhuanlanBean.ZHUANLANBEAN_TYPE;

/**
* Created by Meiji on 2016/11/25.
*/

public class ZhuanlanHelper extends SQLiteOpenHelper {

public static final String ZHUANLAN_TABLE = "zhuanlan";
private static final String BDNAME = "zhuanlan.db";
private static String createTable = "create table zhuanlan" +
"(" +
"id integer primary key autoincrement," +
ZHUANLANBEAN_TYPE + " varcher(10)," +
ZHUANLANBEAN_AVATARURL + " varcher(50)," +
ZHUANLANBEAN_AVATARId + " varcher(30)," +
ZHUANLANBEAN_NAME + " varcher(20)," +
ZHUANLANBEAN_FOLLOWERSCOUNT + " varcher(10)," +
ZHUANLANBEAN_POSTSCOUNT + " varcher(10)," +
ZHUANLANBEAN_INTRO + " varcher(30)," +
ZHUANLANBEAN_SLUG + " varcher(20)" +
")";

public ZhuanlanHelper(Context context, int version) {
super(context, BDNAME, null, version);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
* Created by Meiji on 2016/11/24.
*/

public interface IPostsContent {
interface IPostsContent {

interface View {

/**
* 加载网页
*/
void onSetWebView(String url);

/**
Expand All @@ -18,19 +21,42 @@ interface View {

interface Presenter {

/**
* 请求数据
*/
void doRequestData(int slug);

/**
* 设置浏览器
*/
void doSetWebView();

/**
* 请求数据失败
*/
void onFail();

/**
* 结束
*/
void onDestroy();
}

interface Model {

/**
* 请求数据
*/
boolean getRequestData(int slug);

/**
* 返回内容
*/
String getContent();

/**
* 结束
*/
void onDestroy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Created by Meiji on 2016/11/23.
*/

public class PostsContentModel implements IPostsContent.Model {
class PostsContentModel implements IPostsContent.Model {

private OkHttpClient client = new OkHttpClient();
private Call call;
Expand All @@ -23,14 +23,14 @@ public class PostsContentModel implements IPostsContent.Model {

@Override
public boolean getRequestData(int slug) {

boolean flag = false;
String url = Api.POST_URL + slug;
Request request = new Request.Builder()
.url(url)
.build();
Request request;
Response response;
try {
request = new Request.Builder()
.url(url)
.build();
call = client.newCall(request);
response = call.execute();
if (response.isSuccessful()) {
Expand All @@ -41,7 +41,6 @@ public boolean getRequestData(int slug) {
} catch (IOException e) {
e.printStackTrace();
}

return flag;
}

Expand All @@ -63,6 +62,8 @@ public String getContent() {

@Override
public void onDestroy() {
call.cancel();
if (call != null && call.isCanceled()) {
call.cancel();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
* Created by Meiji on 2016/11/23.
*/

public class PostsContentPresenter implements IPostsContent.Presenter {
class PostsContentPresenter implements IPostsContent.Presenter {

private IPostsContent.View view;
private IPostsContent.Model model;
private Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message message) {
if (message.what == 1) {
view.onSetWebView(model.getContent());
doSetWebView();
}
if (message.what == 2) {
if (message.what == 0) {
onFail();
}
return false;
}
});

public PostsContentPresenter(IPostsContent.View view) {
PostsContentPresenter(IPostsContent.View view) {
this.view = view;
this.model = new PostsContentModel();
}
Expand All @@ -35,22 +35,26 @@ public void doRequestData(final int slug) {
@Override
public void run() {
boolean result = model.getRequestData(slug);
System.out.println(result);
if (result) {
Message message = new Message();
Message message;
message = handler.obtainMessage();
message.what = 1;
message.sendToTarget();
} else {
Message message = new Message();
Message message;
message = handler.obtainMessage();
message.what = 2;
message.what = 0;
message.sendToTarget();
}
}
}).start();
}

@Override
public void doSetWebView() {
view.onSetWebView(model.getContent());
}

@Override
public void onFail() {
view.onFail();
Expand Down
Loading

0 comments on commit 0671b6e

Please sign in to comment.