/**
 * RxRepository
 *
 * Created by Giang Nguyen on 12/24/15.
 * Copyright (c) 2015 Tale. All rights reserved.
 */

package com.tale.rxrepository;

import java.util.ArrayList;
import java.util.List;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;

public class ListRepository<T> extends Repository<List<T>> {

  private int page = 0;

  public ListRepository(DiskProvider<List<T>> diskProvider, CloudProvider<List<T>> cloudProvider,
      Comparator<List<T>> comparator, NetworkVerifier networkVerifier) {
    super(diskProvider, cloudProvider, comparator, networkVerifier);
    cache = new ArrayList<>();
  }

  public Observable<List<T>> refresh() {
    return cloudProvider.get(0)
        .filter(filterNewData())
        .flatMap(new Func1<List<T>, Observable<List<T>>>() {
          @Override public Observable<List<T>> call(List<T> ts) {
            return getDiskProvider().save(ts);
          }
        }).doOnNext(cacheAction()).doOnNext(new Action1<List<T>>() {
          @Override public void call(List<T> ts) {
            page = 0;
          }
        });
  }

  public Observable<List<T>> more() {
    final int nextPage = page + 1;
    return cloudProvider.get(nextPage).filter(new Func1<List<T>, Boolean>() {
      @Override public Boolean call(List<T> ts) {
        return ts != null && ts.size() > 0;
      }
    }).filter(filterNewData()).map(new Func1<List<T>, List<T>>() {
      @Override public List<T> call(List<T> ts) {
        page = nextPage;
        cache.addAll(ts);
        return cache;
      }
    });
  }

  @Override public boolean hasCache() {
    return cache != null && cache.size() > 0;
  }

  @Override Action1<List<T>> cacheAction() {
    return new Action1<List<T>>() {
      @Override public void call(List<T> ts) {
        if (ts == null || ts.size() == 0) {
          return;
        }
        cache.clear();
        cache.addAll(ts);
      }
    };
  }
}

@@ -937,16 +937,25 @@
       %7D)
+%0A        
 .doOnNex
@@ -970,16 +970,25 @@
 ction())
+%0A        
 .doOnNex
@@ -1515,34 +1515,8 @@
 ge;%0A
-        cache.addAll(ts);%0A
     
@@ -1526,21 +1526,27 @@
  return 
-cache
+addMore(ts)
 ;%0A      
@@ -1904,24 +1904,24 @@
 s);%0A      %7D%0A
-
     %7D;%0A  %7D%0A%7D
@@ -1918,11 +1918,257 @@
   %7D;%0A  %7D
+%0A%0A  protected List%3CT%3E addMore(List%3CT%3E ts) %7B%0A    final int size = ts.size();%0A    for (int i = 0; i %3C size; i++) %7B%0A      final T t = ts.get(i);%0A      if (cache.contains(t)) %7B%0A        continue;%0A      %7D%0A      cache.add(t);%0A    %7D%0A    return cache;%0A  %7D
 %0A%7D%0A

fix duplicated item when load more