首页启动性能优化-获取到了新数据后 要更新界面
This commit is contained in:
@ -22,4 +22,6 @@ class SubjectEntity {
|
||||
var tag: String? = null
|
||||
|
||||
var data: List<GameEntity>? = null
|
||||
|
||||
var isCache: Boolean = false
|
||||
}
|
||||
|
||||
@ -71,6 +71,7 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import okhttp3.Interceptor;
|
||||
import retrofit2.HttpException;
|
||||
import rx.android.schedulers.AndroidSchedulers;
|
||||
import rx.functions.Func1;
|
||||
@ -96,6 +97,8 @@ public class GameFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
|
||||
private ArrayMap<String, ArrayList<Integer>> mPluginLocationMap; // 插件化模块 包名对应游戏位置
|
||||
private ArrayMap<String, List<GameEntity>> mSubjectChangedMap; //存储换一换的数据
|
||||
|
||||
private GamePluginAdapter mPluginAdapter;
|
||||
|
||||
private int mItemCount;
|
||||
|
||||
private boolean mIsNetworkError;
|
||||
@ -105,10 +108,10 @@ public class GameFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
|
||||
private boolean mIsOver;
|
||||
private boolean mIsInitPlugin;
|
||||
private boolean mIsAutoScroll;
|
||||
|
||||
private GamePluginAdapter mPluginAdapter;
|
||||
private boolean isOpenPluginList;
|
||||
|
||||
private String mCacheRequestTag;// 为空优先度缓存, 不为空读服务器数据
|
||||
|
||||
public GameFragmentAdapter(GameFragment gameFragment, SwipeRefreshLayout refreshLayout) {
|
||||
super(gameFragment.getContext());
|
||||
|
||||
@ -135,6 +138,7 @@ public class GameFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
|
||||
mIsOver = false;
|
||||
mIsInitPlugin = false;
|
||||
mIsAutoScroll = false;
|
||||
mCacheRequestTag = null;
|
||||
|
||||
if (MainActivity.isNewFirstLaunch) {
|
||||
isOpenPluginList = true;
|
||||
@ -174,7 +178,7 @@ public class GameFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
|
||||
|
||||
public void initSubjectDigest(final boolean isLoadMore) {
|
||||
RetrofitManager.getInstance(mContext).getApi()
|
||||
.getSubjectDigest(null)
|
||||
.getSubjectDigest(mCacheRequestTag)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<List<SubjectDigestEntity>>() {
|
||||
@ -194,6 +198,7 @@ public class GameFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
if (!TextUtils.isEmpty(mCacheRequestTag)) return;
|
||||
mIsSubjectDigestError = true;
|
||||
notifyItemChanged(0);
|
||||
if (isLoadMore) {
|
||||
@ -204,7 +209,7 @@ public class GameFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
|
||||
}
|
||||
|
||||
private void initSlide(final boolean isFirst) {
|
||||
RetrofitManager.getInstance(mContext).getApi().getSlide(null)
|
||||
RetrofitManager.getInstance(mContext).getApi().getSlide(mCacheRequestTag)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<List<SlideEntity>>() {
|
||||
@ -221,6 +226,7 @@ public class GameFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
if (!TextUtils.isEmpty(mCacheRequestTag)) return;
|
||||
mIsSlideError = true;
|
||||
showView();
|
||||
if (isFirst) {
|
||||
@ -231,7 +237,7 @@ public class GameFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
|
||||
}
|
||||
|
||||
private void initSubjectList() {
|
||||
RetrofitManager.getInstance(mContext).getApi().getColumn(null)
|
||||
RetrofitManager.getInstance(mContext).getApi().getColumn(mCacheRequestTag)
|
||||
.map(new Func1<List<SubjectEntity>, List<SubjectEntity>>() {
|
||||
@Override
|
||||
public List<SubjectEntity> call(List<SubjectEntity> list) {
|
||||
@ -300,16 +306,21 @@ public class GameFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
|
||||
notifyItemChanged(getItemCount() - 1);
|
||||
|
||||
initPlugin();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
super.onError(e);
|
||||
Utils.log("========EEEE::" + e.toString());
|
||||
/**
|
||||
* {@link com.gh.gamecenter.retrofit.OkHttpCacheInterceptor#intercept(Interceptor.Chain)}
|
||||
*/
|
||||
if (response.size() > 0 && response.get(0).isCache()) {
|
||||
mCacheRequestTag = "retry";
|
||||
initSubjectDigest(true);
|
||||
} else {
|
||||
mCacheRequestTag = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
if (!TextUtils.isEmpty(mCacheRequestTag)) return;
|
||||
if (mCallBackListener != null) {
|
||||
mCallBackListener.loadDone();
|
||||
}
|
||||
|
||||
@ -10,9 +10,12 @@ import com.gh.common.util.LoginUtils;
|
||||
import com.gh.common.util.NetworkUtils;
|
||||
import com.gh.common.util.StringUtils;
|
||||
import com.gh.common.util.TimestampUtils;
|
||||
import com.gh.gamecenter.entity.SubjectEntity;
|
||||
import com.lightgame.utils.Utils;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
@ -20,7 +23,6 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import okhttp3.Cache;
|
||||
import okhttp3.CacheControl;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.Interceptor;
|
||||
@ -29,7 +31,6 @@ import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import okio.Buffer;
|
||||
import rx.schedulers.Schedulers;
|
||||
|
||||
/**
|
||||
* Created by LGT on 2016/11/8.
|
||||
@ -129,6 +130,19 @@ class OkHttpCacheInterceptor implements Interceptor {
|
||||
.cacheControl(CacheControl.FORCE_CACHE)
|
||||
.build();
|
||||
|
||||
if (url.contains("/index/column")) { // 手动添加isCache 字段 表示这是缓存数据
|
||||
try {
|
||||
JSONArray jsonArray = new JSONArray(new String(data));
|
||||
if (jsonArray.length() > 0) {
|
||||
JSONObject object = (JSONObject) jsonArray.get(0);
|
||||
object.put("isCache", true);
|
||||
data = jsonArray.toString().getBytes();
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
data = GzipUtils.decompressBytes(data);
|
||||
response = chain.proceed(request);
|
||||
response = response.newBuilder()
|
||||
@ -136,22 +150,6 @@ class OkHttpCacheInterceptor implements Interceptor {
|
||||
.message("OK")
|
||||
.body(ResponseBody.create(MediaType.parse("application/json"), data))
|
||||
.build();
|
||||
if (url.contains("/index/column")) {
|
||||
RetrofitManager.getInstance(mContext).getApi()
|
||||
.getColumn("retry")
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe(new com.gh.gamecenter.retrofit.Response<>());
|
||||
} else if (url.contains("/index/slides")) {
|
||||
RetrofitManager.getInstance(mContext).getApi()
|
||||
.getSlide("retry")
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe(new com.gh.gamecenter.retrofit.Response<>());
|
||||
} else if (url.contains("/index/recommend")) {
|
||||
RetrofitManager.getInstance(mContext).getApi()
|
||||
.getSubjectDigest("retry")
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe(new com.gh.gamecenter.retrofit.Response<>());
|
||||
}
|
||||
} else {
|
||||
response = chain.proceed(request);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user