package com.turlir.abakgists;


import com.pushtorefresh.storio.sqlite.StorIOSQLite;
import com.turlir.abakgists.base.BasePresenter;
import com.turlir.abakgists.model.Gist;
import com.turlir.abakgists.network.ApiClient;

import java.util.List;

public class AllGistsPresenter extends BasePresenter<AllGistsFragment> {

    private static final int PAGE_SIZE = 30;

    private final ApiClient mClient;
    private final StorIOSQLite mDatabase;

    public AllGistsPresenter(ApiClient client, StorIOSQLite database) {
        mClient = client;
        mDatabase = database;
    }

    void loadPublicGists(int currentSize) {
        currentSize = Math.max(currentSize, PAGE_SIZE);
        int page = currentSize / PAGE_SIZE;
        addSubscription(mClient.publicGist(page)
                .compose(this.<List<Gist>>defaultSchedule())
                .subscribe(new Handler<List<Gist>>() {

                    @Override
                    public void onNext(List<Gist> value) {
                        if (getView() != null) {
                            mDatabase.put()
                                    .objects(value)
                                    .prepare()
                                    .executeAsBlocking();
                            if (value.size() < PAGE_SIZE) {
                                // достигнут конец списка
                                getView().stopGistLoad();
                            } else {
                                getView().onGistLoaded(value);
                            }
                        }
                    }

                }));
    }

}

@@ -78,16 +78,82 @@
 SQLite;%0A
+import com.pushtorefresh.storio.sqlite.operations.put.PutResults;%0A
 import c
@@ -304,16 +304,176 @@
 .List;%0A%0A
+import rx.Observable;%0Aimport rx.android.schedulers.AndroidSchedulers;%0Aimport rx.functions.Action1;%0Aimport rx.functions.Func1;%0Aimport rx.schedulers.Schedulers;%0A%0A
 public c
@@ -1013,90 +1013,28 @@
    .
-compose(this.%3CList%3CGist%3E%3EdefaultSchedule())%0A                .subscribe(new Handler
+doOnNext(new Action1
 %3CLis
@@ -1046,17 +1046,16 @@
 t%3E%3E() %7B%0A
-%0A
         
@@ -1112,14 +1112,12 @@
 oid 
-onNext
+call
 (Lis
@@ -1124,21 +1124,21 @@
 t%3CGist%3E 
-value
+gists
 ) %7B%0A    
@@ -1166,24 +1166,40 @@
 f (g
-etView() != null
+ists != null && !gists.isEmpty()
 ) %7B%0A
@@ -1287,21 +1287,21 @@
 objects(
-value
+gists
 )%0A      
@@ -1395,24 +1395,390 @@
 Blocking();%0A
+                        %7D%0A                    %7D%0A                %7D)%0A                .subscribeOn(Schedulers.io())%0A                .observeOn(AndroidSchedulers.mainThread())%0A                .subscribe(new Handler%3CList%3CGist%3E%3E() %7B%0A%0A                    @Override%0A                    public void onNext(List%3CGist%3E value) %7B%0A                        if (getView() != null) %7B%0A
             

move database inserting to side-effect code block