Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
门心叼龙
MVPArms
提交
dc41131d
M
MVPArms
项目概览
门心叼龙
/
MVPArms
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
MVPArms
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
dc41131d
编写于
1月 17, 2017
作者:
J
jessyan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
improve cache
上级
7a4efbee
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
51 addition
and
39 deletion
+51
-39
app/src/main/java/me/jessyan/mvparms/demo/mvp/presenter/UserPresenter.java
.../me/jessyan/mvparms/demo/mvp/presenter/UserPresenter.java
+15
-5
arms/src/main/java/com/jess/arms/di/module/ClientModule.java
arms/src/main/java/com/jess/arms/di/module/ClientModule.java
+18
-13
arms/src/main/java/com/jess/arms/utils/DataHelper.java
arms/src/main/java/com/jess/arms/utils/DataHelper.java
+14
-3
arms/src/main/java/com/jess/arms/utils/PermissionUtil.java
arms/src/main/java/com/jess/arms/utils/PermissionUtil.java
+0
-17
arms/src/main/java/com/jess/arms/widget/imageloader/glide/GlideConfiguration.java
...ess/arms/widget/imageloader/glide/GlideConfiguration.java
+4
-1
未找到文件。
app/src/main/java/me/jessyan/mvparms/demo/mvp/presenter/UserPresenter.java
浏览文件 @
dc41131d
...
...
@@ -36,6 +36,7 @@ public class UserPresenter extends BasePresenter<UserContract.Model, UserContrac
private
List
<
User
>
mUsers
=
new
ArrayList
<>();
private
DefaultAdapter
mAdapter
;
private
int
lastUserId
=
1
;
private
boolean
isFirst
=
true
;
@Inject
...
...
@@ -59,9 +60,18 @@ public class UserPresenter extends BasePresenter<UserContract.Model, UserContrac
},
mRootView
.
getRxPermissions
(),
mRootView
,
mErrorHandler
);
if
(
pullToRefresh
)
lastUserId
=
1
;
if
(
pullToRefresh
)
lastUserId
=
1
;
//上拉刷新默认只请求第一页
mModel
.
getUsers
(
lastUserId
,
pullToRefresh
)
//关于RxCache缓存库的使用请参考 http://www.jianshu.com/p/b58ef6b0624b
boolean
isEvictCache
=
pullToRefresh
;
//是否驱逐缓存,为ture即不使用缓存,每次上拉刷新即需要最新数据,则不使用缓存
if
(
pullToRefresh
&&
isFirst
){
//默认在第一次上拉刷新时使用缓存
isFirst
=
false
;
isEvictCache
=
false
;
}
mModel
.
getUsers
(
lastUserId
,
isEvictCache
)
.
subscribeOn
(
Schedulers
.
io
())
.
retryWhen
(
new
RetryWithDelay
(
3
,
2
))
//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
.
doOnSubscribe
(
new
Action0
()
{
...
...
@@ -70,7 +80,7 @@ public class UserPresenter extends BasePresenter<UserContract.Model, UserContrac
if
(
pullToRefresh
)
mRootView
.
showLoading
();
//显示上拉刷新的进度条
else
mRootView
.
startLoadMore
();
mRootView
.
startLoadMore
();
//显示下拉加载更多的进度条
}
}).
subscribeOn
(
AndroidSchedulers
.
mainThread
())
.
observeOn
(
AndroidSchedulers
.
mainThread
())
...
...
@@ -80,7 +90,7 @@ public class UserPresenter extends BasePresenter<UserContract.Model, UserContrac
if
(
pullToRefresh
)
mRootView
.
hideLoading
();
//隐藏上拉刷新的进度条
else
mRootView
.
endLoadMore
();
mRootView
.
endLoadMore
();
//隐藏下拉加载更多的进度条
}
})
.
compose
(
RxUtils
.<
List
<
User
>>
bindToLifecycle
(
mRootView
))
//使用RXlifecycle,使subscription和activity一起销毁
...
...
@@ -88,7 +98,7 @@ public class UserPresenter extends BasePresenter<UserContract.Model, UserContrac
@Override
public
void
onNext
(
List
<
User
>
users
)
{
lastUserId
=
users
.
get
(
users
.
size
()
-
1
).
getId
();
//记录最后一个id,用于下一次请求
if
(
pullToRefresh
)
mUsers
.
clear
();
//如果是上拉刷新则
晴
空列表
if
(
pullToRefresh
)
mUsers
.
clear
();
//如果是上拉刷新则
清
空列表
for
(
User
user
:
users
)
{
mUsers
.
add
(
user
);
}
...
...
arms/src/main/java/com/jess/arms/di/module/ClientModule.java
浏览文件 @
dc41131d
...
...
@@ -4,11 +4,13 @@ import android.app.Application;
import
com.jess.arms.base.AppManager
;
import
com.jess.arms.http.RequestIntercept
;
import
com.jess.arms.utils.DataHelper
;
import
java.io.File
;
import
java.util.List
;
import
java.util.concurrent.TimeUnit
;
import
javax.inject.Named
;
import
javax.inject.Singleton
;
import
dagger.Module
;
...
...
@@ -17,7 +19,6 @@ import io.rx_cache.internal.RxCache;
import
io.victoralbertos.jolyglot.GsonSpeaker
;
import
me.jessyan.rxerrorhandler.core.RxErrorHandler
;
import
me.jessyan.rxerrorhandler.handler.listener.ResponseErroListener
;
import
okhttp3.Cache
;
import
okhttp3.HttpUrl
;
import
okhttp3.Interceptor
;
import
okhttp3.OkHttpClient
;
...
...
@@ -31,7 +32,6 @@ import retrofit2.converter.gson.GsonConverterFactory;
@Module
public
class
ClientModule
{
private
static
final
int
TIME_OUT
=
10
;
public
static
final
int
HTTP_RESPONSE_DISK_CACHE_MAX_SIZE
=
10
*
1024
*
1024
;
//缓存文件最大值为10Mb
private
AppManager
mAppManager
;
...
...
@@ -67,12 +67,11 @@ public class ClientModule {
*/
@Singleton
@Provides
OkHttpClient
provideClient
(
OkHttpClient
.
Builder
okHttpClient
,
Cache
cache
,
Interceptor
intercept
OkHttpClient
provideClient
(
OkHttpClient
.
Builder
okHttpClient
,
Interceptor
intercept
,
List
<
Interceptor
>
interceptors
)
{
OkHttpClient
.
Builder
builder
=
okHttpClient
.
connectTimeout
(
TIME_OUT
,
TimeUnit
.
SECONDS
)
.
readTimeout
(
TIME_OUT
,
TimeUnit
.
SECONDS
)
.
cache
(
cache
)
//设置缓存
.
addNetworkInterceptor
(
intercept
);
if
(
interceptors
!=
null
&&
interceptors
.
size
()
>
0
)
{
//如果外部提供了interceptor的数组则遍历添加
for
(
Interceptor
interceptor
:
interceptors
)
{
...
...
@@ -98,11 +97,6 @@ public class ClientModule {
}
@Singleton
@Provides
Cache
provideCache
(
File
cacheFile
)
{
return
new
Cache
(
cacheFile
,
HTTP_RESPONSE_DISK_CACHE_MAX_SIZE
);
//设置缓存路径和大小
}
@Singleton
...
...
@@ -115,18 +109,30 @@ public class ClientModule {
/**
* 提供RXCache客户端
*
* @param cacheDir
缓存路径
* @param cacheDir
ectory RxCache
缓存路径
* @return
*/
@Singleton
@Provides
RxCache
provideRxCache
(
File
cacheDir
)
{
RxCache
provideRxCache
(
@Named
(
"RxCacheDirectory"
)
File
cacheDirectory
)
{
return
new
RxCache
.
Builder
()
.
persistence
(
cacheDir
,
new
GsonSpeaker
());
.
persistence
(
cacheDir
ectory
,
new
GsonSpeaker
());
}
/**
* 需要单独给RxCache提供缓存路径
* 提供RxCache缓存地址
*/
@Singleton
@Provides
@Named
(
"RxCacheDirectory"
)
File
provideRxCacheDirectory
(
File
cacheDir
)
{
File
cacheDirectory
=
new
File
(
cacheDir
,
"RxCache"
);
return
DataHelper
.
makeDirs
(
cacheDirectory
);
}
/**
* 提供处理Rxjava错误的管理器
*
...
...
@@ -143,7 +149,6 @@ public class ClientModule {
}
/**
* 提供管理所有activity的管理类
*
...
...
arms/src/main/java/com/jess/arms/utils/DataHelper.java
浏览文件 @
dc41131d
...
...
@@ -165,9 +165,7 @@ public class DataHelper {
file
=
context
.
getExternalCacheDir
();
//获取系统管理的sd卡缓存文件
if
(
file
==
null
)
{
//如果获取的为空,就是用自己定义的缓存文件夹做缓存路径
file
=
new
File
(
getCacheFilePath
(
context
));
if
(!
file
.
exists
())
{
file
.
mkdirs
();
}
makeDirs
(
file
);
}
return
file
;
}
else
{
...
...
@@ -185,6 +183,19 @@ public class DataHelper {
return
"/mnt/sdcard/"
+
packageName
;
}
/**
* 创建未存在的文件夹
* @param file
* @return
*/
public
static
File
makeDirs
(
File
file
){
if
(!
file
.
exists
())
{
file
.
mkdirs
();
}
return
file
;
}
/**
* 使用递归获取目录文件大小
*
...
...
arms/src/main/java/com/jess/arms/utils/PermissionUtil.java
浏览文件 @
dc41131d
...
...
@@ -2,11 +2,8 @@ package com.jess.arms.utils;
import
android.Manifest
;
import
com.jess.arms.base.BaseActivity
;
import
com.jess.arms.base.BaseFragment
;
import
com.jess.arms.mvp.BaseView
;
import
com.tbruyelle.rxpermissions.RxPermissions
;
import
com.trello.rxlifecycle.LifecycleTransformer
;
import
me.jessyan.rxerrorhandler.core.RxErrorHandler
;
import
me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber
;
...
...
@@ -26,16 +23,6 @@ public class PermissionUtil {
void
onRequestPermissionSuccess
();
}
public
static
<
T
>
LifecycleTransformer
<
T
>
bindToLifecycle
(
BaseView
view
)
{
if
(
view
instanceof
BaseActivity
)
{
return
((
BaseActivity
)
view
).<
T
>
bindToLifecycle
();
}
else
if
(
view
instanceof
BaseFragment
)
{
return
((
BaseFragment
)
view
).<
T
>
bindToLifecycle
();
}
else
{
throw
new
IllegalArgumentException
(
"view isn't activity or fragment"
);
}
}
/**
...
...
@@ -55,7 +42,6 @@ public class PermissionUtil {
rxPermissions
.
request
(
Manifest
.
permission
.
WRITE_EXTERNAL_STORAGE
,
Manifest
.
permission
.
CAMERA
)
.
compose
(
PermissionUtil
.<
Boolean
>
bindToLifecycle
(
view
))
//使用RXlifecycle,使subscription和activity一起销毁
.
subscribe
(
new
ErrorHandleSubscriber
<
Boolean
>(
errorHandler
)
{
@Override
public
void
onNext
(
Boolean
granted
)
{
...
...
@@ -86,7 +72,6 @@ public class PermissionUtil {
}
else
{
//没有申请过,则申请
rxPermissions
.
request
(
Manifest
.
permission
.
WRITE_EXTERNAL_STORAGE
)
.
compose
(
PermissionUtil
.<
Boolean
>
bindToLifecycle
(
view
))
//使用RXlifecycle,使subscription和activity一起销毁
.
subscribe
(
new
ErrorHandleSubscriber
<
Boolean
>(
errorHandler
)
{
@Override
public
void
onNext
(
Boolean
granted
)
{
...
...
@@ -117,7 +102,6 @@ public class PermissionUtil {
}
else
{
//没有申请过,则申请
rxPermissions
.
request
(
Manifest
.
permission
.
SEND_SMS
)
.
compose
(
PermissionUtil
.<
Boolean
>
bindToLifecycle
(
view
))
//使用RXlifecycle,使subscription和activity一起销毁
.
subscribe
(
new
ErrorHandleSubscriber
<
Boolean
>(
errorHandler
)
{
@Override
public
void
onNext
(
Boolean
granted
)
{
...
...
@@ -147,7 +131,6 @@ public class PermissionUtil {
}
else
{
//没有申请过,则申请
rxPermissions
.
request
(
Manifest
.
permission
.
CALL_PHONE
)
.
compose
(
PermissionUtil
.<
Boolean
>
bindToLifecycle
(
view
))
//使用RXlifecycle,使subscription和activity一起销毁
.
subscribe
(
new
ErrorHandleSubscriber
<
Boolean
>(
errorHandler
)
{
@Override
public
void
onNext
(
Boolean
granted
)
{
...
...
arms/src/main/java/com/jess/arms/widget/imageloader/glide/GlideConfiguration.java
浏览文件 @
dc41131d
...
...
@@ -13,6 +13,8 @@ import com.bumptech.glide.module.GlideModule;
import
com.jess.arms.utils.DataHelper
;
import
com.jess.arms.utils.UiUtils
;
import
java.io.File
;
/**
* Created by jess on 16/4/15.
*/
...
...
@@ -25,7 +27,8 @@ public class GlideConfiguration implements GlideModule {
@Override
public
DiskCache
build
()
{
// Careful: the external cache directory doesn't enforce permissions
return
DiskLruCacheWrapper
.
get
(
DataHelper
.
getCacheFile
(
UiUtils
.
getContext
()),
IMAGE_DISK_CACHE_MAX_SIZE
);
File
cacheDirectory
=
new
File
(
DataHelper
.
getCacheFile
(
UiUtils
.
getContext
()),
"Glide"
);
return
DiskLruCacheWrapper
.
get
(
DataHelper
.
makeDirs
(
cacheDirectory
),
IMAGE_DISK_CACHE_MAX_SIZE
);
}
});
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录