package io.github.kobakei.anago.viewmodel;

import android.databinding.ObservableArrayList;
import android.support.v7.widget.RecyclerView;

import javax.inject.Inject;

import io.github.kobakei.anago.activity.BaseActivity;
import io.github.kobakei.anago.entity.User;
import io.github.kobakei.anago.usecase.GetStargazersUseCase;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import timber.log.Timber;

/**
 * スターした人一覧画面のビューモデル
 * Created by keisuke on 2016/09/20.
 */

public class StargazerListViewModel extends ActivityViewModel {

    private final GetStargazersUseCase getStargazersUseCase;

    public ObservableArrayList<User> users;

    private String paramUser;
    private String paramRepo;

    private int page = 0;
    private boolean hasMore = true;
    private boolean loadingMore = false;

    @Inject
    public StargazerListViewModel(BaseActivity activity, GetStargazersUseCase getStargazersUseCase) {
        super(activity);
        this.getStargazersUseCase = getStargazersUseCase;

        this.users = new ObservableArrayList<>();
    }

    public void setParams(String user, String repo) {
        this.paramUser = user;
        this.paramRepo = repo;
    }

    @Override
    public void onResume() {
        super.onResume();

        getStargazersUseCase.run(paramUser, paramRepo, 1, 20)
                .compose(getActivity().bindToLifecycle().forSingle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(users1 -> {
                    this.users.clear();
                    this.users.addAll(users1);
                }, Throwable::printStackTrace);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        Timber.v("onScrolled");
    }
}

@@ -85,16 +85,70 @@
 ayList;%0A
+import android.support.v7.widget.LinearLayoutManager;%0A
 import a
@@ -837,44 +837,8 @@
  0;%0A
-    private boolean hasMore = true;%0A
     
@@ -1322,16 +1322,805 @@
 sume();%0A
+        load();%0A    %7D%0A%0A    @Override%0A    public void onPause() %7B%0A        super.onPause();%0A    %7D%0A%0A    public void onScrolled(RecyclerView recyclerView, int dx, int dy) %7B%0A        if (dy %3E 0) %7B%0A            LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();%0A            int visible = layoutManager.getChildCount();%0A            int total = layoutManager.getItemCount();%0A            int first = layoutManager.findFirstVisibleItemPosition();%0A            if (visible + first %3E= total) %7B%0A                Timber.v(%22Scroll End%22);%0A                if (!loadingMore) %7B%0A                    Timber.v(%22Start loading more%22);%0A                    loadingMore = true;%0A                    load();%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    private void load() %7B
 %0A       
@@ -2166,16 +2166,23 @@
 ramRepo,
+ page +
  1, 20)%0A
@@ -2430,14 +2430,21 @@
 ers.
-clear(
+addAll(users1
 );%0A 
@@ -2471,260 +2471,116 @@
 his.
-users.addAll(users1);%0A                %7D, Throwable::printStackTrace);%0A    %7D%0A%0A    @Override%0A    public void onPause() %7B%0A        super.onPause();%0A
+page = page + 1;%0A                    this.loadingMore = false;%0A        
     
-%7D%0A%0A
     
-public void onScrolled(RecyclerView recyclerView, int dx, int dy) %7B%0A        Timber.v(%22onScrolled%22
+%7D, Throwable::printStackTrace
 );%0A 

Load more