package com.sinyuk.yuk.data.shot;

import android.support.annotation.NonNull;

import com.f2prateek.rx.preferences.RxSharedPreferences;
import com.sinyuk.yuk.utils.PrefsUtils;

import java.util.Collections;
import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import timber.log.Timber;

/**
 * Created by Sinyuk on 16.6.20.
 */
public class ShotRepository {

    private final RxSharedPreferences prefs;

    private final ShotRemoteDataSource remoteDataSource;

    public ShotRepository(ShotRemoteDataSource remoteDataSource,
                          RxSharedPreferences preferences) {
        this.remoteDataSource = remoteDataSource;
        this.prefs = preferences;
        Timber.tag("ShotRepository");
    }

    public Observable getShots(@NonNull String type,int page) {
        Observable<List<Shot>> remoteObservable = remoteDataSource.getShots(type, page);
        return prefs.getBoolean(PrefsUtils.auto_caching, true)
                .asObservable()
                .flatMap(autoRefresh -> remoteObservable
                        .doOnError(throwable -> Timber.d(throwable.getLocalizedMessage()))
                        .onErrorResumeNext(Observable.just(Collections.emptyList()))
                        .observeOn(AndroidSchedulers.mainThread()));

    }


}

@@ -129,16 +129,58 @@
 rences;%0A
+import com.sinyuk.yuk.api.DribbleService;%0A
 import c
@@ -242,16 +242,71 @@
 ctions;%0A
+import java.util.HashMap;%0Aimport java.util.LinkedList;%0A
 import j
@@ -391,16 +391,76 @@
 dulers;%0A
+import rx.functions.Func1;%0Aimport rx.schedulers.Schedulers;%0A
 import t
@@ -549,16 +549,97 @@
 itory %7B%0A
+    private final HashMap%3CString, LinkedList%3CShot%3E%3E mShotCache = new HashMap%3C%3E();
 %0A    pri
@@ -680,128 +680,98 @@
 fs;%0A
-%0A
     
-private final ShotRemoteDataSource remoteDataSource;%0A%0A    public ShotRepository(ShotRemoteDataSource remoteDataSour
+DribbleService mDribbleService;%0A%0A    public ShotRepository(DribbleService dribbleServi
 ce,%0A
@@ -848,41 +848,38 @@
 his.
-remoteDataSource = remoteDataSour
+mDribbleService = dribbleServi
 ce;%0A
@@ -1009,16 +1009,17 @@
 ng type,
+ 
 int page
@@ -1053,22 +1053,23 @@
 %3CShot%3E%3E 
-remote
+caching
 Observab
@@ -1077,29 +1077,25 @@
 e = 
-remoteDataSource.getS
+mDribbleService.s
 hots
@@ -1232,40 +1232,264 @@
 Map(
-autoRefresh -%3E remoteObservable%0A
+new Func1%3CBoolean, Observable%3C?%3E%3E() %7B%0A                    @Override%0A                    public Observable%3C?%3E call(Boolean autoCaching) %7B%0A                        return cachingObservable%0A                                .map(insertShots(page, type))%0A        
     
@@ -1539,17 +1539,17 @@
  Timber.
-d
+e
 (throwab
@@ -1558,17 +1558,8 @@
 .get
-Localized
 Mess
@@ -1594,68 +1594,394 @@
     
-.onErrorResumeNext(Observable.just(Collections.emptyList()))
+        .subscribeOn(Schedulers.io());%0A                    %7D%0A                %7D)%0A                .observeOn(AndroidSchedulers.mainThread());%0A%0A    %7D%0A%0A    private Func1%3CList%3CShot%3E, List%3CShot%3E%3E insertShots(int page, String type) %7B%0A        return result -%3E %7B%0A            LinkedList%3CShot%3E list = mShotCache.get(type);%0A            if (list == null) %7B%0A                list = new LinkedList%3C%3E();
 %0A   
@@ -1981,32 +1981,34 @@
 ();%0A            
+%7D%0A
             .obs
@@ -2007,60 +2007,523 @@
     
-.observeOn(AndroidSchedulers.mainThread()));%0A%0A    %7D%0A%0A
+Timber.d(%22Shots in cache :%22 + list.toString());%0A            if (page == 1) %7B%0A                if (Collections.disjoint(list, result)) %7B%0A                    list.clear();%0A                    list.addAll(result);%0A                %7D else %7B%0A                    result.removeAll(list);%0A                    list.addAll(0, result);%0A                %7D%0A            %7D else %7B%0A                list.addAll(result);    // append to front%0A            %7D%0A            mShotCache.put(type, list);%0A            return list;%0A        %7D;%0A    %7D
 %0A%7D%0A

Save data in cache