package me.jessyan.mvpart.demo.mvp.model;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.rx_cache2.DynamicKey;
import io.rx_cache2.EvictDynamicKey;
import io.rx_cache2.Reply;
import me.jessyan.art.mvp.IModel;
import me.jessyan.art.mvp.IRepositoryManager;
import me.jessyan.mvpart.demo.mvp.model.api.cache.CommonCache;
import me.jessyan.mvpart.demo.mvp.model.api.service.UserService;
import me.jessyan.mvpart.demo.mvp.model.entity.User;

/**
 *
 * 必须实现 IModel
 * 可以根据不同的业务逻辑划分多个 Repository 类,多个业务逻辑相近的页面可以使用同一个 Repository 类
 * 无需每个页面都创建一个独立的 Repository
 * 通过 {@link IRepositoryManager#createRepository(java.lang.Class)} 获得的 Repository 实例,为单例对象
 *
 * Created by jess on 9/4/16 10:56
 * Contact with jess.yan.effort@gmail.com
 */
public class UserRepository implements IModel {

    private IRepositoryManager mManager;
    public static final int USERS_PER_PAGE = 10;

    /**
     * 必须含有一个接收IRepositoryManager接口的构造函数,否则会报错
     * @param manager
     */
    public UserRepository(IRepositoryManager manager) {
        this.mManager = manager;
    }


    public Observable<List<User>> getUsers(int lastIdQueried, boolean update) {
        Observable<List<User>> users = mManager.createRetrofitService(UserService.class)
                .getUsers(lastIdQueried, USERS_PER_PAGE);
        //使用rxcache缓存,上拉刷新则不读取缓存,加载更多读取缓存
        return mManager.createCacheService(CommonCache.class)
                .getUsers(users
                        , new DynamicKey(lastIdQueried)
                        , new EvictDynamicKey(update))
                .flatMap(new Function<Reply<List<User>>, ObservableSource<List<User>>>() {
                    @Override
                    public ObservableSource<List<User>> apply(@NonNull Reply<List<User>> listReply) throws Exception {
                        return Observable.just(listReply.getData());
                    }
                });
    }

    @Override
    public void onDestroy() {

    }
}

@@ -284,35 +284,8 @@
 ey;%0A
-import io.rx_cache2.Reply;%0A
 impo
@@ -1246,391 +1246,195 @@
     
-Observable%3CList%3CUser%3E%3E users = mManager.createRetrofitService(UserService.class)%0A                .getUsers(lastIdQueried, USERS_PER_PAGE);%0A        //%E4%BD%BF%E7%94%A8rxcache%E7%BC%93%E5%AD%98,%E4%B8%8A%E6%8B%89%E5%88%B7%E6%96%B0%E5%88%99%E4%B8%8D%E8%AF%BB%E5%8F%96%E7%BC%93%E5%AD%98,%E5%8A%A0%E8%BD%BD%E6%9B%B4%E5%A4%9A%E8%AF%BB%E5%8F%96%E7%BC%93%E5%AD%98%0A        return mManager.createCacheService(CommonCache.class)%0A                .getUsers(users%0A                        , new DynamicKey(lastIdQueried)%0A                        , new EvictDynamicKey(update
+//%E4%BD%BF%E7%94%A8rxcache%E7%BC%93%E5%AD%98,%E4%B8%8A%E6%8B%89%E5%88%B7%E6%96%B0%E5%88%99%E4%B8%8D%E8%AF%BB%E5%8F%96%E7%BC%93%E5%AD%98,%E5%8A%A0%E8%BD%BD%E6%9B%B4%E5%A4%9A%E8%AF%BB%E5%8F%96%E7%BC%93%E5%AD%98%0A        return Observable.just(mManager%0A                .createRetrofitService(UserService.class)%0A                .getUsers(lastIdQueried, USERS_PER_PAGE
 ))%0A 
@@ -1470,21 +1470,26 @@
 unction%3C
-Reply
+Observable
 %3CList%3CUs
@@ -1629,21 +1629,26 @@
 NonNull 
-Reply
+Observable
 %3CList%3CUs
@@ -1656,21 +1656,26 @@
 r%3E%3E list
-Reply
+Observable
 ) throws
@@ -1718,32 +1718,313 @@
  return 
-Observable.just(
+mManager.createCacheService(CommonCache.class)%0A                                .getUsers(listObservable%0A                                        , new DynamicKey(lastIdQueried)%0A                                        , new EvictDynamicKey(update))%0A                                .map(listReply -%3E 
 listRepl

Improve Demo