提交 02100092 编写于 作者: J jessyan

Activity provide Cache

上级 aca83931
......@@ -25,8 +25,11 @@ import android.util.AttributeSet;
import android.view.View;
import com.jess.arms.base.delegate.IActivity;
import com.jess.arms.integration.cache.Cache;
import com.jess.arms.integration.cache.CacheType;
import com.jess.arms.integration.lifecycle.ActivityLifecycleable;
import com.jess.arms.mvp.IPresenter;
import com.jess.arms.utils.ArmsUtils;
import com.trello.rxlifecycle2.android.ActivityEvent;
import javax.inject.Inject;
......@@ -52,9 +55,14 @@ public abstract class BaseActivity<P extends IPresenter> extends AppCompatActivi
protected final String TAG = this.getClass().getSimpleName();
private Unbinder mUnbinder;
private final BehaviorSubject<ActivityEvent> mLifecycleSubject = BehaviorSubject.create();
private final Cache<String, Object> mCache = ArmsUtils.obtainAppComponentFromContext(this).cacheFactory().build(CacheType.ACTIVITY_CACHE);
@Inject
protected P mPresenter;
@Override
public Cache<String, Object> provideCache() {
return mCache;
}
@NonNull
@Override
......
/**
* Copyright 2017 JessYan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* Copyright 2017 JessYan
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jess.arms.base.delegate;
......@@ -23,6 +23,8 @@ import android.support.v4.app.FragmentManager;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.integration.cache.Cache;
import com.jess.arms.integration.cache.LruCache;
import org.simple.eventbus.EventBus;
......@@ -38,6 +40,13 @@ import org.simple.eventbus.EventBus;
*/
public interface IActivity {
/**
* 提供在 {@link Activity} 生命周期内的缓存容器, 可向此 {@link Activity} 存取一些必要的数据
*
* @return like {@link LruCache}
*/
Cache<String, Object> provideCache();
/**
* 提供 AppComponent(提供所有的单例对象)给实现类,进行 Component 依赖
*
......
......@@ -76,5 +76,8 @@ public interface AppComponent {
//用来存取一些整个App公用的数据,切勿大量存放大容量数据
Cache<String, Object> extras();
//用于创建框架所需缓存对象的工厂
Cache.Factory cacheFactory();
void inject(AppDelegate delegate);
}
......@@ -15,8 +15,13 @@
*/
package com.jess.arms.integration.cache;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.support.v4.app.Fragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.integration.RepositoryManager;
/**
* ================================================
......@@ -33,8 +38,10 @@ public interface CacheType {
int RETROFIT_SERVICE_CACHE_TYPE_ID = 0;
int CACHE_SERVICE_CACHE_TYPE_ID = 1;
int EXTRAS_TYPE_ID = 2;
int ACTIVITY_CACHE_TYPE_ID = 3;
int FRAGMENT_CACHE_TYPE_ID = 4;
/**
* RepositoryManager 中存储 Retrofit Service 的容器
* {@link RepositoryManager}中存储 Retrofit Service 的容器
*/
CacheType RETROFIT_SERVICE_CACHE = new CacheType() {
private static final int MAX_SIZE = 150;
......@@ -49,7 +56,7 @@ public interface CacheType {
public int calculateCacheSize(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int targetMemoryCacheSize = (int) (activityManager.getMemoryClass() * MAX_SIZE_MULTIPLIER * 1024);
if (targetMemoryCacheSize >= MAX_SIZE){
if (targetMemoryCacheSize >= MAX_SIZE) {
return MAX_SIZE;
}
return targetMemoryCacheSize;
......@@ -57,7 +64,7 @@ public interface CacheType {
};
/**
* RepositoryManager 中储存 Cache Service 的容器
* {@link RepositoryManager} 中储存 Cache Service 的容器
*/
CacheType CACHE_SERVICE_CACHE = new CacheType() {
private static final int MAX_SIZE = 150;
......@@ -72,7 +79,7 @@ public interface CacheType {
public int calculateCacheSize(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int targetMemoryCacheSize = (int) (activityManager.getMemoryClass() * MAX_SIZE_MULTIPLIER * 1024);
if (targetMemoryCacheSize >= MAX_SIZE){
if (targetMemoryCacheSize >= MAX_SIZE) {
return MAX_SIZE;
}
return targetMemoryCacheSize;
......@@ -80,7 +87,7 @@ public interface CacheType {
};
/**
* AppComponent 中的 extras
* {@link AppComponent} 中的 extras
*/
CacheType EXTRAS = new CacheType() {
private static final int MAX_SIZE = 500;
......@@ -95,7 +102,53 @@ public interface CacheType {
public int calculateCacheSize(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int targetMemoryCacheSize = (int) (activityManager.getMemoryClass() * MAX_SIZE_MULTIPLIER * 1024);
if (targetMemoryCacheSize >= MAX_SIZE){
if (targetMemoryCacheSize >= MAX_SIZE) {
return MAX_SIZE;
}
return targetMemoryCacheSize;
}
};
/**
* {@link Activity} 中存储数据的容器
*/
CacheType ACTIVITY_CACHE = new CacheType() {
private static final int MAX_SIZE = 80;
private static final float MAX_SIZE_MULTIPLIER = 0.0008f;
@Override
public int getCacheTypeId() {
return ACTIVITY_CACHE_TYPE_ID;
}
@Override
public int calculateCacheSize(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int targetMemoryCacheSize = (int) (activityManager.getMemoryClass() * MAX_SIZE_MULTIPLIER * 1024);
if (targetMemoryCacheSize >= MAX_SIZE) {
return MAX_SIZE;
}
return targetMemoryCacheSize;
}
};
/**
* {@link Fragment} 中存储数据的容器
*/
CacheType FRAGMENT_CACHE = new CacheType() {
private static final int MAX_SIZE = 80;
private static final float MAX_SIZE_MULTIPLIER = 0.0008f;
@Override
public int getCacheTypeId() {
return FRAGMENT_CACHE_TYPE_ID;
}
@Override
public int calculateCacheSize(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int targetMemoryCacheSize = (int) (activityManager.getMemoryClass() * MAX_SIZE_MULTIPLIER * 1024);
if (targetMemoryCacheSize >= MAX_SIZE) {
return MAX_SIZE;
}
return targetMemoryCacheSize;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册